@@ -138,7 +138,7 @@ fn add_dynamically_linked_library(library: &str) {
138138///
139139/// It would be preferable to use pkg-config here to retrieve the libraries when they are
140140/// installed system-wide but there are issues:
141- /// - OpenVINO does not install itself a system library, e.g., through ldconfig.
141+ /// - OpenVINO does not install itself a system library, e.g., through ldconfig.;
142142/// - OpenVINO relies on a `plugins.xml` file for finding target-specific libraries
143143/// and it is unclear how we would discover this in a system-install scenario.
144144fn find_libraries_in_existing_installation ( ) -> Vec < PathBuf > {
@@ -166,37 +166,56 @@ fn find_libraries_in_existing_installation() -> Vec<PathBuf> {
166166/// Build OpenVINO with CMake. TODO this currently will not work when the crate is published
167167/// because the `upstream` directory will not fit inside the 10MB crate limit. To solve this, we
168168/// could retrieve the sources (cringe), e.g., with `git2`.
169+ ///
170+ /// See https://github.com/openvinotoolkit/openvino/wiki/BuildingForLinux.
169171fn build_from_source_using_cmake ( ) -> ( Option < PathBuf > , Vec < PathBuf > ) {
170172 let out = PathBuf :: from ( env:: var ( "OUT_DIR" ) . unwrap ( ) ) ;
173+ let out_as_str = out. to_str ( ) . unwrap ( ) ;
171174
172175 fn cmake ( out_dir : & str ) -> cmake:: Config {
173176 let mut config = cmake:: Config :: new ( "upstream" ) ;
174177 config
175178 . very_verbose ( true )
176- . define ( "NGRAPH_ONNX_IMPORT_ENABLE" , "ON" )
177- . define ( "ENABLE_OPENCV" , "OFF" )
179+ // Disable code maintenance features.
178180 . define ( "ENABLE_CPPLINT" , "OFF" )
181+ . define ( "ENABLE_CLANG_FORMAT" , "OFF" )
182+ . define ( "ENABLE_NCC_STYLE" , "OFF" )
183+ // Disable unnecessary targets; these are enabled by default (see
184+ // https://github.com/openvinotoolkit/openvino/wiki/CMakeOptionsForCustomCompilation).
185+ // We still need `ENABLE_OV_IR_FRONTEND` (enabled by default) since it is what reads the
186+ // OpenVINO model files. Still unsure if `ENABLE_GAPI_PREPROCESSING` is needed.
187+ . define ( "ENABLE_OPENCV" , "OFF" )
188+ . define ( "ENABLE_OV_ONNX_FRONTEND" , "OFF" )
189+ . define ( "ENABLE_OV_PADDLE_FRONTEND" , "OFF" )
190+ . define ( "ENABLE_OV_PDPD_FRONTEND" , "OFF" )
191+ . define ( "ENABLE_OV_TF_FRONTEND" , "OFF" )
192+ . define ( "ENABLE_SAMPLES" , "OFF" )
179193 // As described in https://github.com/intel/openvino-rs/issues/8, the OpenVINO source
180194 // includes redundant moves. These were previously warnings but newer compilers treat
181195 // them as errors.
182196 . cxxflag ( "-Wno-error=redundant-move" )
197+ . cxxflag ( "-Wno-error=uninitialized" )
183198 // Because OpenVINO by default wants to build its binaries in its own tree, we must
184199 // specify that we actually want them in Cargo's output directory.
185200 . define ( "OUTPUT_ROOT" , out_dir) ;
201+
202+ // Enable or disable each plugin's CMake feature. It is unclear if this adding/removing of
203+ // CMake features is truly needed since we are manually choosing the plugin CMake targets,
204+ // but we do it here to be safe.
205+ apply_plugin_features ( & mut config) ;
206+
186207 config
187208 }
188209
189210 // Specifying the build targets reduces the build time somewhat; this one will trigger
190- // builds for other necessary shared libraries (e.g., `openvino`).
191- let build_path = cmake ( out. to_str ( ) . unwrap ( ) )
192- . build_target ( "openvino_c" )
193- . build ( ) ;
211+ // builds for other necessary shared libraries (e.g., `openvino`, `tbb`).
212+ let build_path = cmake ( out_as_str) . build_target ( "openvino_c" ) . build ( ) ;
194213
195214 // Unfortunately, `openvino_c` will not build the OpenVINO plugins used for
196215 // the actual computation. Here we re-run CMake for each plugin the user specifies using
197216 // Cargo features (see `Cargo.toml`).
198217 for plugin in get_plugin_target_from_features ( ) {
199- cmake ( out . to_str ( ) . unwrap ( ) ) . build_target ( plugin) . build ( ) ;
218+ cmake ( out_as_str ) . build_target ( plugin) . build ( ) ;
200219 }
201220
202221 // Collect the locations of the libraries. Note that ngraph should also be found with the
@@ -212,7 +231,7 @@ fn build_from_source_using_cmake() -> (Option<PathBuf>, Vec<PathBuf>) {
212231 // pre-installed libtbb (on some systems, the nodes_count symbol is not present in the
213232 // system-provided libtbb) so it may be important to include OpenVINO's version of libtbb
214233 // here.
215- let tbb_libraries = dir ( "upstream/inference-engine/ temp/tbb/lib" ) ;
234+ let tbb_libraries = dir ( "upstream/temp/tbb/lib" ) ;
216235 visit_dirs ( & tbb_libraries, & |from : PathBuf | {
217236 let to = openvino_libraries. join ( from. file_name ( ) . unwrap ( ) ) ;
218237 println ! ( "Copying {} to {}" , from. display( ) , to. display( ) ) ;
@@ -232,37 +251,66 @@ fn build_from_source_using_cmake() -> (Option<PathBuf>, Vec<PathBuf>) {
232251}
233252
234253/// Determine CMake targets for the various OpenVINO plugins. The plugin mapping is available in
235- /// OpenVINO's `plugins.xml` file and, usign that, this function wires up the exposed Cargo
236- /// features of openvino-sys to the correct CMake target.
254+ /// OpenVINO's `plugins.xml` file and, using that, this function wires up the exposed Cargo
255+ /// features of openvino-sys to the correct CMake target. Run `cmake --build . --target help` in
256+ /// an upstream CMake build directory to see a list of these.
237257fn get_plugin_target_from_features ( ) -> Vec < & ' static str > {
238258 let mut plugins = vec ! [ ] ;
239259 if cfg ! ( feature = "all" ) {
240260 plugins. push ( "ie_plugins" )
241261 } else {
242262 if cfg ! ( feature = "cpu" ) {
243- plugins. push ( "MKLDNNPlugin " )
263+ plugins. push ( "openvino_intel_cpu_plugin " )
244264 }
245265 if cfg ! ( feature = "gpu" ) {
246- plugins. push ( "clDNNPlugin " )
266+ plugins. push ( "openvino_intel_gpu_plugin " )
247267 }
248268 if cfg ! ( feature = "gna" ) {
249- plugins. push ( "GNAPlugin " )
269+ plugins. push ( "openvino_intel_gna_plugin " )
250270 }
251271 if cfg ! ( feature = "hetero" ) {
252- plugins. push ( "HeteroPlugin" )
272+ plugins. push ( "openvino_hetero_plugin" )
273+ }
274+ if cfg ! ( feature = "auto" ) {
275+ plugins. push ( "openvino_auto_plugin" )
253276 }
254- if cfg ! ( feature = "multi " ) {
255- plugins. push ( "MultiDevicePlugin " )
277+ if cfg ! ( feature = "auto_batch " ) {
278+ plugins. push ( "openvino_auto_batch_plugin " )
256279 }
257280 if cfg ! ( feature = "myriad" ) {
258- plugins. push ( "myriadPlugin " )
281+ plugins. push ( "openvino_intel_myriad_plugin " )
259282 }
260283 }
261284 assert ! ( !plugins. is_empty( ) ) ;
262285 plugins
263286}
264287
265- /// According to https://docs.rs/cmake/0.1.44/cmake/struct.Config.html#method.profile, the cmake
288+ /// Apply CMake `ENABLE_*` features for each of Cargo's plugin features; see `Cargo.toml`. For
289+ /// example, running `cargo build --features gpu` should set `ENABLE_INTEL_GPU=ON` and set all
290+ /// others to `OFF`. See
291+ /// https://github.com/openvinotoolkit/openvino/wiki/CMakeOptionsForCustomCompilation.
292+ fn apply_plugin_features ( config : & mut cmake:: Config ) {
293+ macro_rules! apply {
294+ ( $cargo_feature: literal, $cmake_feature: literal) => {
295+ if cfg!( feature = $cargo_feature) {
296+ config. define( $cmake_feature, "ON" ) ;
297+ } else {
298+ config. define( $cmake_feature, "OFF" ) ;
299+ }
300+ } ;
301+ }
302+
303+ apply ! ( "cpu" , "ENABLE_INTEL_CPU" ) ;
304+ apply ! ( "gpu" , "ENABLE_INTEL_GPU" ) ;
305+ apply ! ( "gna" , "ENABLE_INTEL_GNA" ) ;
306+ apply ! ( "myriad" , "ENABLE_INTEL_MYRIAD" ) ;
307+ apply ! ( "auto" , "ENABLE_AUTO" ) ;
308+ apply ! ( "auto_batch" , "ENABLE_AUTO_BATCH" ) ;
309+ apply ! ( "hetero" , "ENABLE_HETERO" ) ;
310+ apply ! ( "multi" , "ENABLE_MULTI" ) ;
311+ }
312+
313+ /// According to https://docs.rs/cmake/0.1.44/cmake/struct.Config.html#method.profile, the cmake;
266314/// crate will tries to infer the appropriate CMAKE_BUILD_TYPE from a combination of Rust opt-level
267315/// and debug. To avoid duplicating https://docs.rs/cmake/0.1.44/src/cmake/lib.rs.html#553-559, this
268316/// helper searches for build type directories and appends it to the path if a result is found; this
0 commit comments