8000 bpo-41116: Ensure system supplied libraries are found on macOS 11 (GH… · ned-deily/cpython@250937f · GitHub
[go: up one dir, main page]

Skip to content

Commit 250937f

Browse files
miss-islingtonronaldoussoren
authored andcommitted
bpo-41116: Ensure system supplied libraries are found on macOS 11 (pythonGH-23301) (pythonGH-23455)
On macOS system provided libraries are in a shared library cache and not at their usual location. This PR teaches distutils to search in the SDK, even if there was no "-sysroot" argument in the compiler flags. (cherry picked from commit 404a719) Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
1 parent d585f52 commit 250937f

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

Lib/_osx_support.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _find_executable(executable, path=None):
5252
return executable
5353

5454

55-
def _read_output(commandstring):
55+
def _read_output(commandstring, capture_stderr=False):
5656
"""Output from successful command execution or None"""
5757
# Similar to os.popen(commandstring, "r").read(),
5858
# but without actually using os.popen because that
@@ -67,7 +67,10 @@ def _read_output(commandstring):
6767
os.getpid(),), "w+b")
6868

6969
with contextlib.closing(fp) as fp:
70-
cmd = "%s 2>/dev/null >'%s'" % (commandstring, fp.name)
70+
if capture_stderr:
71+
cmd = "%s >'%s' 2>&1" % (commandstring, fp.name)
72+
else:
73+
cmd = "%s 2>/dev/null >'%s'" % (commandstring, fp.name)
7174
return fp.read().decode('utf-8').strip() if not os.system(cmd) else None
7275

7376

@@ -145,6 +148,33 @@ def _save_modified_value(_config_vars, cv, newvalue):
145148
_config_vars[_INITPRE + cv] = oldvalue
146149
_config_vars[cv] = newvalue
147150

151+
152+
_cache_default_sysroot = None
153+
def _default_sysroot(cc):
154+
""" Returns the root of the default SDK for this system, or '/' """
155+
global _cache_default_sysroot
156+
157+
if _cache_default_sysroot is not None:
158+
return _cache_default_sysroot
159+
160+
contents = _read_output('%s -c -E -v - </dev/null' % (cc,), True)
161+
in_incdirs = False
162+
for line in contents.splitlines():
163+
if line.startswith("#include <...>"):
164+
in_incdirs = True
165+
elif line.startswith("End of search list"):
166+
in_incdirs = False
167+
elif in_incdirs:
168+
line = line.strip()
169+
if line == '/usr/include':
170+
_cache_default_sysroot = '/'
171+
elif line.endswith(".sdk/usr/include"):
172+
_cache_default_sysroot = line[:-12]
173+
if _cache_default_sysroot is None:
174+
_cache_default_sysroot = '/'
175+
176+
return _cache_default_sysroot
177+
148178
def _supports_universal_builds():
149179
"""Returns True if universal builds are supported on this system"""
150180
# As an approximation, we assume that if we are running on 10.4 or above,

Lib/distutils/unixccompiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def find_library_file(self, dirs, lib, debug=0):
290290
cflags = sysconfig.get_config_var('CFLAGS')
291291
m = re.search(r'-isysroot\s*(\S+)', cflags)
292292
if m is None:
293-
sysroot = '/'
293+
sysroot = _osx_support._default_sysroot(sysconfig.get_config_var('CC'))
294294
else:
295295
sysroot = m.group(1)
296296

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure distutils.unixxcompiler.find_library_file can find system provided libraries on macOS 11.

setup.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import sysconfig
1111
from glob import glob, escape
12+
import _osx_support
1213

1314
from distutils import log
1415
from distutils.command.build_ext import build_ext
@@ -150,31 +151,8 @@ def macosx_sdk_root():
150151
if m is not None:
151152
MACOS_SDK_ROOT = m.group(1)
152153
else:
153-
MACOS_SDK_ROOT = '/'
154-
cc = sysconfig.get_config_var('CC')
155-
tmpfile = '/tmp/setup_sdk_root.%d' % os.getpid()
156-
try:
157-
os.unlink(tmpfile)
158-
except:
159-
pass
160-
ret = os.system('%s -E -v - </dev/null 2>%s 1>/dev/null' % (cc, tmpfile))
161-
in_incdirs = False
162-
try:
163-
if ret >> 8 == 0:
164-
with open(tmpfile) as fp:
165-
for line in fp.readlines():
166-
if line.startswith("#include <...>"):
167-
in_incdirs = True
168-
elif line.startswith("End of search list"):
169-
in_incdirs = False
170-
elif in_incdirs:
171-
line = line.strip()
172-
if line == '/usr/include':
173-
MACOS_SDK_ROOT = '/'
174-
elif line.endswith(".sdk/usr/include"):
175-
MACOS_SDK_ROOT = line[:-12]
176-
finally:
177-
os.unlink(tmpfile)
154+
MACOS_SDK_ROOT = _osx_support._default_sysroot(
155+
sysconfig.get_config_var('CC'))
178156

179157
return MACOS_SDK_ROOT
180158

0 commit comments

Comments
 (0)
0