blob: 4090c380224ea45aeab3a46ba53da53d1f72695a [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
David Brazdil4a51d652019-12-20 13:27:54 +000017SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
18ROOT_DIR="$(dirname ${SCRIPT_DIR})"
David Brazdil5ecf75f2019-07-21 10:39:47 +020019
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
David Brazdil5e0484e2019-08-07 15:06:57 +010045# Parse command line arguments
David Brazdil5ecf75f2019-07-21 10:39:47 +020046INTERACTIVE=false
David Brazdil5e0484e2019-08-07 15:06:57 +010047ALLOW_PTRACE=false
48while true
49do
50 case "${1:-}" in
51 -i)
52 INTERACTIVE=true
53 shift
54 ;;
55 -p)
56 ALLOW_PTRACE=true
57 shift
58 ;;
59 -*)
60 echo "ERROR: Unknown command line flag: $1" 1>&2
61 echo "Usage: $0 [-i] [-p] <command>"
62 exit 1
63 ;;
64 *)
65 break
66 ;;
67 esac
68done
David Brazdil5ecf75f2019-07-21 10:39:47 +020069
70ARGS=()
71# Run with a pseduo-TTY for nicer logging.
72ARGS+=(-t)
73# Run interactive if this script was invoked with '-i'.
74if [ "${INTERACTIVE}" == "true" ]
75then
76 ARGS+=(-i)
77fi
David Brazdil5e0484e2019-08-07 15:06:57 +010078# Allow ptrace() syscall if invoked with '-p'.
79if [ "${ALLOW_PTRACE}" == "true" ]
80then
81 echo "WARNING: Docker seccomp profile is disabled!" 1>&2
82 ARGS+=(--cap-add=SYS_PTRACE --security-opt seccomp=unconfined)
83fi
David Brazdil3cc24aa2019-09-27 10:24:41 +010084# Propagate "HAFNIUM_*" environment variables.
85# Note: Cannot use `env | while` because the loop would run inside a child
86# process and would not have any effect on variables in the parent.
87while read -r ENV_LINE
88do
89 VAR_NAME="$(echo ${ENV_LINE} | cut -d= -f1)"
90 case "${VAR_NAME}" in
91 HAFNIUM_HERMETIC_BUILD)
92 # Skip this one. It will be overridden below.
93 ;;
94 HAFNIUM_*)
95 ARGS+=(-e "${ENV_LINE}")
96 ;;
97 esac
98done <<< "$(env)"
David Brazdil5ecf75f2019-07-21 10:39:47 +020099# Set environment variable informing the build that we are running inside
100# a container.
101ARGS+=(-e HAFNIUM_HERMETIC_BUILD=inside)
102# Bind-mount the Hafnium root directory. We mount it at the same absolute
103# location so that all paths match across the host and guest.
104ARGS+=(-v "${ROOT_DIR}":"${ROOT_DIR}")
105# Make all files outside of the Hafnium directory read-only to ensure that all
106# generated files are written there.
107ARGS+=(--read-only)
108# Mount a writable /tmp folder. Required by LLVM/Clang for intermediate files.
109ARGS+=(--tmpfs /tmp)
110# Set working directory.
111ARGS+=(-w "${ROOT_DIR}")
112
113echo "Running in container: $*" 1>&2
114${DOCKER} run \
115 ${ARGS[@]} \
116 "${IMAGE_ID}" \
David Brazdil4a51d652019-12-20 13:27:54 +0000117 /bin/bash -c "$*"