build.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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::ToSnakeCase;
  5. use once_cell::sync::OnceCell;
  6. use std::{path::Path, sync::Mutex};
  7. static CHECKED_FEATURES: OnceCell<Mutex<Vec<String>>> = OnceCell::new();
  8. // checks if the given Cargo feature is enabled.
  9. fn has_feature(feature: &str) -> bool {
  10. CHECKED_FEATURES
  11. .get_or_init(Default::default)
  12. .lock()
  13. .unwrap()
  14. .push(feature.to_string());
  15. // when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name` env var to 1
  16. // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
  17. std::env::var(format!(
  18. "CARGO_FEATURE_{}",
  19. feature.to_snake_case().to_uppercase()
  20. ))
  21. .map(|x| x == "1")
  22. .unwrap_or(false)
  23. }
  24. // creates a cfg alias if `has_feature` is true.
  25. // `alias` must be a snake case string.
  26. fn alias(alias: &str, has_feature: bool) {
  27. if has_feature {
  28. println!("cargo:rustc-cfg={}", alias);
  29. }
  30. }
  31. fn main() {
  32. alias("custom_protocol", has_feature("custom-protocol"));
  33. alias("dev", !has_feature("custom-protocol"));
  34. alias(
  35. "updater",
  36. has_feature("updater") || has_feature("__updater-docs"),
  37. );
  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. alias_module(
  94. "dialog",
  95. &["open", "save", "message", "ask", "confirm"],
  96. api_all,
  97. );
  98. alias_module("http", &["request"], api_all);
  99. alias("cli", has_feature("cli"));
  100. alias_module("notification", &[], api_all);
  101. alias_module("global-shortcut", &[], api_all);
  102. alias_module("os", &[], api_all);
  103. alias_module("path", &[], api_all);
  104. alias_module("protocol", &["asset"], api_all);
  105. alias_module("process", &["relaunch", "exit"], api_all);
  106. alias_module("clipboard", &["write-text", "read-text"], api_all);
  107. let checked_features_out_path =
  108. Path::new(&std::env::var("OUT_DIR").unwrap()).join("checked_features");
  109. std::fs::write(
  110. &checked_features_out_path,
  111. &CHECKED_FEATURES.get().unwrap().lock().unwrap().join(","),
  112. )
  113. .expect("failed to write checked_features file");
  114. }
  115. // create aliases for the given module with its apis.
  116. // each api is translated into a feature flag in the format of `<module>-<api>`
  117. // and aliased as `<module_snake_case>_<api_snake_case>`.
  118. //
  119. // The `<module>-all` feature is also aliased to `<module>_all`.
  120. //
  121. // If any of the features is enabled, the `<module_snake_case>_any` alias is created.
  122. //
  123. // Note that both `module` and `apis` strings must be written in kebab case.
  124. fn alias_module(module: &str, apis: &[&str], api_all: bool) {
  125. let all_feature_name = format!("{}-all", module);
  126. let all = has_feature(&all_feature_name) || api_all;
  127. alias(&all_feature_name.to_snake_case(), all);
  128. let mut any = all;
  129. for api in apis {
  130. let has = has_feature(&format!("{}-{}", module, api)) || all;
  131. alias(
  132. &format!("{}_{}", module.to_snake_case(), api.to_snake_case()),
  133. has,
  134. );
  135. any = any || has;
  136. }
  137. alias(&format!("{}_any", module.to_snake_case()), any);
  138. }