build.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2019-2022 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;
  9. use std::path::PathBuf;
  10. use std::{path::Path, sync::Mutex};
  11. static CHECKED_FEATURES: OnceCell<Mutex<Vec<String>>> = OnceCell::new();
  12. // checks if the given Cargo feature is enabled.
  13. fn has_feature(feature: &str) -> bool {
  14. CHECKED_FEATURES
  15. .get_or_init(Default::default)
  16. .lock()
  17. .unwrap()
  18. .push(feature.to_string());
  19. // when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name>` env var to 1
  20. // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
  21. std::env::var(format!("CARGO_FEATURE_{}", AsShoutySnakeCase(feature)))
  22. .map(|x| x == "1")
  23. .unwrap_or(false)
  24. }
  25. // creates a cfg alias if `has_feature` is true.
  26. // `alias` must be a snake case string.
  27. fn alias(alias: &str, has_feature: bool) {
  28. if has_feature {
  29. println!("cargo:rustc-cfg={alias}");
  30. }
  31. }
  32. fn main() {
  33. alias("custom_protocol", has_feature("custom-protocol"));
  34. alias("dev", !has_feature("custom-protocol"));
  35. alias("updater", has_feature("updater"));
  36. let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
  37. let mobile = target_os == "ios" || target_os == "android";
  38. alias("desktop", !mobile);
  39. alias("mobile", mobile);
  40. let api_all = has_feature("api-all");
  41. alias("api_all", api_all);
  42. alias_module(
  43. "fs",
  44. &[
  45. "read-file",
  46. "write-file",
  47. "read-dir",
  48. "copy-file",
  49. "create-dir",
  50. "remove-dir",
  51. "remove-file",
  52. "rename-file",
  53. "exists",
  54. ],
  55. api_all,
  56. );
  57. alias_module(
  58. "window",
  59. &[
  60. "create",
  61. "center",
  62. "request-user-attention",
  63. "set-resizable",
  64. "set-title",
  65. "maximize",
  66. "unmaximize",
  67. "minimize",
  68. "unminimize",
  69. "show",
  70. "hide",
  71. "close",
  72. "set-decorations",
  73. "set-shadow",
  74. "set-always-on-top",
  75. "set-size",
  76. "set-min-size",
  77. "set-max-size",
  78. "set-position",
  79. "set-fullscreen",
  80. "set-focus",
  81. "set-icon",
  82. "set-skip-taskbar",
  83. "set-cursor-grab",
  84. "set-cursor-visible",
  85. "set-cursor-icon",
  86. "set-cursor-position",
  87. "set-ignore-cursor-events",
  88. "start-dragging",
  89. "print",
  90. ],
  91. api_all,
  92. );
  93. alias_module("shell", &["execute", "sidecar", "open"], api_all);
  94. // helper for the command module macro
  95. let shell_script = has_feature("shell-execute") || has_feature("shell-sidecar");
  96. alias("shell_script", shell_script);
  97. alias("shell_scope", has_feature("shell-open-api") || shell_script);
  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. alias("cli", has_feature("cli"));
  107. if !mobile {
  108. alias_module("notification", &[], api_all);
  109. alias_module("global-shortcut", &[], api_all);
  110. }
  111. alias_module("os", &[], api_all);
  112. alias_module("path", &[], api_all);
  113. alias_module("protocol", &["asset"], api_all);
  114. alias_module("process", &["relaunch", "exit"], api_all);
  115. alias_module("clipboard", &["write-text", "read-text"], api_all);
  116. alias_module("app", &["show", "hide"], api_all);
  117. let checked_features_out_path =
  118. Path::new(&std::env::var("OUT_DIR").unwrap()).join("checked_features");
  119. std::fs::write(
  120. checked_features_out_path,
  121. CHECKED_FEATURES.get().unwrap().lock().unwrap().join(","),
  122. )
  123. .expect("failed to write checked_features file");
  124. if target_os == "android" {
  125. if let Ok(project_dir) = var("TAURI_ANDROID_PROJECT_PATH") {
  126. let project_dir = PathBuf::from(project_dir);
  127. tauri_build::mobile::inject_android_project(
  128. "./mobile/android",
  129. project_dir.join("tauri-api"),
  130. &[],
  131. )
  132. .expect("failed to copy tauri-api Android project");
  133. }
  134. let lib_path =
  135. PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/android");
  136. println!("cargo:android_library_path={}", lib_path.display());
  137. }
  138. #[cfg(target_os = "macos")]
  139. {
  140. if target_os == "ios" {
  141. let lib_path =
  142. PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/ios-api");
  143. tauri_build::mobile::link_swift_library("Tauri", &lib_path);
  144. println!("cargo:ios_library_path={}", lib_path.display());
  145. }
  146. }
  147. }
  148. // create aliases for the given module with its apis.
  149. // each api is translated into a feature flag in the format of `<module>-<api>`
  150. // and aliased as `<module_snake_case>_<api_snake_case>`.
  151. //
  152. // The `<module>-all` feature is also aliased to `<module>_all`.
  153. //
  154. // If any of the features is enabled, the `<module_snake_case>_any` alias is created.
  155. //
  156. // Note that both `module` and `apis` strings must be written in kebab case.
  157. fn alias_module(module: &str, apis: &[&str], api_all: bool) {
  158. let all_feature_name = format!("{module}-all");
  159. let all = has_feature(&all_feature_name) || api_all;
  160. alias(&all_feature_name.to_snake_case(), all);
  161. let mut any = all;
  162. for api in apis {
  163. let has = has_feature(&format!("{module}-{api}")) || all;
  164. alias(
  165. &format!("{}_{}", AsSnakeCase(module), AsSnakeCase(api)),
  166. has,
  167. );
  168. any = any || has;
  169. }
  170. alias(&format!("{}_any", AsSnakeCase(module)), any);
  171. }