Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 1 | #!/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 | |
| 11 | set -e |
| 12 | |
| 13 | function show_usage() { |
Don Hinton | 1e4f602 | 2017-07-31 15:18:57 +0000 | [diff] [blame] | 14 | cat << EOF |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 15 | Usage: build_install_llvm.sh [options] -- [cmake-args] |
| 16 | |
| 17 | Checkout svn sources and run cmake with the specified arguments. Used |
| 18 | inside docker container. |
Ilya Biryukov | d950201 | 2018-03-26 15:12:30 +0000 | [diff] [blame] | 19 | Passes additional -DCMAKE_INSTALL_PREFIX and puts the build results into |
| 20 | /tmp/clang-install/ directory. |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 21 | |
| 22 | Available 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 |
Ilya Biryukov | 75db081 | 2017-12-20 14:39:07 +0000 | [diff] [blame] | 28 | -c|--cherrypick revision to cherry-pick. Can be specified multiple times. |
| 29 | Cherry-picks are performed in the sorted order using the |
| 30 | following command: |
| 31 | 'svn patch <(svn diff -c \$rev)'. |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 32 | -p|--llvm-project name of an svn project to checkout. Will also add the |
| 33 | project to a list LLVM_ENABLE_PROJECTS, passed to CMake. |
| 34 | For clang, please use 'clang', not 'cfe'. |
| 35 | Project 'llvm' is always included and ignored, if |
| 36 | specified. |
| 37 | Can be specified multiple times. |
| 38 | -i|--install-target name of a cmake install target to build and include in |
| 39 | the resulting archive. Can be specified multiple times. |
| 40 | Required options: At least one --install-target. |
| 41 | |
| 42 | All options after '--' are passed to CMake invocation. |
| 43 | EOF |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | LLVM_SVN_REV="" |
Ilya Biryukov | 75db081 | 2017-12-20 14:39:07 +0000 | [diff] [blame] | 47 | CHERRYPICKS="" |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 48 | LLVM_BRANCH="" |
| 49 | CMAKE_ARGS="" |
| 50 | CMAKE_INSTALL_TARGETS="" |
| 51 | # We always checkout llvm |
| 52 | LLVM_PROJECTS="llvm" |
| 53 | CMAKE_LLVM_ENABLE_PROJECTS="" |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 54 | CLANG_TOOLS_EXTRA_ENABLED=0 |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 55 | |
| 56 | function contains_project() { |
| 57 | local TARGET_PROJ="$1" |
| 58 | local PROJ |
| 59 | for PROJ in $LLVM_PROJECTS; do |
| 60 | if [ "$PROJ" == "$TARGET_PROJ" ]; then |
| 61 | return 0 |
| 62 | fi |
| 63 | done |
| 64 | return 1 |
| 65 | } |
| 66 | |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 67 | function append_project() { |
| 68 | local PROJ="$1" |
| 69 | |
| 70 | LLVM_PROJECTS="$LLVM_PROJECTS $PROJ" |
| 71 | if [ "$CMAKE_LLVM_ENABLE_PROJECTS" != "" ]; then |
| 72 | CMAKE_LLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLE_PROJECTS;$PROJ" |
| 73 | else |
| 74 | CMAKE_LLVM_ENABLE_PROJECTS="$PROJ" |
| 75 | fi |
| 76 | } |
| 77 | |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 78 | while [[ $# -gt 0 ]]; do |
| 79 | case "$1" in |
| 80 | -r|--revision) |
| 81 | shift |
| 82 | LLVM_SVN_REV="$1" |
Ilya Biryukov | 4d7234c | 2017-07-03 15:16:27 +0000 | [diff] [blame] | 83 | shift |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 84 | ;; |
Ilya Biryukov | 75db081 | 2017-12-20 14:39:07 +0000 | [diff] [blame] | 85 | -c|--cherrypick) |
| 86 | shift |
| 87 | CHERRYPICKS="$CHERRYPICKS $1" |
| 88 | shift |
| 89 | ;; |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 90 | -b|--branch) |
| 91 | shift |
| 92 | LLVM_BRANCH="$1" |
| 93 | shift |
| 94 | ;; |
| 95 | -p|--llvm-project) |
| 96 | shift |
| 97 | PROJ="$1" |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 98 | shift |
| 99 | |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 100 | if [ "$PROJ" == "cfe" ]; then |
| 101 | PROJ="clang" |
| 102 | fi |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 103 | |
| 104 | if [ "$PROJ" == "clang-tools-extra" ]; then |
| 105 | if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then |
Malcolm Parsons | 21e545d | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 106 | echo "Project 'clang-tools-extra' is already enabled, ignoring extra occurrences." |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 107 | else |
| 108 | CLANG_TOOLS_EXTRA_ENABLED=1 |
Ilya Biryukov | 13cde86 | 2017-07-06 13:10:55 +0000 | [diff] [blame] | 109 | fi |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 110 | |
| 111 | continue |
| 112 | fi |
| 113 | |
| 114 | if ! contains_project "$PROJ" ; then |
| 115 | append_project "$PROJ" |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 116 | else |
Malcolm Parsons | 21e545d | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 117 | echo "Project '$PROJ' is already enabled, ignoring extra occurrences." |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 118 | fi |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 119 | ;; |
| 120 | -i|--install-target) |
| 121 | shift |
| 122 | CMAKE_INSTALL_TARGETS="$CMAKE_INSTALL_TARGETS $1" |
| 123 | shift |
| 124 | ;; |
| 125 | --) |
| 126 | shift |
| 127 | CMAKE_ARGS="$*" |
| 128 | shift $# |
| 129 | ;; |
| 130 | -h|--help) |
| 131 | show_usage |
| 132 | exit 0 |
| 133 | ;; |
| 134 | *) |
| 135 | echo "Unknown option: $1" |
| 136 | exit 1 |
| 137 | esac |
| 138 | done |
| 139 | |
| 140 | if [ "$CMAKE_INSTALL_TARGETS" == "" ]; then |
| 141 | echo "No install targets. Please pass one or more --install-target." |
| 142 | exit 1 |
| 143 | fi |
| 144 | |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 145 | if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then |
| 146 | if ! contains_project "clang"; then |
| 147 | echo "Project 'clang-tools-extra' was enabled without 'clang'." |
| 148 | echo "Adding 'clang' to a list of projects." |
| 149 | |
| 150 | append_project "clang" |
| 151 | fi |
| 152 | fi |
| 153 | |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 154 | if [ "$LLVM_BRANCH" == "" ]; then |
| 155 | LLVM_BRANCH="trunk" |
| 156 | fi |
| 157 | |
Ilya Biryukov | b2c0794 | 2017-08-23 15:36:44 +0000 | [diff] [blame] | 158 | if [ "$LLVM_SVN_REV" != "" ]; then |
| 159 | SVN_REV_ARG="-r$LLVM_SVN_REV" |
| 160 | echo "Checking out svn revision r$LLVM_SVN_REV." |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 161 | else |
| 162 | SVN_REV_ARG="" |
Ilya Biryukov | b2c0794 | 2017-08-23 15:36:44 +0000 | [diff] [blame] | 163 | echo "Checking out latest svn revision." |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 164 | fi |
| 165 | |
Ilya Biryukov | 75db081 | 2017-12-20 14:39:07 +0000 | [diff] [blame] | 166 | # Sort cherrypicks and remove duplicates. |
| 167 | CHERRYPICKS="$(echo "$CHERRYPICKS" | xargs -n1 | sort | uniq | xargs)" |
| 168 | |
| 169 | function apply_cherrypicks() { |
| 170 | local CHECKOUT_DIR="$1" |
| 171 | |
| 172 | [ "$CHERRYPICKS" == "" ] || echo "Applying cherrypicks" |
| 173 | pushd "$CHECKOUT_DIR" |
| 174 | |
| 175 | # This function is always called on a sorted list of cherrypicks. |
| 176 | for CHERRY_REV in $CHERRYPICKS; do |
| 177 | echo "Cherry-picking r$CHERRY_REV into $CHECKOUT_DIR" |
| 178 | |
| 179 | local PATCH_FILE="$(mktemp)" |
| 180 | svn diff -c $CHERRY_REV > "$PATCH_FILE" |
| 181 | svn patch "$PATCH_FILE" |
| 182 | rm "$PATCH_FILE" |
| 183 | done |
| 184 | |
| 185 | popd |
| 186 | } |
| 187 | |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 188 | CLANG_BUILD_DIR=/tmp/clang-build |
| 189 | CLANG_INSTALL_DIR=/tmp/clang-install |
| 190 | |
| 191 | mkdir "$CLANG_BUILD_DIR" |
| 192 | |
| 193 | # Get the sources from svn. |
| 194 | echo "Checking out sources from svn" |
| 195 | mkdir "$CLANG_BUILD_DIR/src" |
| 196 | for LLVM_PROJECT in $LLVM_PROJECTS; do |
| 197 | if [ "$LLVM_PROJECT" == "clang" ]; then |
| 198 | SVN_PROJECT="cfe" |
| 199 | else |
| 200 | SVN_PROJECT="$LLVM_PROJECT" |
| 201 | fi |
| 202 | |
Ilya Biryukov | 13cde86 | 2017-07-06 13:10:55 +0000 | [diff] [blame] | 203 | echo "Checking out https://llvm.org/svn/llvm-project/$SVN_PROJECT to $CLANG_BUILD_DIR/src/$LLVM_PROJECT" |
Ilya Biryukov | 827c8ac | 2017-08-18 09:37:23 +0000 | [diff] [blame] | 204 | svn co -q $SVN_REV_ARG \ |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 205 | "https://llvm.org/svn/llvm-project/$SVN_PROJECT/$LLVM_BRANCH" \ |
| 206 | "$CLANG_BUILD_DIR/src/$LLVM_PROJECT" |
Ilya Biryukov | 75db081 | 2017-12-20 14:39:07 +0000 | [diff] [blame] | 207 | |
| 208 | # We apply cherrypicks to all repositories regardless of whether the revision |
| 209 | # changes this repository or not. For repositories not affected by the |
| 210 | # cherrypick, applying the cherrypick is a no-op. |
| 211 | apply_cherrypicks "$CLANG_BUILD_DIR/src/$LLVM_PROJECT" |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 212 | done |
| 213 | |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 214 | if [ $CLANG_TOOLS_EXTRA_ENABLED -ne 0 ]; then |
| 215 | echo "Checking out https://llvm.org/svn/llvm-project/clang-tools-extra to $CLANG_BUILD_DIR/src/clang/tools/extra" |
Ilya Biryukov | 827c8ac | 2017-08-18 09:37:23 +0000 | [diff] [blame] | 216 | svn co -q $SVN_REV_ARG \ |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 217 | "https://llvm.org/svn/llvm-project/clang-tools-extra/$LLVM_BRANCH" \ |
| 218 | "$CLANG_BUILD_DIR/src/clang/tools/extra" |
Ilya Biryukov | 75db081 | 2017-12-20 14:39:07 +0000 | [diff] [blame] | 219 | |
| 220 | apply_cherrypicks "$CLANG_BUILD_DIR/src/clang/tools/extra" |
Ilya Biryukov | e421fc8 | 2017-07-20 08:30:44 +0000 | [diff] [blame] | 221 | fi |
| 222 | |
Ilya Biryukov | 8b62e08 | 2017-09-15 13:35:54 +0000 | [diff] [blame] | 223 | CHECKSUMS_FILE="/tmp/checksums/checksums.txt" |
| 224 | |
| 225 | if [ -f "$CHECKSUMS_FILE" ]; then |
| 226 | echo "Validating checksums for LLVM checkout..." |
| 227 | python "$(dirname $0)/llvm_checksum/llvm_checksum.py" -c "$CHECKSUMS_FILE" \ |
| 228 | --partial --multi_dir "$CLANG_BUILD_DIR/src" |
| 229 | else |
| 230 | echo "Skipping checksumming checks..." |
| 231 | fi |
| 232 | |
Ilya Biryukov | 13cde86 | 2017-07-06 13:10:55 +0000 | [diff] [blame] | 233 | mkdir "$CLANG_BUILD_DIR/build" |
| 234 | pushd "$CLANG_BUILD_DIR/build" |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 235 | |
| 236 | # Run the build as specified in the build arguments. |
| 237 | echo "Running build" |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 238 | cmake -GNinja \ |
| 239 | -DCMAKE_INSTALL_PREFIX="$CLANG_INSTALL_DIR" \ |
| 240 | -DLLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLE_PROJECTS" \ |
| 241 | $CMAKE_ARGS \ |
| 242 | "$CLANG_BUILD_DIR/src/llvm" |
| 243 | ninja $CMAKE_INSTALL_TARGETS |
| 244 | |
| 245 | popd |
| 246 | |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 247 | # Cleanup. |
Ilya Biryukov | d950201 | 2018-03-26 15:12:30 +0000 | [diff] [blame] | 248 | rm -rf "$CLANG_BUILD_DIR" |
Ilya Biryukov | af351da | 2017-06-30 09:46:45 +0000 | [diff] [blame] | 249 | |
| 250 | echo "Done" |