build.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{
  19. env, fs,
  20. io::Write,
  21. path::{Path, PathBuf},
  22. process::Command,
  23. };
  24. /// Adds a temporary workaround for [an issue] with the Rust compiler and Android when
  25. /// compiling sqlite3 bundled C code.
  26. ///
  27. /// The Android NDK used to include `libgcc` for unwind support (which is required by Rust
  28. /// among others). From NDK r23, `libgcc` is removed, replaced by LLVM's `libunwind`.
  29. /// However, `libgcc` was ambiently providing other compiler builtins, one of which we
  30. /// require: `__extenddftf2` for software floating-point emulation. This is used by SQLite
  31. /// (via the `rusqlite` crate), which defines a `LONGDOUBLE_TYPE` type as `long double`.
  32. ///
  33. /// Rust uses a `compiler-builtins` crate that does not provide `__extenddftf2` because
  34. /// it involves floating-point types that are not supported by Rust.
  35. ///
  36. /// The workaround comes from [this Mozilla PR]: we tell Cargo to statically link the
  37. /// builtins from the Clang runtime provided inside the NDK, to provide this symbol.
  38. ///
  39. /// See also this [zcash issue] and [their workaround].
  40. ///
  41. /// [an issue]: https://github.com/rust-lang/rust/issues/109717
  42. /// [this Mozilla PR]: https://github.com/mozilla/application-services/pull/5442
  43. /// [unsupported]: https://github.com/rust-lang/compiler-builtins#unimplemented-functions
  44. /// [zcash issue]: https://github.com/zcash/librustzcash/issues/800
  45. /// [their workaround]: https://github.com/Electric-Coin-Company/zcash-android-wallet-sdk/blob/88058c63461f2808efc953af70db726b9f36f9b9/backend-lib/build.rs
  46. fn main() {
  47. let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set");
  48. let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set");
  49. //println!("cargo:warning={target_arch}");
  50. let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set"));
  51. // Add some useful debug info directly into the app itself
  52. let mut f = fs::File::create("src/build_info.rs").unwrap();
  53. writeln!(f, "pub const TARGET_OS: &'static str = \"{target_os}\";").unwrap();
  54. writeln!(f, "pub const TARGET_ARCH: &'static str = \"{target_arch}\";").unwrap();
  55. if target_os == "windows" {
  56. embed_windows_icon(&out_dir);
  57. }
  58. if target_os == "android" {
  59. // Since we run this inside a container, we can just hardcore the paths directly
  60. println!("cargo:rustc-link-search=/opt/android-ndk-r29/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/21/lib/linux/");
  61. match target_arch.as_str() {
  62. "aarch64" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-aarch64-android"),
  63. "arm" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-arm-android"),
  64. "i686" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-i686-android"),
  65. "x86_64" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android"),
  66. // Maybe this should panic instead
  67. _ => println!(
  68. "cargo:warning='leaving linker args for {target_os}:{target_arch} unchanged"
  69. ),
  70. }
  71. }
  72. }
  73. /// Embeds `release/win/darkfi.ico` as the exe icon on Windows targets,
  74. /// using mingw's windres directly.
  75. fn embed_windows_icon(out_dir: &Path) {
  76. let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
  77. let ico_path = PathBuf::from(manifest_dir).join("release/win/darkfi.ico");
  78. println!("cargo:rerun-if-changed={}", ico_path.display());
  79. let rc_path = out_dir.join("app.rc");
  80. let icon_rc = format!("1 ICON \"{}\"", ico_path.display());
  81. fs::write(&rc_path, icon_rc).expect("write icon rc script");
  82. let res_path = out_dir.join("app.res");
  83. let status = Command::new("x86_64-w64-mingw32-windres")
  84. .arg(&rc_path)
  85. .arg("-O")
  86. .arg("coff")
  87. .arg("-o")
  88. .arg(&res_path)
  89. .status()
  90. .expect("run windres");
  91. assert!(status.success(), "windres failed");
  92. println!("cargo:rustc-link-arg-bins={}", res_path.display());
  93. }