@@ -11,6 +11,17 @@ use std::env;
11
11
use std:: fs;
12
12
use std:: path:: { Path , PathBuf } ;
13
13
14
+ // We search for the library in various different places and early-return if we find it.
15
+ macro_rules! check_and_return {
16
+ ( $path: expr) => {
17
+ log:: debug!( "Searching in: {}" , $path. display( ) ) ;
18
+ if $path. is_file( ) {
19
+ log:: info!( "Found library at path: {}" , $path. display( ) ) ;
20
+ return Some ( $path) ;
21
+ }
22
+ } ;
23
+ }
24
+
14
25
/// Find the path to an OpenVINO library. This will try:
15
26
/// - the `OPENVINO_INSTALL_DIR` environment variable with several subdirectories appended
16
27
/// - the `INTEL_OPENVINO_DIR` environment variable with several subdirectories appended
@@ -25,17 +36,6 @@ pub fn find(library_name: &str) -> Option<PathBuf> {
25
36
) ;
26
37
log:: info!( "Attempting to find library: {}" , file) ;
27
38
28
- // We search for the library in various different places and early-return if we find it.
29
- macro_rules! check_and_return {
30
- ( $path: expr) => {
31
- log:: debug!( "Searching in: {}" , $path. display( ) ) ;
32
- if $path. is_file( ) {
33
- log:: info!( "Found library at path: {}" , $path. display( ) ) ;
34
- return Some ( $path) ;
35
- }
36
- } ;
37
- }
38
-
39
39
// Search using the `OPENVINO_BUILD_DIR` environment variable; this may be set by users of the
40
40
// `openvino-rs` library.
41
41
if let Some ( build_dir) = env:: var_os ( ENV_OPENVINO_BUILD_DIR ) {
@@ -112,6 +112,7 @@ pub fn find(library_name: &str) -> Option<PathBuf> {
112
112
const ENV_OPENVINO_INSTALL_DIR : & str = "OPENVINO_INSTALL_DIR" ;
113
113
const ENV_OPENVINO_BUILD_DIR : & str = "OPENVINO_BUILD_DIR" ;
114
114
const ENV_INTEL_OPENVINO_DIR : & str = "INTEL_OPENVINO_DIR" ;
115
+ const ENV_OPENVINO_PLUGINS_XML : & str = "OPENVINO_PLUGINS_XML" ;
115
116
116
117
cfg_if ! {
117
118
if #[ cfg( any( target_os = "linux" ) ) ] {
@@ -163,6 +164,47 @@ const KNOWN_BUILD_SUBDIRECTORIES: &[&str] = &[
163
164
"temp/tbb/lib" ,
164
165
] ;
165
166
167
+ /// Find the path to the `plugins.xml` configuration file.
168
+ ///
169
+ /// OpenVINO records the location of its plugin libraries in a `plugins.xml` file. This file is
170
+ /// examined by OpenVINO on initialization; not knowing the location to this file can lead to
171
+ /// inference errors later (e.g., `Inference(GeneralError)`). OpenVINO uses the `plugins.xml` file
172
+ /// to load target-specific libraries on demand for performing inference. The `plugins.xml` file
173
+ /// maps targets (e.g., CPU) to their target-specific implementation library.
174
+ ///
175
+ /// This file can be found in multiple locations, depending on the installation mechanism. For TAR
176
+ /// installations, it is found in the same directory as the OpenVINO libraries themselves. For
177
+ /// DEB/RPM installations, it is found in a version-suffixed directory beside the OpenVINO libraries
178
+ /// (e.g., `openvino-2022.3.0/plugins.xml`).
179
+ ///
180
+ /// This function will check:
181
+ /// - the `OPENVINO_PLUGINS_XML` environment variable--this is specific to this library
182
+ /// - the same directory as the `openvino_c` shared library, as discovered by [find]
183
+ /// - the latest version directory beside the `openvino_c` shared library (i.e.,
184
+ /// `openvino-<version>/`)
185
+ pub fn find_plugins_xml ( ) -> Option < PathBuf > {
186
+ const FILE_NAME : & str = "plugins.xml" ;
187
+
188
+ // The `OPENVINO_PLUGINS_XML` should point directly to the file.
189
+ if let Some ( path) = env:: var_os ( ENV_OPENVINO_PLUGINS_XML ) {
190
+ return Some ( PathBuf :: from ( path) ) ;
191
+ }
192
+
193
+ // Check in the same directory as the `openvino_c` library; e.g.,
194
+ // `/opt/intel/openvino_.../runtime/lib/intel64/plugins.xml`.
195
+ let library = find ( "openvino_c" ) ?;
196
+ let library_parent_dir = library. parent ( ) ?;
197
+ check_and_return ! ( library_parent_dir. join( FILE_NAME ) ) ;
198
+
199
+ // Check in OpenVINO's special system installation directory; e.g.,
200
+ // `/usr/lib/x86_64-linux-gnu/openvino-2022.3.0/plugins.xml`.
201
+ let filenames = list_directory ( library_parent_dir) ?;
202
+ let versions = get_suffixes ( filenames, "openvino-" ) ;
203
+ let path = build_latest_version ( library_parent_dir, "openvino-" , versions) ?. join ( "plugins.xml" ) ;
204
+ check_and_return ! ( path) ;
205
+
206
+ None
207
+ }
166
208
167
209
#[ inline]
168
210
fn list_directory ( dir : & Path ) -> Option < impl IntoIterator < Item = String > > {
@@ -224,4 +266,19 @@ mod test {
224
266
) )
225
267
) ;
226
268
}
269
+
270
+ /// This test shows how the finder would discover the latest `plugins.xml` directory on an
271
+ /// APT installation.
272
+ #[ test]
273
+ fn find_latest_plugin_xml ( ) {
274
+ let path = build_latest_version (
275
+ & PathBuf :: from ( "/usr/lib/x86_64-linux-gnu" ) ,
276
+ "openvino-" ,
277
+ vec ! [ "2022.3.0" . into( ) , "2023.1.0" . into( ) , "2022.1.0" . into( ) ] ,
278
+ ) ;
279
+ assert_eq ! (
280
+ path,
281
+ Some ( PathBuf :: from( "/usr/lib/x86_64-linux-gnu/openvino-2023.1.0" ) )
282
+ ) ;
283
+ }
227
284
}
0 commit comments