Skip to content

Commit 4e9c1be

Browse files
authored
Merge pull request #407 from gvartany/grigory.AIESW-3006.build-without-pybind11
Add option to build without pybind11
2 parents d6bca38 + 1eb6d40 commit 4e9c1be

File tree

25 files changed

+131
-72
lines changed

25 files changed

+131
-72
lines changed

.github/workflows/ubuntu-build-intree.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jobs:
5252
-DLLVM_ENABLE_PROJECTS=mlir \
5353
-DLLVM_EXTERNAL_PROJECTS="onnx-mlir" \
5454
-DONNX_MLIR_ENABLE_STABLEHLO=OFF \
55+
-DONNX_MLIR_ENABLE_PYBIND=OFF \
5556
-DLLVM_EXTERNAL_ONNX_MLIR_SOURCE_DIR=. \
5657
-DLLVM_TARGETS_TO_BUILD=host \
5758
-DLLVM_BUILD_TOOLS=OFF \

CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ option(ONNX_MLIR_INSTALL_HEADERS "Install onnx-mlir headers" ON)
1515
option(ONNX_MLIR_INSTALL_LIBS "Install onnx-mlir libraries" ON)
1616
option(ONNX_MLIR_INSTALL_PYTHON_EXTENSIONS "Install onnx-mlir python bindings" ON)
1717
option(ONNX_MLIR_ENABLE_PYRUNTIME_LIGHT "Set to ON for building Python driver of running the compiled model without llvm-project." OFF)
18+
option(ONNX_MLIR_ENABLE_PYBIND "Set to OFF for building without pybind11 and features or tests depending on it." ON)
19+
20+
if (NOT ONNX_MLIR_ENABLE_PYBIND)
21+
set(ONNX_MLIR_ENABLE_PYRUNTIME_LIGHT OFF)
22+
set(ONNX_MLIR_INSTALL_PYTHON_EXTENSIONS OFF)
23+
endif()
1824

1925
set(CMAKE_CXX_STANDARD 17)
2026

@@ -183,9 +189,13 @@ if (ONNX_MLIR_ENABLE_PYRUNTIME_LIGHT)
183189
add_subdirectory(third_party/onnx)
184190
add_subdirectory(third_party/pybind11)
185191
else()
186-
set(pybind11_FIND_QUIETLY ON)
192+
if (ONNX_MLIR_ENABLE_PYBIND)
193+
set(pybind11_FIND_QUIETLY ON)
194+
add_subdirectory(third_party/pybind11)
195+
else()
196+
set(BUILD_ONNX_PYTHON FALSE) # prevent onnx from using pybind
197+
endif()
187198
add_subdirectory(third_party/onnx EXCLUDE_FROM_ALL)
188-
add_subdirectory(third_party/pybind11)
189199
add_subdirectory(third_party/rapidcheck EXCLUDE_FROM_ALL)
190200

191201
if (ONNX_MLIR_ENABLE_STABLEHLO)

docs/doc_example/CMakeLists.txt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,30 @@ add_onnx_mlir_executable(OMRuntimeTest
7777

7878
set_output_directory(OMRuntimeTest BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
7979

80-
add_onnx_mlir_executable(OMRuntimeCppTest
81-
main.cpp
80+
if (ONNX_MLIR_ENABLE_PYBIND)
81+
add_onnx_mlir_executable(OMRuntimeCppTest
82+
main.cpp
8283

83-
NO_INSTALL
84+
NO_INSTALL
8485

85-
INCLUDE_DIRS PRIVATE
86-
${ONNX_MLIR_SRC_ROOT}/include
86+
INCLUDE_DIRS PRIVATE
87+
${ONNX_MLIR_SRC_ROOT}/include
8788

88-
LINK_LIBS PRIVATE
89-
OMCompiler
90-
OMExecutionSession
91-
)
89+
LINK_LIBS PRIVATE
90+
OMCompiler
91+
OMExecutionSession
92+
)
9293

93-
set_output_directory(OMRuntimeCppTest BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
94+
set_output_directory(OMRuntimeCppTest BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
9495

95-
add_test(NAME OMRuntimeTest COMMAND OMRuntimeTest WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
96-
set_property(TARGET OMRuntimeTest PROPERTY FOLDER "Docs")
97-
set_tests_properties(OMRuntimeTest PROPERTIES LABELS doc-example)
98-
add_dependencies(doc-example OMRuntimeTest)
96+
add_test(NAME OMRuntimeTest COMMAND OMRuntimeTest WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
97+
set_property(TARGET OMRuntimeTest PROPERTY FOLDER "Docs")
98+
set_tests_properties(OMRuntimeTest PROPERTIES LABELS doc-example)
99+
add_dependencies(doc-example OMRuntimeTest)
99100

100-
add_test(NAME OMRuntimeCppTest COMMAND OMRuntimeCppTest WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
101-
set_property(TARGET OMRuntimeCppTest PROPERTY FOLDER "Docs")
102-
set_tests_properties(OMRuntimeCppTest PROPERTIES LABELS doc-example)
103-
set_tests_properties(OMRuntimeCppTest PROPERTIES ENVIRONMENT "ONNX_MLIR_LIBRARY_PATH=${ONNX_MLIR_LIBRARY_PATH}")
104-
add_dependencies(doc-example OMRuntimeCppTest)
101+
add_test(NAME OMRuntimeCppTest COMMAND OMRuntimeCppTest WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
102+
set_property(TARGET OMRuntimeCppTest PROPERTY FOLDER "Docs")
103+
set_tests_properties(OMRuntimeCppTest PROPERTIES LABELS doc-example)
104+
set_tests_properties(OMRuntimeCppTest PROPERTIES ENVIRONMENT "ONNX_MLIR_LIBRARY_PATH=${ONNX_MLIR_LIBRARY_PATH}")
105+
add_dependencies(doc-example OMRuntimeCppTest)
106+
endif()

include/onnx-mlir/Runtime/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3-
if(ONNX_MLIR_INSTALL_HEADERS)
3+
if(ONNX_MLIR_INSTALL_HEADERS AND ONNX_MLIR_ENABLE_PYBIND)
44
install(FILES OMEntryPoint.h DESTINATION include/onnx-mlir/Runtime)
55
install(FILES OMInstrument.h DESTINATION include/onnx-mlir/Runtime)
66
install(FILES OMSignature.h DESTINATION include/onnx-mlir/Runtime)

src/Compiler/CMakeLists.txt

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,14 @@ add_onnx_mlir_library(OMCompilerUtils
209209
# CompilerUtils does not require cruntime or jniruntime to build,
210210
# however, they are required for execution when using the EmitLib or EmitJNI
211211
# options
212-
add_dependencies(OMCompilerUtils cruntime)
213-
if (ONNX_MLIR_ENABLE_JNI)
214-
add_dependencies(OMCompilerUtils jniruntime)
215-
endif()
216-
if (TARGET omp)
217-
add_dependencies(OMCompilerUtils ompruntime)
212+
if (ONNX_MLIR_ENABLE_PYBIND)
213+
add_dependencies(OMCompilerUtils cruntime)
214+
if (ONNX_MLIR_ENABLE_JNI)
215+
add_dependencies(OMCompilerUtils jniruntime)
216+
endif()
217+
if (TARGET omp)
218+
add_dependencies(OMCompilerUtils ompruntime)
219+
endif()
218220
endif()
219221

220222
add_onnx_mlir_library(OMCompiler
@@ -239,29 +241,31 @@ if (NOT BUILD_SHARED_LIBS)
239241
target_compile_definitions(OMCompiler PUBLIC ONNX_MLIR_BUILT_AS_STATIC)
240242
endif()
241243

242-
pybind11_add_module(PyCompile PyOMCompileSession.cpp)
243-
add_dependencies(PyCompile onnx_proto)
244-
if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
245-
target_compile_options(PyCompile PRIVATE /EHsc /GR)
246-
elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
247-
target_compile_options(PyCompile PRIVATE -frtti -fexceptions)
248-
endif()
249-
250-
target_compile_definitions(PyCompile
251-
PRIVATE
252-
$<TARGET_PROPERTY:onnx,COMPILE_DEFINITIONS>
253-
)
254-
target_include_directories(PyCompile
255-
PRIVATE
256-
$<TARGET_PROPERTY:onnx,INCLUDE_DIRECTORIES>
257-
)
258-
target_link_libraries(PyCompile
259-
PRIVATE
260-
OMCompiler
261-
)
244+
if (ONNX_MLIR_ENABLE_PYBIND)
245+
pybind11_add_module(PyCompile PyOMCompileSession.cpp)
246+
add_dependencies(PyCompile onnx_proto)
247+
if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
248+
target_compile_options(PyCompile PRIVATE /EHsc /GR)
249+
elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU")
250+
target_compile_options(PyCompile PRIVATE -frtti -fexceptions)
251+
endif()
262252

263-
if(ONNX_MLIR_INSTALL_PYTHON_EXTENSIONS)
264-
install(TARGETS PyCompile
265-
DESTINATION lib
253+
target_compile_definitions(PyCompile
254+
PRIVATE
255+
$<TARGET_PROPERTY:onnx,COMPILE_DEFINITIONS>
256+
)
257+
target_include_directories(PyCompile
258+
PRIVATE
259+
$<TARGET_PROPERTY:onnx,INCLUDE_DIRECTORIES>
266260
)
261+
target_link_libraries(PyCompile
262+
PRIVATE
263+
OMCompiler
264+
)
265+
266+
if(ONNX_MLIR_INSTALL_PYTHON_EXTENSIONS)
267+
install(TARGETS PyCompile
268+
DESTINATION lib
269+
)
270+
endif()
267271
endif()

src/Runtime/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
# SPDX-License-Identifier: Apache-2.0
33

4+
# Runtime depends on pybind11, skip it if unavailable
5+
if (NOT ONNX_MLIR_ENABLE_PYBIND)
6+
return()
7+
endif()
8+
49
if (NOT ONNX_MLIR_ENABLE_PYRUNTIME_LIGHT)
510
add_subdirectory(jni)
611
add_subdirectory(omp)

test/accelerators/NNPA/backend/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
# Disabled when pybind unavailable.
4+
if (NOT ONNX_MLIR_ENABLE_PYBIND)
5+
return()
6+
endif()
7+
8+
39
set(ONNX_BACKENDTEST_SRC_DIR ${ONNX_MLIR_SRC_ROOT}/test/backend)
410

511
file(GENERATE

test/backend/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
# Disabled when pybind unavailable.
4+
if (NOT ONNX_MLIR_ENABLE_PYBIND)
5+
return()
6+
endif()
7+
38
file(GENERATE
49
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/conftest.py
510
INPUT ${CMAKE_CURRENT_SOURCE_DIR}/conftest.py

test/mlir/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ else()
2121
set(ONNX_MLIR_STABLEHLO_ENABLED 0)
2222
endif()
2323

24+
if (ONNX_MLIR_ENABLE_PYBIND)
25+
set(ONNX_MLIR_PYBIND_ENABLED 1)
26+
else()
27+
set(ONNX_MLIR_PYBIND_ENABLED 0)
28+
endif()
29+
2430
# Set LLVM_DEFAULT_EXTERNAL_LIT to an empty string to avoid warnings about the path
2531
# when using multi-config generators such as VS or Xcode
2632
set(LLVM_DEFAULT_EXTERNAL_LIT "")

test/mlir/driver/check_passthrough_options.mlir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REQUIRES: pybind
2+
13
// RUN: onnx-mlir -Xopt --data-sections -tag="test" -v %s -o %t 2>&1 | FileCheck --check-prefix=OPT %s
24
// RUN: onnx-mlir -Xllc --data-sections -tag="test" -v %s -o %t 2>&1 | FileCheck --check-prefix=LLC %s
35
// RUN: onnx-mlir -mllvm --data-sections -tag="test" -v %s -o %t 2>&1 | FileCheck --check-prefix=LLVM %s

0 commit comments

Comments
 (0)