build.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use heck::AsShoutySnakeCase;
  5. use heck::AsSnakeCase;
  6. use heck::ToSnakeCase;
  7. use once_cell::sync::OnceCell;
  8. use std::env::var_os;
  9. use std::{
  10. env::var,
  11. path::{Path, PathBuf},
  12. sync::Mutex,
  13. };
  14. static CHECKED_FEATURES: OnceCell<Mutex<Vec<String>>> = OnceCell::new();
  15. // checks if the given Cargo feature is enabled.
  16. fn has_feature(feature: &str) -> bool {
  17. CHECKED_FEATURES
  18. .get_or_init(Default::default)
  19. .lock()
  20. .unwrap()
  21. .push(feature.to_string());
  22. // when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name>` env var to 1
  23. // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
  24. std::env::var(format!("CARGO_FEATURE_{}", AsShoutySnakeCase(feature)))
  25. .map(|x| x == "1")
  26. .unwrap_or(false)
  27. }
  28. // creates a cfg alias if `has_feature` is true.
  29. // `alias` must be a snake case string.
  30. fn alias(alias: &str, has_feature: bool) {
  31. if has_feature {
  32. println!("cargo:rustc-cfg={alias}");
  33. }
  34. }
  35. fn main() {
  36. alias("custom_protocol", has_feature("custom-protocol"));
  37. alias("dev", !has_feature("custom-protocol"));
  38. alias("updater", has_feature("updater"));
  39. let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
  40. let mobile = target_os == "ios" || target_os == "android";
  41. alias("desktop", !mobile);
  42. alias("mobile", mobile);
  43. let api_all = has_feature("api-all");
  44. alias("api_all", api_all);
  45. alias_module(
  46. "fs",
  47. &[
  48. "read-file",
  49. "write-file",
  50. "read-dir",
  51. "copy-file",
  52. "create-dir",
  53. "remove-dir",
  54. "remove-file",
  55. "rename-file",
  56. "exists",
  57. ],
  58. api_all,
  59. );
  60. alias_module(
  61. "window",
  62. &[
  63. "create",
  64. "center",
  65. "request-user-attention",
  66. "set-resizable",
  67. "set-title",
  68. "maximize",
  69. "unmaximize",
  70. "minimize",
  71. "unminimize",
  72. "show",
  73. "hide",
  74. "close",
  75. "set-decorations",
  76. "set-shadow",
  77. "set-always-on-top",
  78. "set-content-protected",
  79. "set-size",
  80. "set-min-size",
  81. "set-max-size",
  82. "set-position",
  83. "set-fullscreen",
  84. "set-focus",
  85. "set-icon",
  86. "set-skip-taskbar",
  87. "set-cursor-grab",
  88. "set-cursor-visible",
  89. "set-cursor-icon",
  90. "set-cursor-position",
  91. "set-ignore-cursor-events",
  92. "start-dragging",
  93. "print",
  94. ],
  95. api_all,
  96. );
  97. alias_module("shell", &["execute", "sidecar", "open"], api_all);
  98. // helper for the command module macro
  99. let shell_script = has_feature("shell-execute") || has_feature("shell-sidecar");
  100. alias("shell_script", shell_script);
  101. alias("shell_scope", has_feature("shell-open-api") || shell_script);
  102. if !mobile {
  103. alias_module(
  104. "dialog",
  105. &["open", "save", "message", "ask", "confirm"],
  106. api_all,
  107. );
  108. }
  109. alias_module("http", &["request"], api_all);
  110. if !mobile {
  111. alias_module("notification", &[], api_all);
  112. alias_module("global-shortcut", &[], api_all);
  113. }
  114. alias_module("os", &[], api_all);
  115. alias_module("path", &[], api_all);
  116. alias_module("protocol", &["asset"], api_all);
  117. alias_module("process", &["relaunch", "exit"], api_all);
  118. alias_module("clipboard", &["write-text", "read-text"], api_all);
  119. alias_module("app", &["show", "hide"], api_all);
  120. let checked_features_out_path = Path::new(&var("OUT_DIR").unwrap()).join("checked_features");
  121. std::fs::write(
  122. checked_features_out_path,
  123. CHECKED_FEATURES.get().unwrap().lock().unwrap().join(","),
  124. )
  125. .expect("failed to write checked_features file");
  126. // workaround needed to prevent `STATUS_ENTRYPOINT_NOT_FOUND` error
  127. // see https://github.com/tauri-apps/tauri/pull/4383#issuecomment-1212221864
  128. let target_env = std::env::var("CARGO_CFG_TARGET_ENV");
  129. let is_tauri_workspace = std::env::var("__TAURI_WORKSPACE__").map_or(false, |v| v == "true");
  130. if is_tauri_workspace && target_os == "windows" && Ok("msvc") == target_env.as_deref() {
  131. add_manifest();
  132. }
  133. if target_os == "android" {
  134. if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
  135. tauri_build::mobile::inject_android_project(
  136. "./mobile/android",
  137. project_dir.join(".tauri").join("tauri-api"),
  138. &[],
  139. )
  140. .expect("failed to copy tauri-api Android project");
  141. let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace(
  142. "$PACKAGE",
  143. &var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"),
  144. );
  145. std::fs::write(
  146. project_dir.join("app").join("proguard-tauri.pro"),
  147. tauri_proguard,
  148. )
  149. .expect("failed to write proguard-tauri.pro");
  150. }
  151. let lib_path =
  152. PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/android");
  153. println!("cargo:android_library_path={}", lib_path.display());
  154. }
  155. #[cfg(target_os = "macos")]
  156. {
  157. if target_os == "ios" {
  158. let lib_path =
  159. PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/ios-api");
  160. tauri_build::mobile::link_swift_library("Tauri", &lib_path);
  161. println!("cargo:ios_library_path={}", lib_path.display());
  162. }
  163. }
  164. }
  165. // create aliases for the given module with its apis.
  166. // each api is translated into a feature flag in the format of `<module>-<api>`
  167. // and aliased as `<module_snake_case>_<api_snake_case>`.
  168. //
  169. // The `<module>-all` feature is also aliased to `<module>_all`.
  170. //
  171. // If any of the features is enabled, the `<module_snake_case>_any` alias is created.
  172. //
  173. // Note that both `module` and `apis` strings must be written in kebab case.
  174. fn alias_module(module: &str, apis: &[&str], api_all: bool) {
  175. let all_feature_name = format!("{module}-all");
  176. let all = has_feature(&all_feature_name) || api_all;
  177. alias(&all_feature_name.to_snake_case(), all);
  178. let mut any = all;
  179. for api in apis {
  180. let has = has_feature(&format!("{module}-{api}")) || all;
  181. alias(
  182. &format!("{}_{}", AsSnakeCase(module), AsSnakeCase(api)),
  183. has,
  184. );
  185. any = any || has;
  186. }
  187. alias(&format!("{}_any", AsSnakeCase(module)), any);
  188. }
  189. fn add_manifest() {
  190. static WINDOWS_MANIFEST_FILE: &str = "window-app-manifest.xml";
  191. let manifest = std::env::current_dir()
  192. .unwrap()
  193. .join("../tauri-build/src")
  194. .join(WINDOWS_MANIFEST_FILE);
  195. println!("cargo:rerun-if-changed={}", manifest.display());
  196. // Embed the Windows application manifest file.
  197. println!("cargo:rustc-link-arg=/MANIFEST:EMBED");
  198. println!(
  199. "cargo:rustc-link-arg=/MANIFESTINPUT:{}",
  200. manifest.to_str().unwrap()
  201. );
  202. // Turn linker warnings into errors.
  203. println!("cargo:rustc-link-arg=/WX");
  204. }