build.rs 4.2 KB

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