David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # Copyright 2019 The Hafnium Authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | set -euo pipefail |
| 16 | |
| 17 | SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")" |
| 18 | ROOT_DIR="$(realpath ${SCRIPT_DIR}/..)" |
| 19 | |
| 20 | source "${SCRIPT_DIR}/docker/common.inc" |
| 21 | |
| 22 | if [ "${HAFNIUM_HERMETIC_BUILD:-}" == "inside" ] |
| 23 | then |
| 24 | echo "ERROR: Invoked $0 recursively" 1>&2 |
| 25 | exit 1 |
| 26 | fi |
| 27 | |
| 28 | # Set up a temp directory and register a cleanup function on exit. |
| 29 | TMP_DIR="$(mktemp -d)" |
| 30 | function cleanup() { |
| 31 | rm -rf "${TMP_DIR}" |
| 32 | } |
| 33 | trap cleanup EXIT |
| 34 | |
| 35 | # Build local image and write its hash to a temporary file. |
| 36 | IID_FILE="${TMP_DIR}/imgid.txt" |
| 37 | "${DOCKER}" build \ |
| 38 | --build-arg LOCAL_UID="$(id -u)" \ |
| 39 | --build-arg LOCAL_GID="$(id -g)" \ |
| 40 | --iidfile="${IID_FILE}" \ |
| 41 | -f "${SCRIPT_DIR}/docker/Dockerfile.local" \ |
| 42 | "${SCRIPT_DIR}/docker" |
| 43 | IMAGE_ID="$(cat ${IID_FILE})" |
| 44 | |
| 45 | # Check if script was invoked with '-i' as first argument. If so, run |
| 46 | # container in interactive mode. |
| 47 | INTERACTIVE=false |
| 48 | if [ "${1:-}" == "-i" ] |
| 49 | then |
| 50 | INTERACTIVE=true |
| 51 | shift |
| 52 | fi |
| 53 | |
| 54 | ARGS=() |
| 55 | # Run with a pseduo-TTY for nicer logging. |
| 56 | ARGS+=(-t) |
| 57 | # Run interactive if this script was invoked with '-i'. |
| 58 | if [ "${INTERACTIVE}" == "true" ] |
| 59 | then |
| 60 | ARGS+=(-i) |
| 61 | fi |
| 62 | # Set environment variable informing the build that we are running inside |
| 63 | # a container. |
| 64 | ARGS+=(-e HAFNIUM_HERMETIC_BUILD=inside) |
| 65 | # Bind-mount the Hafnium root directory. We mount it at the same absolute |
| 66 | # location so that all paths match across the host and guest. |
| 67 | ARGS+=(-v "${ROOT_DIR}":"${ROOT_DIR}") |
| 68 | # Make all files outside of the Hafnium directory read-only to ensure that all |
| 69 | # generated files are written there. |
| 70 | ARGS+=(--read-only) |
| 71 | # Mount a writable /tmp folder. Required by LLVM/Clang for intermediate files. |
| 72 | ARGS+=(--tmpfs /tmp) |
| 73 | # Set working directory. |
| 74 | ARGS+=(-w "${ROOT_DIR}") |
| 75 | |
| 76 | echo "Running in container: $*" 1>&2 |
| 77 | ${DOCKER} run \ |
| 78 | ${ARGS[@]} \ |
| 79 | "${IMAGE_ID}" \ |
| 80 | /bin/bash -c "$*" |