Skip to content

Commit b9fd18f

Browse files
cclausstargos
authored andcommitted
tools: pull xcode_emulation.py from node-gyp
PR-URL: #30272Fixes: #30129 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Shelley Vohr <codebytere@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 2810f1a commit b9fd18f

2 files changed

Lines changed: 34 additions & 25 deletions

File tree

‎tools/gyp/pylib/gyp/mac_tool.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
importsys
2525
importtempfile
2626

27+
PY3=bytes!=str
28+
2729

2830
defmain(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+
ifPY3:
269+
err=err.decode('utf-8')
266270
forlineinerr.splitlines():
267271
ifnotlibtool_re.match(line) andnotlibtool_re5.match(line):
268272
print(line, file=sys.stderr)

‎tools/gyp/pylib/gyp/xcode_emulation.py‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
importtempfile
2121
fromgyp.commonimportGypError
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
returnGetStdoutQuiet(['xcrun', '--sdk', sdk, infoitem])
501-
except:
503+
exceptGypError:
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-
ifXcodeVersion() <'0900':
933+
xcode_version, _=XcodeVersion()
934+
ifxcode_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
ifcompilerisnotNone:
@@ -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_versionor'')
1219-
ifxcode>='0720':
1222+
ifxcode_version>='0720':
12201223
cache['DTSDKBuild'] =self._GetSdkVersionInfoItem(
12211224
sdk_root, '--show-sdk-build-version')
1222-
elifxcode>='0430':
1225+
elifxcode_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
ifxcode_version<'0500':
12591262
return''
12601263
default_sdk_path=self._XcodeSdkPath('')
@@ -1263,7 +1266,7 @@ def _DefaultSdkRoot(self):
12631266
returndefault_sdk_root
12641267
try:
12651268
all_sdks=GetStdout(['xcodebuild', '-showsdks'])
1266-
except:
1269+
exceptGypError:
12671270
# If xcodebuild fails, there will be no valid SDKs
12681271
return''
12691272
forlineinall_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
globalXCODE_VERSION_CACHE
13961399
ifXCODE_VERSION_CACHE:
13971400
returnXCODE_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
iflen(version_list) <2:
14061411
raiseGypError("xcodebuild returned unexpected results")
1407-
except:
1408-
version=CLTVersion()
1409-
ifversion:
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+
exceptGypError: # Xcode not installed so look for XCode Command Line Tools
1415+
version=CLTVersion() # macOS Catalina returns 11.0.0.0.1.1567737322
1416+
ifnotversion:
14121417
raiseGypError("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-
ifbuild:
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
returnXCODE_VERSION_CACHE
14241424

@@ -1442,7 +1442,7 @@ def CLTVersion():
14421442
try:
14431443
output=GetStdout(['/usr/sbin/pkgutil', '--pkg-info', key])
14441444
returnre.search(regex, output).groupdict()['version']
1445-
except:
1445+
exceptGypError:
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+
ifPY3:
1457+
out=out.decode("utf-8")
14561458
ifjob.returncode!=0:
14571459
raiseGypError('Error %d running %s'% (job.returncode, cmdlist[0]))
14581460
returnout.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+
ifPY3:
1469+
out=out.decode("utf-8")
14661470
ifjob.returncode!=0:
14671471
sys.stderr.write(out+'\n')
14681472
raiseGypError('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
ifinstall_name_base:
16761680
env['DYLIB_INSTALL_NAME_BASE'] =install_name_base
1677-
ifXcodeVersion() >='0500'andnotenv.get('SDKROOT'):
1681+
xcode_version, _=XcodeVersion()
1682+
ifxcode_version>='0500'andnotenv.get('SDKROOT'):
16781683
sdk_root=xcode_settings._SdkRoot(configuration)
16791684
ifnotsdk_root:
16801685
sdk_root=xcode_settings._XcodeSdkPath('')

0 commit comments

Comments
 (0)