build.rs 4.4 KB

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