@@ -228,22 +228,27 @@ endfunction()
228228# aggregate dylib that is linked against.
229229function (declare_mlir_python_extension name )
230230 cmake_parse_arguments (ARG
231- ""
232- "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT"
231+ "SUPPORT_LIB "
232+ "ROOT_DIR;MODULE_NAME;ADD_TO_PARENT;SOURCES_TYPE "
233233 "SOURCES;PRIVATE_LINK_LIBS;EMBED_CAPI_LINK_LIBS"
234234 ${ARGN} )
235235
236236 if (NOT ARG_ROOT_DIR)
237237 set (ARG_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR} " )
238238 endif ()
239+ if (ARG_SUPPORT_LIB)
240+ set (SOURCES_TYPE "support" )
241+ else ()
242+ set (SOURCES_TYPE "extension" )
243+ endif ()
239244 set (_install_destination "src/python/${name} " )
240245
241246 add_library (${name} INTERFACE )
242247 set_target_properties (${name} PROPERTIES
243248 # Yes: Leading-lowercase property names are load bearing and the recommended
244249 # way to do this: https://gitlab.kitware.com/cmake/cmake/-/issues/19261
245250 EXPORT_PROPERTIES "mlir_python_SOURCES_TYPE;mlir_python_EXTENSION_MODULE_NAME;mlir_python_EMBED_CAPI_LINK_LIBS;mlir_python_DEPENDS"
246- mlir_python_SOURCES_TYPE extension
251+ mlir_python_SOURCES_TYPE " ${SOURCES_TYPE} "
247252 mlir_python_EXTENSION_MODULE_NAME "${ARG_MODULE_NAME} "
248253 mlir_python_EMBED_CAPI_LINK_LIBS "${ARG_EMBED_CAPI_LINK_LIBS} "
249254 mlir_python_DEPENDS ""
@@ -318,8 +323,21 @@ function(add_mlir_python_modules name)
318323 "ROOT_PREFIX;INSTALL_PREFIX"
319324 "COMMON_CAPI_LINK_LIBS;DECLARED_SOURCES"
320325 ${ARGN} )
326+
327+ # build nanobind shared lib first
328+ if (NB_ABI MATCHES "[0-9]t" )
329+ set (_ft "-ft" )
330+ endif ()
331+ # nanobind does a string match on the suffix to figure out whether to build
332+ # the lib with free threading...
333+ set (NB_LIBRARY_TARGET_NAME "nanobind${_ft} -${MLIR_BINDINGS_PYTHON_NB_DOMAIN} " )
334+ nanobind_build_library(${NB_LIBRARY_TARGET_NAME} AS_SYSINCLUDE)
335+ if (CMAKE_SYSTEM_NAME STREQUAL "Linux" )
336+ target_link_options (${NB_LIBRARY_TARGET_NAME} PRIVATE "-Wl,-z,undefs" )
337+ endif ()
338+
321339 # Helper to process an individual target.
322- function (_process_target modules_target sources_target)
340+ function (_process_target modules_target sources_target support_libs )
323341 get_target_property (_source_type ${sources_target} mlir_python_SOURCES_TYPE)
324342
325343 if (_source_type STREQUAL "pure" )
@@ -337,16 +355,19 @@ function(add_mlir_python_modules name)
337355 get_target_property (_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
338356 # Transform relative source to based on root dir.
339357 set (_extension_target "${modules_target} .extension.${_module_name} .dso" )
340- add_mlir_python_extension(${_extension_target} "${_module_name} "
358+ add_mlir_python_extension(${_extension_target} "${_module_name} " ${NB_LIBRARY_TARGET_NAME}
341359 INSTALL_COMPONENT ${modules_target}
342360 INSTALL_DIR "${ARG_INSTALL_PREFIX} /_mlir_libs"
343361 OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX} /_mlir_libs"
344362 LINK_LIBS PRIVATE
345363 ${sources_target}
346364 ${ARG_COMMON_CAPI_LINK_LIBS}
365+ ${support_libs}
347366 )
348367 add_dependencies (${modules_target} ${_extension_target} )
349368 mlir_python_setup_extension_rpath(${_extension_target} )
369+ elseif (_source_type STREQUAL "support" )
370+ # do nothing because already built
350371 else ()
351372 message (SEND_ERROR "Unrecognized source type '${_source_type} ' for python source target ${sources_target} " )
352373 return ()
@@ -356,8 +377,33 @@ function(add_mlir_python_modules name)
356377 # Build the modules target.
357378 add_custom_target (${name} ALL )
358379 _flatten_mlir_python_targets(_flat_targets ${ARG_DECLARED_SOURCES} )
380+
381+ # Build all support libs first
382+ set (_mlir_python_support_libs)
359383 foreach (sources_target ${_flat_targets} )
360- _process_target(${name} ${sources_target} )
384+ get_target_property (_source_type ${sources_target} mlir_python_SOURCES_TYPE)
385+ if (_source_type STREQUAL "support" )
386+ get_target_property (_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
387+ set (_extension_target "${name} .extension.${_module_name} .dso" )
388+ add_mlir_python_extension(${_extension_target} "${_module_name} " ${NB_LIBRARY_TARGET_NAME}
389+ INSTALL_COMPONENT ${name}
390+ INSTALL_DIR "${ARG_INSTALL_PREFIX} /_mlir_libs"
391+ OUTPUT_DIRECTORY "${ARG_ROOT_PREFIX} /_mlir_libs"
392+ SUPPORT_LIB
393+ LINK_LIBS PRIVATE
394+ LLVMSupport
395+ Python::Module
396+ ${sources_target}
397+ ${ARG_COMMON_CAPI_LINK_LIBS}
398+ )
399+ add_dependencies (${name} ${_extension_target} )
400+ mlir_python_setup_extension_rpath(${_extension_target} )
401+ list (APPEND _mlir_python_support_libs "${_extension_target} " )
402+ endif ()
403+ endforeach ()
404+
405+ foreach (sources_target ${_flat_targets} )
406+ _process_target(${name} ${sources_target} ${_mlir_python_support_libs} )
361407 endforeach ()
362408
363409 # Create an install target.
@@ -741,9 +787,9 @@ endfunction()
741787################################################################################
742788# Build python extension
743789################################################################################
744- function (add_mlir_python_extension libname extname)
790+ function (add_mlir_python_extension libname extname nb_library_target_name )
745791 cmake_parse_arguments (ARG
746- ""
792+ "SUPPORT_LIB "
747793 "INSTALL_COMPONENT;INSTALL_DIR;OUTPUT_DIRECTORY"
748794 "SOURCES;LINK_LIBS"
749795 ${ARGN} )
@@ -760,41 +806,57 @@ function(add_mlir_python_extension libname extname)
760806 set (eh_rtti_enable -frtti -fexceptions)
761807 endif ()
762808
763- nanobind_add_module(${libname}
764- NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
765- FREE_THREADED
766- NB_SHARED
767- ${ARG_SOURCES}
768- )
809+ if (NOT MLIR_BINDINGS_PYTHON_NB_DOMAIN)
810+ set (MLIR_BINDINGS_PYTHON_NB_DOMAIN "mlir" CACHE STRING "" FORCE)
811+ endif ()
812+
813+ if (ARG_SUPPORT_LIB)
814+ add_library (${libname} SHARED ${ARG_SOURCES} )
815+ if (CMAKE_SYSTEM_NAME STREQUAL "Linux" )
816+ target_link_options (${libname} PRIVATE "-Wl,-z,undefs" )
817+ endif ()
818+ nanobind_link_options(${libname} )
819+ target_compile_definitions (${libname} PRIVATE NB_DOMAIN=${MLIR_BINDINGS_PYTHON_NB_DOMAIN} )
820+ if (MSVC )
821+ set_property (TARGET ${libname} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON )
822+ endif ()
823+ else ()
824+ nanobind_add_module(${libname}
825+ NB_DOMAIN ${MLIR_BINDINGS_PYTHON_NB_DOMAIN}
826+ FREE_THREADED
827+ NB_SHARED
828+ ${ARG_SOURCES}
829+ )
830+ endif ()
831+ target_link_libraries (${libname} PRIVATE ${nb_library_target_name} )
769832
770833 if (NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
771834 AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
772835 # Avoid some warnings from upstream nanobind.
773836 # If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
774837 # 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}
838+ target_compile_options (${nb_library_target_name}
777839 PRIVATE
778840 -Wno-c++98-compat-extra-semi
779- -Wno-cast-qual
780- -Wno-covered-switch-default
781- -Wno-deprecated-literal-operator
782- -Wno-nested-anon-types
783- -Wno-unused-parameter
784- -Wno-zero-length -array
785- -Wno-missing-field-initializers
841+ -Wno-cast-qual
842+ -Wno-covered-switch-default
843+ -Wno-deprecated-literal-operator
844+ -Wno-nested-anon-types
845+ -Wno-unused-parameter
846+ -Wno-zero-length -array
847+ -Wno-missing-field-initializers
786848 ${eh_rtti_enable} )
787849
788850 target_compile_options (${libname}
789851 PRIVATE
790852 -Wno-c++98-compat-extra-semi
791- -Wno-cast-qual
792- -Wno-covered-switch-default
793- -Wno-deprecated-literal-operator
794- -Wno-nested-anon-types
795- -Wno-unused-parameter
796- -Wno-zero-length -array
797- -Wno-missing-field-initializers
853+ -Wno-cast-qual
854+ -Wno-covered-switch-default
855+ -Wno-deprecated-literal-operator
856+ -Wno-nested-anon-types
857+ -Wno-unused-parameter
858+ -Wno-zero-length -array
859+ -Wno-missing-field-initializers
798860 ${eh_rtti_enable} )
799861 endif ()
800862
@@ -813,11 +875,16 @@ function(add_mlir_python_extension libname extname)
813875 target_compile_options (${libname} PRIVATE ${eh_rtti_enable} )
814876
815877 # Configure the output to match python expectations.
878+ if (ARG_SUPPORT_LIB)
879+ set (_no_soname OFF )
880+ else ()
881+ set (_no_soname ON )
882+ endif ()
816883 set_target_properties (
817884 ${libname} PROPERTIES
818885 LIBRARY_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIRECTORY}
819886 OUTPUT_NAME "${extname} "
820- NO_SONAME ON
887+ NO_SONAME ${_no_soname}
821888 )
822889
823890 if (WIN32 )
0 commit comments