Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ jobs:
- name: Build (openvino-sys from source)
run: cargo build --verbose --features openvino-sys/from-source
- name: Run tests
run: cargo test --verbose --features openvino-sys/from-source
# For some reason the library path is not set during the doc tests (`--doc`, see
# https://doc.rust-lang.org/cargo/commands/cargo-test.html#target-selection) so we skip them
# here: issue at https://github.com/intel/openvino-rs/issues/25.
run: cargo test --verbose --features openvino-sys/from-source --lib --tests

# Build and test from an existing OpenVINO installation inside a Docker image (i.e. download the
# binaries, then compile against these).
Expand Down
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "crates/upstream"]
path = crates/openvino-sys/upstream
url = https://github.com/openvinotoolkit/openvino
depth = 1
47 changes: 45 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/openvino-finder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ documentation = "https://docs.rs/openvino-finder"
edition = "2018"

[dependencies]
cfg-if = "1.0"
log = "0.4"

[dev-dependencies]
pretty_env_logger = "0.4"
87 changes: 60 additions & 27 deletions crates/openvino-finder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cfg_if::cfg_if;
use std::env;
use std::path::PathBuf;

Expand All @@ -13,16 +14,36 @@ pub fn find(library_name: &str) -> Option<PathBuf> {
library_name,
env::consts::DLL_SUFFIX
);
log::info!("Attempting to find library: {}", file);

// We search for the library in various different places and early-return if we find it.
macro_rules! check_and_return {
($path: expr) => {
log::debug!("Searching in: {}", $path.display());
if $path.is_file() {
log::info!("Found library at path: {}", $path.display());
return Some($path);
}
};
}

// Search using the `OPENVINO_INSTALL_DIR` environment variable; this may be set by users of the
// openvino-rs library.
if let Some(install_dir) = env::var_os(ENV_OPENVINO_INSTALL_DIR) {
let install_dir = PathBuf::from(install_dir);
for lib_dir in KNOWN_INSTALLATION_SUBDIRECTORIES {
let search_path = install_dir.join(lib_dir).join(&file);
if search_path.is_file() {
return Some(search_path);
}
check_and_return!(search_path);
}
}

// Search using the `OPENVINO_BUILD_DIR` environment variable; this may be set by users of the
// openvino-rs library.
if let Some(build_dir) = env::var_os(ENV_OPENVINO_BUILD_DIR) {
let install_dir = PathBuf::from(build_dir);
for lib_dir in KNOWN_BUILD_SUBDIRECTORIES {
let search_path = install_dir.join(lib_dir).join(&file);
check_and_return!(search_path);
}
}

Expand All @@ -32,9 +53,7 @@ pub fn find(library_name: &str) -> Option<PathBuf> {
let install_dir = PathBuf::from(install_dir);
for lib_dir in KNOWN_INSTALLATION_SUBDIRECTORIES {
let search_path = install_dir.join(lib_dir).join(&file);
if search_path.is_file() {
return Some(search_path);
}
check_and_return!(search_path);
}
}

Expand All @@ -44,9 +63,7 @@ pub fn find(library_name: &str) -> Option<PathBuf> {
if let Some(path) = env::var_os(ENV_LIBRARY_PATH) {
for lib_dir in env::split_paths(&path) {
let search_path = lib_dir.join(&file);
if search_path.is_file() {
return Some(search_path);
}
check_and_return!(search_path);
}
}

Expand All @@ -58,34 +75,43 @@ pub fn find(library_name: &str) -> Option<PathBuf> {
{
for lib_dir in KNOWN_INSTALLATION_SUBDIRECTORIES {
let search_path = default_dir.join(lib_dir).join(&file);
if search_path.is_file() {
return Some(search_path);
}
check_and_return!(search_path);
}
}

None
}

const ENV_OPENVINO_INSTALL_DIR: &'static str = "OPENVINO_INSTALL_DIR";

const ENV_OPENVINO_BUILD_DIR: &'static str = "OPENVINO_BUILD_DIR";
const ENV_INTEL_OPENVINO_DIR: &'static str = "INTEL_OPENVINO_DIR";

#[cfg(target_os = "linux")]
const ENV_LIBRARY_PATH: &'static str = "LD_LIBRARY_PATH";
#[cfg(target_os = "macos")]
const ENV_LIBRARY_PATH: &'static str = "DYLD_LIBRARY_PATH";
#[cfg(target_os = "windows")]
const ENV_LIBRARY_PATH: &'static str = "PATH";
cfg_if! {
if #[cfg(any(target_os = "linux"))] {
const ENV_LIBRARY_PATH: &'static str = "LD_LIBRARY_PATH";
} else if #[cfg(target_os = "macos")] {
const ENV_LIBRARY_PATH: &'static str = "DYLD_LIBRARY_PATH";
} else if #[cfg(target_os = "windows")] {
const ENV_LIBRARY_PATH: &'static str = "PATH";
} else {
// This may not work but seems like a sane default for target OS' not listed above.
const ENV_LIBRARY_PATH: &'static str = "LD_LIBRARY_PATH";
}
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
const DEFAULT_INSTALLATION_DIRECTORIES: &'static [&'static str] =
&["/opt/intel/openvino", "/opt/intel/openvino_2021"];
#[cfg(target_os = "windows")]
const DEFAULT_INSTALLATION_DIRECTORIES: &'static [&'static str] = &[
"C:\\Program Files (x86)\\Intel\\openvino",
"C:\\Program Files (x86)\\Intel\\openvino_2021",
];
cfg_if! {
if #[cfg(any(target_os = "linux", target_os = "macos"))] {
const DEFAULT_INSTALLATION_DIRECTORIES: &'static [&'static str] =
&["/opt/intel/openvino", "/opt/intel/openvino_2021"];
} else if #[cfg(target_os = "windows")] {
const DEFAULT_INSTALLATION_DIRECTORIES: &'static [&'static str] = &[
"C:\\Program Files (x86)\\Intel\\openvino",
"C:\\Program Files (x86)\\Intel\\openvino_2021",
];
} else {
const DEFAULT_INSTALLATION_DIRECTORIES: &'static [&'static str] = &[];
}
}

const KNOWN_INSTALLATION_SUBDIRECTORIES: &'static [&'static str] = &[
"deployment_tools/ngraph/lib",
Expand All @@ -95,6 +121,12 @@ const KNOWN_INSTALLATION_SUBDIRECTORIES: &'static [&'static str] = &[
"deployment_tools/inference_engine/external/tbb/lib",
];

const KNOWN_BUILD_SUBDIRECTORIES: &'static [&'static str] = &[
"bin/intel64/Debug/lib",
"bin/intel64/Release/lib",
"inference-engine/temp/tbb/lib",
];

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -103,6 +135,7 @@ mod test {
/// system.
#[test]
fn find_inference_engine_c_api_locally() {
pretty_env_logger::init();
assert!(find("inference_engine_c_api").is_some());
}
}