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