1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
fn main() {
use std::path::PathBuf;
let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
if target_env != "ohos" {
return;
}
// ── OHOS NDK library search path ─────────────────────────────────────────
//
// The OHOS NDK places platform libraries under:
// $OHOS_NDK_HOME/sysroot/usr/lib/<arch>-linux-ohos/
// where OHOS_NDK_HOME points to the `native/` directory inside the SDK.
//
// Set the OHOS_NDK_HOME environment variable to the root of your OHOS NDK
// installation before running `cargo build`. Typical paths:
//
// Linux/macOS:
// export OHOS_NDK_HOME=/path/to/ohos-sdk/linux/native
//
// Windows:
// set OHOS_NDK_HOME=C:\ohos-sdk\windows\native
//
// The `native/` directory contains `sysroot/`, `llvm/`, `build-tools/`, etc.
//
// The NDK is bundled with DevEco Studio and can also be downloaded from:
// https://developer.huawei.com/consumer/cn/develop
//
// Required system libraries (linked via #[link] in src/ohos/ffi.rs):
// libnative_display_manager.so — OH_NativeDisplayManager APIs
// libnative_avscreen_capture.so — OH_AVScreenCapture APIs
// libnative_media_core.so — OH_AVBuffer helpers
// libnative_buffer.so — OH_NativeBuffer helpers
let ndk_home = std::env::var_os("OHOS_NDK_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| {
// Fall back to OHOS_SDK_HOME/<platform>/native if OHOS_NDK_HOME is not set.
// OHOS_SDK_HOME is sometimes set to the SDK root (e.g. ~/.ohos/sdk/default).
let platform = if cfg!(target_os = "windows") {
"windows"
} else {
"linux"
};
std::env::var_os("OHOS_SDK_HOME")
.map(|sdk_home| PathBuf::from(sdk_home).join(platform).join("native"))
.unwrap_or_default()
});
if ndk_home.as_os_str().is_empty() {
// Emit a warning but don't hard-fail; the user may be using a custom
// linker configuration or cross-compilation wrapper that already
// provides the necessary search paths.
println!(
"cargo:warning=OHOS_NDK_HOME is not set. \
Make sure the OHOS NDK library paths are available to the linker \
(libnative_display_manager, libnative_avscreen_capture, \
libnative_media_core, libnative_buffer)."
);
return;
}
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
// Map Rust target arch names to the OHOS triple component.
let ohos_arch = match target_arch.as_str() {
"aarch64" => "aarch64",
"arm" => "arm",
"x86_64" => "x86_64",
other => {
println!(
"cargo:warning=Unrecognised OHOS target arch '{other}'. Skipping NDK link-search."
);
return;
}
};
// OHOS_NDK_HOME already points to the `native/` directory, so the sysroot
// library path is: $OHOS_NDK_HOME/sysroot/usr/lib/<arch>-linux-ohos/
let lib_dir = ndk_home
.join("sysroot")
.join("usr")
.join("lib")
.join(format!("{ohos_arch}-linux-ohos"));
println!("cargo:rustc-link-search=native={}", lib_dir.display());
// Some NDK versions also place stub libraries directly under sysroot/usr/lib/.
let sysroot_lib = ndk_home.join("sysroot").join("usr").join("lib");
println!("cargo:rustc-link-search=native={}", sysroot_lib.display());
// Re-run this script whenever the NDK home variable changes.
println!("cargo:rerun-if-env-changed=OHOS_NDK_HOME");
println!("cargo:rerun-if-env-changed=OHOS_SDK_HOME");
}