Skip to content

Commit ff883bc

Browse files
use system libffi for Mac OS 10.15 and up
This updates setup.py to default to the system libffi on Mac OS 10.15 and forward. It also updates detect_ctypes to prefer finding libffi in the Xcode SDK, rather than /usr/include
1 parent 1648c99 commit ff883bc

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

setup.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ def macosx_sdk_specified():
229229
macosx_sdk_root()
230230
return MACOS_SDK_SPECIFIED
231231

232+
def is_macosx_at_least(vers):
233+
if MACOS:
234+
dep_target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
235+
if dep_target:
236+
return tuple(map(int, dep_target.split('.'))) >= vers
237+
return False
238+
232239

233240
def is_macosx_sdk_path(path):
234241
"""
@@ -2136,7 +2143,12 @@ def configure_ctypes(self, ext):
21362143

21372144
def detect_ctypes(self):
21382145
# Thomas Heller's _ctypes module
2139-
self.use_system_libffi = False
2146+
2147+
if not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and is_macosx_at_least((10,15)):
2148+
self.use_system_libffi = True
2149+
else:
2150+
self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS")
2151+
21402152
include_dirs = []
21412153
extra_compile_args = ['-DPy_BUILD_CORE_MODULE']
21422154
extra_link_args = []
@@ -2183,24 +2195,31 @@ def detect_ctypes(self):
21832195
sources=['_ctypes/_ctypes_test.c'],
21842196
libraries=['m']))
21852197

2198+
ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
2199+
ffi_lib = None
2200+
21862201
ffi_inc_dirs = self.inc_dirs.copy()
21872202
if MACOS:
2188-
if '--with-system-ffi' not in sysconfig.get_config_var("CONFIG_ARGS"):
2203+
if not self.use_system_libffi:
21892204
return
2190-
# OS X 10.5 comes with libffi.dylib; the include files are
2191-
# in /usr/include/ffi
2192-
ffi_inc_dirs.append('/usr/include/ffi')
2205+
ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi")
2206+
if os.path.exists(ffi_in_sdk):
2207+
ffi_inc = [ffi_in_sdk]
2208+
ffi_lib = 'ffi'
2209+
sources.remove('_ctypes/malloc_closure.c')
2210+
else:
2211+
# OS X 10.5 comes with libffi.dylib; the include files are
2212+
# in /usr/include/ffi
2213+
ffi_inc_dirs.append('/usr/include/ffi')
21932214

2194-
ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
21952215
if not ffi_inc or ffi_inc[0] == '':
21962216
ffi_inc = find_file('ffi.h', [], ffi_inc_dirs)
21972217
if ffi_inc is not None:
21982218
ffi_h = ffi_inc[0] + '/ffi.h'
21992219
if not os.path.exists(ffi_h):
22002220
ffi_inc = None
22012221
print('Header file {} does not exist'.format(ffi_h))
2202-
ffi_lib = None
2203-
if ffi_inc is not None:
2222+
if ffi_lib is None and ffi_inc is not None:
22042223
for lib_name in ('ffi', 'ffi_pic'):
22052224
if (self.compiler.find_library_file(self.lib_dirs, lib_name)):
22062225
ffi_lib = lib_name

0 commit comments

Comments
 (0)