build.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use heck::AsShoutySnakeCase;
  5. use once_cell::sync::OnceCell;
  6. use std::env::var_os;
  7. use std::fs::read_dir;
  8. use std::fs::read_to_string;
  9. use std::fs::write;
  10. use std::{
  11. env::var,
  12. path::{Path, PathBuf},
  13. sync::Mutex,
  14. };
  15. static CHECKED_FEATURES: OnceCell<Mutex<Vec<String>>> = OnceCell::new();
  16. // checks if the given Cargo feature is enabled.
  17. fn has_feature(feature: &str) -> bool {
  18. CHECKED_FEATURES
  19. .get_or_init(Default::default)
  20. .lock()
  21. .unwrap()
  22. .push(feature.to_string());
  23. // when a feature is enabled, Cargo sets the `CARGO_FEATURE_<name>` env var to 1
  24. // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
  25. std::env::var(format!("CARGO_FEATURE_{}", AsShoutySnakeCase(feature)))
  26. .map(|x| x == "1")
  27. .unwrap_or(false)
  28. }
  29. // creates a cfg alias if `has_feature` is true.
  30. // `alias` must be a snake case string.
  31. fn alias(alias: &str, has_feature: bool) {
  32. if has_feature {
  33. println!("cargo:rustc-cfg={alias}");
  34. }
  35. }
  36. fn main() {
  37. alias("custom_protocol", has_feature("custom-protocol"));
  38. alias("dev", !has_feature("custom-protocol"));
  39. let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
  40. let mobile = target_os == "ios" || target_os == "android";
  41. alias("desktop", !mobile);
  42. alias("mobile", mobile);
  43. alias(
  44. "ipc_custom_protocol",
  45. target_os != "android" && (target_os != "linux" || has_feature("linux-ipc-protocol")),
  46. );
  47. let checked_features_out_path = Path::new(&var("OUT_DIR").unwrap()).join("checked_features");
  48. std::fs::write(
  49. checked_features_out_path,
  50. CHECKED_FEATURES.get().unwrap().lock().unwrap().join(","),
  51. )
  52. .expect("failed to write checked_features file");
  53. // workaround needed to prevent `STATUS_ENTRYPOINT_NOT_FOUND` error
  54. // see https://github.com/tauri-apps/tauri/pull/4383#issuecomment-1212221864
  55. let target_env = std::env::var("CARGO_CFG_TARGET_ENV");
  56. let is_tauri_workspace = std::env::var("__TAURI_WORKSPACE__").map_or(false, |v| v == "true");
  57. if is_tauri_workspace && target_os == "windows" && Ok("msvc") == target_env.as_deref() {
  58. add_manifest();
  59. }
  60. if target_os == "android" {
  61. if let Ok(kotlin_out_dir) = std::env::var("WRY_ANDROID_KOTLIN_FILES_OUT_DIR") {
  62. fn env_var(var: &str) -> String {
  63. std::env::var(var).unwrap_or_else(|_| {
  64. panic!(
  65. "`{}` is not set, which is needed to generate the kotlin files for android.",
  66. var
  67. )
  68. })
  69. }
  70. let package = env_var("WRY_ANDROID_PACKAGE");
  71. let library = env_var("WRY_ANDROID_LIBRARY");
  72. let kotlin_out_dir = PathBuf::from(&kotlin_out_dir)
  73. .canonicalize()
  74. .unwrap_or_else(move |_| {
  75. panic!("Failed to canonicalize `WRY_ANDROID_KOTLIN_FILES_OUT_DIR` path {kotlin_out_dir}")
  76. });
  77. let kotlin_files_path =
  78. PathBuf::from(env_var("CARGO_MANIFEST_DIR")).join("mobile/android-codegen");
  79. println!("cargo:rerun-if-changed={}", kotlin_files_path.display());
  80. let kotlin_files =
  81. read_dir(kotlin_files_path).expect("failed to read Android codegen directory");
  82. for file in kotlin_files {
  83. let file = file.unwrap();
  84. let content = read_to_string(file.path())
  85. .expect("failed to read kotlin file as string")
  86. .replace("{{package}}", &package)
  87. .replace("{{library}}", &library);
  88. let out_path = kotlin_out_dir.join(file.file_name());
  89. write(&out_path, content).expect("Failed to write kotlin file");
  90. println!("cargo:rerun-if-changed={}", out_path.display());
  91. }
  92. }
  93. if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
  94. let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace(
  95. "$PACKAGE",
  96. &var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"),
  97. );
  98. std::fs::write(
  99. project_dir.join("app").join("proguard-tauri.pro"),
  100. tauri_proguard,
  101. )
  102. .expect("failed to write proguard-tauri.pro");
  103. }
  104. let lib_path =
  105. PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/android");
  106. println!("cargo:android_library_path={}", lib_path.display());
  107. }
  108. #[cfg(target_os = "macos")]
  109. {
  110. if target_os == "ios" {
  111. let lib_path =
  112. PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/ios-api");
  113. tauri_build::mobile::link_swift_library("Tauri", &lib_path);
  114. println!("cargo:ios_library_path={}", lib_path.display());
  115. }
  116. }
  117. }
  118. fn add_manifest() {
  119. static WINDOWS_MANIFEST_FILE: &str = "window-app-manifest.xml";
  120. let manifest = std::env::current_dir()
  121. .unwrap()
  122. .join("../tauri-build/src")
  123. .join(WINDOWS_MANIFEST_FILE);
  124. println!("cargo:rerun-if-changed={}", manifest.display());
  125. // Embed the Windows application manifest file.
  126. println!("cargo:rustc-link-arg=/MANIFEST:EMBED");
  127. println!(
  128. "cargo:rustc-link-arg=/MANIFESTINPUT:{}",
  129. manifest.to_str().unwrap()
  130. );
  131. // Turn linker warnings into errors.
  132. println!("cargo:rustc-link-arg=/WX");
  133. }