@@ -123,12 +123,12 @@ function(mlir_generate_type_stubs)
123
123
"IMPORT_PATHS;DEPENDS_TARGETS;OUTPUTS;DEPENDS_TARGET_SRC_DEPS"
124
124
${ARGN} )
125
125
126
- # for people doing find_package(nanobind)
126
+ # for people installing a distro (e.g., pip install) of nanobind
127
127
if (EXISTS ${nanobind_DIR} /../src/stubgen.py)
128
128
set (NB_STUBGEN "${nanobind_DIR} /../src/stubgen.py" )
129
129
elseif (EXISTS ${nanobind_DIR} /../stubgen.py)
130
130
set (NB_STUBGEN "${nanobind_DIR} /../stubgen.py" )
131
- # for people using FetchContent_Declare and FetchContent_MakeAvailable
131
+ # for people using nanobind git source tree (e.g., FetchContent_Declare and FetchContent_MakeAvailable)
132
132
elseif (EXISTS ${nanobind_SOURCE_DIR} /src/stubgen.py)
133
133
set (NB_STUBGEN "${nanobind_SOURCE_DIR} /src/stubgen.py" )
134
134
elseif (EXISTS ${nanobind_SOURCE_DIR} /stubgen.py)
@@ -203,11 +203,10 @@ endfunction()
203
203
# EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
204
204
# on. These will be collected for all extensions and put into an
205
205
# aggregate dylib that is linked against.
206
- # PYTHON_BINDINGS_LIBRARY: Either pybind11 or nanobind.
207
206
function (declare_mlir_python_extension name )
208
207
cmake_parse_arguments (ARG
209
208
""
210
- "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT;PYTHON_BINDINGS_LIBRARY "
209
+ "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
211
210
"SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
212
211
${ARGN} )
213
212
@@ -216,20 +215,15 @@ function(declare_mlir_python_extension name)
216
215
endif ()
217
216
set (_install_destination "src/python/${name} " )
218
217
219
- if (NOT ARG_PYTHON_BINDINGS_LIBRARY)
220
- set (ARG_PYTHON_BINDINGS_LIBRARY "pybind11" )
221
- endif ()
222
-
223
218
add_library (${name} INTERFACE )
224
219
set_target_properties (${name} PROPERTIES
225
220
# Yes: Leading-lowercase property names are load bearing and the recommended
226
221
# way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
227
- EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS;mlir_python_BINDINGS_LIBRARY "
222
+ EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
228
223
mlir_python_SOURCES_TYPE extension
229
224
mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME} "
230
225
mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS} "
231
226
mlir_python_DEPENDS ""
232
- mlir_python_BINDINGS_LIBRARY "${ARG_PYTHON_BINDINGS_LIBRARY} "
233
227
)
234
228
235
229
# Set the interface source and link_libs properties of the target
@@ -318,14 +312,12 @@ function(add_mlir_python_modules name)
318
312
elseif (_source_type STREQUAL "extension" )
319
313
# Native CPP extension.
320
314
get_target_property (_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
321
- get_target_property (_bindings_library ${sources_target} mlir_python_BINDINGS_LIBRARY)
322
315
# Transform relative source to based on root dir.
323
316
set (_extension_target "${modules_target} .extension.${_module_name} .dso" )
324
317
add_mlir_python_extension(${_extension_target} "${_module_name} "
325
318
INSTALL_COMPONENT ${modules_target}
326
319
INSTALL_DIR "${ARG_INSTALL_PREFIX} /_mlir_libs"
327
320
OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX} /_mlir_libs"
328
- PYTHON_BINDINGS_LIBRARY ${_bindings_library}
329
321
LINK_LIBS PRIVATE
330
322
${sources_target}
331
323
${ARG_COMMON_CAPI_LINK_LIBS}
@@ -730,78 +722,69 @@ endfunction()
730
722
function (add_mlir_python_extension libname extname)
731
723
cmake_parse_arguments (ARG
732
724
""
733
- "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY;PYTHON_BINDINGS_LIBRARY "
725
+ "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
734
726
"SOURCES;LINK_LIBS"
735
727
${ARGN} )
736
728
if (ARG_UNPARSED_ARGUMENTS)
737
729
message (FATAL_ERROR "Unhandled arguments to add_mlir_python_extension(${libname} , ... : ${ARG_UNPARSED_ARGUMENTS} " )
738
730
endif ()
739
731
740
732
# The extension itself must be compiled with RTTI and exceptions enabled.
741
- # Also, some warning classes triggered by pybind11 are disabled.
733
+ # Also, some warning classes triggered by nanobind are disabled.
742
734
set (eh_rtti_enable)
743
735
if (MSVC )
744
736
set (eh_rtti_enable /EHsc /GR)
745
737
elseif (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
746
738
set (eh_rtti_enable -frtti -fexceptions)
747
739
endif ()
748
740
749
- # The actual extension library produces a shared-object or DLL and has
750
- # sources that must be compiled in accordance with pybind11 needs (RTTI and
751
- # exceptions).
752
- if (NOT DEFINED ARG_PYTHON_BINDINGS_LIBRARY OR ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "pybind11" )
753
- pybind11_add_module(${libname}
754
- ${ARG_SOURCES}
755
- )
756
- elseif (ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "nanobind" )
757
- nanobind_add_module(${libname}
758
- NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
759
- FREE_THREADED
760
- ${ARG_SOURCES}
761
- )
741
+ nanobind_add_module(${libname}
742
+ NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
743
+ FREE_THREADED
744
+ ${ARG_SOURCES}
745
+ )
762
746
763
- if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
764
- AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
765
- # Avoid some warnings from upstream nanobind.
766
- # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
767
- # the super project handle compile options as it wishes.
768
- get_property (NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES )
769
- target_compile_options (${NB_LIBRARY_TARGET_NAME}
770
- PRIVATE
771
- -Wall -Wextra -Wpedantic
772
- -Wno-c++98-compat-extra-semi
773
- -Wno-cast-qual
774
- -Wno-covered-switch-default
775
- -Wno-deprecated-literal-operator
776
- -Wno-nested-anon-types
777
- -Wno-unused-parameter
778
- -Wno-zero-length -array
779
- ${eh_rtti_enable} )
780
-
781
- target_compile_options (${libname}
782
- PRIVATE
783
- -Wall -Wextra -Wpedantic
784
- -Wno-c++98-compat-extra-semi
785
- -Wno-cast-qual
786
- -Wno-covered-switch-default
787
- -Wno-deprecated-literal-operator
788
- -Wno-nested-anon-types
789
- -Wno-unused-parameter
790
- -Wno-zero-length -array
791
- ${eh_rtti_enable} )
792
- endif ()
747
+ if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
748
+ AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
749
+ # Avoid some warnings from upstream nanobind.
750
+ # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
751
+ # the super project handle compile options as it wishes.
752
+ get_property (NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES )
753
+ target_compile_options (${NB_LIBRARY_TARGET_NAME}
754
+ PRIVATE
755
+ -Wall -Wextra -Wpedantic
756
+ -Wno-c++98-compat-extra-semi
757
+ -Wno-cast-qual
758
+ -Wno-covered-switch-default
759
+ -Wno-deprecated-literal-operator
760
+ -Wno-nested-anon-types
761
+ -Wno-unused-parameter
762
+ -Wno-zero-length -array
763
+ ${eh_rtti_enable} )
764
+
765
+ target_compile_options (${libname}
766
+ PRIVATE
767
+ -Wall -Wextra -Wpedantic
768
+ -Wno-c++98-compat-extra-semi
769
+ -Wno-cast-qual
770
+ -Wno-covered-switch-default
771
+ -Wno-deprecated-literal-operator
772
+ -Wno-nested-anon-types
773
+ -Wno-unused-parameter
774
+ -Wno-zero-length -array
775
+ ${eh_rtti_enable} )
776
+ endif ()
793
777
794
- if (APPLE )
795
- # NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
796
- # doesn't declare this API as undefined in its linker flags. So we need to declare it as such
797
- # for downstream users that do not do something like `-undefined dynamic_lookup`.
798
- # Same for the rest.
799
- target_link_options (${libname} PUBLIC
800
- "LINKER:-U,_PyClassMethod_New"
801
- "LINKER:-U,_PyCode_Addr2Location"
802
- "LINKER:-U,_PyFrame_GetLasti"
803
- )
804
- endif ()
778
+ if (APPLE )
779
+ # NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
780
+ # doesn't declare this API as undefined in its linker flags. So we need to declare it as such
781
+ # for downstream users that do not do something like `-undefined dynamic_lookup`.
782
+ # Same for the rest.
783
+ target_link_options (${libname} PUBLIC
784
+ "LINKER:-U,_PyClassMethod_New"
785
+ "LINKER:-U,_PyCode_Addr2Location"
786
+ "LINKER:-U,_PyFrame_GetLasti"
787
+ )
805
788
endif ()
806
789
807
790
target_compile_options (${libname} PRIVATE ${eh_rtti_enable} )
@@ -839,11 +822,11 @@ function(add_mlir_python_extension libname extname)
839
822
if (WIN32 )
840
823
# On Windows, pyconfig.h (and by extension python.h) hardcode the version of the
841
824
# python library which will be used for linkage depending on the flavor of the build.
842
- # pybind11 has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
843
- # is not passed in as a compile definition, pybind11 undefs _DEBUG when including
825
+ # nanobind has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
826
+ # is not passed in as a compile definition, nanobind undefs _DEBUG when including
844
827
# python.h, so that the release python library would be used).
845
- # Since mlir uses pybind11 , we can leverage their workaround by never directly
846
- # pyconfig.h or python.h and instead relying on the pybind11 headers to include the
828
+ # Since mlir uses nanobind , we can leverage their workaround by never directly
829
+ # pyconfig.h or python.h and instead relying on the nanobind headers to include the
847
830
# necessary python headers. This results in mlir always linking against the
848
831
# release python library via the (undocumented) cmake property Python3_LIBRARY_RELEASE.
849
832
target_link_libraries (${libname} PRIVATE ${Python3_LIBRARY_RELEASE} )
0 commit comments