Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

Commit 270eae0

Browse files
committed
Fix Xcode 11.3.1 detection in an Xcode workspace
When using a Tulsi-generated project by itself in Xcode 11.3.1, Xcode sets XCODE_VERSION_ACTUAL to 1131, which Tulsi parses correctly to get the version 11.3.1. But when used in an Xcode workspace, Xcode sets XCODE_VERSION_ACTUAL to 1130, leading to Tulsi parsing 11.3 as the version, and later attempts to use a non-existing version of Xcode to build, which results in an error. Fix by ignoring XCODE_VERSION_ACTUAL because it's unreliable; instead, parse Xcode's version.plist to read the 'CFBundleShortVersionString' entry, which seems to return correct results. Caveat: this implementation uses plistlib, which has a slightly different API in python2 and python3. This change uses the python2 API, and will need to be updated to work with python3 in the future. PiperOrigin-RevId: 315886285
1 parent dad7c85 commit 270eae0

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/TulsiGenerator/Scripts/bazel_build.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import json
2626
import os
2727
import pipes
28+
import plistlib
2829
import re
2930
import shutil
3031
import signal
@@ -340,17 +341,39 @@ def _GetXcodeBuildVersionString():
340341

341342
@staticmethod
342343
def _GetXcodeVersionString():
343-
"""Returns Xcode version info from the environment as a string."""
344-
reported_version = os.environ['XCODE_VERSION_ACTUAL']
345-
match = re.match(r'(\d{2})(\d)(\d)$', reported_version)
346-
if not match:
347-
_PrintUnbuffered('Warning: Failed to extract Xcode version from %s' % (
348-
reported_version))
344+
"""Returns Xcode version info from the Xcode's version.plist.
345+
346+
Just reading XCODE_VERSION_ACTUAL from the environment seems like
347+
a more reasonable implementation, but has shown to be unreliable,
348+
at least when using Xcode 11.3.1 and opening the project within an
349+
Xcode workspace.
350+
"""
351+
developer_dir = os.environ['DEVELOPER_DIR']
352+
app_dir = developer_dir.split('.app')[0] + '.app'
353+
version_plist_path = os.path.join(app_dir, 'Contents', 'version.plist')
354+
try:
355+
# python2 API to plistlib - needs updating if/when Tulsi bumps to python3
356+
plist = plistlib.readPlist(version_plist_path)
357+
except IOError:
358+
_PrintXcodeWarning('Tulsi cannot determine Xcode version, error '
359+
'reading from {}'.format(version_plist_path))
349360
return None
350-
major_version = int(match.group(1))
351-
minor_version = int(match.group(2))
352-
fix_version = int(match.group(3))
353-
return '%d.%d.%d' % (major_version, minor_version, fix_version)
361+
try:
362+
# Example: "11.3.1", "11.3", "11.0"
363+
key = 'CFBundleShortVersionString'
364+
version_string = plist[key]
365+
except KeyError:
366+
_PrintXcodeWarning('Tulsi cannot determine Xcode version from {}, no '
367+
'"{}" key'.format(version_plist_path, key))
368+
return None
369+
370+
# But we need to normalize to major.minor.patch, e.g. 11.3.0 or
371+
# 11.0.0, so add one or two ".0" if needed (two just in case
372+
# there is ever just a single version number like "12")
373+
dots_count = version_string.count('.')
374+
dot_zeroes_to_add = 2 - dots_count
375+
version_string += '.0' * dot_zeroes_to_add
376+
return version_string
354377

355378
@staticmethod
356379
def _ComputeXcodeVersionFlag():

0 commit comments

Comments
 (0)