Skip to content

Commit 67fe38c

Browse files
committed
feat(sys): allow overriding libstdc++/libc++ linkage
1 parent 4c9f324 commit 67fe38c

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

ort-sys/build.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const ONNXRUNTIME_VERSION: &str = "1.19.2";
99
const ORT_ENV_SYSTEM_LIB_LOCATION: &str = "ORT_LIB_LOCATION";
1010
const ORT_ENV_SYSTEM_LIB_PROFILE: &str = "ORT_LIB_PROFILE";
1111
const ORT_ENV_PREFER_DYNAMIC_LINK: &str = "ORT_PREFER_DYNAMIC_LINK";
12+
const ORT_ENV_CXX_STDLIB: &str = "ORT_CXX_STDLIB";
13+
const ENV_CXXSTDLIB: &str = "CXXSTDLIB"; // Used by the `cc` crate - we should mirror if this is set for other C++ crates
1214
#[cfg(feature = "download-binaries")]
1315
const ORT_EXTRACT_DIR: &str = "onnxruntime";
1416

@@ -133,13 +135,26 @@ fn add_search_dir<P: AsRef<Path>>(base: P) {
133135
}
134136

135137
fn static_link_prerequisites(using_pyke_libs: bool) {
136-
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
137-
if target_os == "macos" || target_os == "ios" {
138-
println!("cargo:rustc-link-lib=c++");
138+
let target_triple = env::var("TARGET").unwrap();
139+
140+
let cpp_link_stdlib = if let Ok(stdlib) = env::var(ORT_ENV_CXX_STDLIB).or_else(|_| env::var(ENV_CXXSTDLIB)) {
141+
if stdlib.is_empty() { None } else { Some(stdlib) }
142+
} else if target_triple.contains("msvc") {
143+
None
144+
} else if target_triple.contains("apple") {
145+
Some("c++".to_string())
146+
} else if target_triple.contains("android") {
147+
Some("c++_shared".to_string())
148+
} else {
149+
Some("stdc++".to_string())
150+
};
151+
if let Some(cpp_link_stdlib) = cpp_link_stdlib {
152+
println!("cargo:rustc-link-lib={cpp_link_stdlib}");
153+
}
154+
155+
if target_triple.contains("apple") {
139156
println!("cargo:rustc-link-lib=framework=Foundation");
140-
} else if target_os == "linux" || target_os == "android" {
141-
println!("cargo:rustc-link-lib=stdc++");
142-
} else if target_os == "windows" && (using_pyke_libs || cfg!(feature = "directml")) {
157+
} else if target_triple.contains("windows") && (using_pyke_libs || cfg!(feature = "directml")) {
143158
println!("cargo:rustc-link-lib=dxguid");
144159
println!("cargo:rustc-link-lib=DXCORE");
145160
println!("cargo:rustc-link-lib=DXGI");
@@ -437,6 +452,8 @@ fn real_main(link: bool) {
437452
println!("cargo:rerun-if-env-changed={}", ORT_ENV_SYSTEM_LIB_LOCATION);
438453
println!("cargo:rerun-if-env-changed={}", ORT_ENV_SYSTEM_LIB_PROFILE);
439454
println!("cargo:rerun-if-env-changed={}", ORT_ENV_PREFER_DYNAMIC_LINK);
455+
println!("cargo:rerun-if-env-changed={}", ORT_ENV_CXX_STDLIB);
456+
println!("cargo:rerun-if-env-changed={}", ENV_CXXSTDLIB);
440457

441458
let (install_dir, needs_link) = prepare_libort_dir();
442459

0 commit comments

Comments
 (0)