7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2025 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for that specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use std::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | env, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | error::Error, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | path::{Path, PathBuf}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | process::Command, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | use vergen_gitcl::{Emitter, GitclBuilder}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | /// Adds a temporary workaround for an issue with the Rust compiler and Android |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | /// in x86_64 devices: https://github.com/rust-lang/rust/issues/109717. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | /// The workaround is based on: https://github.com/mozilla/application-services/pull/5442 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | /// TODO: I think this is obsolete with cargo-ndk 4.0.0: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | /// https://github.com/bbqsrc/cargo-ndk/commit/03c738653f935416580803b82fc6ad72c10dbebc |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | /// IMPORTANT: if you modify this, make sure to modify |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | /// [../matrix-sdk-crypto-ffi/build.rs] too! |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | fn setup_x86_64_android_workaround() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | if target_arch == "x86_64" && target_os == "android" { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | // Configure rust to statically link against the `libclang_rt.builtins` supplied |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | // with clang. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | // cargo-ndk sets CC_x86_64-linux-android to the path to `clang`, within the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | // Android NDK. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | let clang_path = PathBuf::from( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | // clang_path should now look something like |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | // `.../sdk/ndk/28.0.12674087/toolchains/llvm/prebuilt/linux-x86_64/bin/clang`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | // We strip `/bin/clang` from the end to get the toolchain path. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | let toolchain_path = clang_path |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | .ancestors() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | .nth(2) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | .expect("could not find NDK toolchain path") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | .to_str() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | .expect("NDK toolchain path is not valid UTF-8"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | let clang_version = get_clang_major_version(&clang_path); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | println!("cargo:rustc-link-search={toolchain_path}/lib/clang/{clang_version}/lib/linux/"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | // Ensure we re-run this build script if the location of the toolchain changes. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | println!("cargo:rerun-if-env-changed=CC_x86_64-linux-android"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | /// Adds a workaround for watchOS simulator builds to manually link against the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | /// CoreFoundation framework in order to avoid linker errors. Otherwise, errors |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | /// like the following may occur: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | /// = note: Undefined symbols for architecture arm64: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | /// "_CFArrayCreate", referenced from: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | /// "_CFDataCreate", referenced from: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | /// "_CFRelease", referenced from: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | /// etc. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | fn setup_watchos_simulator_workaround() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | let target = env::var("TARGET").expect("TARGET not set"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | if target.ends_with("watchos-sim") { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | println!("cargo:rustc-link-arg=-framework"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | println!("cargo:rustc-link-arg=CoreFoundation"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | /// Run the clang binary at `clang_path`, and return its major version number |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | fn get_clang_major_version(clang_path: &Path) -> String { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | let clang_output = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | Command::new(clang_path).arg("-dumpversion").output().expect("failed to start clang"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | if !clang_output.status.success() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | panic!("failed to run clang: {}", String::from_utf8_lossy(&clang_output.stderr)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | let clang_version = String::from_utf8(clang_output.stdout).expect("clang output is not utf8"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | clang_version.split('.').next().expect("could not parse clang output").to_owned() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | fn main() -> Result<(), Box<dyn Error>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | setup_x86_64_android_workaround(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | setup_watchos_simulator_workaround(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | uniffi::generate_scaffolding("./src/api.udl").expect("Building the UDL file failed"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 100 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 101 | let git_config = GitclBuilder::default().sha(true).build()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 102 | Emitter::default().add_instructions(&git_config)?.emit()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 103 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 104 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 105 | } |