@@ -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)
@@ -226,11 +226,10 @@ endfunction()
226
226
# EMBED_CAPI_LINK_LIBS: Dependent CAPI libraries that this extension depends
227
227
# on. These will be collected for all extensions and put into an
228
228
# aggregate dylib that is linked against.
229
- # PYTHON_BINDINGS_LIBRARY: Either pybind11 or nanobind.
230
229
function (declare_mlir_python_extension name )
231
230
cmake_parse_arguments (ARG
232
231
""
233
- "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT;PYTHON_BINDINGS_LIBRARY "
232
+ "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
234
233
"SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
235
234
${ARGN} )
236
235
@@ -239,20 +238,15 @@ function(declare_mlir_python_extension name)
239
238
endif ()
240
239
set (_install_destination "src/python/${name} " )
241
240
242
- if (NOT ARG_PYTHON_BINDINGS_LIBRARY)
243
- set (ARG_PYTHON_BINDINGS_LIBRARY "pybind11" )
244
- endif ()
245
-
246
241
add_library (${name} INTERFACE )
247
242
set_target_properties (${name} PROPERTIES
248
243
# Yes: Leading-lowercase property names are load bearing and the recommended
249
244
# way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
250
- EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS;mlir_python_BINDINGS_LIBRARY "
245
+ EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
251
246
mlir_python_SOURCES_TYPE extension
252
247
mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME} "
253
248
mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS} "
254
249
mlir_python_DEPENDS ""
255
- mlir_python_BINDINGS_LIBRARY "${ARG_PYTHON_BINDINGS_LIBRARY} "
256
250
)
257
251
258
252
# Set the interface source and link_libs properties of the target
@@ -341,14 +335,12 @@ function(add_mlir_python_modules name)
341
335
elseif (_source_type STREQUAL "extension" )
342
336
# Native CPP extension.
343
337
get_target_property (_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
344
- get_target_property (_bindings_library ${sources_target} mlir_python_BINDINGS_LIBRARY)
345
338
# Transform relative source to based on root dir.
346
339
set (_extension_target "${modules_target} .extension.${_module_name} .dso" )
347
340
add_mlir_python_extension(${_extension_target} "${_module_name} "
348
341
INSTALL_COMPONENT ${modules_target}
349
342
INSTALL_DIR "${ARG_INSTALL_PREFIX} /_mlir_libs"
350
343
OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX} /_mlir_libs"
351
- PYTHON_BINDINGS_LIBRARY ${_bindings_library}
352
344
LINK_LIBS PRIVATE
353
345
${sources_target}
354
346
${ARG_COMMON_CAPI_LINK_LIBS}
@@ -753,78 +745,69 @@ endfunction()
753
745
function (add_mlir_python_extension libname extname)
754
746
cmake_parse_arguments (ARG
755
747
""
756
- "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY;PYTHON_BINDINGS_LIBRARY "
748
+ "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
757
749
"SOURCES;LINK_LIBS"
758
750
${ARGN} )
759
751
if (ARG_UNPARSED_ARGUMENTS)
760
752
message (FATAL_ERROR "Unhandled arguments to add_mlir_python_extension(${libname} , ... : ${ARG_UNPARSED_ARGUMENTS} " )
761
753
endif ()
762
754
763
755
# The extension itself must be compiled with RTTI and exceptions enabled.
764
- # Also, some warning classes triggered by pybind11 are disabled.
756
+ # Also, some warning classes triggered by nanobind are disabled.
765
757
set (eh_rtti_enable)
766
758
if (MSVC )
767
759
set (eh_rtti_enable /EHsc /GR)
768
760
elseif (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
769
761
set (eh_rtti_enable -frtti -fexceptions)
770
762
endif ()
771
763
772
- # The actual extension library produces a shared-object or DLL and has
773
- # sources that must be compiled in accordance with pybind11 needs (RTTI and
774
- # exceptions).
775
- if (NOT DEFINED ARG_PYTHON_BINDINGS_LIBRARY OR ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "pybind11" )
776
- pybind11_add_module(${libname}
777
- ${ARG_SOURCES}
778
- )
779
- elseif (ARG_PYTHON_BINDINGS_LIBRARY STREQUAL "nanobind" )
780
- nanobind_add_module(${libname}
781
- NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
782
- FREE_THREADED
783
- ${ARG_SOURCES}
784
- )
764
+ nanobind_add_module(${libname}
765
+ NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
766
+ FREE_THREADED
767
+ ${ARG_SOURCES}
768
+ )
785
769
786
- if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
787
- AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
788
- # Avoid some warnings from upstream nanobind.
789
- # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
790
- # the super project handle compile options as it wishes.
791
- get_property (NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES )
792
- target_compile_options (${NB_LIBRARY_TARGET_NAME}
793
- PRIVATE
794
- -Wall -Wextra -Wpedantic
795
- -Wno-c++98-compat-extra-semi
796
- -Wno-cast-qual
797
- -Wno-covered-switch-default
798
- -Wno-deprecated-literal-operator
799
- -Wno-nested-anon-types
800
- -Wno-unused-parameter
801
- -Wno-zero-length -array
802
- ${eh_rtti_enable} )
803
-
804
- target_compile_options (${libname}
805
- PRIVATE
806
- -Wall -Wextra -Wpedantic
807
- -Wno-c++98-compat-extra-semi
808
- -Wno-cast-qual
809
- -Wno-covered-switch-default
810
- -Wno-deprecated-literal-operator
811
- -Wno-nested-anon-types
812
- -Wno-unused-parameter
813
- -Wno-zero-length -array
814
- ${eh_rtti_enable} )
815
- endif ()
770
+ if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
771
+ AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
772
+ # Avoid some warnings from upstream nanobind.
773
+ # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
774
+ # the super project handle compile options as it wishes.
775
+ get_property (NB_LIBRARY_TARGET_NAME TARGET ${libname} PROPERTY LINK_LIBRARIES )
776
+ target_compile_options (${NB_LIBRARY_TARGET_NAME}
777
+ PRIVATE
778
+ -Wall -Wextra -Wpedantic
779
+ -Wno-c++98-compat-extra-semi
780
+ -Wno-cast-qual
781
+ -Wno-covered-switch-default
782
+ -Wno-deprecated-literal-operator
783
+ -Wno-nested-anon-types
784
+ -Wno-unused-parameter
785
+ -Wno-zero-length -array
786
+ ${eh_rtti_enable} )
787
+
788
+ target_compile_options (${libname}
789
+ PRIVATE
790
+ -Wall -Wextra -Wpedantic
791
+ -Wno-c++98-compat-extra-semi
792
+ -Wno-cast-qual
793
+ -Wno-covered-switch-default
794
+ -Wno-deprecated-literal-operator
795
+ -Wno-nested-anon-types
796
+ -Wno-unused-parameter
797
+ -Wno-zero-length -array
798
+ ${eh_rtti_enable} )
799
+ endif ()
816
800
817
- if (APPLE )
818
- # NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
819
- # doesn't declare this API as undefined in its linker flags. So we need to declare it as such
820
- # for downstream users that do not do something like `-undefined dynamic_lookup`.
821
- # Same for the rest.
822
- target_link_options (${libname} PUBLIC
823
- "LINKER:-U,_PyClassMethod_New"
824
- "LINKER:-U,_PyCode_Addr2Location"
825
- "LINKER:-U,_PyFrame_GetLasti"
826
- )
827
- endif ()
801
+ if (APPLE )
802
+ # NanobindAdaptors.h uses PyClassMethod_New to build `pure_subclass`es but nanobind
803
+ # doesn't declare this API as undefined in its linker flags. So we need to declare it as such
804
+ # for downstream users that do not do something like `-undefined dynamic_lookup`.
805
+ # Same for the rest.
806
+ target_link_options (${libname} PUBLIC
807
+ "LINKER:-U,_PyClassMethod_New"
808
+ "LINKER:-U,_PyCode_Addr2Location"
809
+ "LINKER:-U,_PyFrame_GetLasti"
810
+ )
828
811
endif ()
829
812
830
813
target_compile_options (${libname} PRIVATE ${eh_rtti_enable} )
@@ -862,11 +845,11 @@ function(add_mlir_python_extension libname extname)
862
845
if (WIN32 )
863
846
# On Windows, pyconfig.h (and by extension python.h) hardcode the version of the
864
847
# python library which will be used for linkage depending on the flavor of the build.
865
- # pybind11 has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
866
- # is not passed in as a compile definition, pybind11 undefs _DEBUG when including
848
+ # nanobind has a workaround which depends on the definition of Py_DEBUG (if Py_DEBUG
849
+ # is not passed in as a compile definition, nanobind undefs _DEBUG when including
867
850
# python.h, so that the release python library would be used).
868
- # Since mlir uses pybind11 , we can leverage their workaround by never directly
869
- # pyconfig.h or python.h and instead relying on the pybind11 headers to include the
851
+ # Since mlir uses nanobind , we can leverage their workaround by never directly
852
+ # pyconfig.h or python.h and instead relying on the nanobind headers to include the
870
853
# necessary python headers. This results in mlir always linking against the
871
854
# release python library via the (undocumented) cmake property Python3_LIBRARY_RELEASE.
872
855
target_link_libraries (${libname} PRIVATE ${Python3_LIBRARY_RELEASE} )
0 commit comments