blob: cc5cd29c3280b9a0fab8459db80bde26df76b29c [file] [log] [blame]
Ilya Biryukovaf351da2017-06-30 09:46:45 +00001#!/usr/bin/env bash
2#===- llvm/utils/docker/scripts/build_install_llvm.sh ---------------------===//
3#
4# The LLVM Compiler Infrastructure
5#
6# This file is distributed under the University of Illinois Open Source
7# License. See LICENSE.TXT for details.
8#
9#===-----------------------------------------------------------------------===//
10
11set -e
12
13function show_usage() {
14 usage=$(cat << EOF
15Usage: build_install_llvm.sh [options] -- [cmake-args]
16
17Checkout svn sources and run cmake with the specified arguments. Used
18inside docker container.
19Passes additional -DCMAKE_INSTALL_PREFIX and archives the contents of
20the directory to /tmp/clang.tar.gz.
21
22Available options:
23 -h|--help show this help message
24 -b|--branch svn branch to checkout, i.e. 'trunk',
25 'branches/release_40'
26 (default: 'trunk')
27 -r|--revision svn revision to checkout
28 -p|--llvm-project name of an svn project to checkout. Will also add the
29 project to a list LLVM_ENABLE_PROJECTS, passed to CMake.
30 For clang, please use 'clang', not 'cfe'.
31 Project 'llvm' is always included and ignored, if
32 specified.
33 Can be specified multiple times.
34 -i|--install-target name of a cmake install target to build and include in
35 the resulting archive. Can be specified multiple times.
36Required options: At least one --install-target.
37
38All options after '--' are passed to CMake invocation.
39EOF
40)
41 echo "$usage"
42}
43
44LLVM_SVN_REV=""
45LLVM_BRANCH=""
46CMAKE_ARGS=""
47CMAKE_INSTALL_TARGETS=""
48# We always checkout llvm
49LLVM_PROJECTS="llvm"
50CMAKE_LLVM_ENABLE_PROJECTS=""
51
52function contains_project() {
53 local TARGET_PROJ="$1"
54 local PROJ
55 for PROJ in $LLVM_PROJECTS; do
56 if [ "$PROJ" == "$TARGET_PROJ" ]; then
57 return 0
58 fi
59 done
60 return 1
61}
62
63while [[ $# -gt 0 ]]; do
64 case "$1" in
65 -r|--revision)
66 shift
67 LLVM_SVN_REV="$1"
Ilya Biryukov4d7234c2017-07-03 15:16:27 +000068 shift
Ilya Biryukovaf351da2017-06-30 09:46:45 +000069 ;;
70 -b|--branch)
71 shift
72 LLVM_BRANCH="$1"
73 shift
74 ;;
75 -p|--llvm-project)
76 shift
77 PROJ="$1"
78 if [ "$PROJ" == "cfe" ]; then
79 PROJ="clang"
80 fi
81 if ! contains_project "$PROJ" ; then
82 LLVM_PROJECTS="$LLVM_PROJECTS $PROJ"
83 CMAKE_LLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLED_PROJECTS;$PROJ"
84 else
85 echo "Project '$PROJ' is already enabled, ignoring extra occurences."
86 fi
87 shift
88 ;;
89 -i|--install-target)
90 shift
91 CMAKE_INSTALL_TARGETS="$CMAKE_INSTALL_TARGETS $1"
92 shift
93 ;;
94 --)
95 shift
96 CMAKE_ARGS="$*"
97 shift $#
98 ;;
99 -h|--help)
100 show_usage
101 exit 0
102 ;;
103 *)
104 echo "Unknown option: $1"
105 exit 1
106 esac
107done
108
109if [ "$CMAKE_INSTALL_TARGETS" == "" ]; then
110 echo "No install targets. Please pass one or more --install-target."
111 exit 1
112fi
113
114if [ "$LLVM_BRANCH" == "" ]; then
115 LLVM_BRANCH="trunk"
116fi
117
118if [ "$LLVM_SVN_REVISION" != "" ]; then
119 SVN_REV_ARG="-r$LLVM_SVN_REVISION"
120else
121 SVN_REV_ARG=""
122fi
123
124CLANG_BUILD_DIR=/tmp/clang-build
125CLANG_INSTALL_DIR=/tmp/clang-install
126
127mkdir "$CLANG_BUILD_DIR"
128
129# Get the sources from svn.
130echo "Checking out sources from svn"
131mkdir "$CLANG_BUILD_DIR/src"
132for LLVM_PROJECT in $LLVM_PROJECTS; do
133 if [ "$LLVM_PROJECT" == "clang" ]; then
134 SVN_PROJECT="cfe"
135 else
136 SVN_PROJECT="$LLVM_PROJECT"
137 fi
138
139 echo "Checking out http://llvm.org/svn/llvm-project/$SVN_PROJECT to $CLANG_BUILD_DIR/src/$LLVM_PROJECT"
140 # FIXME: --trust-server-cert is required to workaround 'SSL issuer is not
141 # trusted' error. Using https seems preferable to http either way,
142 # albeit this is not secure.
143 svn co -q $SVN_REV_ARG --trust-server-cert \
144 "https://llvm.org/svn/llvm-project/$SVN_PROJECT/$LLVM_BRANCH" \
145 "$CLANG_BUILD_DIR/src/$LLVM_PROJECT"
146done
147
148pushd "$CLANG_BUILD_DIR"
149
150# Run the build as specified in the build arguments.
151echo "Running build"
152mkdir "$CLANG_BUILD_DIR/build"
153cmake -GNinja \
154 -DCMAKE_INSTALL_PREFIX="$CLANG_INSTALL_DIR" \
155 -DLLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLE_PROJECTS" \
156 $CMAKE_ARGS \
157 "$CLANG_BUILD_DIR/src/llvm"
158ninja $CMAKE_INSTALL_TARGETS
159
160popd
161
162# Pack the installed clang into an archive.
163echo "Archiving clang installation to /tmp/clang.tar.gz"
164cd "$CLANG_INSTALL_DIR"
165tar -czf /tmp/clang.tar.gz *
166
167# Cleanup.
168rm -rf "$CLANG_BUILD_DIR" "$CLANG_INSTALL_DIR"
169
170echo "Done"