Skip to content

Commit b9fd18f

Browse files
cclausstargos
authored andcommitted
tools: pull xcode_emulation.py from node-gyp
PR-URL: #30272 Fixes: #30129 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Shelley Vohr <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 2810f1a commit b9fd18f

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

tools/gyp/pylib/gyp/mac_tool.py

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import sys
2525
import tempfile
2626

27+
PY3 = bytes != str
28+
2729

2830
def main(args):
2931
executor = MacTool()
@@ -263,6 +265,8 @@ def ExecFilterLibtool(self, *cmd_list):
263265
env['ZERO_AR_DATE'] = '1'
264266
libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env)
265267
_, err = libtoolout.communicate()
268+
if PY3:
269+
err = err.decode('utf-8')
266270
for line in err.splitlines():
267271
if not libtool_re.match(line) and not libtool_re5.match(line):
268272
print(line, file=sys.stderr)

tools/gyp/pylib/gyp/xcode_emulation.py

+30-25
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import tempfile
2121
from gyp.common import GypError
2222

23+
PY3 = bytes != str
24+
2325
# Populated lazily by XcodeVersion, for efficiency, and to fix an issue when
2426
# "xcodebuild" is called too quickly (it has been found to return incorrect
2527
# version number).
@@ -498,7 +500,7 @@ def _GetSdkVersionInfoItem(self, sdk, infoitem):
498500
# most sensible route and should still do the right thing.
499501
try:
500502
return GetStdoutQuiet(['xcrun', '--sdk', sdk, infoitem])
501-
except:
503+
except GypError:
502504
pass
503505

504506
def _SdkRoot(self, configname):
@@ -928,7 +930,8 @@ def GetLdflags(self, configname, product_dir, gyp_to_build_path, arch=None):
928930
# extensions and provide loader and main function.
929931
# These flags reflect the compilation options used by xcode to compile
930932
# extensions.
931-
if XcodeVersion() < '0900':
933+
xcode_version, _ = XcodeVersion()
934+
if xcode_version < '0900':
932935
ldflags.append('-lpkstart')
933936
ldflags.append(sdk_root +
934937
'/System/Library/PrivateFrameworks/PlugInKit.framework/PlugInKit')
@@ -1204,8 +1207,8 @@ def GetExtraPlistItems(self, configname=None):
12041207
cache = {}
12051208
cache['BuildMachineOSBuild'] = self._BuildMachineOSBuild()
12061209

1207-
xcode, xcode_build = XcodeVersion()
1208-
cache['DTXcode'] = xcode
1210+
xcode_version, xcode_build = XcodeVersion()
1211+
cache['DTXcode'] = xcode_version
12091212
cache['DTXcodeBuild'] = xcode_build
12101213
compiler = self.xcode_settings[configname].get('GCC_VERSION')
12111214
if compiler is not None:
@@ -1216,10 +1219,10 @@ def GetExtraPlistItems(self, configname=None):
12161219
sdk_root = self._DefaultSdkRoot()
12171220
sdk_version = self._GetSdkVersionInfoItem(sdk_root, '--show-sdk-version')
12181221
cache['DTSDKName'] = sdk_root + (sdk_version or '')
1219-
if xcode >= '0720':
1222+
if xcode_version >= '0720':
12201223
cache['DTSDKBuild'] = self._GetSdkVersionInfoItem(
12211224
sdk_root, '--show-sdk-build-version')
1222-
elif xcode >= '0430':
1225+
elif xcode_version >= '0430':
12231226
cache['DTSDKBuild'] = sdk_version
12241227
else:
12251228
cache['DTSDKBuild'] = cache['BuildMachineOSBuild']
@@ -1254,7 +1257,7 @@ def _DefaultSdkRoot(self):
12541257
project, then the environment variable was empty. Starting with this
12551258
version, Xcode uses the name of the newest SDK installed.
12561259
"""
1257-
xcode_version, xcode_build = XcodeVersion()
1260+
xcode_version, _ = XcodeVersion()
12581261
if xcode_version < '0500':
12591262
return ''
12601263
default_sdk_path = self._XcodeSdkPath('')
@@ -1263,7 +1266,7 @@ def _DefaultSdkRoot(self):
12631266
return default_sdk_root
12641267
try:
12651268
all_sdks = GetStdout(['xcodebuild', '-showsdks'])
1266-
except:
1269+
except GypError:
12671270
# If xcodebuild fails, there will be no valid SDKs
12681271
return ''
12691272
for line in all_sdks.splitlines():
@@ -1391,10 +1394,12 @@ def XcodeVersion():
13911394
# Xcode 3.2.6
13921395
# Component versions: DevToolsCore-1809.0; DevToolsSupport-1806.0
13931396
# BuildVersion: 10M2518
1394-
# Convert that to '0463', '4H1503'.
1397+
# Convert that to ('0463', '4H1503') or ('0326', '10M2518').
13951398
global XCODE_VERSION_CACHE
13961399
if XCODE_VERSION_CACHE:
13971400
return XCODE_VERSION_CACHE
1401+
version = ""
1402+
build = ""
13981403
try:
13991404
version_list = GetStdoutQuiet(['xcodebuild', '-version']).splitlines()
14001405
# In some circumstances xcodebuild exits 0 but doesn't return
@@ -1404,21 +1409,16 @@ def XcodeVersion():
14041409
# checking that version.
14051410
if len(version_list) < 2:
14061411
raise GypError("xcodebuild returned unexpected results")
1407-
except:
1408-
version = CLTVersion()
1409-
if version:
1410-
version = re.match(r'(\d+\.\d+\.?\d*)', version).groups()[0]
1411-
else:
1412+
version = version_list[0].split()[-1] # Last word on first line
1413+
build = version_list[-1].split()[-1] # Last word on last line
1414+
except GypError: # Xcode not installed so look for XCode Command Line Tools
1415+
version = CLTVersion() # macOS Catalina returns 11.0.0.0.1.1567737322
1416+
if not version:
14121417
raise GypError("No Xcode or CLT version detected!")
1413-
# The CLT has no build information, so we return an empty string.
1414-
version_list = [version, '']
1415-
version = version_list[0]
1416-
build = version_list[-1]
1417-
# Be careful to convert "4.2" to "0420":
1418-
version = version.split()[-1].replace('.', '')
1419-
version = (version + '0' * (3 - len(version))).zfill(4)
1420-
if build:
1421-
build = build.split()[-1]
1418+
# Be careful to convert "4.2.3" to "0423" and "11.0.0" to "1100":
1419+
version = version.split(".")[:3] # Just major, minor, micro
1420+
version[0] = version[0].zfill(2) # Add a leading zero if major is one digit
1421+
version = ("".join(version) + "00")[:4] # Limit to exactly four characters
14221422
XCODE_VERSION_CACHE = (version, build)
14231423
return XCODE_VERSION_CACHE
14241424

@@ -1442,7 +1442,7 @@ def CLTVersion():
14421442
try:
14431443
output = GetStdout(['/usr/sbin/pkgutil', '--pkg-info', key])
14441444
return re.search(regex, output).groupdict()['version']
1445-
except:
1445+
except GypError:
14461446
continue
14471447

14481448

@@ -1453,6 +1453,8 @@ def GetStdoutQuiet(cmdlist):
14531453
job = subprocess.Popen(cmdlist, stdout=subprocess.PIPE,
14541454
stderr=subprocess.PIPE)
14551455
out = job.communicate()[0]
1456+
if PY3:
1457+
out = out.decode("utf-8")
14561458
if job.returncode != 0:
14571459
raise GypError('Error %d running %s' % (job.returncode, cmdlist[0]))
14581460
return out.rstrip('\n')
@@ -1463,6 +1465,8 @@ def GetStdout(cmdlist):
14631465
Raises |GypError| if the command return with a non-zero return code."""
14641466
job = subprocess.Popen(cmdlist, stdout=subprocess.PIPE)
14651467
out = job.communicate()[0]
1468+
if PY3:
1469+
out = out.decode("utf-8")
14661470
if job.returncode != 0:
14671471
sys.stderr.write(out + '\n')
14681472
raise GypError('Error %d running %s' % (job.returncode, cmdlist[0]))
@@ -1674,7 +1678,8 @@ def _GetXcodeEnv(xcode_settings, built_products_dir, srcroot, configuration,
16741678
install_name_base = xcode_settings.GetInstallNameBase()
16751679
if install_name_base:
16761680
env['DYLIB_INSTALL_NAME_BASE'] = install_name_base
1677-
if XcodeVersion() >= '0500' and not env.get('SDKROOT'):
1681+
xcode_version, _ = XcodeVersion()
1682+
if xcode_version >= '0500' and not env.get('SDKROOT'):
16781683
sdk_root = xcode_settings._SdkRoot(configuration)
16791684
if not sdk_root:
16801685
sdk_root = xcode_settings._XcodeSdkPath('')

0 commit comments

Comments
 (0)