blob: 1cc3512f88e2b6c41055ff6dff83378b33a36d10 [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.
Andrew Walbran5e71e9b2020-06-17 15:44:49 +010015
David Brazdil5ecf75f2019-07-21 10:39:47 +020016set -euo pipefail
17
David Brazdil4a51d652019-12-20 13:27:54 +000018SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19ROOT_DIR="$(dirname ${SCRIPT_DIR})"
David Brazdil5ecf75f2019-07-21 10:39:47 +020020
21source "${SCRIPT_DIR}/docker/common.inc"
22
23if [ "${HAFNIUM_HERMETIC_BUILD:-}" == "inside" ]
24then
25 echo "ERROR: Invoked $0 recursively" 1>&2
26 exit 1
27fi
28
29# Set up a temp directory and register a cleanup function on exit.
30TMP_DIR="$(mktemp -d)"
31function cleanup() {
32 rm -rf "${TMP_DIR}"
33}
34trap cleanup EXIT
35
36# Build local image and write its hash to a temporary file.
37IID_FILE="${TMP_DIR}/imgid.txt"
38"${DOCKER}" build \
39 --build-arg LOCAL_UID="$(id -u)" \
40 --build-arg LOCAL_GID="$(id -g)" \
41 --iidfile="${IID_FILE}" \
42 -f "${SCRIPT_DIR}/docker/Dockerfile.local" \
43 "${SCRIPT_DIR}/docker"
44IMAGE_ID="$(cat ${IID_FILE})"
45
David Brazdil5e0484e2019-08-07 15:06:57 +010046# Parse command line arguments
David Brazdil5ecf75f2019-07-21 10:39:47 +020047INTERACTIVE=false
David Brazdil5e0484e2019-08-07 15:06:57 +010048ALLOW_PTRACE=false
49while true
50do
51 case "${1:-}" in
52 -i)
53 INTERACTIVE=true
54 shift
55 ;;
56 -p)
57 ALLOW_PTRACE=true
58 shift
59 ;;
60 -*)
61 echo "ERROR: Unknown command line flag: $1" 1>&2
62 echo "Usage: $0 [-i] [-p] <command>"
63 exit 1
64 ;;
65 *)
66 break
67 ;;
68 esac
69done
David Brazdil5ecf75f2019-07-21 10:39:47 +020070
71ARGS=()
72# Run with a pseduo-TTY for nicer logging.
73ARGS+=(-t)
74# Run interactive if this script was invoked with '-i'.
75if [ "${INTERACTIVE}" == "true" ]
76then
77 ARGS+=(-i)
78fi
David Brazdil5e0484e2019-08-07 15:06:57 +010079# Allow ptrace() syscall if invoked with '-p'.
80if [ "${ALLOW_PTRACE}" == "true" ]
81then
82 echo "WARNING: Docker seccomp profile is disabled!" 1>&2
83 ARGS+=(--cap-add=SYS_PTRACE --security-opt seccomp=unconfined)
84fi
David Brazdil3cc24aa2019-09-27 10:24:41 +010085# Propagate "HAFNIUM_*" environment variables.
86# Note: Cannot use `env | while` because the loop would run inside a child
87# process and would not have any effect on variables in the parent.
88while read -r ENV_LINE
89do
90 VAR_NAME="$(echo ${ENV_LINE} | cut -d= -f1)"
91 case "${VAR_NAME}" in
92 HAFNIUM_HERMETIC_BUILD)
93 # Skip this one. It will be overridden below.
94 ;;
95 HAFNIUM_*)
96 ARGS+=(-e "${ENV_LINE}")
97 ;;
98 esac
99done <<< "$(env)"
David Brazdil5ecf75f2019-07-21 10:39:47 +0200100# Set environment variable informing the build that we are running inside
101# a container.
102ARGS+=(-e HAFNIUM_HERMETIC_BUILD=inside)
103# Bind-mount the Hafnium root directory. We mount it at the same absolute
104# location so that all paths match across the host and guest.
105ARGS+=(-v "${ROOT_DIR}":"${ROOT_DIR}")
106# Make all files outside of the Hafnium directory read-only to ensure that all
107# generated files are written there.
108ARGS+=(--read-only)
109# Mount a writable /tmp folder. Required by LLVM/Clang for intermediate files.
110ARGS+=(--tmpfs /tmp)
111# Set working directory.
112ARGS+=(-w "${ROOT_DIR}")
113
114echo "Running in container: $*" 1>&2
115${DOCKER} run \
116 ${ARGS[@]} \
117 "${IMAGE_ID}" \
David Brazdil4a51d652019-12-20 13:27:54 +0000118 /bin/bash -c "$*"