diff --git a/tensorflow-sys/README.md b/tensorflow-sys/README.md index 6872695e96..84da1fa7fb 100644 --- a/tensorflow-sys/README.md +++ b/tensorflow-sys/README.md @@ -60,6 +60,9 @@ compiled library will be picked up. **macOS Note**: Via [Homebrew](https://brew.sh/), you can just run `brew install libtensorflow`. +To statically link Tensorflow, set the environment variable +`TENSORFLOW_LIB_STATIC` to the directory containing `libtensorflow.a` + ## Resources [bazel]: http://www.bazel.io diff --git a/tensorflow-sys/build.rs b/tensorflow-sys/build.rs index 7b74be5be0..0d623d8443 100644 --- a/tensorflow-sys/build.rs +++ b/tensorflow-sys/build.rs @@ -44,6 +44,14 @@ fn main() { return; } + if check_static_link() { + log!( + "Returing early because {} is being statically linked", + LIBRARY + ); + return; + } + if check_windows_lib() { log!("Returning early because {} was already found", LIBRARY); return; @@ -469,3 +477,20 @@ fn check_bazel() -> Result<(), Box> { } Ok(()) } + +fn check_static_link() -> bool { + if let Ok(path) = env::var("TENSORFLOW_LIB_STATIC") { + println!("cargo:rustc-link-search=native={}", path); + println!("cargo:rustc-link-lib=static={}", LIBRARY); + #[cfg(target_os = "linux")] + println!("cargo:rustc-link-lib=dylib=stdc++"); + #[cfg(target_os = "macos")] + { + println!("cargo:rustc-link-lib=c++"); + println!("cargo:rustc-link-lib=framework=foundation"); + } + true + } else { + false + } +}