build.rs 5.6 KB

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