OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Download necessary mac toolchain files under certain conditions. If | |
7 xcode-select is already set and points to an external folder | |
8 (e.g. /Application/Xcode.app), this script only runs if the GYP_DEFINE | |
9 |force_mac_toolchain| is set. To override the values in | |
10 |TOOLCHAIN_REVISION|-|TOOLCHAIN_SUB_REVISION| below, GYP_DEFINE | |
11 mac_toolchain_revision can be used instead. | |
12 | |
13 This script will only run on machines with the following programs added to a | |
14 sudoers list: | |
15 /usr/bin/xcode-select | |
erikchen
2016/03/15 20:31:26
do we still need sudo for xcode-select?
justincohen
2016/03/20 23:35:22
Nope, updated the comment.
| |
16 /usr/bin/xcodebuild | |
17 | |
18 Otherwise user input would be required to complete the script. Perhaps future | |
19 versions can be modified to allow for user input on developer machines. | |
20 """ | |
21 | |
22 import os | |
23 import shutil | |
24 import subprocess | |
25 import sys | |
26 import tarfile | |
27 import time | |
28 import tempfile | |
29 import urllib2 | |
30 | |
31 # Update sys.path to import gyp. | |
32 script_dir = os.path.dirname(os.path.realpath(__file__)) | |
33 chrome_src = os.path.abspath(os.path.join(script_dir, os.pardir)) | |
34 sys.path.insert(0, os.path.join(chrome_src, 'tools', 'gyp', 'pylib')) | |
35 import gyp | |
36 | |
37 # This can be changed after running /build/package_mac_toolchain.py. | |
38 TOOLCHAIN_REVISION = '7C68' | |
39 TOOLCHAIN_SUB_REVISION = 1 | |
40 TOOLCHAIN_VERSION = "%s-%s" % (TOOLCHAIN_REVISION, TOOLCHAIN_SUB_REVISION) | |
41 | |
42 BASE_DIR = os.path.abspath(os.path.dirname(__file__)) | |
43 TOOLCHAIN_BUILD_DIR = os.path.join(BASE_DIR, 'mac_files', 'Xcode.app') | |
44 STAMP_FILE = os.path.join(BASE_DIR, 'mac_files', 'toolchain_build_revision') | |
45 TOOLCHAIN_URL = 'gs://chrome-mac-sdk/' | |
46 | |
47 | |
48 def SetEnvironment(): | |
49 if sys.platform == 'darwin' and not UseLocalMacSDK(): | |
50 os.environ['DEVELOPER_DIR'] = TOOLCHAIN_BUILD_DIR | |
51 | |
52 | |
53 def ReadStampFile(): | |
54 """Return the contents of the stamp file, or '' if it doesn't exist.""" | |
55 try: | |
56 with open(STAMP_FILE, 'r') as f: | |
57 return f.read().rstrip() | |
58 except IOError: | |
59 return '' | |
60 | |
61 | |
62 def WriteStampFile(s): | |
63 """Write s to the stamp file.""" | |
64 EnsureDirExists(os.path.dirname(STAMP_FILE)) | |
65 with open(STAMP_FILE, 'w') as f: | |
66 f.write(s) | |
67 f.write('\n') | |
68 | |
69 | |
70 def EnsureDirExists(path): | |
71 if not os.path.exists(path): | |
72 os.makedirs(path) | |
73 | |
74 | |
75 def DownloadAndUnpack(url, output_dir): | |
76 """Decompresses |url| into a cleared |output_dir|.""" | |
77 try: | |
78 temp_name = tempfile.mktemp(prefix='mac_toolchain') | |
erikchen
2016/03/15 20:31:26
move this line to 76. Otherwise if tempfile.mktemp
justincohen
2016/03/20 23:35:22
Done.
| |
79 print 'Downloading new toolchain.' | |
80 subprocess.check_call(['gsutil.py', 'cp', url, temp_name]) | |
81 if os.path.exists(output_dir): | |
82 print 'Deleting old toolchain.' | |
83 shutil.rmtree(output_dir) | |
84 EnsureDirExists(output_dir) | |
85 print 'Unpacking new toolchain.' | |
86 tarfile.open(mode='r:gz', name=temp_name).extractall(path=output_dir) | |
87 finally: | |
88 if os.path.exists(temp_name): | |
89 os.unlink(temp_name) | |
90 | |
91 | |
92 def CanAccessToolchainBucket(): | |
93 """Checks whether the user has access to |TOOLCHAIN_URL|.""" | |
94 proc = subprocess.Popen(['gsutil.py', 'ls', TOOLCHAIN_URL], | |
95 stdout=subprocess.PIPE) | |
96 proc.communicate() | |
97 return proc.returncode == 0 | |
98 | |
99 def SwitchToolchain(directory): | |
100 """Use xcodebuild to accept new toolchain license. This only | |
101 works if xcodebuild is in sudoers.""" | |
102 xcodebuild = os.path.join(TOOLCHAIN_BUILD_DIR, | |
103 'Contents/Developer/usr/bin/xcodebuild') | |
104 subprocess.check_call(['sudo', xcodebuild, '-license', 'accept']) | |
105 | |
106 | |
107 def UseLocalMacSDK(): | |
108 gyp_defines = gyp.NameValueListToDict(gyp.ShlexEnv('GYP_DEFINES')) | |
109 force_pull = 'force_mac_toolchain' in gyp_defines | |
110 | |
111 # Don't update the toolchain if there's already one installed outside of the | |
112 # expected location for a Chromium mac toolchain, unless |force_pull| is set. | |
113 proc = subprocess.Popen(['xcode-select', '-p'], stdout=subprocess.PIPE) | |
114 xcode_select_dir = proc.communicate()[0] | |
115 rc = proc.returncode | |
116 return (not force_pull and rc == 0 and | |
117 TOOLCHAIN_BUILD_DIR not in xcode_select_dir) | |
118 | |
119 | |
120 def main(): | |
121 # TODO(justincohen): Add support for GN per crbug.com/570091 | |
122 if UseLocalMacSDK(): | |
123 print 'Using local toolchain.' | |
124 return 0 | |
125 | |
126 gyp_defines = gyp.NameValueListToDict(gyp.ShlexEnv('GYP_DEFINES')) | |
127 toolchain_revision = gyp_defines.get('mac_toolchain_revision', | |
128 TOOLCHAIN_VERSION) | |
129 if ReadStampFile() == toolchain_revision: | |
130 print 'Toolchain (%s) is already up to date.' % toolchain_revision | |
131 SwitchToolchain(TOOLCHAIN_BUILD_DIR) | |
132 return 0 | |
133 | |
134 if not CanAccessToolchainBucket(): | |
135 print 'Cannot access toolchain bucket.' | |
136 return 0 | |
137 | |
138 # Reset the stamp file in case the build is unsuccessful. | |
139 WriteStampFile('') | |
140 | |
141 toolchain_file = "%s.tgz" % toolchain_revision | |
142 toolchain_full_url = TOOLCHAIN_URL + toolchain_file | |
143 | |
144 print 'Updating toolchain to %s...' % toolchain_revision | |
145 try: | |
146 toolchain_file = "toolchain-%s.tgz" % toolchain_revision | |
147 toolchain_full_url = TOOLCHAIN_URL + toolchain_file | |
148 DownloadAndUnpack(toolchain_full_url, TOOLCHAIN_BUILD_DIR) | |
149 SwitchToolchain(TOOLCHAIN_BUILD_DIR) | |
150 | |
151 print 'Toolchain %s unpacked.' % toolchain_revision | |
152 WriteStampFile(toolchain_revision) | |
153 return 0 | |
154 except: | |
155 print 'Failed to download toolchain %s.' % toolchain_file | |
156 print 'Exiting.' | |
157 return 1 | |
158 | |
159 if __name__ == '__main__': | |
160 sys.exit(main()) | |
OLD | NEW |