OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2015 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 """This script will check out llvm and clang, and then package the results up |
| 7 to a tgz file.""" |
| 8 |
| 9 import argparse |
| 10 import fnmatch |
| 11 import itertools |
| 12 import os |
| 13 import shutil |
| 14 import subprocess |
| 15 import sys |
| 16 import tarfile |
| 17 |
| 18 # Path constants. |
| 19 THIS_DIR = os.path.dirname(__file__) |
| 20 THIRD_PARTY_DIR = os.path.join(THIS_DIR, '..', '..', '..', 'third_party') |
| 21 LLVM_DIR = os.path.join(THIRD_PARTY_DIR, 'llvm') |
| 22 LLVM_BOOTSTRAP_DIR = os.path.join(THIRD_PARTY_DIR, 'llvm-bootstrap') |
| 23 LLVM_BOOTSTRAP_INSTALL_DIR = os.path.join(THIRD_PARTY_DIR, |
| 24 'llvm-bootstrap-install') |
| 25 LLVM_BUILD_DIR = os.path.join(THIRD_PARTY_DIR, 'llvm-build') |
| 26 LLVM_RELEASE_DIR = os.path.join(LLVM_BUILD_DIR, 'Release+Asserts') |
| 27 STAMP_FILE = os.path.join(LLVM_BUILD_DIR, 'cr_build_revision') |
| 28 |
| 29 |
| 30 def Tee(output, logfile): |
| 31 logfile.write(output) |
| 32 print output, |
| 33 |
| 34 |
| 35 def TeeCmd(cmd, logfile, fail_hard=True): |
| 36 """Runs cmd and writes the output to both stdout and logfile.""" |
| 37 # Reading from PIPE can deadlock if one buffer is full but we wait on a |
| 38 # different one. To work around this, pipe the subprocess's stderr to |
| 39 # its stdout buffer and don't give it a stdin. |
| 40 # shell=True is required in cmd.exe since depot_tools has an svn.bat, and |
| 41 # bat files only work with shell=True set. |
| 42 proc = subprocess.Popen(cmd, bufsize=1, shell=sys.platform == 'win32', |
| 43 stdin=open(os.devnull), stdout=subprocess.PIPE, |
| 44 stderr=subprocess.STDOUT) |
| 45 for line in iter(proc.stdout.readline,''): |
| 46 Tee(line, logfile) |
| 47 if proc.poll() is not None: |
| 48 break |
| 49 exit_code = proc.wait() |
| 50 if exit_code != 0 and fail_hard: |
| 51 print 'Failed:', cmd |
| 52 sys.exit(1) |
| 53 |
| 54 |
| 55 def PrintTarProgress(tarinfo): |
| 56 print 'Adding', tarinfo.name |
| 57 return tarinfo |
| 58 |
| 59 |
| 60 def main(): |
| 61 parser = argparse.ArgumentParser(description='build and package clang') |
| 62 parser.add_argument('--gcc-toolchain', |
| 63 help="the prefix for the GCC version used for building. " |
| 64 "For /opt/foo/bin/gcc, pass " |
| 65 "'--gcc-toolchain '/opt/foo'") |
| 66 |
| 67 args = parser.parse_args() |
| 68 |
| 69 with open('buildlog.txt', 'w') as log: |
| 70 Tee('Diff in llvm:\n', log) |
| 71 TeeCmd(['svn', 'stat', LLVM_DIR], log, fail_hard=False) |
| 72 TeeCmd(['svn', 'diff', LLVM_DIR], log, fail_hard=False) |
| 73 Tee('Diff in llvm/tools/clang:\n', log) |
| 74 TeeCmd(['svn', 'stat', os.path.join(LLVM_DIR, 'tools', 'clang')], |
| 75 log, fail_hard=False) |
| 76 TeeCmd(['svn', 'diff', os.path.join(LLVM_DIR, 'tools', 'clang')], |
| 77 log, fail_hard=False) |
| 78 Tee('Diff in llvm/compiler-rt:\n', log) |
| 79 TeeCmd(['svn', 'stat', os.path.join(LLVM_DIR, 'compiler-rt')], |
| 80 log, fail_hard=False) |
| 81 TeeCmd(['svn', 'diff', os.path.join(LLVM_DIR, 'compiler-rt')], |
| 82 log, fail_hard=False) |
| 83 Tee('Diff in llvm/projects/libcxx:\n', log) |
| 84 TeeCmd(['svn', 'stat', os.path.join(LLVM_DIR, 'projects', 'libcxx')], |
| 85 log, fail_hard=False) |
| 86 TeeCmd(['svn', 'diff', os.path.join(LLVM_DIR, 'projects', 'libcxx')], |
| 87 log, fail_hard=False) |
| 88 Tee('Diff in llvm/projects/libcxxabi:\n', log) |
| 89 TeeCmd(['svn', 'stat', os.path.join(LLVM_DIR, 'projects', 'libcxxabi')], |
| 90 log, fail_hard=False) |
| 91 TeeCmd(['svn', 'diff', os.path.join(LLVM_DIR, 'projects', 'libcxxabi')], |
| 92 log, fail_hard=False) |
| 93 |
| 94 Tee('Starting build\n', log) |
| 95 |
| 96 # Do a clobber build. |
| 97 shutil.rmtree(LLVM_BOOTSTRAP_DIR, ignore_errors=True) |
| 98 shutil.rmtree(LLVM_BOOTSTRAP_INSTALL_DIR, ignore_errors=True) |
| 99 shutil.rmtree(LLVM_BUILD_DIR, ignore_errors=True) |
| 100 |
| 101 build_cmd = [sys.executable, os.path.join(THIS_DIR, 'update.py'), |
| 102 '--bootstrap', '--force-local-build', '--run-tests', |
| 103 '--no-stdin-hack'] |
| 104 if args.gcc_toolchain is not None: |
| 105 build_cmd.extend(['--gcc-toolchain', args.gcc_toolchain]) |
| 106 TeeCmd(build_cmd, log) |
| 107 |
| 108 stamp = open(STAMP_FILE).read().rstrip() |
| 109 pdir = 'clang-' + stamp |
| 110 print pdir |
| 111 shutil.rmtree(pdir, ignore_errors=True) |
| 112 |
| 113 # Copy a whitelist of files to the directory we're going to tar up. |
| 114 # This supports the same patterns that the fnmatch module understands. |
| 115 exe_ext = '.exe' if sys.platform == 'win32' else '' |
| 116 want = ['bin/llvm-symbolizer' + exe_ext, |
| 117 'lib/clang/*/asan_blacklist.txt', |
| 118 # Copy built-in headers (lib/clang/3.x.y/include). |
| 119 'lib/clang/*/include/*', |
| 120 ] |
| 121 if sys.platform == 'win32': |
| 122 want.append('bin/clang-cl.exe') |
| 123 else: |
| 124 so_ext = 'dylib' if sys.platform == 'darwin' else 'so' |
| 125 want.extend(['bin/clang', |
| 126 'lib/libFindBadConstructs.' + so_ext, |
| 127 'lib/libBlinkGCPlugin.' + so_ext, |
| 128 ]) |
| 129 if sys.platform == 'darwin': |
| 130 want.extend(['bin/libc++.1.dylib', |
| 131 # Copy only the OSX (ASan and profile) and iossim (ASan) |
| 132 # runtime libraries: |
| 133 'lib/clang/*/lib/darwin/*asan_osx*', |
| 134 'lib/clang/*/lib/darwin/*asan_iossim*', |
| 135 'lib/clang/*/lib/darwin/*profile_osx*', |
| 136 ]) |
| 137 elif sys.platform.startswith('linux'): |
| 138 # Copy only |
| 139 # lib/clang/*/lib/linux/libclang_rt.{[atm]san,san,ubsan,profile}-*.a , |
| 140 # but not dfsan. |
| 141 want.extend(['lib/clang/*/lib/linux/*[atm]san*', |
| 142 'lib/clang/*/lib/linux/*ubsan*', |
| 143 'lib/clang/*/lib/linux/*libclang_rt.san*', |
| 144 'lib/clang/*/lib/linux/*profile*', |
| 145 'lib/clang/*/msan_blacklist.txt', |
| 146 ]) |
| 147 elif sys.platform == 'win32': |
| 148 want.extend(['lib/clang/*/lib/windows/clang_rt.asan*.dll', |
| 149 'lib/clang/*/lib/windows/clang_rt.asan*.lib', |
| 150 'lib/clang/*/include_sanitizer/*', |
| 151 ]) |
| 152 if args.gcc_toolchain is not None: |
| 153 # Copy the stdlibc++.so.6 we linked Clang against so it can run. |
| 154 want.append('lib/libstdc++.so.6') |
| 155 |
| 156 for root, dirs, files in os.walk(LLVM_RELEASE_DIR): |
| 157 # root: third_party/llvm-build/Release+Asserts/lib/..., rel_root: lib/... |
| 158 rel_root = root[len(LLVM_RELEASE_DIR)+1:] |
| 159 rel_files = [os.path.join(rel_root, f) for f in files] |
| 160 wanted_files = list(set(itertools.chain.from_iterable( |
| 161 fnmatch.filter(rel_files, p) for p in want))) |
| 162 if wanted_files: |
| 163 # Guaranteed to not yet exist at this point: |
| 164 os.makedirs(os.path.join(pdir, rel_root)) |
| 165 for f in wanted_files: |
| 166 src = os.path.join(LLVM_RELEASE_DIR, f) |
| 167 dest = os.path.join(pdir, f) |
| 168 shutil.copy(src, dest) |
| 169 # Strip libraries. |
| 170 if sys.platform == 'darwin' and f.endswith('.dylib'): |
| 171 # Fix LC_ID_DYLIB for the ASan dynamic libraries to be relative to |
| 172 # @executable_path. |
| 173 # TODO(glider): this is transitional. We'll need to fix the dylib |
| 174 # name either in our build system, or in Clang. See also |
| 175 # http://crbug.com/344836. |
| 176 subprocess.call(['install_name_tool', '-id', |
| 177 '@executable_path/' + os.path.basename(dest), dest]) |
| 178 subprocess.call(['strip', '-x', dest]) |
| 179 elif (sys.platform.startswith('linux') and |
| 180 os.path.splitext(f)[1] in ['.so', '.a']): |
| 181 subprocess.call(['strip', '-g', dest]) |
| 182 |
| 183 # Set up symlinks. |
| 184 if sys.platform != 'win32': |
| 185 os.symlink('clang', os.path.join(pdir, 'bin', 'clang++')) |
| 186 if sys.platform == 'darwin': |
| 187 os.symlink('libc++.1.dylib', os.path.join(pdir, 'bin', 'libc++.dylib')) |
| 188 # Also copy libc++ headers. |
| 189 shutil.copytree(os.path.join(LLVM_BOOTSTRAP_INSTALL_DIR, 'include', 'c++'), |
| 190 os.path.join(pdir, 'include', 'c++')) |
| 191 |
| 192 # Copy buildlog over. |
| 193 shutil.copy('buildlog.txt', pdir) |
| 194 |
| 195 # Create archive. |
| 196 tar_entries = ['bin', 'lib', 'buildlog.txt'] |
| 197 if sys.platform == 'darwin': |
| 198 tar_entries += ['include'] |
| 199 with tarfile.open(pdir + '.tgz', 'w:gz') as tar: |
| 200 for entry in tar_entries: |
| 201 tar.add(os.path.join(pdir, entry), arcname=entry, filter=PrintTarProgress) |
| 202 |
| 203 if sys.platform == 'darwin': |
| 204 platform = 'Mac' |
| 205 elif sys.platform == 'win32': |
| 206 platform = 'Win' |
| 207 else: |
| 208 platform = 'Linux_x64' |
| 209 |
| 210 print 'To upload, run:' |
| 211 print ('gsutil cp -a public-read %s.tgz ' |
| 212 'gs://chromium-browser-clang/%s/%s.tgz') % (pdir, platform, pdir) |
| 213 |
| 214 # Zip up gold plugin on Linux. |
| 215 if sys.platform.startswith('linux'): |
| 216 golddir = 'llvmgold-' + stamp |
| 217 shutil.rmtree(golddir, ignore_errors=True) |
| 218 os.makedirs(os.path.join(golddir, 'lib')) |
| 219 shutil.copy(os.path.join(LLVM_RELEASE_DIR, 'lib', 'LLVMgold.so'), |
| 220 os.path.join(golddir, 'lib')) |
| 221 with tarfile.open(golddir + '.tgz', 'w:gz') as tar: |
| 222 tar.add(os.path.join(golddir, 'lib'), arcname='lib', |
| 223 filter=PrintTarProgress) |
| 224 print ('gsutil cp -a public-read %s.tgz ' |
| 225 'gs://chromium-browser-clang/%s/%s.tgz') % (golddir, platform, |
| 226 golddir) |
| 227 |
| 228 # FIXME: Warn if the file already exists on the server. |
| 229 |
| 230 |
| 231 if __name__ == '__main__': |
| 232 sys.exit(main()) |
OLD | NEW |