blob: dd70247c2ff31bf064496133bc32acd4ed5f5f43 [file] [log] [blame]
David Brazdil39fb8662020-01-09 10:20:37 +00001# Copyright 2019 The Hafnium Authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Fail on any error.
16set -e
17# Fail on any part of a pipeline failing.
18set -o pipefail
19# Treat unset variables as an error.
20set -u
21# Display commands being run.
22set -x
23
24# Returns absolute path to a file or a directory. The path must exist.
25function abs_path() {
26 local rel_path="$1"
27 if [ -d "$rel_path" ]
28 then
29 # Parameter is a directory
30 local rel_dir="$rel_path"
31 local rel_base=""
32 elif [ -f "$rel_path" ]
33 then
34 # Parameter is a regular file
35 local rel_dir="$(dirname "$rel_path")"
36 local rel_base="/$(basename "$rel_path")"
37 else
38 # File does not exist
39 echo "File not found: $rel_path" >&2
40 exit 1
41 fi
42 echo "$(cd $rel_dir && pwd)$rel_base"
43 return 0
44}
45
46# Returns true if the environment contains Kokoro build variables.
47function is_kokoro_build() {
48 [ -v KOKORO_JOB_NAME ]
49 return $?
50}
51
52# Returns true if the `.repo_manifest` folder exists. The folder should contain
53# the manifest and be present in all Repo builds. Eventually this should be
54# obsolete as we switch exclusively to Repo.
55function is_repo_build() {
56 [ -d "${ROOT_DIR}/.repo_manifest" ]
57 return $?
58}
59
60# Returns absolute path to the source file that called this function.
61function get_script_path() {
62 abs_path "${BASH_SOURCE[1]}"
63}
64
65# Returns absolute path to the directory containing the source file that called
66# this function.
67function get_script_dir() {
68 local caller_path="${BASH_SOURCE[1]}"
69 local caller_abs_path="$(abs_path $caller_path)"
70 dirname "$caller_abs_path"
71}
72
73# Assigns value (second arg) of a variable (first arg) if it is not set already.
74function default_value {
75 local var_name=$1
76 local value=$2
77 export ${var_name}=${!var_name:-${value}}
78}
79
80# Returns true if `git status` reports uncommitted changes in the source tree.
81# Runs on all projects if Repo is detected.
82function is_repo_dirty() {
83 local cmd=(git status --porcelain=v1)
David Brazdil3d85bb12020-03-25 16:03:22 +000084
85 # Temporary workaround for b/152398137 (all symlinks dirty on Kokoro).
86 # All affected files are in submodules, skip them.
87 if [ "${HAFNIUM_IGNORE_SUBMODULE_STATUS:-false}" == "true" ]
88 then
89 cmd+=(--ignore-submodules)
90 fi
91
David Brazdil39fb8662020-01-09 10:20:37 +000092 if is_repo_build
93 then
94 # This source tree was checked out using `repo`. Check the
95 # status of all projects.
96 cmd=(${REPO} forall -c "${cmd[@]}")
97 fi
98 ! (u="$(${cmd[@]})" && test -z "$u")
99 return $?
100}
101
102# Prepares the source directory for building. This includes setting global
103# variables and workaronuds for different build environments.
104function init_build() {
105 ##
106 ## Find Hafnium's root directory.
107 ##
108 ROOT_DIR="$(abs_path $(get_script_dir)/../..)"
109 # Temporary workaround for Repo builds. Check if there is a project
110 # folder specific to Repo builds in a parent directory. If it is, the
111 # root is further one level up.
112 if [ -d "${ROOT_DIR}/../.repo_manifest" ]
113 then
114 ROOT_DIR="$(dirname ${ROOT_DIR})"
115 fi
116
117 ##
118 ## Paths to tools
119 ##
120 CLANG="${ROOT_DIR}/prebuilts/linux-x64/clang/bin/clang"
121 REPO="${ROOT_DIR}/prebuilts/generic/repo/repo"
122
123 ##
124 ## Workarounds for Kokoro+Repo builds.
125 ##
126 if is_kokoro_build && is_repo_build
127 then
128 # Kokoro does not correctly initialize the `.repo` folder which
129 # causes `is_repo_dirty` checks to fail. We check out the
130 # manifest as one of the projects and use it to initialize the
131 # `.repo` folder here.
132 (cd "${ROOT_DIR}/.repo_manifest" && git branch master)
133 (cd "${ROOT_DIR}" && "${REPO}" init -u .repo_manifest)
134 # Kokoro does not support '<linkfile>' manifest tags. Parse the
135 # manifest and symlink files here.
136 "$(get_script_dir)/symlink_repo.py" "${ROOT_DIR}"
137 fi
David Brazdil3d85bb12020-03-25 16:03:22 +0000138
139 ##
140 ## Temporary workaround for b/152398137 (all symlinks dirty on Kokoro).
141 ## The `is_kokoro_build` check does not work inside container builds.
142 ## Use an environment variable with HAFNIUM_ prefix to pass that
143 ## information into the container (run_in_container.sh propagates them).
144 ##
145 if is_kokoro_build
146 then
147 export HAFNIUM_IGNORE_SUBMODULE_STATUS=true
148 fi
David Brazdil39fb8662020-01-09 10:20:37 +0000149}