Skip to content

Commit

Permalink
feat(sys): Support explicit dynamic linking of onnxruntime (#295)
Browse files Browse the repository at this point in the history
Previously, if static library files were found, the build script in ort-sys
would always prefer static linking.

This PR introduces a new env variable, ORT_PREFER_DYNAMIC_LINK, that can be
set (to 1 or "true") to use dynamic linking even if the static library files are
present in the onnxruntime path.

Additionally, since cuda and tensorrt require dynamic linking, static linking is
disabled when these features are present.
  • Loading branch information
bradneuman authored Oct 11, 2024
1 parent c882c58 commit ae7b594
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ort-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ONNXRUNTIME_VERSION: &str = "1.19.2";

const ORT_ENV_SYSTEM_LIB_LOCATION: &str = "ORT_LIB_LOCATION";
const ORT_ENV_SYSTEM_LIB_PROFILE: &str = "ORT_LIB_PROFILE";
const ORT_ENV_PREFER_DYNAMIC_LINK: &str = "ORT_PREFER_DYNAMIC_LINK";
#[cfg(feature = "download-binaries")]
const ORT_EXTRACT_DIR: &str = "onnxruntime";

Expand Down Expand Up @@ -148,6 +149,18 @@ fn static_link_prerequisites(using_pyke_libs: bool) {
}
}

fn prefer_dynamic_linking() -> bool {
// If the cuda or tensorrt features are enabled, we need to use dynamic linking.
if cfg!(feature = "cuda") || cfg!(feature = "tensorrt") {
return true;
}

match env::var(ORT_ENV_PREFER_DYNAMIC_LINK) {
Ok(val) => val == "1" || val.to_lowercase() == "true",
Err(_) => false,
}
}

fn prepare_libort_dir() -> (PathBuf, bool) {
if let Ok(lib_dir) = env::var(ORT_ENV_SYSTEM_LIB_LOCATION) {
let lib_dir = PathBuf::from(lib_dir);
Expand All @@ -174,7 +187,7 @@ fn prepare_libort_dir() -> (PathBuf, bool) {
if lib_dir.join(platform_format_lib("onnxruntime")).exists() {
println!("cargo:rustc-link-lib=static=onnxruntime");
needs_link = false;
} else {
} else if !prefer_dynamic_linking() {
#[allow(clippy::type_complexity)]
let static_configs: Vec<(PathBuf, PathBuf, PathBuf, Box<dyn Fn(PathBuf, &String) -> PathBuf>)> = vec![
(lib_dir.join(&profile), lib_dir.join("lib"), lib_dir.join("_deps"), Box::new(|p: PathBuf, profile| p.join(profile))),
Expand Down Expand Up @@ -415,6 +428,7 @@ fn try_setup_with_pkg_config() -> bool {
fn real_main(link: bool) {
println!("cargo:rerun-if-env-changed={}", ORT_ENV_SYSTEM_LIB_LOCATION);
println!("cargo:rerun-if-env-changed={}", ORT_ENV_SYSTEM_LIB_PROFILE);
println!("cargo:rerun-if-env-changed={}", ORT_ENV_PREFER_DYNAMIC_LINK);

let (install_dir, needs_link) = prepare_libort_dir();

Expand All @@ -426,7 +440,7 @@ fn real_main(link: bool) {
let static_lib_file_name = if target_os.contains("windows") { "onnxruntime.lib" } else { "libonnxruntime.a" };

let static_lib_path = lib_dir.join(static_lib_file_name);
if static_lib_path.exists() {
if !prefer_dynamic_linking() && static_lib_path.exists() {
println!("cargo:rustc-link-lib=static=onnxruntime");
} else {
println!("cargo:rustc-link-lib=onnxruntime");
Expand Down

0 comments on commit ae7b594

Please sign in to comment.