build.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  123. // create aliases for the given module with its apis.
  124. // each api is translated into a feature flag in the format of `<module>-<api>`
  125. // and aliased as `<module_snake_case>_<api_snake_case>`.
  126. //
  127. // The `<module>-all` feature is also aliased to `<module>_all`.
  128. //
  129. // If any of the features is enabled, the `<module_snake_case>_any` alias is created.
  130. //
  131. // Note that both `module` and `apis` strings must be written in kebab case.
  132. fn alias_module(module: &str, apis: &[&str], api_all: bool) {
  133. let all_feature_name = format!("{}-all", module);
  134. let all = has_feature(&all_feature_name) || api_all;
  135. alias(&all_feature_name.to_snake_case(), all);
  136. let mut any = all;
  137. for api in apis {
  138. let has = has_feature(&format!("{}-{}", module, api)) || all;
  139. alias(
  140. &format!("{}_{}", AsSnakeCase(module), AsSnakeCase(api)),
  141. has,
  142. );
  143. any = any || has;
  144. }
  145. alias(&format!("{}_any", AsSnakeCase(module)), any);
  146. }