2 # Copyright (c) 2012 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.
6 """Windows can't run .sh files, so this is a Python implementation of
7 update.sh. This script should replace update.sh on all platforms eventually."""
15 # Do NOT CHANGE this if you don't know what you're doing -- see
16 # https://code.google.com/p/chromium/wiki/UpdatingClang
17 # Reverting problematic clang rolls is safe, though.
18 # Note: this revision is only used for Windows. Other platforms use update.sh.
19 LLVM_WIN_REVISION = 'HEAD'
21 # ASan on Windows is useful enough to use it even while the clang/win is still
22 # in bringup. Use a pinned revision to make it slightly more stable.
23 if (re.search(r'\b(asan)=1', os.environ.get('GYP_DEFINES', '')) and
24 not 'LLVM_FORCE_HEAD_REVISION' in os.environ):
25 LLVM_WIN_REVISION = '217738'
27 # Path constants. (All of these should be absolute paths.)
28 THIS_DIR = os.path.abspath(os.path.dirname(__file__))
29 CHROMIUM_DIR = os.path.abspath(os.path.join(THIS_DIR, '..', '..', '..'))
30 LLVM_DIR = os.path.join(CHROMIUM_DIR, 'third_party', 'llvm')
31 LLVM_BUILD_DIR = os.path.join(CHROMIUM_DIR, 'third_party', 'llvm-build',
33 COMPILER_RT_BUILD_DIR = os.path.join(LLVM_BUILD_DIR, '32bit-compiler-rt')
34 CLANG_DIR = os.path.join(LLVM_DIR, 'tools', 'clang')
35 COMPILER_RT_DIR = os.path.join(LLVM_DIR, 'projects', 'compiler-rt')
36 STAMP_FILE = os.path.join(LLVM_BUILD_DIR, 'cr_build_revision')
38 LLVM_REPO_URL='https://llvm.org/svn/llvm-project'
39 if 'LLVM_REPO_URL' in os.environ:
40 LLVM_REPO_URL = os.environ['LLVM_REPO_URL']
44 """Return the contents of the stamp file, or '' if it doesn't exist."""
46 with open(STAMP_FILE, 'r') as f:
52 def WriteStampFile(s):
53 """Write s to the stamp file."""
54 if not os.path.exists(LLVM_BUILD_DIR):
55 os.makedirs(LLVM_BUILD_DIR)
56 with open(STAMP_FILE, 'w') as f:
60 def DeleteFiles(dir, pattern):
61 """Delete all files in dir matching pattern."""
63 regex = re.compile(r'^' + pattern + r'$')
64 for root, _, files in os.walk(dir):
67 os.remove(os.path.join(root, f))
72 def ClobberChromiumBuildFiles():
73 """Clobber Chomium build files."""
74 print 'Clobbering Chromium build files...'
75 out_dir = os.path.join(CHROMIUM_DIR, 'out')
76 if os.path.isdir(out_dir):
77 shutil.rmtree(out_dir)
78 print 'Removed Chromium out dir: %s.' % (out_dir)
81 def RunCommand(command, tries=1):
82 """Run a command, possibly with multiple retries."""
83 for i in range(0, tries):
84 print 'Running %s (try #%d)' % (str(command), i + 1)
85 if subprocess.call(command, shell=True) == 0:
91 def CopyFile(src, dst):
92 """Copy a file from src to dst."""
94 print "Copying %s to %s" % (src, dst)
97 def CopyDirectoryContents(src, dst, filename_filter=None):
98 """Copy the files from directory src to dst
99 with an optional filename filter."""
100 if not os.path.exists(dst):
102 for root, _, files in os.walk(src):
104 if filename_filter and not re.match(filename_filter, f):
106 CopyFile(os.path.join(root, f), dst)
109 def Checkout(name, url, dir):
110 """Checkout the SVN module at url into dir. Use name for the log message."""
111 print "Checking out %s r%s into '%s'" % (name, LLVM_WIN_REVISION, dir)
112 RunCommand(['svn', 'checkout', '--force',
113 url + '@' + LLVM_WIN_REVISION, dir], tries=2)
116 def AddCMakeToPath():
117 """Look for CMake and add it to PATH if it's not there already."""
119 # First check if cmake is already on PATH.
120 subprocess.call(['cmake', '--version'])
123 if e.errno != os.errno.ENOENT:
126 cmake_locations = ['C:\\Program Files (x86)\\CMake\\bin',
127 'C:\\Program Files (x86)\\CMake 2.8\\bin']
128 for d in cmake_locations:
130 os.environ['PATH'] = os.environ.get('PATH', '') + os.pathsep + d
132 print 'Failed to find CMake!'
142 # Try using the toolchain in depot_tools.
143 # This sets environment variables used by SelectVisualStudioVersion below.
144 sys.path.append(os.path.join(CHROMIUM_DIR, 'build'))
146 vs_toolchain.SetEnvironmentAndGetRuntimeDllDirs()
148 # Use gyp to find the MSVS installation, either in depot_tools as per above,
149 # or a system-wide installation otherwise.
150 sys.path.append(os.path.join(CHROMIUM_DIR, 'tools', 'gyp', 'pylib'))
151 import gyp.MSVSVersion
152 vs_version = gyp.MSVSVersion.SelectVisualStudioVersion('2013')
156 def SubversionCmakeArg():
157 # Since cmake's find_program can only find .exe and .com,
158 # svn.bat in depot_tools will be ignored.
159 default_pathext = ('.com', '.exe', '.bat', '.cmd')
160 for path in os.environ.get('PATH', '').split(os.pathsep):
161 for ext in default_pathext:
162 candidate = os.path.join(path, 'svn' + ext)
163 if os.path.isfile(candidate):
164 return '-DSubversion_SVN_EXECUTABLE=%s' % candidate
169 print 'Updating Clang to %s...' % (LLVM_WIN_REVISION)
170 if LLVM_WIN_REVISION != 'HEAD' and ReadStampFile() == LLVM_WIN_REVISION:
171 print 'Already up to date.'
175 ClobberChromiumBuildFiles()
177 # Reset the stamp file in case the build is unsuccessful.
180 Checkout('LLVM', LLVM_REPO_URL + '/llvm/trunk', LLVM_DIR)
181 Checkout('Clang', LLVM_REPO_URL + '/cfe/trunk', CLANG_DIR)
182 Checkout('compiler-rt', LLVM_REPO_URL + '/compiler-rt/trunk', COMPILER_RT_DIR)
184 if not os.path.exists(LLVM_BUILD_DIR):
185 os.makedirs(LLVM_BUILD_DIR)
186 os.chdir(LLVM_BUILD_DIR)
188 RunCommand(GetVSVersion().SetupScript('x64') +
189 ['&&', 'cmake', '-GNinja', '-DCMAKE_BUILD_TYPE=Release',
190 '-DLLVM_ENABLE_ASSERTIONS=ON', SubversionCmakeArg(), LLVM_DIR])
191 RunCommand(GetVSVersion().SetupScript('x64') + ['&&', 'ninja', 'all'])
193 # Do an x86 build of compiler-rt to get the 32-bit ASan run-time.
194 # TODO(hans): Remove once the regular build above produces this.
195 if not os.path.exists(COMPILER_RT_BUILD_DIR):
196 os.makedirs(COMPILER_RT_BUILD_DIR)
197 os.chdir(COMPILER_RT_BUILD_DIR)
198 RunCommand(GetVSVersion().SetupScript('x86') +
199 ['&&', 'cmake', '-GNinja', '-DCMAKE_BUILD_TYPE=Release',
200 '-DLLVM_ENABLE_ASSERTIONS=ON', LLVM_DIR])
201 RunCommand(GetVSVersion().SetupScript('x86') + ['&&', 'ninja', 'compiler-rt'])
203 asan_rt_bin_src_dir = os.path.join(COMPILER_RT_BUILD_DIR, 'bin')
204 asan_rt_bin_dst_dir = os.path.join(LLVM_BUILD_DIR, 'bin')
205 CopyDirectoryContents(asan_rt_bin_src_dir, asan_rt_bin_dst_dir,
208 # TODO(hans): Make this (and the .gypi file) version number independent.
209 asan_rt_lib_src_dir = os.path.join(COMPILER_RT_BUILD_DIR, 'lib', 'clang',
210 '3.6.0', 'lib', 'windows')
211 asan_rt_lib_dst_dir = os.path.join(LLVM_BUILD_DIR, 'lib', 'clang',
212 '3.6.0', 'lib', 'windows')
213 CopyDirectoryContents(asan_rt_lib_src_dir, asan_rt_lib_dst_dir,
216 CopyFile(os.path.join(asan_rt_lib_src_dir, '..', '..', 'asan_blacklist.txt'),
217 os.path.join(asan_rt_lib_dst_dir, '..', '..'))
219 # Make an extra copy of the sanitizer headers, to be put on the include path
220 # of the fallback compiler.
221 sanitizer_include_dir = os.path.join(LLVM_BUILD_DIR, 'lib', 'clang', '3.6.0',
222 'include', 'sanitizer')
223 aux_sanitizer_include_dir = os.path.join(LLVM_BUILD_DIR, 'lib', 'clang',
224 '3.6.0', 'include_sanitizer',
226 if not os.path.exists(aux_sanitizer_include_dir):
227 os.makedirs(aux_sanitizer_include_dir)
228 for _, _, files in os.walk(sanitizer_include_dir):
230 CopyFile(os.path.join(sanitizer_include_dir, f),
231 aux_sanitizer_include_dir)
233 WriteStampFile(LLVM_WIN_REVISION)
234 print 'Clang update was successful.'
239 if not sys.platform in ['win32', 'cygwin']:
240 # For non-Windows, fall back to update.sh.
241 # TODO(hans): Make update.py replace update.sh completely.
243 # This script is called by gclient. gclient opens its hooks subprocesses
244 # with (stdout=subprocess.PIPE, stderr=subprocess.STDOUT) and then does
245 # custom output processing that breaks printing '\r' characters for
246 # single-line updating status messages as printed by curl and wget.
247 # Work around this by setting stderr of the update.sh process to stdin (!):
248 # gclient doesn't redirect stdin, and while stdin itself is read-only, a
249 # dup()ed sys.stdin is writable, try
250 # fd2 = os.dup(sys.stdin.fileno()); os.write(fd2, 'hi')
251 # TODO: Fix gclient instead, http://crbug.com/95350
252 return subprocess.call(
253 [os.path.join(os.path.dirname(__file__), 'update.sh')] + sys.argv[1:],
254 stderr=os.fdopen(os.dup(sys.stdin.fileno())))
256 if not re.search(r'\b(clang|asan)=1', os.environ.get('GYP_DEFINES', '')):
257 print 'Skipping Clang update (clang=1 was not set in GYP_DEFINES).'
260 if re.search(r'\b(make_clang_dir)=', os.environ.get('GYP_DEFINES', '')):
261 print 'Skipping Clang update (make_clang_dir= was set in GYP_DEFINES).'
267 if __name__ == '__main__':