From 6b63385890fec89172bf2062ad8fdb0688e72fbc Mon Sep 17 00:00:00 2001 From: liaboveall <2628370933@qq.com> Date: Thu, 21 Aug 2025 10:53:39 +0800 Subject: [PATCH 1/3] fix_mac_issue --- setup.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 6d690c6..86273fe 100755 --- a/setup.py +++ b/setup.py @@ -401,7 +401,15 @@ def build_shared_lib(self, lib_name, build_info): if platform_system == 'Darwin': build_info['extra_link_args'].append(f"-Wl,-install_name,@loader_path/{lib_file}") - self.compiler.linker_so = ['-dynamiclib' if val=='-bundle' else val for val in self.compiler.linker_so] + # On macOS, building a shared library must use '-dynamiclib' instead of '-bundle'. + # Depending on setuptools/distutils, linker_so can be a string or a list. + linker_so = getattr(self.compiler, 'linker_so', None) + if isinstance(linker_so, (list, tuple)): + self.compiler.linker_so = [('-dynamiclib' if val == '-bundle' else val) for val in linker_so] + elif isinstance(linker_so, str): + # Replace all occurrences safely in the string variant + self.compiler.linker_so = linker_so.replace('-bundle', '-dynamiclib') + # If linker_so is None or unexpected type, do nothing; default behavior will apply self.compiler.link_shared_object( objects, lib_file, @@ -677,7 +685,7 @@ def copy_extensions_to_source(self): classifiers = project_config['classifiers'], platforms = config['platforms']['platforms'], keywords = ', '.join(project_config['description']), - license = project_config['license'], + license = project_config['license']['text'], # Options install_requires=project_config['dependencies'], python_requires =project_config['requires-python'], From 2ebb25cbdbdc9184db54a529f84d06725973d3b7 Mon Sep 17 00:00:00 2001 From: liaboveall <2628370933@qq.com> Date: Thu, 21 Aug 2025 11:02:17 +0800 Subject: [PATCH 2/3] Revert "fix_mac_issue" This reverts commit 6b63385890fec89172bf2062ad8fdb0688e72fbc. --- setup.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 86273fe..6d690c6 100755 --- a/setup.py +++ b/setup.py @@ -401,15 +401,7 @@ def build_shared_lib(self, lib_name, build_info): if platform_system == 'Darwin': build_info['extra_link_args'].append(f"-Wl,-install_name,@loader_path/{lib_file}") - # On macOS, building a shared library must use '-dynamiclib' instead of '-bundle'. - # Depending on setuptools/distutils, linker_so can be a string or a list. - linker_so = getattr(self.compiler, 'linker_so', None) - if isinstance(linker_so, (list, tuple)): - self.compiler.linker_so = [('-dynamiclib' if val == '-bundle' else val) for val in linker_so] - elif isinstance(linker_so, str): - # Replace all occurrences safely in the string variant - self.compiler.linker_so = linker_so.replace('-bundle', '-dynamiclib') - # If linker_so is None or unexpected type, do nothing; default behavior will apply + self.compiler.linker_so = ['-dynamiclib' if val=='-bundle' else val for val in self.compiler.linker_so] self.compiler.link_shared_object( objects, lib_file, @@ -685,7 +677,7 @@ def copy_extensions_to_source(self): classifiers = project_config['classifiers'], platforms = config['platforms']['platforms'], keywords = ', '.join(project_config['description']), - license = project_config['license']['text'], + license = project_config['license'], # Options install_requires=project_config['dependencies'], python_requires =project_config['requires-python'], From 8656b6f6a304c417245763168ff049e1a77f3f75 Mon Sep 17 00:00:00 2001 From: liaboveall <2628370933@qq.com> Date: Thu, 21 Aug 2025 11:23:06 +0800 Subject: [PATCH 3/3] fix_mac_issue --- setup.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 6d690c6..7074eca 100755 --- a/setup.py +++ b/setup.py @@ -381,7 +381,20 @@ def build_shared_lib(self, lib_name, build_info): global built_libs log.info("building '%s' shared library", lib_name) - if platform_system=='Windows': + # On macOS, distutils uses '-bundle' by default for shared objects, + # which produces an MH_BUNDLE. Later, Python extension linking expects + # an MH_DYLIB and fails with "unsupported mach-o filetype". To ensure + # we produce a proper dynamic library, build via a tiny CMake project + # (same approach we use on Windows) so that add_library(... SHARED ...) + # generates an MH_DYLIB. Keep Linux using the standard path. + if platform_system in ('Windows', 'Darwin'): + if platform_system == 'Darwin': + # Ensure we set a proper install_name so the extension can load + # the dylib from @loader_path (same folder as the extension). + lib_file = f"{get_lib_prefix()}{lib_name}{get_lib_suffix('shared')}" + install_name_flag = f"-Wl,-install_name,@loader_path/{lib_file}" + if install_name_flag not in build_info['extra_link_args']: + build_info['extra_link_args'].append(install_name_flag) self.build_mocked_cmake_lib(lib_name, build_info) return # 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): lib_file = f"{get_lib_prefix()}{lib_name}{get_lib_suffix('shared')}" if platform_system == 'Darwin': + # This branch is now unused for Darwin (handled above via CMake), + # but keep the logic as a fallback in case of refactors. build_info['extra_link_args'].append(f"-Wl,-install_name,@loader_path/{lib_file}") - self.compiler.linker_so = ['-dynamiclib' if val=='-bundle' else val for val in self.compiler.linker_so] + try: + self.compiler.linker_so = ['-dynamiclib' if val=='-bundle' else val for val in self.compiler.linker_so] + except Exception: + pass self.compiler.link_shared_object( objects, lib_file,