diff --git a/setup.py b/setup.py index d0629230..a72d1034 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,7 @@ "free-threaded build.") def _ask_pkg_config(resultlist, option, result_prefix='', sysroot=False): + # returns True if pkg-config gave a non-empty result pkg_config = os.environ.get('PKG_CONFIG','pkg-config') try: p = subprocess.Popen([pkg_config, option, 'libffi'], @@ -51,6 +52,8 @@ def _ask_pkg_config(resultlist, option, result_prefix='', sysroot=False): for path in res] # resultlist[:] = res + return bool(res) + return False no_compiler_found = False def no_working_compiler_found(): @@ -114,11 +117,13 @@ def use_pkg_config(): if sys.platform == 'darwin' and os.path.exists('/usr/local/bin/brew'): use_homebrew_for_libffi() - _ask_pkg_config(include_dirs, '--cflags-only-I', '-I', sysroot=True) + found_include_dirs = _ask_pkg_config(include_dirs, '--cflags-only-I', '-I', + sysroot=True) _ask_pkg_config(extra_compile_args, '--cflags-only-other') _ask_pkg_config(library_dirs, '--libs-only-L', '-L', sysroot=True) _ask_pkg_config(extra_link_args, '--libs-only-other') _ask_pkg_config(libraries, '--libs-only-l', '-l') + return found_include_dirs def use_homebrew_for_libffi(): # We can build by setting: @@ -129,6 +134,7 @@ def use_homebrew_for_libffi(): os.environ['PKG_CONFIG_PATH'] = ( os.environ.get('PKG_CONFIG_PATH', '') + ':' + pkgconfig) +pkg_config_found_include_dirs = False if sys.platform == "win32" and uses_msvc(): if platform.machine() == "ARM64" or os.getenv("VSCMD_ARG_TGT_ARCH") == "arm64": include_dirs.append(os.path.join("src/c/libffi_arm64/include")) @@ -150,12 +156,16 @@ def use_homebrew_for_libffi(): sources.extend(os.path.join(COMPILE_LIBFFI, filename) for filename in _filenames) else: - use_pkg_config() + pkg_config_found_include_dirs = use_pkg_config() ask_supports_thread() ask_supports_sync_synchronize() -if sysconfig.get_platform().startswith('macosx'): - # priority is given to `pkg_config`, but always fall back on SDK's libffi. +if (sysconfig.get_platform().startswith('macosx') and + not pkg_config_found_include_dirs): + # Fall back on the SDK's libffi only if pkg-config did not give us an + # include dir. Adding it unconditionally can shadow pkg-config's dir: + # clang drops '-I/usr/local/include' as a duplicate of a built-in system + # dir, so the SDK's ffi.h would win over the one we link against (#297). extra_compile_args += ['-iwithsysroot/usr/include/ffi'] if 'freebsd' in sys.platform: