justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 1 | #!/usr/bin/env python |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 2 | # Copyright 2018 The Chromium Authors. All rights reserved. |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 6 | """ |
| 7 | If should_use_hermetic_xcode.py emits "1", and the current toolchain is out of |
| 8 | date: |
| 9 | * Downloads the hermetic mac toolchain |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 10 | * Requires CIPD authentication. Run `cipd auth-login`, use Google account. |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 11 | * Accepts the license. |
| 12 | * If xcode-select and xcodebuild are not passwordless in sudoers, requires |
| 13 | user interaction. |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 14 | * Downloads standalone binaries from [a possibly different version of Xcode]. |
Justin Cohen | 170c409b7 | 2017-09-13 02:37:02 | [diff] [blame] | 15 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 16 | The toolchain version can be overridden by setting MAC_TOOLCHAIN_REVISION with |
| 17 | the full revision, e.g. 9A235. |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 18 | """ |
| 19 | |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 20 | from __future__ import print_function |
| 21 | |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 22 | import os |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 23 | import pkg_resources |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 24 | import platform |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 25 | import plistlib |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 26 | import shutil |
| 27 | import subprocess |
| 28 | import sys |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 29 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 30 | |
Erik Chen | 9a856e8 | 2019-07-27 01:40:22 | [diff] [blame] | 31 | # This contains binaries from Xcode 10.12.1, along with the 10.14 SDKs. To build |
| 32 | # this package, see comments in build/xcode_binaries.yaml |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 33 | MAC_BINARIES_LABEL = 'infra_internal/ios/xcode/xcode_binaries/mac-amd64' |
Erik Chen | 9a856e8 | 2019-07-27 01:40:22 | [diff] [blame] | 34 | MAC_BINARIES_TAG = 'hKD0dobc7nP5I5bX5OZ0v9GkUJXb7ADJO7SuK5070G8C' |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 35 | |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 36 | # The toolchain will not be downloaded if the minimum OS version is not met. |
Elly Fong-Jones | 6998987 | 2019-03-06 18:34:11 | [diff] [blame] | 37 | # 17 is the major version number for macOS 10.13. |
| 38 | # 9E145 (Xcode 9.3) only runs on 10.13.2 and newer. |
| 39 | MAC_MINIMUM_OS_VERSION = 17 |
Erik Chen | a537a905 | 2018-11-13 01:44:43 | [diff] [blame] | 40 | |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 41 | BASE_DIR = os.path.abspath(os.path.dirname(__file__)) |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 42 | TOOLCHAIN_ROOT = os.path.join(BASE_DIR, 'mac_files') |
| 43 | TOOLCHAIN_BUILD_DIR = os.path.join(TOOLCHAIN_ROOT, 'Xcode.app') |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 44 | |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 45 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 46 | def PlatformMeetsHermeticXcodeRequirements(): |
Erik Chen | a537a905 | 2018-11-13 01:44:43 | [diff] [blame] | 47 | major_version = int(platform.release().split('.')[0]) |
Elly Fong-Jones | 6998987 | 2019-03-06 18:34:11 | [diff] [blame] | 48 | return major_version >= MAC_MINIMUM_OS_VERSION |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 49 | |
| 50 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 51 | def _UseHermeticToolchain(): |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 52 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 53 | script_path = os.path.join(current_dir, 'mac/should_use_hermetic_xcode.py') |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 54 | proc = subprocess.Popen([script_path, 'mac'], stdout=subprocess.PIPE) |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 55 | return '1' in proc.stdout.readline() |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 56 | |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 57 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 58 | def RequestCipdAuthentication(): |
| 59 | """Requests that the user authenticate to access Xcode CIPD packages.""" |
| 60 | |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 61 | print('Access to Xcode CIPD package requires authentication.') |
| 62 | print('-----------------------------------------------------------------') |
| 63 | print() |
| 64 | print('You appear to be a Googler.') |
| 65 | print() |
| 66 | print('I\'m sorry for the hassle, but you may need to do a one-time manual') |
| 67 | print('authentication. Please run:') |
| 68 | print() |
| 69 | print(' cipd auth-login') |
| 70 | print() |
| 71 | print('and follow the instructions.') |
| 72 | print() |
| 73 | print('NOTE: Use your google.com credentials, not chromium.org.') |
| 74 | print() |
| 75 | print('-----------------------------------------------------------------') |
| 76 | print() |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 77 | sys.stdout.flush() |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 78 | |
| 79 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 80 | def PrintError(message): |
| 81 | # Flush buffers to ensure correct output ordering. |
| 82 | sys.stdout.flush() |
| 83 | sys.stderr.write(message + '\n') |
| 84 | sys.stderr.flush() |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 85 | |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 86 | |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 87 | def InstallXcodeBinaries(): |
| 88 | """Installs the Xcode binaries needed to build Chrome and accepts the license. |
| 89 | |
| 90 | This is the replacement for InstallXcode that installs a trimmed down version |
| 91 | of Xcode that is OS-version agnostic. |
| 92 | """ |
| 93 | # First make sure the directory exists. It will serve as the cipd root. This |
| 94 | # also ensures that there will be no conflicts of cipd root. |
| 95 | binaries_root = os.path.join(TOOLCHAIN_ROOT, 'xcode_binaries') |
| 96 | if not os.path.exists(binaries_root): |
Nico Weber | a3d706d | 2019-08-09 23:04:38 | [diff] [blame^] | 97 | os.makedirs(binaries_root) |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 98 | |
| 99 | # 'cipd ensure' is idempotent. |
| 100 | args = [ |
| 101 | 'cipd', 'ensure', '-root', binaries_root, '-ensure-file', '-' |
| 102 | ] |
Erik Chen | dea65134 | 2019-07-23 05:02:47 | [diff] [blame] | 103 | |
| 104 | # Buildbot slaves need to use explicit credentials. LUCI bots should NOT set |
| 105 | # this variable. This is temporary code used to make official Xcode bots |
| 106 | # happy. https://crbug.com/986488 |
| 107 | creds = os.environ.get('MAC_TOOLCHAIN_CREDS') |
| 108 | if creds: |
| 109 | args.extend(['--service-account-json', creds]) |
| 110 | |
Erik Chen | 890362ea | 2019-07-22 22:43:48 | [diff] [blame] | 111 | p = subprocess.Popen( |
| 112 | args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 113 | stderr=subprocess.PIPE) |
| 114 | stdout, stderr = p.communicate( |
| 115 | input=MAC_BINARIES_LABEL + ' ' + MAC_BINARIES_TAG) |
| 116 | if p.returncode != 0: |
| 117 | print(stdout) |
| 118 | print(stderr) |
| 119 | RequestCipdAuthentication() |
| 120 | return 1 |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 121 | |
| 122 | # Accept the license for this version of Xcode if it's newer than the |
| 123 | # currently accepted version. |
| 124 | cipd_xcode_version_plist_path = os.path.join( |
| 125 | binaries_root, 'Contents/version.plist') |
| 126 | cipd_xcode_version_plist = plistlib.readPlist(cipd_xcode_version_plist_path) |
| 127 | cipd_xcode_version = cipd_xcode_version_plist['CFBundleShortVersionString'] |
| 128 | |
| 129 | cipd_license_path = os.path.join( |
| 130 | binaries_root, 'Contents/Resources/LicenseInfo.plist') |
| 131 | cipd_license_plist = plistlib.readPlist(cipd_license_path) |
| 132 | cipd_license_version = cipd_license_plist['licenseID'] |
| 133 | |
| 134 | should_overwrite_license = True |
| 135 | current_license_path = '/Library/Preferences/com.apple.dt.Xcode.plist' |
| 136 | if os.path.exists(current_license_path): |
| 137 | current_license_plist = plistlib.readPlist(current_license_path) |
| 138 | xcode_version = current_license_plist['IDEXcodeVersionForAgreedToGMLicense'] |
| 139 | if (pkg_resources.parse_version(xcode_version) >= |
| 140 | pkg_resources.parse_version(cipd_xcode_version)): |
| 141 | should_overwrite_license = False |
| 142 | |
| 143 | if not should_overwrite_license: |
Erik Chen | 890362ea | 2019-07-22 22:43:48 | [diff] [blame] | 144 | return 0 |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 145 | |
| 146 | # Use puppet's sudoers script to accept the license if its available. |
| 147 | license_accept_script = '/usr/local/bin/xcode_accept_license.py' |
| 148 | if os.path.exists(license_accept_script): |
Erik Chen | 687a936 | 2019-07-23 03:47:06 | [diff] [blame] | 149 | args = ['sudo', license_accept_script, '--xcode-version', |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 150 | cipd_xcode_version, '--license-version', cipd_license_version] |
Erik Chen | 687a936 | 2019-07-23 03:47:06 | [diff] [blame] | 151 | subprocess.check_call(args) |
Erik Chen | 890362ea | 2019-07-22 22:43:48 | [diff] [blame] | 152 | return 0 |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 153 | |
| 154 | # Otherwise manually accept the license. This will prompt for sudo. |
Erik Chen | ffc4275 | 2019-07-20 01:26:26 | [diff] [blame] | 155 | print('Accepting new Xcode license. Requires sudo.') |
| 156 | sys.stdout.flush() |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 157 | args = ['sudo', 'defaults', 'write', current_license_path, |
| 158 | 'IDEXcodeVersionForAgreedToGMLicense', cipd_xcode_version] |
Erik Chen | 687a936 | 2019-07-23 03:47:06 | [diff] [blame] | 159 | subprocess.check_call(args) |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 160 | args = ['sudo', 'defaults', 'write', current_license_path, |
| 161 | 'IDELastGMLicenseAgreedTo', cipd_license_version] |
Erik Chen | 687a936 | 2019-07-23 03:47:06 | [diff] [blame] | 162 | subprocess.check_call(args) |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 163 | args = ['sudo', 'plutil', '-convert', 'xml1', current_license_path] |
Erik Chen | 687a936 | 2019-07-23 03:47:06 | [diff] [blame] | 164 | subprocess.check_call(args) |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 165 | |
Erik Chen | 890362ea | 2019-07-22 22:43:48 | [diff] [blame] | 166 | return 0 |
| 167 | |
Erik Chen | bc7409485 | 2019-07-17 21:47:00 | [diff] [blame] | 168 | |
sdefresne | aacc2845 | 2017-01-12 10:33:37 | [diff] [blame] | 169 | def main(): |
| 170 | if sys.platform != 'darwin': |
| 171 | return 0 |
| 172 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 173 | if not _UseHermeticToolchain(): |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 174 | print('Skipping Mac toolchain installation for mac') |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 175 | return 0 |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 176 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 177 | if not PlatformMeetsHermeticXcodeRequirements(): |
Raul Tambre | 9e24293b | 2019-05-12 06:11:07 | [diff] [blame] | 178 | print('OS version does not support toolchain.') |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 179 | return 0 |
sdefresne | aacc2845 | 2017-01-12 10:33:37 | [diff] [blame] | 180 | |
Nico Weber | 6b9763fb | 2019-08-09 18:03:10 | [diff] [blame] | 181 | # Delete obsolete hermetic full Xcode folder, the build now uses |
| 182 | # build/mac_files/xcode_binaries instead. |
| 183 | if os.path.exists(TOOLCHAIN_BUILD_DIR): |
| 184 | # TODO(thakis): Remove this after it's been here for a few months. |
| 185 | print('Deleting obsolete build/mac_files/Xcode.app...', end='') |
| 186 | sys.stdout.flush() |
| 187 | shutil.rmtree(TOOLCHAIN_BUILD_DIR) |
| 188 | print('done') |
sdefresne | aacc2845 | 2017-01-12 10:33:37 | [diff] [blame] | 189 | |
Erik Chen | 890362ea | 2019-07-22 22:43:48 | [diff] [blame] | 190 | return InstallXcodeBinaries() |
sdefresne | aacc2845 | 2017-01-12 10:33:37 | [diff] [blame] | 191 | |
| 192 | |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 193 | if __name__ == '__main__': |
| 194 | sys.exit(main()) |