1#!/bin/bash
2# Copyright (c) 2021 Google LLC.
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#     http://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#
16# Linux Build Script.
17
18# Fail on any error.
19set -e
20
21SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
22ROOT_DIR="$( cd "${SCRIPT_DIR}/../../.." >/dev/null 2>&1 && pwd )"
23
24CONFIG=$1
25COMPILER=$2
26TOOL=$3
27BUILD_SHA=${KOKORO_GITHUB_COMMIT:-$KOKORO_GITHUB_PULL_REQUEST_COMMIT}
28
29# chown the given directory to the current user, if it exists.
30# Docker creates files with the root user - this can upset the Kokoro artifact copier.
31function chown_dir() {
32  dir=$1
33  if [[ -d "$dir" ]]; then
34    sudo chown -R "$(id -u):$(id -g)" "$dir"
35  fi
36}
37
38set +e
39# Allow build failures
40
41# "--privileged" is required to run ptrace in the asan builds.
42docker run --rm -i \
43  --privileged \
44  --volume "${ROOT_DIR}:${ROOT_DIR}" \
45  --volume "${KOKORO_ARTIFACTS_DIR}:${KOKORO_ARTIFACTS_DIR}" \
46  --workdir "${ROOT_DIR}" \
47  --env SCRIPT_DIR=${SCRIPT_DIR} \
48  --env ROOT_DIR=${ROOT_DIR} \
49  --env CONFIG=${CONFIG} \
50  --env COMPILER=${COMPILER} \
51  --env TOOL=${TOOL} \
52  --env KOKORO_ARTIFACTS_DIR="${KOKORO_ARTIFACTS_DIR}" \
53  --env BUILD_SHA="${BUILD_SHA}" \
54  --entrypoint "${SCRIPT_DIR}/build-docker.sh" \
55  "gcr.io/shaderc-build/radial-build:latest"
56RESULT=$?
57
58# This is important. If the permissions are not fixed, kokoro will fail
59# to pull build artifacts, and put the build in tool-failure state, which
60# blocks the logs.
61chown_dir "${ROOT_DIR}/build"
62chown_dir "${ROOT_DIR}/external"
63exit $RESULT
64