forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcode_util.py
335 lines (269 loc) · 12.8 KB
/
xcode_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import distutils.version
import glob
import logging
import os
import shutil
import subprocess
import test_runner_errors
LOGGER = logging.getLogger(__name__)
XcodeIOSSimulatorDefaultRuntimeFilename = 'iOS.simruntime'
XcodeIOSSimulatorRuntimeRelPath = ('Contents/Developer/Platforms/'
'iPhoneOS.platform/Library/Developer/'
'CoreSimulator/Profiles/Runtimes')
def _using_new_mac_toolchain(mac_toolchain):
"""Returns if the mac_toolchain command passed in is new version.
New mac_toolchain can download an Xcode without bundled runtime, and can
download single runtimes. Legacy mac_toolchain can only download Xcode package
as a whole package. The function tells the difference by checking the
existence of a new command line switch in new version.
TODO(crbug.com/1191260): Remove this util function when the new mac_toolchain
version is rolled to everywhere using this script.
"""
cmd = [
mac_toolchain,
'help',
]
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
# "install-runtime" presents as a command line switch in help output in the
# new mac_toolchain.
using_new_mac_toolchain = 'install-runtime' in output
return using_new_mac_toolchain
def _is_legacy_xcode_package(xcode_app_path):
"""Checks and returns if the installed Xcode package is legacy version.
Legacy Xcode package are uploaded with legacy version of mac_toolchain.
Typically, multiple iOS runtimes are bundled into legacy Xcode packages. No
runtime is bundled into new format Xcode packages.
Args:
xcode_app_path: (string) Path to install the contents of Xcode.app.
Returns:
(bool) True if the package is legacy(with runtime bundled). False otherwise.
"""
# More than one iOS runtimes indicate the downloaded Xcode is a legacy one.
# If no runtimes are found in the package, it's a new format package. If only
# one runtime is found in package, it typically means it's an incorrectly
# cached new format Xcode package. (The single runtime wasn't moved out from
# Xcode in the end of last task, because last task was killed before moving.)
runtimes_in_xcode = glob.glob(
os.path.join(xcode_app_path, XcodeIOSSimulatorRuntimeRelPath,
'*.simruntime'))
is_legacy = len(runtimes_in_xcode) >= 2
if not is_legacy:
for runtime in runtimes_in_xcode:
LOGGER.warning('Removing %s from incorrectly cached Xcode.', runtime)
shutil.rmtree(runtime)
return is_legacy
def _install_runtime(mac_toolchain, install_path, xcode_build_version,
ios_version):
"""Invokes mac_toolchain to install the runtime.
mac_toolchain will resolve & find the best suitable runtime and install to the
path, with Xcode and ios version as input.
Args:
install_path: (string) Path to install the runtime package into.
xcode_build_version: (string) Xcode build version, e.g. 12d4e.
ios_version: (string) Runtime version (number only), e.g. 13.4.
"""
existing_runtimes = glob.glob(os.path.join(install_path, '*.simruntime'))
# When no runtime file exists, remove any remaining .cipd or .xcode_versions
# status folders, so mac_toolchain(underlying CIPD) will work to download a
# new one.
if len(existing_runtimes) == 0:
for dir_name in ['.cipd', '.xcode_versions']:
dir_path = os.path.join(install_path, dir_name)
if os.path.exists(dir_path):
LOGGER.warning('Removing %s in runtime cache folder.', dir_path)
shutil.rmtree(dir_path)
# Transform iOS version to the runtime version format required my the tool.
# e.g. "14.4" -> "ios-14-4"
runtime_version = 'ios-' + ios_version.replace('.', '-')
cmd = [
mac_toolchain,
'install-runtime',
'-xcode-version',
xcode_build_version.lower(),
'-runtime-version',
runtime_version,
'-output-dir',
install_path,
]
LOGGER.debug('Installing runtime with command: %s' % cmd)
output = subprocess.check_call(cmd, stderr=subprocess.STDOUT)
return output
def construct_runtime_cache_folder(runtime_cache_prefix, ios_version):
"""Composes runtime cache folder from it's prefix and ios_version.
Note: Please keep the pattern consistent between what's being passed into
runner script in gn(build/config/ios/ios_test_runner_wrapper.gni), and what's
being configured for swarming cache in test configs (testing/buildbot/*).
"""
return runtime_cache_prefix + ios_version
def move_runtime(runtime_cache_folder, xcode_app_path, into_xcode):
"""Moves runtime from runtime cache into xcode or vice versa.
The function is intended to only work with new Xcode packages.
The function assumes that there's exactly one *.simruntime file in the source
folder. It also removes existing runtimes in the destination folder. The above
assumption & handling can ensure no incorrect Xcode package is cached from
corner cases.
Args:
runtime_cache_folder: (string) Path to the runtime cache directory.
xcode_app_path: (string) Path to install the contents of Xcode.app.
into_xcode: (bool) Whether the function moves from cache dir into Xcode or
from Xcode to cache dir.
Raises:
IOSRuntimeHandlingError for issues moving runtime around.
shutil.Error for exceptions from shutil when moving files around.
"""
xcode_runtime_folder = os.path.join(xcode_app_path,
XcodeIOSSimulatorRuntimeRelPath)
src_folder = runtime_cache_folder if into_xcode else xcode_runtime_folder
dst_folder = xcode_runtime_folder if into_xcode else runtime_cache_folder
runtimes_in_src = glob.glob(os.path.join(src_folder, '*.simruntime'))
if len(runtimes_in_src) != 1:
raise test_runner_errors.IOSRuntimeHandlingError(
'Not exactly one runtime files (files: %s) to move from %s!' %
(runtimes_in_src, src_folder))
runtimes_in_dst = glob.glob(os.path.join(dst_folder, '*.simruntime'))
for runtime in runtimes_in_dst:
LOGGER.warning('Removing existing %s in destination folder.', runtime)
shutil.rmtree(runtime)
# Get the runtime package filename. It might not be the default name.
runtime_name = os.path.basename(runtimes_in_src[0])
dst_runtime = os.path.join(dst_folder, runtime_name)
LOGGER.debug('Moving %s from %s to %s.' %
(runtime_name, src_folder, dst_folder))
shutil.move(os.path.join(src_folder, runtime_name), dst_runtime)
return
def select(xcode_app_path):
"""Invokes sudo xcode-select -s {xcode_app_path}
Raises:
subprocess.CalledProcessError on exit codes non zero
"""
cmd = [
'sudo',
'xcode-select',
'-s',
xcode_app_path,
]
LOGGER.debug('Selecting XCode with command %s and "xcrun simctl list".' % cmd)
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
# This is to avoid issues caused by mixed usage of different Xcode versions on
# one machine.
xcrun_simctl_cmd = ['xcrun', 'simctl', 'list']
output += subprocess.check_output(xcrun_simctl_cmd, stderr=subprocess.STDOUT)
return output
def _install_xcode(mac_toolchain, xcode_build_version, xcode_path,
using_new_mac_toolchain):
"""Invokes mac_toolchain to install the given xcode version.
If using legacy mac_toolchain, install the whole Xcode package. If using the
new mac_toolchain, add a command line switch to try to install an Xcode
without runtime. However, the existence of runtime depends on the actual Xcode
package in CIPD. e.g. An Xcode package uploaded with legacy mac_toolchain will
include runtimes, even though it's installed with new mac_toolchain and
"-with-runtime=False" switch.
TODO(crbug.com/1191260): Remove the last argument when the new mac_toolchain
version is rolled to everywhere using this script.
Args:
xcode_build_version: (string) Xcode build version to install.
mac_toolchain: (string) Path to mac_toolchain command to install Xcode
See https://chromium.googlesource.com/infra/infra/+/main/go/src/infra/cmd/mac_toolchain/
xcode_path: (string) Path to install the contents of Xcode.app.
using_new_mac_toolchain: (bool) Using new mac_toolchain.
Raises:
subprocess.CalledProcessError on exit codes non zero
"""
cmd = [
mac_toolchain,
'install',
'-kind',
'ios',
'-xcode-version',
xcode_build_version.lower(),
'-output-dir',
xcode_path,
]
if using_new_mac_toolchain:
cmd.append('-with-runtime=False')
LOGGER.debug('Installing xcode with command: %s' % cmd)
output = subprocess.check_call(cmd, stderr=subprocess.STDOUT)
return output
def install(mac_toolchain, xcode_build_version, xcode_app_path, **runtime_args):
"""Installs the Xcode and returns if the installed one is a legacy package.
Installs the Xcode of given version to path. Returns if the Xcode package
of the version is a legacy package (with runtimes bundled in). Runtime related
arguments will only work when |mac_toolchain| is a new version (with runtime
features), and the |xcode_build_version| in CIPD is a new package (uploaded
by new mac_toolchain).
If using legacy mac_toolchain, install the whole legacy Xcode package. (Will
raise if the Xcode package isn't legacy.)
If using new mac_toolchain, first install the Xcode package:
* If installed Xcode is legacy one (with runtimes bundled), return.
* If installed Xcode isn't legacy (without runtime bundled), install and copy
* the specified runtime version into Xcode.
Args:
xcode_build_version: (string) Xcode build version to install.
mac_toolchain: (string) Path to mac_toolchain command to install Xcode
See https://chromium.googlesource.com/infra/infra/+/main/go/src/infra/cmd/mac_toolchain/
xcode_app_path: (string) Path to install the contents of Xcode.app.
runtime_args: Keyword arguments related with runtime installation. Can be
empty when installing an Xcode w/o runtime (for real device tasks). Namely:
runtime_cache_folder: (string) Path to the folder where runtime package
file (e.g. iOS.simruntime) is stored.
ios_version: (string) iOS version requested to be in Xcode package.
Raises:
subprocess.CalledProcessError on exit codes non zero
XcodeMacToolchainMismatchError if an Xcode without runtime is installed with
a legacy mac_toolchain.
Returns:
True, if the Xcode package in CIPD is legacy (bundled with runtimes).
False, if the Xcode package in CIPD is new (not bundled with runtimes).
"""
using_new_mac_toolchain = _using_new_mac_toolchain(mac_toolchain)
_install_xcode(mac_toolchain, xcode_build_version, xcode_app_path,
using_new_mac_toolchain)
is_legacy_xcode_package = _is_legacy_xcode_package(xcode_app_path)
if not using_new_mac_toolchain and not is_legacy_xcode_package:
# Legacy mac_toolchain can't handle the situation when no runtime is in
# Xcode package.
raise test_runner_errors.XcodeMacToolchainMismatchError(xcode_build_version)
# Install & move the runtime to Xcode. Can only work with new mac_toolchain.
# Only install runtime when it's working for a simulator task.
if not is_legacy_xcode_package and runtime_args.get('ios_version'):
runtime_cache_folder = runtime_args.get('runtime_cache_folder')
ios_version = runtime_args.get('ios_version')
if not runtime_cache_folder or not ios_version:
raise test_runner_errors.IOSRuntimeHandlingError(
'Insufficient runtime_args. runtime_cache_folder: %s, ios_version: %s'
% s(runtime_cache_folder, ios_version))
# Try to install the runtime to it's cache folder. mac_toolchain will test
# and install only when the runtime doesn't exist in cache.
_install_runtime(mac_toolchain, runtime_cache_folder, xcode_build_version,
ios_version)
move_runtime(runtime_cache_folder, xcode_app_path, into_xcode=True)
return is_legacy_xcode_package
def version():
"""Invokes xcodebuild -version
Raises:
subprocess.CalledProcessError on exit codes non zero
Returns:
version (12.0), build_version (12a6163b)
"""
cmd = [
'xcodebuild',
'-version',
]
LOGGER.debug('Checking XCode version with command: %s' % cmd)
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
output = output.splitlines()
# output sample:
# Xcode 12.0
# Build version 12A6159
LOGGER.info(output)
version = output[0].decode('UTF-8').split(' ')[1]
build_version = output[1].decode('UTF-8').split(' ')[2].lower()
return version, build_version
def using_xcode_11_or_higher():
"""Returns true if using Xcode version 11 or higher."""
LOGGER.debug('Checking if Xcode version is 11 or higher')
return distutils.version.LooseVersion(
'11.0') <= distutils.version.LooseVersion(version()[0])