|
1 | 1 | use std::env; |
2 | | -use std::path::{Path, PathBuf}; |
3 | | - |
4 | | -/// When cross-compiling for Android, the `cmake` crate (cmake-rs) sets |
5 | | -/// `CMAKE_SYSTEM_NAME=Android` but does **not** set `CMAKE_ANDROID_NDK`. |
6 | | -/// CMake ≥ 3.21's `Platform/Android-Determine.cmake` then fails because it |
7 | | -/// cannot locate the NDK. |
8 | | -/// |
9 | | -/// Detection strategy: |
10 | | -/// 1. Check well-known environment variables (`ANDROID_NDK_ROOT`, etc.). |
11 | | -/// 2. Infer from the C compiler path that `cc` selects for this target. |
12 | | -/// NDK compilers live at `<NDK>/toolchains/llvm/prebuilt/<host>/bin/…`, |
13 | | -/// so we walk up from the compiler looking for the `toolchains` dir. |
14 | | -fn detect_android_ndk() -> Option<PathBuf> { |
15 | | - // 1. Prefer explicit env vars (same ones CMake itself checks) |
16 | | - for var in ["ANDROID_NDK_ROOT", "ANDROID_NDK_HOME", "ANDROID_NDK"] { |
17 | | - if let Ok(val) = env::var(var) { |
18 | | - let p = PathBuf::from(&val); |
19 | | - if p.is_dir() { |
20 | | - return Some(p); |
21 | | - } |
22 | | - } |
23 | | - } |
24 | | - |
25 | | - // 2. Infer from the C compiler path |
26 | | - let compiler = cc::Build::new() |
27 | | - .cargo_metadata(false) |
28 | | - .opt_level(0) |
29 | | - .warnings(false) |
30 | | - .try_get_compiler() |
31 | | - .ok()?; |
32 | | - let cc_path = compiler.path().canonicalize().ok()?; |
33 | | - let mut dir: &Path = cc_path.parent()?; |
34 | | - loop { |
35 | | - if dir.file_name().and_then(|n| n.to_str()) == Some("toolchains") |
36 | | - && dir.join("llvm").is_dir() |
37 | | - { |
38 | | - return dir.parent().map(|p| p.to_path_buf()); |
39 | | - } |
40 | | - dir = dir.parent()?; |
41 | | - } |
42 | | -} |
| 2 | +use std::path::PathBuf; |
43 | 3 |
|
44 | 4 | fn main() { |
45 | | - let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); |
46 | | - |
47 | | - // Build libversion static library using cmake |
48 | | - let mut cmake_cfg = cmake::Config::new("libversion"); |
49 | | - cmake_cfg.build_target("libversion_static"); |
50 | | - |
51 | | - // Work around cmake-rs not setting CMAKE_ANDROID_NDK for Android targets. |
52 | | - |
53 | | - if target_os == "android" |
54 | | - && let Some(ndk_root) = detect_android_ndk() |
55 | | - { |
56 | | - cmake_cfg.define("CMAKE_ANDROID_NDK", &ndk_root); |
57 | | - } |
58 | | - |
59 | | - let dst = cmake_cfg.build(); |
60 | | - |
61 | | - let build_dir = dst.join("build").join("libversion"); |
62 | | - println!("cargo:rustc-link-search=native={}", build_dir.display()); |
63 | | - println!("cargo:rustc-link-lib=static=version"); |
| 5 | + // Build libversion static library using cc. |
| 6 | + // The cmake-generated headers (config.h, export.h) are pre-committed under |
| 7 | + // generated/ — run `bash scripts/generate-headers.sh` to regenerate them |
| 8 | + // whenever the libversion submodule is updated. |
| 9 | + cc::Build::new() |
| 10 | + .file("libversion/libversion/compare.c") |
| 11 | + .file("libversion/libversion/private/compare.c") |
| 12 | + .file("libversion/libversion/private/parse.c") |
| 13 | + .include("libversion") // source headers (libversion/version.h, etc.) |
| 14 | + .include("generated") // pre-generated headers (libversion/config.h, libversion/export.h) |
| 15 | + .define("LIBVERSION_STATIC_DEFINE", None) |
| 16 | + .compile("version"); |
64 | 17 |
|
65 | 18 | // Generate FFI bindings via bindgen |
66 | 19 | let bindings = bindgen::Builder::default() |
67 | 20 | .header("wrapper.h") |
68 | | - // cmake-generated headers (config.h, export.h) are in build/libversion/ |
69 | | - .clang_arg(format!("-I{}", dst.join("build").display())) |
70 | | - // source headers are under libversion/ (the submodule root) |
71 | | - .clang_arg("-Ilibversion") |
72 | | - // static build: LIBVERSION_EXPORT expands to nothing |
| 21 | + .clang_arg("-Igenerated") // pre-generated config.h, export.h |
| 22 | + .clang_arg("-Ilibversion") // source headers |
73 | 23 | .clang_arg("-DLIBVERSION_STATIC_DEFINE") |
74 | 24 | .default_enum_style(bindgen::EnumVariation::Consts) |
75 | 25 | .allowlist_function("version_compare.*") |
|
0 commit comments