blob: 4614f2ca20357a2cd00d8f62883e37af6210a3a6 [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() {
Don Hinton1e4f6022017-07-31 15:18:57 +000014 cat << EOF
Ilya Biryukovaf351da2017-06-30 09:46:45 +000015Usage: 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
Ilya Biryukovaf351da2017-06-30 09:46:45 +000040}
41
42LLVM_SVN_REV=""
43LLVM_BRANCH=""
44CMAKE_ARGS=""
45CMAKE_INSTALL_TARGETS=""
46# We always checkout llvm
47LLVM_PROJECTS="llvm"
48CMAKE_LLVM_ENABLE_PROJECTS=""
Ilya Biryukove421fc82017-07-20 08:30:44 +000049CLANG_TOOLS_EXTRA_ENABLED=0
Ilya Biryukovaf351da2017-06-30 09:46:45 +000050
51function contains_project() {
52 local TARGET_PROJ="$1"
53 local PROJ
54 for PROJ in $LLVM_PROJECTS; do
55 if [ "$PROJ" == "$TARGET_PROJ" ]; then
56 return 0
57 fi
58 done
59 return 1
60}
61
Ilya Biryukove421fc82017-07-20 08:30:44 +000062function append_project() {
63 local PROJ="$1"
64
65 LLVM_PROJECTS="$LLVM_PROJECTS $PROJ"
66 if [ "$CMAKE_LLVM_ENABLE_PROJECTS" != "" ]; then
67 CMAKE_LLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLE_PROJECTS;$PROJ"
68 else
69 CMAKE_LLVM_ENABLE_PROJECTS="$PROJ"
70 fi
71}
72
Ilya Biryukovaf351da2017-06-30 09:46:45 +000073while [[ $# -gt 0 ]]; do
74 case "$1" in
75 -r|--revision)
76 shift
77 LLVM_SVN_REV="$1"
Ilya Biryukov4d7234c2017-07-03 15:16:27 +000078 shift
Ilya Biryukovaf351da2017-06-30 09:46:45 +000079 ;;
80 -b|--branch)
81 shift
82 LLVM_BRANCH="$1"
83 shift
84 ;;
85 -p|--llvm-project)
86 shift
87 PROJ="$1"
Ilya Biryukove421fc82017-07-20 08:30:44 +000088 shift
89
Ilya Biryukovaf351da2017-06-30 09:46:45 +000090 if [ "$PROJ" == "cfe" ]; then
91 PROJ="clang"
92 fi
Ilya Biryukove421fc82017-07-20 08:30:44 +000093
94 if [ "$PROJ" == "clang-tools-extra" ]; then
95 if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then
96 echo "Project 'clang-tools-extra' is already enabled, ignoring extra occurences."
97 else
98 CLANG_TOOLS_EXTRA_ENABLED=1
Ilya Biryukov13cde862017-07-06 13:10:55 +000099 fi
Ilya Biryukove421fc82017-07-20 08:30:44 +0000100
101 continue
102 fi
103
104 if ! contains_project "$PROJ" ; then
105 append_project "$PROJ"
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000106 else
107 echo "Project '$PROJ' is already enabled, ignoring extra occurences."
108 fi
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000109 ;;
110 -i|--install-target)
111 shift
112 CMAKE_INSTALL_TARGETS="$CMAKE_INSTALL_TARGETS $1"
113 shift
114 ;;
115 --)
116 shift
117 CMAKE_ARGS="$*"
118 shift $#
119 ;;
120 -h|--help)
121 show_usage
122 exit 0
123 ;;
124 *)
125 echo "Unknown option: $1"
126 exit 1
127 esac
128done
129
130if [ "$CMAKE_INSTALL_TARGETS" == "" ]; then
131 echo "No install targets. Please pass one or more --install-target."
132 exit 1
133fi
134
Ilya Biryukove421fc82017-07-20 08:30:44 +0000135if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then
136 if ! contains_project "clang"; then
137 echo "Project 'clang-tools-extra' was enabled without 'clang'."
138 echo "Adding 'clang' to a list of projects."
139
140 append_project "clang"
141 fi
142fi
143
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000144if [ "$LLVM_BRANCH" == "" ]; then
145 LLVM_BRANCH="trunk"
146fi
147
148if [ "$LLVM_SVN_REVISION" != "" ]; then
149 SVN_REV_ARG="-r$LLVM_SVN_REVISION"
150else
151 SVN_REV_ARG=""
152fi
153
154CLANG_BUILD_DIR=/tmp/clang-build
155CLANG_INSTALL_DIR=/tmp/clang-install
156
157mkdir "$CLANG_BUILD_DIR"
158
159# Get the sources from svn.
160echo "Checking out sources from svn"
161mkdir "$CLANG_BUILD_DIR/src"
162for LLVM_PROJECT in $LLVM_PROJECTS; do
163 if [ "$LLVM_PROJECT" == "clang" ]; then
164 SVN_PROJECT="cfe"
165 else
166 SVN_PROJECT="$LLVM_PROJECT"
167 fi
168
Ilya Biryukov13cde862017-07-06 13:10:55 +0000169 echo "Checking out https://llvm.org/svn/llvm-project/$SVN_PROJECT to $CLANG_BUILD_DIR/src/$LLVM_PROJECT"
Ilya Biryukov827c8ac2017-08-18 09:37:23 +0000170 svn co -q $SVN_REV_ARG \
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000171 "https://llvm.org/svn/llvm-project/$SVN_PROJECT/$LLVM_BRANCH" \
172 "$CLANG_BUILD_DIR/src/$LLVM_PROJECT"
173done
174
Ilya Biryukove421fc82017-07-20 08:30:44 +0000175if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then
176 echo "Checking out https://llvm.org/svn/llvm-project/clang-tools-extra to $CLANG_BUILD_DIR/src/clang/tools/extra"
Ilya Biryukov827c8ac2017-08-18 09:37:23 +0000177 svn co -q $SVN_REV_ARG \
Ilya Biryukove421fc82017-07-20 08:30:44 +0000178 "https://llvm.org/svn/llvm-project/clang-tools-extra/$LLVM_BRANCH" \
179 "$CLANG_BUILD_DIR/src/clang/tools/extra"
180fi
181
Ilya Biryukov13cde862017-07-06 13:10:55 +0000182mkdir "$CLANG_BUILD_DIR/build"
183pushd "$CLANG_BUILD_DIR/build"
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000184
185# Run the build as specified in the build arguments.
186echo "Running build"
Ilya Biryukovaf351da2017-06-30 09:46:45 +0000187cmake -GNinja \
188 -DCMAKE_INSTALL_PREFIX="$CLANG_INSTALL_DIR" \
189 -DLLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLE_PROJECTS" \
190 $CMAKE_ARGS \
191 "$CLANG_BUILD_DIR/src/llvm"
192ninja $CMAKE_INSTALL_TARGETS
193
194popd
195
196# Pack the installed clang into an archive.
197echo "Archiving clang installation to /tmp/clang.tar.gz"
198cd "$CLANG_INSTALL_DIR"
199tar -czf /tmp/clang.tar.gz *
200
201# Cleanup.
202rm -rf "$CLANG_BUILD_DIR" "$CLANG_INSTALL_DIR"
203
204echo "Done"