build.rs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if !mobile {
  99. alias_module(
  100. "dialog",
  101. &["open", "save", "message", "ask", "confirm"],
  102. api_all,
  103. );
  104. }
  105. alias_module("http", &["request"], api_all);
  106. if !mobile {
  107. alias_module("notification", &[], api_all);
  108. alias_module("global-shortcut", &[], api_all);
  109. }
  110. alias_module("os", &[], api_all);
  111. alias_module("path", &[], api_all);
  112. alias_module("protocol", &["asset"], api_all);
  113. alias_module("process", &["relaunch", "exit"], api_all);
  114. alias_module("clipboard", &["write-text", "read-text"], api_all);
  115. alias_module("app", &["show", "hide"], api_all);
  116. let checked_features_out_path = Path::new(&var("OUT_DIR").unwrap()).join("checked_features");
  117. std::fs::write(
  118. checked_features_out_path,
  119. CHECKED_FEATURES.get().unwrap().lock().unwrap().join(","),
  120. )
  121. .expect("failed to write checked_features file");
  122. // workaround needed to prevent `STATUS_ENTRYPOINT_NOT_FOUND` error
  123. // see https://github.com/tauri-apps/tauri/pull/4383#issuecomment-1212221864
  124. let target_env = std::env::var("CARGO_CFG_TARGET_ENV");
  125. let is_tauri_workspace = std::env::var("__TAURI_WORKSPACE__").map_or(false, |v| v == "true");
  126. if is_tauri_workspace && target_os == "windows" && Ok("msvc") == target_env.as_deref() {
  127. add_manifest();
  128. }
  129. if target_os == "android" {
  130. if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
  131. let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace(
  132. "$PACKAGE",
  133. &var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"),
  134. );
  135. std::fs::write(
  136. project_dir.join("app").join("proguard-tauri.pro"),
  137. tauri_proguard,
  138. )
  139. .expect("failed to write proguard-tauri.pro");
  140. }
  141. let lib_path =
  142. PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/android");
  143. println!("cargo:android_library_path={}", lib_path.display());
  144. }
  145. #[cfg(target_os = "macos")]
  146. {
  147. if target_os == "ios" {
  148. let lib_path =
  149. PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/ios-api");
  150. tauri_build::mobile::link_swift_library("Tauri", &lib_path);
  151. println!("cargo:ios_library_path={}", lib_path.display());
  152. }
  153. }
  154. }
  155. // create aliases for the given module with its apis.
  156. // each api is translated into a feature flag in the format of `<module>-<api>`
  157. // and aliased as `<module_snake_case>_<api_snake_case>`.
  158. //
  159. // The `<module>-all` feature is also aliased to `<module>_all`.
  160. //
  161. // If any of the features is enabled, the `<module_snake_case>_any` alias is created.
  162. //
  163. // Note that both `module` and `apis` strings must be written in kebab case.
  164. fn alias_module(module: &str, apis: &[&str], api_all: bool) {
  165. let all_feature_name = format!("{module}-all");
  166. let all = has_feature(&all_feature_name) || api_all;
  167. alias(&all_feature_name.to_snake_case(), all);
  168. let mut any = all;
  169. for api in apis {
  170. let has = has_feature(&format!("{module}-{api}")) || all;
  171. alias(
  172. &format!("{}_{}", AsSnakeCase(module), AsSnakeCase(api)),
  173. has,
  174. );
  175. any = any || has;
  176. }
  177. alias(&format!("{}_any", AsSnakeCase(module)), any);
  178. }
  179. fn add_manifest() {
  180. static WINDOWS_MANIFEST_FILE: &str = "window-app-manifest.xml";
  181. let manifest = std::env::current_dir()
  182. .unwrap()
  183. .join("../tauri-build/src")
  184. .join(WINDOWS_MANIFEST_FILE);
  185. println!("cargo:rerun-if-changed={}", manifest.display());
  186. // Embed the Windows application manifest file.
  187. println!("cargo:rustc-link-arg=/MANIFEST:EMBED");
  188. println!(
  189. "cargo:rustc-link-arg=/MANIFESTINPUT:{}",
  190. manifest.to_str().unwrap()
  191. );
  192. // Turn linker warnings into errors.
  193. println!("cargo:rustc-link-arg=/WX");
  194. }