Skip to content

Commit cfc3700

Browse files
committed
feat: add option to build without pybind11
1 parent da66cda commit cfc3700

File tree

14 files changed

+102
-52
lines changed

14 files changed

+102
-52
lines changed

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 or the relevant tests." 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/modellib/CMakeLists.txt

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

3+
if (NOT ONNX_MLIR_ENABLE_PYBIND)
4+
return()
5+
endif()
6+
37
add_onnx_mlir_library(ModelLib
48
CategoryMapperModel.cpp
59
ConvModel.cpp

test/multiple-models/CMakeLists.txt

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

3+
if (NOT ONNX_MLIR_ENABLE_PYBIND)
4+
return()
5+
endif()
6+
37
if (NOT ONNX_MLIR_BUILD_TESTS)
48
set(EXCLUDE_FROM_ALL ON)
59
endif()

test/unit/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function(add_onnx_mlir_unittest test_name)
3434
set_tests_properties(${test_name} PROPERTIES LABELS unittest)
3535
endfunction()
3636

37-
add_onnx_mlir_unittest(TestInstrumentation
37+
if (ONNX_MLIR_ENABLE_PYBIND)
38+
add_onnx_mlir_unittest(TestInstrumentation
3839
TestInstrumentation.cpp
3940

4041
INCLUDE_DIRS PUBLIC
@@ -43,6 +44,7 @@ add_onnx_mlir_unittest(TestInstrumentation
4344
LINK_LIBS PRIVATE
4445
cruntime
4546
)
47+
endif()
4648

4749
add_subdirectory(BType)
4850
add_subdirectory(CustomFn)

0 commit comments

Comments
 (0)