Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / third_party / om-file-format-sys / build.rs

build.rs72 lines · 2.8 KiBRaw
1use std::env;
2use std::path::Path;
3
4fn main() {
5 const LIB_NAME: &str = "omfileformatc";
6 let submodule_path = "c";
7 if !Path::new(submodule_path).exists() {
8 panic!("Submodule not found at path: {}", submodule_path);
9 }
10 println!("cargo:rerun-if-changed=build.rs");
11 println!("cargo:rerun-if-changed=src/lib.rs");
12 println!("cargo:rerun-if-changed=src/bindings_android.rs");
13 println!("cargo:rerun-if-changed=src/bindings_host.rs");
14 println!("cargo:rerun-if-changed={}", submodule_path);
15 println!("cargo:rerun-if-env-changed=CC");
16 println!("cargo:rerun-if-env-changed=SYSROOT");
17 println!("cargo:rerun-if-env-changed=TARGET");
18
19 let target = env::var("TARGET").unwrap_or_default();
20 let is_android = target.contains("android");
21 let sysroot = env::var("SYSROOT").ok();
22 let cc_env = env::var("CC").unwrap_or_default();
23 let has_ndk = sysroot.is_some() || cc_env.contains("android");
24
25 // When checking aarch64-linux-android without NDK (host `cargo check --target ...`),
26 // skip compiling C – host clang lacks Android headers. Gradle RustNative.kt sets
27 // CC to NDK clang + SYSROOT so real Android builds still compile C.
28 if is_android && !has_ndk {
29 println!("cargo:rustc-link-lib=static={}", LIB_NAME);
30 return;
31 }
32
33 let mut build = cc::Build::new();
34 if !cc_env.is_empty() {
35 build.compiler(cc_env);
36 }
37 build.include(format!("{}/include", submodule_path));
38 let src_path = format!("{}/src", submodule_path);
39 let mut c_files: Vec<_> = std::fs::read_dir(&src_path)
40 .unwrap()
41 .filter_map(|e| e.ok())
42 .map(|e| e.path())
43 .filter(|p| p.extension().and_then(|e| e.to_str()) == Some("c"))
44 .collect();
45 c_files.sort();
46 for path in c_files {
47 build.file(path);
48 }
49 // armv8-only flags
50 if env::var("CFLAGS").is_err() {
51 build.flag("-O3");
52 let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_else(|_| "aarch64".into());
53 if arch == "aarch64" {
54 build.flag("-march=armv8-a");
55 }
56 // bitunpack.c uses SSSE3 (`_mm_shuffle_epi8`) on its x86 SSE path, but
57 // nothing enables the instruction set: GCC only declares the intrinsic
58 // with `-mssse3` (MSVC declares it unconditionally, so this is a no-op
59 // there). Host-only smoke check; Android/aarch64 builds are unaffected.
60 if arch == "x86_64" || arch == "x86" {
61 build.flag_if_supported("-mssse3");
62 }
63 build.flag_if_supported("-Wdate-time");
64 build.flag_if_supported("-Werror=date-time");
65 }
66 if let Some(sysroot_path) = sysroot {
67 build.flag(format!("--sysroot={}", sysroot_path));
68 }
69 build.warnings(false);
70 build.compile(LIB_NAME);
71 println!("cargo:rustc-link-lib=static={}", LIB_NAME);
72}