build.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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::{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-title",
  63. "maximize",
  64. "unmaximize",
  65. "minimize",
  66. "unminimize",
  67. "show",
  68. "hide",
  69. "close",
  70. "set-decorations",
  71. "set-always-on-top",
  72. "set-content-protected",
  73. "set-size",
  74. "set-min-size",
  75. "set-max-size",
  76. "set-position",
  77. "set-fullscreen",
  78. "set-focus",
  79. "set-icon",
  80. "set-skip-taskbar",
  81. "set-cursor-grab",
  82. "set-cursor-visible",
  83. "set-cursor-icon",
  84. "set-cursor-position",
  85. "set-ignore-cursor-events",
  86. "start-dragging",
  87. "print",
  88. ],
  89. api_all,
  90. );
  91. alias_module("shell", &["execute", "sidecar", "open"], api_all);
  92. // helper for the command module macro
  93. let shell_script = has_feature("shell-execute") || has_feature("shell-sidecar");
  94. alias("shell_script", shell_script);
  95. alias("shell_scope", has_feature("shell-open-api") || shell_script);
  96. if !mobile {
  97. alias_module(
  98. "dialog",
  99. &["open", "save", "message", "ask", "confirm"],
  100. api_all,
  101. );
  102. }
  103. alias_module("http", &["request"], api_all);
  104. alias("cli", has_feature("cli"));
  105. if !mobile {
  106. alias_module("notification", &[], api_all);
  107. alias_module("global-shortcut", &[], api_all);
  108. }
  109. alias_module("os", &[], api_all);
  110. alias_module("path", &[], api_all);
  111. alias_module("protocol", &["asset"], api_all);
  112. alias_module("process", &["relaunch", "exit"], api_all);
  113. alias_module("clipboard", &["write-text", "read-text"], api_all);
  114. alias_module("app", &["show", "hide"], api_all);
  115. let checked_features_out_path =
  116. Path::new(&std::env::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 preven `STATUS_ENTRYPOINT_NOT_FOUND` error
  123. // see https://github.com/tauri-apps/tauri/pull/4383#issuecomment-1212221864
  124. let target_os = std::env::var("CARGO_CFG_TARGET_OS");
  125. let target_env = std::env::var("CARGO_CFG_TARGET_ENV");
  126. let is_tauri_workspace = std::env::var("__TAURI_WORKSPACE__").map_or(false, |v| v == "true");
  127. if is_tauri_workspace
  128. && Ok("windows") == target_os.as_deref()
  129. && Ok("msvc") == target_env.as_deref()
  130. {
  131. add_manifest();
  132. }
  133. }
  134. // create aliases for the given module with its apis.
  135. // each api is translated into a feature flag in the format of `<module>-<api>`
  136. // and aliased as `<module_snake_case>_<api_snake_case>`.
  137. //
  138. // The `<module>-all` feature is also aliased to `<module>_all`.
  139. //
  140. // If any of the features is enabled, the `<module_snake_case>_any` alias is created.
  141. //
  142. // Note that both `module` and `apis` strings must be written in kebab case.
  143. fn alias_module(module: &str, apis: &[&str], api_all: bool) {
  144. let all_feature_name = format!("{module}-all");
  145. let all = has_feature(&all_feature_name) || api_all;
  146. alias(&all_feature_name.to_snake_case(), all);
  147. let mut any = all;
  148. for api in apis {
  149. let has = has_feature(&format!("{module}-{api}")) || all;
  150. alias(
  151. &format!("{}_{}", AsSnakeCase(module), AsSnakeCase(api)),
  152. has,
  153. );
  154. any = any || has;
  155. }
  156. alias(&format!("{}_any", AsSnakeCase(module)), any);
  157. }
  158. fn add_manifest() {
  159. static WINDOWS_MANIFEST_FILE: &str = "Windows Manifest.xml";
  160. let mut manifest = std::env::current_dir().unwrap();
  161. manifest.push(WINDOWS_MANIFEST_FILE);
  162. println!("cargo:rerun-if-changed={}", WINDOWS_MANIFEST_FILE);
  163. // Embed the Windows application manifest file.
  164. println!("cargo:rustc-link-arg=/MANIFEST:EMBED");
  165. println!(
  166. "cargo:rustc-link-arg=/MANIFESTINPUT:{}",
  167. manifest.to_str().unwrap()
  168. );
  169. // Turn linker warnings into errors.
  170. println!("cargo:rustc-link-arg=/WX");
  171. }