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. |
Justin Cohen | 170c409b7 | 2017-09-13 02:37:02 | [diff] [blame] | 14 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 15 | The toolchain version can be overridden by setting MAC_TOOLCHAIN_REVISION with |
| 16 | the full revision, e.g. 9A235. |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 17 | """ |
| 18 | |
| 19 | import os |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 20 | import platform |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 21 | import shutil |
| 22 | import subprocess |
| 23 | import sys |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 24 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 25 | |
| 26 | # This can be changed after running: |
| 27 | # mac_toolchain upload -xcode-path path/to/Xcode.app |
Elly Fong-Jones | 6998987 | 2019-03-06 18:34:11 | [diff] [blame^] | 28 | MAC_TOOLCHAIN_VERSION = '9E501' |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 29 | |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 30 | # 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^] | 31 | # 17 is the major version number for macOS 10.13. |
| 32 | # 9E145 (Xcode 9.3) only runs on 10.13.2 and newer. |
| 33 | MAC_MINIMUM_OS_VERSION = 17 |
Erik Chen | a537a905 | 2018-11-13 01:44:43 | [diff] [blame] | 34 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 35 | MAC_TOOLCHAIN_INSTALLER = 'mac_toolchain' |
justincohen | abbd93dc | 2016-11-30 20:11:28 | [diff] [blame] | 36 | |
| 37 | # Absolute path to src/ directory. |
| 38 | REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 39 | |
| 40 | # Absolute path to a file with gclient solutions. |
| 41 | GCLIENT_CONFIG = os.path.join(os.path.dirname(REPO_ROOT), '.gclient') |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 42 | |
| 43 | BASE_DIR = os.path.abspath(os.path.dirname(__file__)) |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 44 | TOOLCHAIN_ROOT = os.path.join(BASE_DIR, 'mac_files') |
| 45 | TOOLCHAIN_BUILD_DIR = os.path.join(TOOLCHAIN_ROOT, 'Xcode.app') |
| 46 | STAMP_FILE = os.path.join(TOOLCHAIN_ROOT, 'toolchain_build_revision') |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 47 | |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 48 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 49 | def PlatformMeetsHermeticXcodeRequirements(): |
Erik Chen | a537a905 | 2018-11-13 01:44:43 | [diff] [blame] | 50 | major_version = int(platform.release().split('.')[0]) |
Elly Fong-Jones | 6998987 | 2019-03-06 18:34:11 | [diff] [blame^] | 51 | return major_version >= MAC_MINIMUM_OS_VERSION |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 52 | |
| 53 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 54 | def _UseHermeticToolchain(): |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 55 | current_dir = os.path.dirname(os.path.realpath(__file__)) |
| 56 | script_path = os.path.join(current_dir, 'mac/should_use_hermetic_xcode.py') |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 57 | proc = subprocess.Popen([script_path, 'mac'], stdout=subprocess.PIPE) |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 58 | return '1' in proc.stdout.readline() |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 59 | |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 60 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 61 | def RequestCipdAuthentication(): |
| 62 | """Requests that the user authenticate to access Xcode CIPD packages.""" |
| 63 | |
| 64 | print 'Access to Xcode CIPD package requires authentication.' |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 65 | print '-----------------------------------------------------------------' |
| 66 | print |
| 67 | print 'You appear to be a Googler.' |
| 68 | print |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 69 | print 'I\'m sorry for the hassle, but you may need to do a one-time manual' |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 70 | print 'authentication. Please run:' |
| 71 | print |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 72 | print ' cipd auth-login' |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 73 | print |
| 74 | print 'and follow the instructions.' |
| 75 | print |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 76 | print 'NOTE: Use your google.com credentials, not chromium.org.' |
erikchen | da01626 | 2016-11-09 04:32:13 | [diff] [blame] | 77 | print |
| 78 | print '-----------------------------------------------------------------' |
| 79 | print |
| 80 | sys.stdout.flush() |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 81 | |
| 82 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 83 | def PrintError(message): |
| 84 | # Flush buffers to ensure correct output ordering. |
| 85 | sys.stdout.flush() |
| 86 | sys.stderr.write(message + '\n') |
| 87 | sys.stderr.flush() |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 88 | |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 89 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 90 | def InstallXcode(xcode_build_version, installer_cmd, xcode_app_path): |
| 91 | """Installs the requested Xcode build version. |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 92 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 93 | Args: |
| 94 | xcode_build_version: (string) Xcode build version to install. |
| 95 | installer_cmd: (string) Path to mac_toolchain command to install Xcode. |
| 96 | See https://chromium.googlesource.com/infra/infra/+/master/go/src/infra/cmd/mac_toolchain/ |
| 97 | xcode_app_path: (string) Path to install the contents of Xcode.app. |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 98 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 99 | Returns: |
| 100 | True if installation was successful. False otherwise. |
| 101 | """ |
| 102 | args = [ |
| 103 | installer_cmd, 'install', |
| 104 | '-kind', 'mac', |
| 105 | '-xcode-version', xcode_build_version.lower(), |
| 106 | '-output-dir', xcode_app_path, |
| 107 | ] |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 108 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 109 | # Buildbot slaves need to use explicit credentials. LUCI bots should NOT set |
| 110 | # this variable. |
| 111 | creds = os.environ.get('MAC_TOOLCHAIN_CREDS') |
| 112 | if creds: |
| 113 | args.extend(['--service-account-json', creds]) |
| 114 | |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 115 | try: |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 116 | subprocess.check_call(args) |
| 117 | except subprocess.CalledProcessError as e: |
| 118 | PrintError('Xcode build version %s failed to install: %s\n' % ( |
| 119 | xcode_build_version, e)) |
| 120 | RequestCipdAuthentication() |
| 121 | return False |
| 122 | except OSError as e: |
| 123 | PrintError(('Xcode installer "%s" failed to execute' |
| 124 | ' (not on PATH or not installed).') % installer_cmd) |
| 125 | return False |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 126 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 127 | return True |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 128 | |
sdefresne | aacc2845 | 2017-01-12 10:33:37 | [diff] [blame] | 129 | |
| 130 | def main(): |
| 131 | if sys.platform != 'darwin': |
| 132 | return 0 |
| 133 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 134 | if not _UseHermeticToolchain(): |
| 135 | print 'Skipping Mac toolchain installation for mac' |
| 136 | return 0 |
erikchen | d5dfcdb0 | 2017-06-29 00:22:53 | [diff] [blame] | 137 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 138 | if not PlatformMeetsHermeticXcodeRequirements(): |
| 139 | print 'OS version does not support toolchain.' |
| 140 | return 0 |
sdefresne | aacc2845 | 2017-01-12 10:33:37 | [diff] [blame] | 141 | |
Sergey Berezin | 5654182d | 2018-04-30 21:24:49 | [diff] [blame] | 142 | toolchain_version = os.environ.get('MAC_TOOLCHAIN_REVISION', |
| 143 | MAC_TOOLCHAIN_VERSION) |
| 144 | |
| 145 | # On developer machines, mac_toolchain tool is provided by |
| 146 | # depot_tools. On the bots, the recipe is responsible for installing |
| 147 | # it and providing the path to the executable. |
| 148 | installer_cmd = os.environ.get('MAC_TOOLCHAIN_INSTALLER', |
| 149 | MAC_TOOLCHAIN_INSTALLER) |
| 150 | |
| 151 | toolchain_root = TOOLCHAIN_ROOT |
| 152 | xcode_app_path = TOOLCHAIN_BUILD_DIR |
| 153 | stamp_file = STAMP_FILE |
| 154 | |
| 155 | # Delete the old "hermetic" installation if detected. |
| 156 | # TODO(crbug.com/797051): remove this once the old "hermetic" solution is no |
| 157 | # longer in use. |
| 158 | if os.path.exists(stamp_file): |
| 159 | print 'Detected old hermetic installation at %s. Deleting.' % ( |
| 160 | toolchain_root) |
| 161 | shutil.rmtree(toolchain_root) |
| 162 | |
| 163 | success = InstallXcode(toolchain_version, installer_cmd, xcode_app_path) |
| 164 | if not success: |
| 165 | return 1 |
sdefresne | aacc2845 | 2017-01-12 10:33:37 | [diff] [blame] | 166 | |
| 167 | return 0 |
| 168 | |
| 169 | |
justincohen | 6a03a3d | 2016-03-26 21:44:38 | [diff] [blame] | 170 | if __name__ == '__main__': |
| 171 | sys.exit(main()) |