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() { |
| 14 | usage=$(cat << EOF |
| 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. |
| 19 | Passes additional -DCMAKE_INSTALL_PREFIX and archives the contents of |
| 20 | the directory to /tmp/clang.tar.gz. |
| 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 |
| 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. |
| 36 | Required options: At least one --install-target. |
| 37 | |
| 38 | All options after '--' are passed to CMake invocation. |
| 39 | EOF |
| 40 | ) |
| 41 | echo "$usage" |
| 42 | } |
| 43 | |
| 44 | LLVM_SVN_REV="" |
| 45 | LLVM_BRANCH="" |
| 46 | CMAKE_ARGS="" |
| 47 | CMAKE_INSTALL_TARGETS="" |
| 48 | # We always checkout llvm |
| 49 | LLVM_PROJECTS="llvm" |
| 50 | CMAKE_LLVM_ENABLE_PROJECTS="" |
| 51 | |
| 52 | function 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 | |
| 63 | while [[ $# -gt 0 ]]; do |
| 64 | case "$1" in |
| 65 | -r|--revision) |
| 66 | shift |
| 67 | LLVM_SVN_REV="$1" |
| 68 | ;; |
| 69 | -b|--branch) |
| 70 | shift |
| 71 | LLVM_BRANCH="$1" |
| 72 | shift |
| 73 | ;; |
| 74 | -p|--llvm-project) |
| 75 | shift |
| 76 | PROJ="$1" |
| 77 | if [ "$PROJ" == "cfe" ]; then |
| 78 | PROJ="clang" |
| 79 | fi |
| 80 | if ! contains_project "$PROJ" ; then |
| 81 | LLVM_PROJECTS="$LLVM_PROJECTS $PROJ" |
| 82 | CMAKE_LLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLED_PROJECTS;$PROJ" |
| 83 | else |
| 84 | echo "Project '$PROJ' is already enabled, ignoring extra occurences." |
| 85 | fi |
| 86 | shift |
| 87 | ;; |
| 88 | -i|--install-target) |
| 89 | shift |
| 90 | CMAKE_INSTALL_TARGETS="$CMAKE_INSTALL_TARGETS $1" |
| 91 | shift |
| 92 | ;; |
| 93 | --) |
| 94 | shift |
| 95 | CMAKE_ARGS="$*" |
| 96 | shift $# |
| 97 | ;; |
| 98 | -h|--help) |
| 99 | show_usage |
| 100 | exit 0 |
| 101 | ;; |
| 102 | *) |
| 103 | echo "Unknown option: $1" |
| 104 | exit 1 |
| 105 | esac |
| 106 | done |
| 107 | |
| 108 | if [ "$CMAKE_INSTALL_TARGETS" == "" ]; then |
| 109 | echo "No install targets. Please pass one or more --install-target." |
| 110 | exit 1 |
| 111 | fi |
| 112 | |
| 113 | if [ "$LLVM_BRANCH" == "" ]; then |
| 114 | LLVM_BRANCH="trunk" |
| 115 | fi |
| 116 | |
| 117 | if [ "$LLVM_SVN_REVISION" != "" ]; then |
| 118 | SVN_REV_ARG="-r$LLVM_SVN_REVISION" |
| 119 | else |
| 120 | SVN_REV_ARG="" |
| 121 | fi |
| 122 | |
| 123 | CLANG_BUILD_DIR=/tmp/clang-build |
| 124 | CLANG_INSTALL_DIR=/tmp/clang-install |
| 125 | |
| 126 | mkdir "$CLANG_BUILD_DIR" |
| 127 | |
| 128 | # Get the sources from svn. |
| 129 | echo "Checking out sources from svn" |
| 130 | mkdir "$CLANG_BUILD_DIR/src" |
| 131 | for LLVM_PROJECT in $LLVM_PROJECTS; do |
| 132 | if [ "$LLVM_PROJECT" == "clang" ]; then |
| 133 | SVN_PROJECT="cfe" |
| 134 | else |
| 135 | SVN_PROJECT="$LLVM_PROJECT" |
| 136 | fi |
| 137 | |
| 138 | echo "Checking out http://llvm.org/svn/llvm-project/$SVN_PROJECT to $CLANG_BUILD_DIR/src/$LLVM_PROJECT" |
| 139 | # FIXME: --trust-server-cert is required to workaround 'SSL issuer is not |
| 140 | # trusted' error. Using https seems preferable to http either way, |
| 141 | # albeit this is not secure. |
| 142 | svn co -q $SVN_REV_ARG --trust-server-cert \ |
| 143 | "https://llvm.org/svn/llvm-project/$SVN_PROJECT/$LLVM_BRANCH" \ |
| 144 | "$CLANG_BUILD_DIR/src/$LLVM_PROJECT" |
| 145 | done |
| 146 | |
| 147 | pushd "$CLANG_BUILD_DIR" |
| 148 | |
| 149 | # Run the build as specified in the build arguments. |
| 150 | echo "Running build" |
| 151 | mkdir "$CLANG_BUILD_DIR/build" |
| 152 | cmake -GNinja \ |
| 153 | -DCMAKE_INSTALL_PREFIX="$CLANG_INSTALL_DIR" \ |
| 154 | -DLLVM_ENABLE_PROJECTS="$CMAKE_LLVM_ENABLE_PROJECTS" \ |
| 155 | $CMAKE_ARGS \ |
| 156 | "$CLANG_BUILD_DIR/src/llvm" |
| 157 | ninja $CMAKE_INSTALL_TARGETS |
| 158 | |
| 159 | popd |
| 160 | |
| 161 | # Pack the installed clang into an archive. |
| 162 | echo "Archiving clang installation to /tmp/clang.tar.gz" |
| 163 | cd "$CLANG_INSTALL_DIR" |
| 164 | tar -czf /tmp/clang.tar.gz * |
| 165 | |
| 166 | # Cleanup. |
| 167 | rm -rf "$CLANG_BUILD_DIR" "$CLANG_INSTALL_DIR" |
| 168 | |
| 169 | echo "Done" |