@@ -381,7 +381,20 @@ def build_shared_lib(self, lib_name, build_info):
381381 global built_libs
382382 log .info ("building '%s' shared library" , lib_name )
383383
384- if platform_system == 'Windows' :
384+ # On macOS, distutils uses '-bundle' by default for shared objects,
385+ # which produces an MH_BUNDLE. Later, Python extension linking expects
386+ # an MH_DYLIB and fails with "unsupported mach-o filetype". To ensure
387+ # we produce a proper dynamic library, build via a tiny CMake project
388+ # (same approach we use on Windows) so that add_library(... SHARED ...)
389+ # generates an MH_DYLIB. Keep Linux using the standard path.
390+ if platform_system in ('Windows' , 'Darwin' ):
391+ if platform_system == 'Darwin' :
392+ # Ensure we set a proper install_name so the extension can load
393+ # the dylib from @loader_path (same folder as the extension).
394+ lib_file = f"{ get_lib_prefix ()} { lib_name } { get_lib_suffix ('shared' )} "
395+ install_name_flag = f"-Wl,-install_name,@loader_path/{ lib_file } "
396+ if install_name_flag not in build_info ['extra_link_args' ]:
397+ build_info ['extra_link_args' ].append (install_name_flag )
385398 self .build_mocked_cmake_lib (lib_name , build_info )
386399 return
387400 # First, compile the source code to object files in the temp directory.
@@ -400,8 +413,13 @@ def build_shared_lib(self, lib_name, build_info):
400413 lib_file = f"{ get_lib_prefix ()} { lib_name } { get_lib_suffix ('shared' )} "
401414
402415 if platform_system == 'Darwin' :
416+ # This branch is now unused for Darwin (handled above via CMake),
417+ # but keep the logic as a fallback in case of refactors.
403418 build_info ['extra_link_args' ].append (f"-Wl,-install_name,@loader_path/{ lib_file } " )
404- self .compiler .linker_so = ['-dynamiclib' if val == '-bundle' else val for val in self .compiler .linker_so ]
419+ try :
420+ self .compiler .linker_so = ['-dynamiclib' if val == '-bundle' else val for val in self .compiler .linker_so ]
421+ except Exception :
422+ pass
405423 self .compiler .link_shared_object (
406424 objects ,
407425 lib_file ,
0 commit comments