@@ -246,6 +246,13 @@ def is_macosx_sdk_path(path):
246
246
or path .startswith ('/Library/' ) )
247
247
248
248
249
+ def grep_headers_for (function , headers ):
250
+ for header in headers :
251
+ with open (header , 'r' ) as f :
252
+ if function in f .read ():
253
+ return True
254
+ return False
255
+
249
256
def find_file (filename , std_dirs , paths ):
250
257
"""Searches for the directory where a given file is located,
251
258
and returns a possibly-empty list of additional directories, or None
@@ -2195,7 +2202,7 @@ def detect_ctypes(self):
2195
2202
sources = ['_ctypes/_ctypes_test.c' ],
2196
2203
libraries = ['m' ]))
2197
2204
2198
- ffi_inc = [ sysconfig .get_config_var ("LIBFFI_INCLUDEDIR" )]
2205
+ ffi_inc = sysconfig .get_config_var ("LIBFFI_INCLUDEDIR" )
2199
2206
ffi_lib = None
2200
2207
2201
2208
ffi_inc_dirs = self .inc_dirs .copy ()
@@ -2204,29 +2211,38 @@ def detect_ctypes(self):
2204
2211
return
2205
2212
ffi_in_sdk = os .path .join (macosx_sdk_root (), "usr/include/ffi" )
2206
2213
if os .path .exists (ffi_in_sdk ):
2207
- ffi_inc = [ ffi_in_sdk ]
2214
+ ffi_inc = ffi_in_sdk
2208
2215
ffi_lib = 'ffi'
2209
- sources .remove ('_ctypes/malloc_closure.c' )
2210
2216
else :
2211
2217
# OS X 10.5 comes with libffi.dylib; the include files are
2212
2218
# in /usr/include/ffi
2213
2219
ffi_inc_dirs .append ('/usr/include/ffi' )
2214
2220
2215
- if not ffi_inc or ffi_inc [0 ] == '' :
2216
- ffi_inc = find_file ('ffi.h' , [], ffi_inc_dirs )
2217
- if ffi_inc is not None :
2218
- ffi_h = ffi_inc [0 ] + '/ffi.h'
2221
+ if not ffi_inc :
2222
+ found = find_file ('ffi.h' , [], ffi_inc_dirs )
2223
+ if found :
2224
+ ffi_inc = found [0 ]
2225
+ if ffi_inc :
2226
+ ffi_h = ffi_inc + '/ffi.h'
2219
2227
if not os .path .exists (ffi_h ):
2220
2228
ffi_inc = None
2221
2229
print ('Header file {} does not exist' .format (ffi_h ))
2222
- if ffi_lib is None and ffi_inc is not None :
2230
+ if ffi_lib is None and ffi_inc :
2223
2231
for lib_name in ('ffi' , 'ffi_pic' ):
2224
2232
if (self .compiler .find_library_file (self .lib_dirs , lib_name )):
2225
2233
ffi_lib = lib_name
2226
2234
break
2227
2235
2228
2236
if ffi_inc and ffi_lib :
2229
- ext .include_dirs .extend (ffi_inc )
2237
+ ffi_headers = glob (os .path .join (ffi_inc , '*.h' ))
2238
+ if grep_headers_for ('ffi_closure_alloc' , ffi_headers ):
2239
+ try :
2240
+ sources .remove ('_ctypes/malloc_closure.c' )
2241
+ except ValueError :
2242
+ pass
2243
+ if grep_headers_for ('ffi_prep_cif_var' , ffi_headers ):
2244
+ ext .extra_compile_args .append ("-DHAVE_FFI_PREP_CIF_VAR=1" )
2245
+ ext .include_dirs .append (ffi_inc )
2230
2246
ext .libraries .append (ffi_lib )
2231
2247
self .use_system_libffi = True
2232
2248
0 commit comments