Skip to content

Commit c979f52

Browse files
authored
Merge pull request pytorch#13 from ynimmaga/cmake_updates
cmake updates for openvino
2 parents 7f877d4 + 81460f3 commit c979f52

File tree

4 files changed

+73
-83
lines changed

4 files changed

+73
-83
lines changed

backends/openvino/CMakeLists.txt

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,69 @@
44
# except in compliance with the License. See the license file in the root
55
# directory of this source tree for more details.
66

7+
# Set minimum required CMake version
8+
cmake_minimum_required(VERSION 3.19)
9+
10+
# Set project name
11+
project(openvino_backend_project)
12+
713
# Set C++ standard
814
set(CMAKE_CXX_STANDARD 17)
915
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1016

11-
# Ensure compile_commands are generated
17+
# Ensure compile_commands.json is generated
1218
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1319

14-
# Define common include directories
15-
set(COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
16-
17-
# Include common directories before others to ensure proper order
18-
include_directories(BEFORE ${COMMON_INCLUDE_DIRS})
19-
2020
# Set up EXECUTORCH_ROOT if not already set
2121
if(NOT EXECUTORCH_ROOT)
2222
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
2323
endif()
2424

25-
# Include utility cmake script from the executorch repository
26-
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
27-
28-
# Update common include directory for ExecuteTorch
25+
# Define common include directories
2926
set(COMMON_INCLUDE_DIRS ${EXECUTORCH_ROOT}/..)
3027

31-
# Set OpenVINO directory and include directories from environment variable
28+
# Include utility CMake scripts from ExecuteTorch
29+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
30+
31+
# Set OpenVINO directory from environment variable
3232
set(OPENVINO_DIR "$ENV{INTEL_OPENVINO_DIR}")
3333
if(NOT OPENVINO_DIR)
34-
message(FATAL_ERROR "INTEL_OPENVINO_DIR environment variable is not set.")
34+
message(FATAL_ERROR "ERROR: INTEL_OPENVINO_DIR environment variable is not set.")
3535
endif()
3636

37+
# Set OpenVINO include directories
3738
set(OPENVINO_INCLUDE_DIRS
38-
${OPENVINO_DIR}/deployment_tools/inference_engine/include
3939
${OPENVINO_DIR}/runtime/include
40+
${OPENVINO_DIR}/deployment_tools/inference_engine/include
4041
)
4142

42-
# Define OpenVINO library path
43+
# Set OpenVINO library path
4344
set(OPENVINO_LIB_PATH ${OPENVINO_DIR}/runtime/lib/intel64)
4445

45-
# Define OpenVINO libraries
46-
set(OPENVINO_LIB ${OPENVINO_LIB_PATH}/libopenvino.so)
46+
# Try to locate OpenVINO automatically
47+
find_library(OPENVINO_LIB NAMES openvino PATHS ${OPENVINO_LIB_PATH} NO_DEFAULT_PATH)
48+
if(NOT OPENVINO_LIB)
49+
message(FATAL_ERROR "ERROR: OpenVINO library (libopenvino.so) not found in ${OPENVINO_LIB_PATH}")
50+
endif()
4751

48-
# Add the OpenVINO backend library as a shared library
52+
# Define OpenVINO backend as a shared library
4953
add_library(openvino_backend SHARED)
5054

5155
# Enable exceptions and RTTI for OpenVINO backend
52-
target_compile_options(openvino_backend PRIVATE "-frtti" "-fexceptions")
56+
target_compile_options(openvino_backend PRIVATE -frtti -fexceptions)
5357

54-
# Include directories for ExecuteTorch and OpenVINO
55-
target_include_directories(
56-
openvino_backend PUBLIC
57-
${COMMON_INCLUDE_DIRS}
58-
${OPENVINO_INCLUDE_DIRS}
59-
)
58+
# Include ExecuteTorch and OpenVINO directories
59+
target_include_directories(openvino_backend PUBLIC ${COMMON_INCLUDE_DIRS} ${OPENVINO_INCLUDE_DIRS})
6060

61-
# Link OpenVINO libraries and executorch core to the backend
62-
target_link_libraries(openvino_backend PRIVATE
63-
${OPENVINO_LIB}
64-
executorch_core
65-
)
61+
# Link OpenVINO and ExecuteTorch core libraries
62+
target_link_libraries(openvino_backend PRIVATE ${OPENVINO_LIB} executorch_core)
6663

67-
# Add source files to the OpenVINO backend library
68-
target_sources(openvino_backend PRIVATE
69-
${CMAKE_CURRENT_LIST_DIR}/runtime/OpenvinoBackend.cpp
70-
)
64+
# Add source files for OpenVINO backend
65+
target_sources(openvino_backend PRIVATE ${CMAKE_CURRENT_LIST_DIR}/runtime/OpenvinoBackend.cpp)
7166

72-
# Set additional link options for shared library
67+
# Set runtime library path for OpenVINO
7368
target_link_options(openvino_backend PRIVATE -Wl,-rpath=${OPENVINO_LIB_PATH})
7469

75-
# Install the OpenVINO backend library to the lib directory
70+
# Install OpenVINO backend library to the lib directory
7671
install(TARGETS openvino_backend DESTINATION lib)
7772

backends/openvino/scripts/build.sh renamed to backends/openvino/scripts/openvino_build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ main() {
1818
# Configure the project with CMake
1919
# Note: Add any additional configuration options you need here
2020
cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \
21+
-DCMAKE_BUILD_TYPE=Release \
2122
-DEXECUTORCH_BUILD_OPENVINO=ON \
2223
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
2324
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \

examples/openvino/CMakeLists.txt

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
# except in compliance with the License. See the license file in the root
55
# directory of this source tree for more details.
66

7-
set(CMAKE_CXX_STANDARD 17)
8-
97
cmake_minimum_required(VERSION 3.19)
108
project(openvino_runner_example)
119

10+
set(CMAKE_CXX_STANDARD 17)
11+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
12+
set(CMAKE_CXX_EXTENSIONS OFF)
13+
1214
# Source root directory for executorch.
1315
if(NOT EXECUTORCH_ROOT)
1416
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
@@ -26,31 +28,20 @@ if(NOT CMAKE_BUILD_TYPE)
2628
endif()
2729

2830
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
29-
30-
# Let files say "include <executorch/path/to/header.h>".
3131
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
3232

33-
#
34-
# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
35-
#
36-
set(EXECUTORCH_SRCS_FILE
37-
"${CMAKE_CURRENT_BINARY_DIR}/../../../build/executorch_srcs.cmake"
38-
)
33+
set(EXECUTORCH_SRCS_FILE "${CMAKE_CURRENT_BINARY_DIR}/../../../build/executorch_srcs.cmake")
3934
extract_sources(${EXECUTORCH_SRCS_FILE})
4035
include(${EXECUTORCH_SRCS_FILE})
4136

42-
set(_openvino_executor_runner__srcs ${CMAKE_CURRENT_LIST_DIR}/../openvino/executor_runner/openvino_executor_runner.cpp)
43-
44-
# preprocess executor runner src files
45-
list(PREPEND _openvino_executor_runner__srcs
46-
${CMAKE_CURRENT_LIST_DIR}/../openvino/executor_runner/openvino_executor_runner.cpp
37+
set(_openvino_executor_runner__srcs
38+
${CMAKE_CURRENT_LIST_DIR}/../openvino/executor_runner/openvino_executor_runner.cpp
4739
)
4840

4941
find_package(executorch CONFIG REQUIRED)
50-
target_include_directories(executorch INTERFACE ${_common_include_directories})
51-
target_compile_options(executorch INTERFACE ${_common_compile_options})
42+
include_directories(${EXECUTORCH_INCLUDE_DIRS})
5243

53-
# portable_ops_lib
44+
# Portable Ops Library
5445
gen_selected_ops(LIB_NAME "openvino_portable_ops_lib" INCLUDE_ALL_OPS "ON")
5546
generate_bindings_for_kernels(
5647
LIB_NAME "openvino_portable_ops_lib" FUNCTIONS_YAML
@@ -59,43 +50,45 @@ generate_bindings_for_kernels(
5950
gen_operators_lib(
6051
LIB_NAME "openvino_portable_ops_lib" KERNEL_LIBS portable_kernels DEPS executorch
6152
)
62-
target_compile_options(
63-
openvino_portable_ops_lib INTERFACE -DET_EVENT_TRACER_ENABLED
64-
)
65-
target_include_directories(
66-
openvino_portable_ops_lib PUBLIC ${_common_include_directories}
67-
)
53+
target_compile_options(openvino_portable_ops_lib INTERFACE -DET_EVENT_TRACER_ENABLED)
54+
target_include_directories(openvino_portable_ops_lib PUBLIC ${_common_include_directories})
6855

69-
70-
# build executor runner
56+
# Build Executor Runner
7157
add_executable(openvino_executor_runner ${_openvino_executor_runner__srcs})
7258
target_include_directories(
73-
openvino_executor_runner PUBLIC ${_common_include_directories}
59+
openvino_executor_runner PUBLIC ${_common_include_directories} ${EXECUTORCH_ROOT}/cmake-openvino-out/third-party/gflags/include
7460
)
7561

76-
# Set the path to the library directory
77-
set(LIBRARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../../cmake-openvino-out/lib/")
78-
79-
# List the libraries you want to link (without the 'lib' prefix and file extension)
80-
set(LIBRARIES_TO_LINK ${LIBRARY_DIR}/libopenvino_backend.so
81-
${LIBRARY_DIR}/libexecutorch.a
82-
${LIBRARY_DIR}/libexecutorch_core.a
83-
${EXECUTORCH_ROOT}/third-party/gflags/build/lib/libgflags_nothreads.a
84-
${LIBRARY_DIR}/libpthreadpool.a
85-
${LIBRARY_DIR}/libextension_data_loader.a
86-
${LIBRARY_DIR}/libextension_runner_util.a
87-
)
62+
# Set Library Directory
63+
set(LIBRARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../../cmake-openvino-out/lib/;${CMAKE_CURRENT_LIST_DIR}/../../cmake-openvino-out/third-party/gflags")
64+
message(STATUS "Library directory path: ${LIBRARY_DIR}")
8865

89-
# Add the library directory to the link search path
90-
link_directories(${LIBRARY_DIR})
66+
# Locate OpenVINO Backend Library
67+
find_library(OPENVINO_BACKEND_LIB NAMES openvino_backend PATHS ${LIBRARY_DIR} NO_DEFAULT_PATH)
68+
if(NOT OPENVINO_BACKEND_LIB)
69+
message(FATAL_ERROR "OpenVINO backend library not found in ${LIBRARY_DIR}")
70+
endif()
9171

92-
# Link all libraries at once
93-
target_link_libraries(openvino_executor_runner PRIVATE ${LIBRARIES_TO_LINK} openvino_portable_ops_lib)
72+
# Locate OpenVINO Backend Library
73+
find_library(GFLAGS_LIB NAMES gflags_nothreads PATHS ${LIBRARY_DIR} NO_DEFAULT_PATH)
74+
if(NOT GFLAGS_LIB)
75+
message(FATAL_ERROR "Gflags library not found in ${LIBRARY_DIR}")
76+
endif()
9477

95-
set_target_properties(
96-
openvino_executor_runner PROPERTIES LINK_FLAGS "-Wl,-rpath='$ORIGIN'"
78+
# Link Libraries
79+
target_link_libraries(openvino_executor_runner PRIVATE
80+
${OPENVINO_BACKEND_LIB}
81+
${GFLAGS_LIB}
82+
executorch
83+
executorch_core
84+
openvino_portable_ops_lib
85+
extension_data_loader
86+
extension_runner_util
87+
pthreadpool
9788
)
9889

90+
# Ensure Proper RPATH Handling
91+
set_target_properties(openvino_executor_runner PROPERTIES INSTALL_RPATH "$ORIGIN")
9992

10093
get_filename_component(
10194
EXECUTORCH_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE

examples/openvino/openvino_build.sh renamed to examples/openvino/openvino_build_example.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ main() {
1818
# Configure the project with CMake
1919
# Note: Add any additional configuration options you need here
2020
cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \
21+
-DCMAKE_BUILD_TYPE=Release \
2122
-DEXECUTORCH_BUILD_OPENVINO=ON \
2223
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
2324
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
@@ -27,7 +28,7 @@ main() {
2728

2829

2930
# Build the project
30-
cmake --build cmake-openvino-out --target install --config Release -j5
31+
cmake --build cmake-openvino-out --target install --config Release -j$(nproc)
3132

3233
## Build example
3334
local example_dir=examples/openvino
@@ -41,7 +42,7 @@ main() {
4142
-B"${example_build_dir}" \
4243
$EXECUTORCH_ROOT/$example_dir
4344

44-
cmake --build "${example_build_dir}" -j5
45+
cmake --build "${example_build_dir}" -j$(nproc)
4546

4647
# Switch back to the original directory
4748
cd - > /dev/null

0 commit comments

Comments
 (0)