Skip to content

Commit 0aab352

Browse files
bpo-41116: Ensure system supplied libraries are found on macOS 11 (GH-23301) (GH-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 <[email protected]>
1 parent 4f87126 commit 0aab352

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

@@ -125,6 +128,33 @@ def _save_modified_value(_config_vars, cv, newvalue):
125128
_config_vars[_INITPRE + cv] = oldvalue
126129
_config_vars[cv] = newvalue
127130

131+
132+
_cache_default_sysroot = None
133+
def _default_sysroot(cc):
134+
""" Returns the root of the default SDK for this system, or '/' """
135+
global _cache_default_sysroot
136+
137+
if _cache_default_sysroot is not None:
138+
return _cache_default_sysroot
139+
140+
contents = _read_output('%s -c -E -v - </dev/null' % (cc,), True)
141+
in_incdirs = False
142+
for line in contents.splitlines():
143+
if line.startswith("#include <...>"):
144+
in_incdirs = True
145+
elif line.startswith("End of search list"):
146+
in_incdirs = False
147+
elif in_incdirs:
148+
line = line.strip()
149+
if line == '/usr/include':
150+
_cache_default_sysroot = '/'
151+
elif line.endswith(".sdk/usr/include"):
152+
_cache_default_sysroot = line[:-12]
153+
if _cache_default_sysroot is None:
154+
_cache_default_sysroot = '/'
155+
156+
return _cache_default_sysroot
157+
128158
def _supports_universal_builds():
129159
"""Returns True if universal builds are supported on this system"""
130160
# 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 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)