[3.9] bpo-41100: Support macOS 11 and Apple Silicon (GH-22855) (GH-23295)

* [3.9] bpo-41100: Support macOS 11 and Apple Silicon (GH-22855)

Co-authored-by:  Lawrence D’Anna <lawrence_danna@apple.com>

* Add support for macOS 11 and Apple Silicon (aka arm64)

  As a side effect of this work use the system copy of libffi on macOS, and remove the vendored copy

* Support building on recent versions of macOS while deploying to older versions

  This allows building installers on macOS 11 while still supporting macOS 10.9..
(cherry picked from commit 41761933c1)

Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>

* Back port of changes to _decimal to support arm64

* temp_dir is in test.support in 3.9
This commit is contained in:
Ronald Oussoren 2020-11-22 11:18:40 +01:00 committed by GitHub
parent 0aab3522b2
commit e8b1c038b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 1659 additions and 413 deletions

View file

@ -216,6 +216,13 @@ def is_macosx_sdk_path(path):
or path.startswith('/Library/') )
def grep_headers_for(function, headers):
for header in headers:
with open(header, 'r', errors='surrogateescape') as f:
if function in f.read():
return True
return False
def find_file(filename, std_dirs, paths):
"""Searches for the directory where a given file is located,
and returns a possibly-empty list of additional directories, or None
@ -2078,43 +2085,17 @@ class PyBuildExt(build_ext):
library_dirs=added_lib_dirs))
return True
def configure_ctypes_darwin(self, ext):
# Darwin (OS X) uses preconfigured files, in
# the Modules/_ctypes/libffi_osx directory.
ffi_srcdir = os.path.abspath(os.path.join(self.srcdir, 'Modules',
'_ctypes', 'libffi_osx'))
sources = [os.path.join(ffi_srcdir, p)
for p in ['ffi.c',
'x86/darwin64.S',
'x86/x86-darwin.S',
'x86/x86-ffi_darwin.c',
'x86/x86-ffi64.c',
'powerpc/ppc-darwin.S',
'powerpc/ppc-darwin_closure.S',
'powerpc/ppc-ffi_darwin.c',
'powerpc/ppc64-darwin_closure.S',
]]
# Add .S (preprocessed assembly) to C compiler source extensions.
self.compiler.src_extensions.append('.S')
include_dirs = [os.path.join(ffi_srcdir, 'include'),
os.path.join(ffi_srcdir, 'powerpc')]
ext.include_dirs.extend(include_dirs)
ext.sources.extend(sources)
return True
def configure_ctypes(self, ext):
if not self.use_system_libffi:
if MACOS:
return self.configure_ctypes_darwin(ext)
print('INFO: Could not locate ffi libs and/or headers')
return False
return True
def detect_ctypes(self):
# Thomas Heller's _ctypes module
self.use_system_libffi = False
if (not sysconfig.get_config_var("LIBFFI_INCLUDEDIR") and MACOS):
self.use_system_libffi = True
else:
self.use_system_libffi = '--with-system-ffi' in sysconfig.get_config_var("CONFIG_ARGS")
include_dirs = []
extra_compile_args = ['-DPy_BUILD_CORE_MODULE']
extra_link_args = []
@ -2127,11 +2108,9 @@ class PyBuildExt(build_ext):
if MACOS:
sources.append('_ctypes/malloc_closure.c')
sources.append('_ctypes/darwin/dlfcn_simple.c')
extra_compile_args.append('-DUSING_MALLOC_CLOSURE_DOT_C=1')
extra_compile_args.append('-DMACOSX')
include_dirs.append('_ctypes/darwin')
# XXX Is this still needed?
# extra_link_args.extend(['-read_only_relocs', 'warning'])
elif HOST_PLATFORM == 'sunos5':
# XXX This shouldn't be necessary; it appears that some
@ -2161,31 +2140,48 @@ class PyBuildExt(build_ext):
sources=['_ctypes/_ctypes_test.c'],
libraries=['m']))
ffi_inc = sysconfig.get_config_var("LIBFFI_INCLUDEDIR")
ffi_lib = None
ffi_inc_dirs = self.inc_dirs.copy()
if MACOS:
if '--with-system-ffi' not in sysconfig.get_config_var("CONFIG_ARGS"):
return
# OS X 10.5 comes with libffi.dylib; the include files are
# in /usr/include/ffi
ffi_inc_dirs.append('/usr/include/ffi')
ffi_in_sdk = os.path.join(macosx_sdk_root(), "usr/include/ffi")
ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
if not ffi_inc or ffi_inc[0] == '':
ffi_inc = find_file('ffi.h', [], ffi_inc_dirs)
if ffi_inc is not None:
ffi_h = ffi_inc[0] + '/ffi.h'
if not ffi_inc:
if os.path.exists(ffi_in_sdk):
ext.extra_compile_args.append("-DUSING_APPLE_OS_LIBFFI=1")
ffi_inc = ffi_in_sdk
ffi_lib = 'ffi'
else:
# OS X 10.5 comes with libffi.dylib; the include files are
# in /usr/include/ffi
ffi_inc_dirs.append('/usr/include/ffi')
if not ffi_inc:
found = find_file('ffi.h', [], ffi_inc_dirs)
if found:
ffi_inc = found[0]
if ffi_inc:
ffi_h = ffi_inc + '/ffi.h'
if not os.path.exists(ffi_h):
ffi_inc = None
print('Header file {} does not exist'.format(ffi_h))
ffi_lib = None
if ffi_inc is not None:
if ffi_lib is None and ffi_inc:
for lib_name in ('ffi', 'ffi_pic'):
if (self.compiler.find_library_file(self.lib_dirs, lib_name)):
ffi_lib = lib_name
break
if ffi_inc and ffi_lib:
ext.include_dirs.extend(ffi_inc)
ffi_headers = glob(os.path.join(ffi_inc, '*.h'))
if grep_headers_for('ffi_prep_cif_var', ffi_headers):
ext.extra_compile_args.append("-DHAVE_FFI_PREP_CIF_VAR=1")
if grep_headers_for('ffi_prep_closure_loc', ffi_headers):
ext.extra_compile_args.append("-DHAVE_FFI_PREP_CLOSURE_LOC=1")
if grep_headers_for('ffi_closure_alloc', ffi_headers):
ext.extra_compile_args.append("-DHAVE_FFI_CLOSURE_ALLOC=1")
ext.include_dirs.append(ffi_inc)
ext.libraries.append(ffi_lib)
self.use_system_libffi = True