tensorflow_sys_runtime/
finder.rs1use cfg_if::cfg_if;
2use std::env;
3
4pub fn find(library_name: &str) -> Option<PathBuf> {
9 let file = format!(
10 "{}{}{}",
11 env::consts::DLL_PREFIX,
12 library_name,
13 env::consts::DLL_SUFFIX
14 );
15 log::info!("Attempting to find library: {}", file);
16
17 macro_rules! check_and_return {
19 ($path: expr) => {
20 let path = $path;
21 log::debug!("Searching in: {}", path.display());
22 if path.is_file() {
23 log::info!("Found library at path: {}", path.display());
24 return Some(path);
25 }
26 };
27 }
28
29 if let Some(install_dir) = env::var_os(ENV_TENSORFLOW_DIR) {
31 check_and_return!(PathBuf::from(install_dir).join(&file));
32 }
33
34 if let Some(path) = env::var_os(ENV_LIBRARY_PATH) {
37 for lib_dir in env::split_paths(&path) {
38 check_and_return!(lib_dir.join(&file));
39 }
40 }
41
42 for default_dir in DEFAULT_INSTALLATION_DIRECTORIES
44 .iter()
45 .map(PathBuf::from)
46 .filter(|d| d.is_dir())
47 {
48 check_and_return!(default_dir.join(&file));
49 }
50
51 None
52}
53
54const ENV_TENSORFLOW_DIR: &'static str = "TENSORFLOW_DIR";
55
56cfg_if! {
57 if #[cfg(any(target_os = "linux"))] {
58 const ENV_LIBRARY_PATH: &'static str = "LD_LIBRARY_PATH";
59 } else if #[cfg(target_os = "macos")] {
60 const ENV_LIBRARY_PATH: &'static str = "DYLD_LIBRARY_PATH";
61 } else if #[cfg(target_os = "windows")] {
62 const ENV_LIBRARY_PATH: &'static str = "PATH";
63 } else {
64 const ENV_LIBRARY_PATH: &'static str = "LD_LIBRARY_PATH";
66 }
67}
68
69cfg_if! {
70 if #[cfg(any(target_os = "linux", target_os = "macos"))] {
71 const DEFAULT_INSTALLATION_DIRECTORIES: &'static [&'static str] =
72 &["/usr/local/lib", "/usr/local/lib/libtensorflow"];
73 } else if #[cfg(target_os = "windows")] {
74 const DEFAULT_INSTALLATION_DIRECTORIES: &'static [&'static str] = &[
75 "C:\\Program Files (x86)\\Tensorflow",
76 "C:\\Program Files (x86)\\tensorflow",
77 ];
78 } else {
79 const DEFAULT_INSTALLATION_DIRECTORIES: &'static [&'static str] = &[];
80 }
81}
82