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

Skip to content

Commit 404a719

Browse files
bpo-41116: Ensure system supplied libraries are found on macOS 11 (pythonGH-23301)
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.
1 parent 453bc1d commit 404a719

File tree

4 files changed

+38
-30
lines changed

4 files changed

+38
-30
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: 4 additions & 27 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

1415
try:
@@ -176,34 +177,10 @@ def macosx_sdk_root():
176177
m = re.search(r'-isysroot\s*(\S+)', cflags)
177178
if m is not None:
178179
MACOS_SDK_ROOT = m.group(1)
179-
MACOS_SDK_SPECIFIED = MACOS_SDK_ROOT != '/'
180180
else:
181-
MACOS_SDK_ROOT = '/'
182-
MACOS_SDK_SPECIFIED = False
183-
cc = sysconfig.get_config_var('CC')
184-
tmpfile = '/tmp/setup_sdk_root.%d' % os.getpid()
185-
try:
186-
os.unlink(tmpfile)
187-
except:
188-
pass
189-
ret = run_command('%s -E -v - </dev/null 2>%s 1>/dev/null' % (cc, tmpfile))
190-
in_incdirs = False
191-
try:
192-
if ret == 0:
193-
with open(tmpfile) as fp:
194-
for line in < 57AE span class=pl-s1>fp.readlines():
195-
if line.startswith("#include <...>"):
196-
in_incdirs = True
197-
elif line.startswith("End of search list"):
198-
in_incdirs = False
199-
elif in_incdirs:
200-
line = line.strip()
201-
if line == '/usr/include':
202-
MACOS_SDK_ROOT = '/'
203-
elif line.endswith(".sdk/usr/include"):
204-
MACOS_SDK_ROOT = line[:-12]
205-
finally:
206-
os.unlink(tmpfile)
181+
MACOS_SDK_ROOT = _osx_support._default_sysroot(
182+
sysconfig.get_config_var('CC'))
183+
MACOS_SDK_SPECIFIED = MACOS_SDK_ROOT != '/'
207184

208185
return MACOS_SDK_ROOT
209186

0 commit comments

Comments
 (0)
0