build.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #[cfg(windows)]
  2. fn build_windows() {
  3. let file = "src/platform/windows.cc";
  4. cc::Build::new().file(file).compile("windows");
  5. println!("cargo:rustc-link-lib=WtsApi32");
  6. println!("cargo:rerun-if-changed={}", file);
  7. }
  8. #[cfg(target_os = "macos")]
  9. fn build_mac() {
  10. let file = "src/platform/macos.mm";
  11. let mut b = cc::Build::new();
  12. if let Ok(os_version::OsVersion::MacOS(v)) = os_version::detect() {
  13. let v = v.version;
  14. if v.contains("10.14") {
  15. b.flag("-DNO_InputMonitoringAuthStatus=1");
  16. }
  17. }
  18. b.file(file).compile("macos");
  19. println!("cargo:rerun-if-changed={}", file);
  20. }
  21. #[cfg(all(windows, feature = "inline"))]
  22. fn build_manifest() {
  23. use std::io::Write;
  24. if std::env::var("PROFILE").unwrap() == "release" {
  25. let mut res = winres::WindowsResource::new();
  26. res.set_icon("res/icon.ico")
  27. .set_language(winapi::um::winnt::MAKELANGID(
  28. winapi::um::winnt::LANG_ENGLISH,
  29. winapi::um::winnt::SUBLANG_ENGLISH_US,
  30. ))
  31. .set_manifest_file("res/manifest.xml");
  32. match res.compile() {
  33. Err(e) => {
  34. write!(std::io::stderr(), "{}", e).unwrap();
  35. std::process::exit(1);
  36. }
  37. Ok(_) => {}
  38. }
  39. }
  40. }
  41. fn install_oboe() {
  42. let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
  43. if target_os != "android" {
  44. return;
  45. }
  46. let mut target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
  47. if target_arch == "x86_64" {
  48. target_arch = "x64".to_owned();
  49. } else if target_arch == "aarch64" {
  50. target_arch = "arm64".to_owned();
  51. } else {
  52. target_arch = "arm".to_owned();
  53. }
  54. let target = format!("{}-android", target_arch);
  55. let vcpkg_root = std::env::var("VCPKG_ROOT").unwrap();
  56. let mut path: std::path::PathBuf = vcpkg_root.into();
  57. path.push("installed");
  58. path.push(target);
  59. println!(
  60. "{}",
  61. format!(
  62. "cargo:rustc-link-search={}",
  63. path.join("lib").to_str().unwrap()
  64. )
  65. );
  66. println!("cargo:rustc-link-lib=oboe");
  67. println!("cargo:rustc-link-lib=c++");
  68. println!("cargo:rustc-link-lib=OpenSLES");
  69. // I always got some strange link error with oboe, so as workaround, put oboe.cc into oboe src: src/common/AudioStreamBuilder.cpp
  70. // also to avoid libc++_shared not found issue, cp ndk's libc++_shared.so to jniLibs, e.g.
  71. // ./flutter_hbb/android/app/src/main/jniLibs/arm64-v8a/libc++_shared.so
  72. // let include = path.join("include");
  73. //cc::Build::new().file("oboe.cc").include(include).compile("oboe_wrapper");
  74. }
  75. #[cfg(feature = "flutter")]
  76. fn gen_flutter_rust_bridge() {
  77. if !std::env::var("RUN_FFIGEN").is_ok() {
  78. return;
  79. }
  80. use lib_flutter_rust_bridge_codegen::{
  81. config_parse, frb_codegen, get_symbols_if_no_duplicates, RawOpts,
  82. };
  83. let llvm_path = match std::env::var("LLVM_HOME") {
  84. Ok(path) => Some(vec![path]),
  85. Err(_) => None,
  86. };
  87. // Tell Cargo that if the given file changes, to rerun this build script.
  88. println!("cargo:rerun-if-changed=src/flutter_ffi.rs");
  89. // Options for frb_codegen
  90. let raw_opts = RawOpts {
  91. // Path of input Rust code
  92. rust_input: vec!["src/flutter_ffi.rs".to_string()],
  93. // Path of output generated Dart code
  94. dart_output: vec!["flutter/lib/generated_bridge.dart".to_string()],
  95. // Path of output generated C header
  96. c_output: Some(vec!["flutter/macos/Runner/bridge_generated.h".to_string()]),
  97. /// Path to the installed LLVM
  98. llvm_path,
  99. // for other options use defaults
  100. ..Default::default()
  101. };
  102. // get opts from raw opts
  103. let configs = config_parse(raw_opts);
  104. // generation of rust api for ffi
  105. let all_symbols = get_symbols_if_no_duplicates(&configs).unwrap();
  106. for config in configs.iter() {
  107. frb_codegen(config, &all_symbols).unwrap();
  108. }
  109. }
  110. fn main() {
  111. hbb_common::gen_version();
  112. install_oboe();
  113. // there is problem with cfg(target_os) in build.rs, so use our workaround
  114. // let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
  115. // if target_os == "android" || target_os == "ios" {
  116. #[cfg(feature = "flutter")]
  117. gen_flutter_rust_bridge();
  118. // return;
  119. // }
  120. #[cfg(all(windows, feature = "inline"))]
  121. build_manifest();
  122. #[cfg(windows)]
  123. build_windows();
  124. let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
  125. if target_os == "macos" {
  126. #[cfg(target_os = "macos")]
  127. build_mac();
  128. println!("cargo:rustc-link-lib=framework=ApplicationServices");
  129. }
  130. println!("cargo:rerun-if-changed=build.rs");
  131. }