Skip to content

Commit 836dd87

Browse files
rahulchaphalkarBTOdellabrown
authored
Fix windows linking (#104)
* Fix finding of OpenVINO 2024.0.0 on Windows. * Fixed linking on Windows. * Fix Linux compile error. * fix clippy error * Refactor to pass linkage kind as a parameter --------- Co-authored-by: Bradley Odell <[email protected]> Co-authored-by: Andrew Brown <[email protected]>
1 parent 6c551bb commit 836dd87

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ jobs:
6363
# path and Cargo links the binary to it in the `build.rs` script.
6464
- name: Check dynamic linking
6565
run: cargo test
66-
# TODO: the finder does not yet know how to distinguish between `*.lib` (required here) and
67-
# `*.dll` (required by runtime-linking).
68-
if: ${{ !startsWith(runner.os, 'windows') }}
6966
# Finally, run the runtime-linking tests: the binddings do not link at build time, instead
7067
# as the tests are run.
7168
- name: Check runtime linking

crates/openvino-finder/src/lib.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ macro_rules! check_and_return {
5656
};
5757
}
5858

59+
/// Distinguish which kind of library to link to.
60+
///
61+
/// The difference is important on Windows, e.g., which [requires] `*.lib` libraries when linking
62+
/// dependent libraries.
63+
///
64+
/// [requires]:
65+
/// https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation#creating-an-import-library
66+
#[derive(Clone, Copy, Debug, PartialEq)]
67+
pub enum Linking {
68+
/// Find _static_ libraries: OpenVINO comes with static libraries on some platforms (e.g.,
69+
/// Windows).
70+
Static,
71+
/// Find _dynamic_ libraries.
72+
Dynamic,
73+
}
74+
5975
/// Find the path to an OpenVINO library.
6076
///
6177
/// Because OpenVINO can be installed in quite a few ways (see module documentation), this function
@@ -87,13 +103,19 @@ macro_rules! check_and_return {
87103
/// # Panics
88104
///
89105
/// Panics if it cannot list the contents of a search directory.
90-
pub fn find(library_name: &str) -> Option<PathBuf> {
91-
let file = format!(
92-
"{}{}{}",
93-
env::consts::DLL_PREFIX,
94-
library_name,
106+
pub fn find(library_name: &str, kind: Linking) -> Option<PathBuf> {
107+
let suffix = if kind == Linking::Static {
108+
// This is a bit rudimentary but works for the top three supported platforms: `linux`,
109+
// `macos`, and `windows`.
110+
if cfg!(target_os = "windows") {
111+
".lib"
112+
} else {
113+
".a"
114+
}
115+
} else {
95116
env::consts::DLL_SUFFIX
96-
);
117+
};
118+
let file = format!("{}{}{}", env::consts::DLL_PREFIX, library_name, suffix);
97119
log::info!("Attempting to find library: {}", file);
98120

99121
// Search using the `OPENVINO_BUILD_DIR` environment variable; this may be set by users of the
@@ -220,6 +242,7 @@ const KNOWN_INSTALLATION_SUBDIRECTORIES: &[&str] = &[
220242
"runtime/lib/intel64/Release",
221243
"runtime/lib/intel64",
222244
"runtime/3rdparty/tbb/lib",
245+
"runtime/bin/intel64/Release",
223246
"runtime/bin/intel64",
224247
"runtime/3rdparty/tbb/bin",
225248
];
@@ -259,7 +282,7 @@ pub fn find_plugins_xml() -> Option<PathBuf> {
259282

260283
// Check in the same directory as the `openvino_c` library; e.g.,
261284
// `/opt/intel/openvino_.../runtime/lib/intel64/plugins.xml`.
262-
let library = find("openvino_c")?;
285+
let library = find("openvino_c", Linking::Dynamic)?;
263286
let library_parent_dir = library.parent()?;
264287
check_and_return!(library_parent_dir.join(FILE_NAME));
265288

@@ -314,7 +337,7 @@ mod test {
314337
#[test]
315338
fn find_openvino_c_locally() {
316339
env_logger::init();
317-
assert!(find("openvino_c").is_some());
340+
assert!(find("openvino_c", Linking::Dynamic).is_some());
318341
}
319342

320343
/// This test shows how the finder would discover the latest shared library on an

crates/openvino-sys/build.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ fn main() {
4646
};
4747

4848
// Find the OpenVINO libraries to link to, either from a pre-installed location or by building
49-
// from source.
49+
// from source. We always look for the dynamic libraries here.
50+
let link_kind = openvino_finder::Linking::Dynamic;
5051
let (c_api_library_path, library_search_paths) = if linking == Linking::None {
51-
(openvino_finder::find("openvino_c"), vec![])
52-
} else if let Some(path) = openvino_finder::find("openvino_c") {
52+
// Why try to find the library if we're not going to link against it? Well, this is for the
53+
// helpful Cargo warnings that get printed below if we can't find the library on the system.
54+
(openvino_finder::find("openvino_c", link_kind), vec![])
55+
} else if let Some(path) = openvino_finder::find("openvino_c", link_kind) {
5356
(Some(path), find_libraries_in_existing_installation())
5457
} else {
5558
panic!("Unable to find an OpenVINO installation on your system; build with runtime linking using `--features runtime-linking` or build from source with `OPENVINO_BUILD_DIR`.")
@@ -138,8 +141,16 @@ fn add_dynamically_linked_library(library: &str) {
138141
/// unclear how we would discover this in a system-install scenario.
139142
fn find_libraries_in_existing_installation() -> Vec<PathBuf> {
140143
let mut dirs = vec![];
144+
let link_kind = if cfg!(target_os = "windows") {
145+
// Retrieve `*.lib` files on Windows. This is important because, when linking, Windows
146+
// expects `*.lib` files. See
147+
// https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-creation#creating-an-import-library.
148+
openvino_finder::Linking::Static
149+
} else {
150+
openvino_finder::Linking::Dynamic
151+
};
141152
for library in LIBRARIES {
142-
if let Some(path) = openvino_finder::find(library) {
153+
if let Some(path) = openvino_finder::find(library, link_kind) {
143154
println!(
144155
"cargo:warning=Found library to link against: {}",
145156
path.display()

crates/openvino-sys/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub mod library {
6262
/// `find().unwrap().parent()`.
6363
pub fn find() -> Option<PathBuf> {
6464
if cfg!(feature = "runtime-linking") {
65-
openvino_finder::find("openvino_c")
65+
openvino_finder::find("openvino_c", openvino_finder::Linking::Dynamic)
6666
} else {
6767
Some(PathBuf::from(env!("OPENVINO_LIB_PATH")))
6868
}

0 commit comments

Comments
 (0)