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