atagen

matrix-rust-sdk

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