blob: e3c6bd01ae54f534a489c086f25e7d598266bfef [file] [log] [blame]
David Brazdil5ecf75f2019-07-21 10:39:47 +02001#!/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.
15set -euo pipefail
16
17SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
18ROOT_DIR="$(realpath ${SCRIPT_DIR}/..)"
19
20source "${SCRIPT_DIR}/docker/common.inc"
21
22if [ "${HAFNIUM_HERMETIC_BUILD:-}" == "inside" ]
23then
24 echo "ERROR: Invoked $0 recursively" 1>&2
25 exit 1
26fi
27
28# Set up a temp directory and register a cleanup function on exit.
29TMP_DIR="$(mktemp -d)"
30function cleanup() {
31 rm -rf "${TMP_DIR}"
32}
33trap cleanup EXIT
34
35# Build local image and write its hash to a temporary file.
36IID_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"
43IMAGE_ID="$(cat ${IID_FILE})"
44
45# Check if script was invoked with '-i' as first argument. If so, run
46# container in interactive mode.
47INTERACTIVE=false
48if [ "${1:-}" == "-i" ]
49then
50 INTERACTIVE=true
51 shift
52fi
53
54ARGS=()
55# Run with a pseduo-TTY for nicer logging.
56ARGS+=(-t)
57# Run interactive if this script was invoked with '-i'.
58if [ "${INTERACTIVE}" == "true" ]
59then
60 ARGS+=(-i)
61fi
62# Set environment variable informing the build that we are running inside
63# a container.
64ARGS+=(-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.
67ARGS+=(-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.
70ARGS+=(--read-only)
71# Mount a writable /tmp folder. Required by LLVM/Clang for intermediate files.
72ARGS+=(--tmpfs /tmp)
73# Set working directory.
74ARGS+=(-w "${ROOT_DIR}")
75
76echo "Running in container: $*" 1>&2
77${DOCKER} run \
78 ${ARGS[@]} \
79 "${IMAGE_ID}" \
80 /bin/bash -c "$*"