blob: 6016e3afe7d8646cf0a50a2e2194041f424eaa06 [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=""
Ilya Biryukove421fc82017-07-20 08:30:44 +000051CLANG_TOOLS_EXTRA_ENABLED=0
Ilya Biryukovaf351da2017-06-30 09:46:45 +000052
53function contains_project() {
54 local TARGET_PROJ="$1"
55 local PROJ
56 for PROJ in $LLVM_PROJECTS; do
57 if [ "$PROJ" == "$TARGET_PROJ" ]; then
58 return 0
59 fi
60 done
61 return 1
62}
63
Ilya Biryukove421fc82017-07-20 08:30:44 +000064function append_project() {
65 local PROJ="$1"
66
67 LLVM_PROJECTS="$LLVM_PROJECTS $PROJ"
68 if [ "$CMAKE_LLVM_ENABLE_PROJECTS" != "" ]; then
69 CMAKE_LLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLE_PROJECTS;$PROJ"
70 else
71 CMAKE_LLVM_ENABLE_PROJECTS="$PROJ"
72 fi
73}
74
Ilya Biryukovaf351da2017-06-30 09:46:45 +000075while [[ $# -gt 0 ]]; do
76 case "$1" in
77 -r|--revision)
78 shift
79 LLVM_SVN_REV="$1"
Ilya Biryukov4d7234c2017-07-03 15:16:27 +000080 shift
Ilya Biryukovaf351da2017-06-30 09:46:45 +000081 ;;
82 -b|--branch)
83 shift
84 LLVM_BRANCH="$1"
85 shift
86 ;;
87 -p|--llvm-project)
88 shift
89 PROJ="$1"
Ilya Biryukove421fc82017-07-20 08:30:44 +000090 shift
91
Ilya Biryukovaf351da2017-06-30 09:46:45 +000092 if [ "$PROJ" == "cfe" ]; then
93 PROJ="clang"
94 fi
Ilya Biryukove421fc82017-07-20 08:30:44 +000095
96 if [ "$PROJ" == "clang-tools-extra" ]; then
97 if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then
98 echo "Project 'clang-tools-extra' is already enabled, ignoring extra occurences."
99 else
100 CLANG_TOOLS_EXTRA_ENABLED=1
Ilya Biryukov13cde862017-07-06 13:10:55 +0000101 fi
Ilya Biryukove421fc82017-07-20 08:30:44 +0000102
103 continue
104 fi
105
106 if ! contains_project "$PROJ" ; then
107 append_project "$PROJ"
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000108 else
109 echo "Project '$PROJ' is already enabled, ignoring extra occurences."
110 fi
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000111 ;;
112 -i|--install-target)
113 shift
114 CMAKE_INSTALL_TARGETS="$CMAKE_INSTALL_TARGETS $1"
115 shift
116 ;;
117 --)
118 shift
119 CMAKE_ARGS="$*"
120 shift $#
121 ;;
122 -h|--help)
123 show_usage
124 exit 0
125 ;;
126 *)
127 echo "Unknown option: $1"
128 exit 1
129 esac
130done
131
132if [ "$CMAKE_INSTALL_TARGETS" == "" ]; then
133 echo "No install targets. Please pass one or more --install-target."
134 exit 1
135fi
136
Ilya Biryukove421fc82017-07-20 08:30:44 +0000137if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then
138 if ! contains_project "clang"; then
139 echo "Project 'clang-tools-extra' was enabled without 'clang'."
140 echo "Adding 'clang' to a list of projects."
141
142 append_project "clang"
143 fi
144fi
145
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000146if [ "$LLVM_BRANCH" == "" ]; then
147 LLVM_BRANCH="trunk"
148fi
149
150if [ "$LLVM_SVN_REVISION" != "" ]; then
151 SVN_REV_ARG="-r$LLVM_SVN_REVISION"
152else
153 SVN_REV_ARG=""
154fi
155
156CLANG_BUILD_DIR=/tmp/clang-build
157CLANG_INSTALL_DIR=/tmp/clang-install
158
159mkdir "$CLANG_BUILD_DIR"
160
161# Get the sources from svn.
162echo "Checking out sources from svn"
163mkdir "$CLANG_BUILD_DIR/src"
164for LLVM_PROJECT in $LLVM_PROJECTS; do
165 if [ "$LLVM_PROJECT" == "clang" ]; then
166 SVN_PROJECT="cfe"
167 else
168 SVN_PROJECT="$LLVM_PROJECT"
169 fi
170
Ilya Biryukov13cde862017-07-06 13:10:55 +0000171 echo "Checking out https://llvm.org/svn/llvm-project/$SVN_PROJECT to $CLANG_BUILD_DIR/src/$LLVM_PROJECT"
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000172 # FIXME: --trust-server-cert is required to workaround 'SSL issuer is not
173 # trusted' error. Using https seems preferable to http either way,
174 # albeit this is not secure.
175 svn co -q $SVN_REV_ARG --trust-server-cert \
176 "https://llvm.org/svn/llvm-project/$SVN_PROJECT/$LLVM_BRANCH" \
177 "$CLANG_BUILD_DIR/src/$LLVM_PROJECT"
178done
179
Ilya Biryukove421fc82017-07-20 08:30:44 +0000180if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then
181 echo "Checking out https://llvm.org/svn/llvm-project/clang-tools-extra to $CLANG_BUILD_DIR/src/clang/tools/extra"
182 # FIXME: --trust-server-cert is required to workaround 'SSL issuer is not
183 # trusted' error. Using https seems preferable to http either way,
184 # albeit this is not secure.
185 svn co -q $SVN_REV_ARG --trust-server-cert \
186 "https://llvm.org/svn/llvm-project/clang-tools-extra/$LLVM_BRANCH" \
187 "$CLANG_BUILD_DIR/src/clang/tools/extra"
188fi
189
Ilya Biryukov13cde862017-07-06 13:10:55 +0000190mkdir "$CLANG_BUILD_DIR/build"
191pushd "$CLANG_BUILD_DIR/build"
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000192
193# Run the build as specified in the build arguments.
194echo "Running build"
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000195cmake -GNinja \
196 -DCMAKE_INSTALL_PREFIX="$CLANG_INSTALL_DIR" \
197 -DLLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLE_PROJECTS" \
198 $CMAKE_ARGS \
199 "$CLANG_BUILD_DIR/src/llvm"
200ninja $CMAKE_INSTALL_TARGETS
201
202popd
203
204# Pack the installed clang into an archive.
205echo "Archiving clang installation to /tmp/clang.tar.gz"
206cd "$CLANG_INSTALL_DIR"
207tar -czf /tmp/clang.tar.gz *
208
209# Cleanup.
210rm -rf "$CLANG_BUILD_DIR" "$CLANG_INSTALL_DIR"
211
212echo "Done"