build.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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-size",
  73. "set-min-size",
  74. "set-max-size",
  75. "set-position",
  76. "set-fullscreen",
  77. "set-focus",
  78. "set-icon",
  79. "set-skip-taskbar",
  80. "set-cursor-grab",
  81. "set-cursor-visible",
  82. "set-cursor-icon",
  83. "set-cursor-position",
  84. "set-ignore-cursor-events",
  85. "start-dragging",
  86. "print",
  87. ],
  88. api_all,
  89. );
  90. alias_module("shell", &["execute", "sidecar", "open"], api_all);
  91. // helper for the command module macro
  92. let shell_script = has_feature("shell-execute") || has_feature("shell-sidecar");
  93. alias("shell_script", shell_script);
  94. alias("shell_scope", has_feature("shell-open-api") || shell_script);
  95. if !mobile {
  96. alias_module(
  97. "dialog",
  98. &["open", "save", "message", "ask", "confirm"],
  99. api_all,
  100. );
  101. }
  102. alias_module("http", &["request"], api_all);
  103. alias("cli", has_feature("cli"));
  104. if !mobile {
  105. alias_module("notification", &[], api_all);
  106. alias_module("global-shortcut", &[], api_all);
  107. }
  108. alias_module("os", &[], api_all);
  109. alias_module("path", &[], api_all);
  110. alias_module("protocol", &["asset"], api_all);
  111. alias_module("process", &["relaunch", "exit"], api_all);
  112. alias_module("clipboard", &["write-text", "read-text"], api_all);
  113. alias_module("app", &["show", "hide"], api_all);
  114. let checked_features_out_path =
  115. Path::new(&std::env::var("OUT_DIR").unwrap()).join("checked_features");
  116. std::fs::write(
  117. checked_features_out_path,
  118. CHECKED_FEATURES.get().unwrap().lock().unwrap().join(","),
  119. )
  120. .expect("failed to write checked_features file");
  121. }
  122. // create aliases for the given module with its apis.
  123. // each api is translated into a feature flag in the format of `<module>-<api>`
  124. // and aliased as `<module_snake_case>_<api_snake_case>`.
  125. //
  126. // The `<module>-all` feature is also aliased to `<module>_all`.
  127. //
  128. // If any of the features is enabled, the `<module_snake_case>_any` alias is created.
  129. //
  130. // Note that both `module` and `apis` strings must be written in kebab case.
  131. fn alias_module(module: &str, apis: &[&str], api_all: bool) {
  132. let all_feature_name = format!("{}-all", module);
  133. let all = has_feature(&all_feature_name) || api_all;
  134. alias(&all_feature_name.to_snake_case(), all);
  135. let mut any = all;
  136. for api in apis {
  137. let has = has_feature(&format!("{}-{}", module, api)) || all;
  138. alias(
  139. &format!("{}_{}", AsSnakeCase(module), AsSnakeCase(api)),
  140. has,
  141. );
  142. any = any || has;
  143. }
  144. alias(&format!("{}_any", AsSnakeCase(module)), any);
  145. }