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
11set -e
12
13function show_usage() {
14  cat << EOF
15Usage: build_install_llvm.sh [options] -- [cmake-args]
16
17Run cmake with the specified arguments. Used inside docker container.
18Passes additional -DCMAKE_INSTALL_PREFIX and puts the build results into
19the directory specified by --to option.
20
21Available options:
22  -h|--help           show this help message
23  -i|--install-target name of a cmake install target to build and include in
24                      the resulting archive. Can be specified multiple times.
25  --to                destination directory where to install the targets.
26Required options: --to, at least one --install-target.
27
28All options after '--' are passed to CMake invocation.
29EOF
30}
31
32CMAKE_ARGS=""
33CMAKE_INSTALL_TARGETS=""
34CLANG_INSTALL_DIR=""
35
36while [[ $# -gt 0 ]]; do
37  case "$1" in
38    -i|--install-target)
39      shift
40      CMAKE_INSTALL_TARGETS="$CMAKE_INSTALL_TARGETS $1"
41      shift
42      ;;
43    --to)
44      shift
45      CLANG_INSTALL_DIR="$1"
46      shift
47      ;;
48    --)
49      shift
50      CMAKE_ARGS="$*"
51      shift $#
52      ;;
53    -h|--help)
54      show_usage
55      exit 0
56      ;;
57    *)
58      echo "Unknown option: $1"
59      exit 1
60  esac
61done
62
63if [ "$CMAKE_INSTALL_TARGETS" == "" ]; then
64  echo "No install targets. Please pass one or more --install-target."
65  exit 1
66fi
67
68if [ "$CLANG_INSTALL_DIR" == "" ]; then
69  echo "No install directory. Please specify the --to argument."
70  exit 1
71fi
72
73CLANG_BUILD_DIR=/tmp/clang-build
74
75mkdir -p "$CLANG_INSTALL_DIR"
76
77mkdir -p "$CLANG_BUILD_DIR/build"
78pushd "$CLANG_BUILD_DIR/build"
79
80# Run the build as specified in the build arguments.
81echo "Running build"
82cmake -GNinja \
83  -DCMAKE_INSTALL_PREFIX="$CLANG_INSTALL_DIR" \
84  $CMAKE_ARGS \
85  "$CLANG_BUILD_DIR/src/llvm"
86ninja $CMAKE_INSTALL_TARGETS
87
88popd
89
90# Cleanup.
91rm -rf "$CLANG_BUILD_DIR/build"
92
93echo "Done"
94