build.rs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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::{fs::File, io::Write};
  19. /// Adds a temporary workaround for [an issue] with the Rust compiler and Android when
  20. /// compiling sqlite3 bundled C code.
  21. ///
  22. /// The Android NDK used to include `libgcc` for unwind support (which is required by Rust
  23. /// among others). From NDK r23, `libgcc` is removed, replaced by LLVM's `libunwind`.
  24. /// However, `libgcc` was ambiently providing other compiler builtins, one of which we
  25. /// require: `__extenddftf2` for software floating-point emulation. This is used by SQLite
  26. /// (via the `rusqlite` crate), which defines a `LONGDOUBLE_TYPE` type as `long double`.
  27. ///
  28. /// Rust uses a `compiler-builtins` crate that does not provide `__extenddftf2` because
  29. /// it involves floating-point types that are not supported by Rust.
  30. ///
  31. /// The workaround comes from [this Mozilla PR]: we tell Cargo to statically link the
  32. /// builtins from the Clang runtime provided inside the NDK, to provide this symbol.
  33. ///
  34. /// See also this [zcash issue] and [their workaround].
  35. ///
  36. /// [an issue]: https://github.com/rust-lang/rust/issues/109717
  37. /// [this Mozilla PR]: https://github.com/mozilla/application-services/pull/5442
  38. /// [unsupported]: https://github.com/rust-lang/compiler-builtins#unimplemented-functions
  39. /// [zcash issue]: https://github.com/zcash/librustzcash/issues/800
  40. /// [their workaround]: https://github.com/Electric-Coin-Company/zcash-android-wallet-sdk/blob/88058c63461f2808efc953af70db726b9f36f9b9/backend-lib/build.rs
  41. fn main() {
  42. let target_os = std::env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set");
  43. let target_arch =
  44. std::env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set");
  45. //println!("cargo:warning={target_arch}");
  46. // Add some useful debug info directly into the app itself
  47. let mut f = File::create("src/build_info.rs").unwrap();
  48. writeln!(f, "pub const TARGET_OS: &'static str = \"{target_os}\";").unwrap();
  49. writeln!(f, "pub const TARGET_ARCH: &'static str = \"{target_arch}\";").unwrap();
  50. if target_os == "android" {
  51. // Since we run this inside a container, we can just hardcore the paths directly
  52. println!("cargo:rustc-link-search=/opt/android-ndk-r29/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/21/lib/linux/");
  53. match target_arch.as_str() {
  54. "aarch64" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-aarch64-android"),
  55. "arm" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-arm-android"),
  56. "i686" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-i686-android"),
  57. "x86_64" => println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android"),
  58. // Maybe this should panic instead
  59. _ => println!(
  60. "cargo:warning='leaving linker args for {target_os}:{target_arch} unchanged"
  61. ),
  62. }
  63. }
  64. }