1#!/usr/bin/env python
2#
3# Copyright 2016 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8
9"""Create a Clang toolchain for Linux hosts."""
10
11
12import argparse
13import os
14import subprocess
15import tempfile
16
17REPO = "https://llvm.googlesource.com/"
18BRANCH = "release_50"
19
20def create_asset(target_dir):
21  # CMake will sometimes barf if we pass it a relative path.
22  target_dir = os.path.abspath(target_dir)
23
24  # Build Clang, lld, compiler-rt (sanitizer support) and libc++.
25  os.chdir(tempfile.mkdtemp())
26  subprocess.check_call(["git", "clone", "--depth", "1", "-b",
27                         BRANCH, REPO + "llvm"])
28  os.chdir("llvm/tools")
29  subprocess.check_call(["git", "clone", "--depth", "1", "-b",
30                         BRANCH, REPO + "clang"])
31  subprocess.check_call(["git", "clone", "--depth", "1", "-b",
32                         BRANCH, REPO + "lld"])
33  os.chdir("../projects")
34  subprocess.check_call(["git", "clone", "--depth", "1", "-b",
35                         BRANCH, REPO + "compiler-rt"])
36  subprocess.check_call(["git", "clone", "--depth", "1", "-b",
37                         BRANCH, REPO + "libcxx"])
38  subprocess.check_call(["git", "clone", "--depth", "1", "-b",
39                         BRANCH, REPO + "libcxxabi"])
40  os.chdir("..")
41  os.mkdir("out")
42  os.chdir("out")
43  subprocess.check_call(["cmake", "..", "-G", "Ninja",
44                         "-DCMAKE_BUILD_TYPE=MinSizeRel",
45                         "-DCMAKE_INSTALL_PREFIX=" + target_dir,
46                         "-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON",
47                         "-DLLVM_ENABLE_TERMINFO=OFF"])
48  subprocess.check_call(["ninja", "install"])
49
50  # Copy a couple extra files we need.
51  subprocess.check_call(["cp", "bin/llvm-symbolizer", target_dir + "/bin"])
52  subprocess.check_call(["cp", "bin/llvm-profdata", target_dir + "/bin"])
53  subprocess.check_call(["cp", "bin/llvm-cov", target_dir + "/bin"])
54  libstdcpp = subprocess.check_output(["c++",
55                                       "-print-file-name=libstdc++.so.6"])
56  subprocess.check_call(["cp", libstdcpp.strip(), target_dir + "/lib"])
57
58  # Finally, build libc++ for MSAN bots using the Clang we just built.
59  os.mkdir("../msan_out")
60  os.chdir("../msan_out")
61  subprocess.check_call(["cmake", "..", "-G", "Ninja",
62                         "-DCMAKE_BUILD_TYPE=MinSizeRel",
63                         "-DCMAKE_C_COMPILER="   + target_dir + "/bin/clang",
64                         "-DCMAKE_CXX_COMPILER=" + target_dir + "/bin/clang++",
65                         "-DLLVM_USE_SANITIZER=MemoryWithOrigins"])
66  subprocess.check_call(["ninja", "cxx"])
67  subprocess.check_call(["cp", "-r", "lib",  target_dir + "/msan"])
68
69
70def main():
71  parser = argparse.ArgumentParser()
72  parser.add_argument('--target_dir', '-t', required=True)
73  args = parser.parse_args()
74  create_asset(args.target_dir)
75
76
77if __name__ == '__main__':
78  main()
79