blob: 821da0c2e6304d89bb9ce0a3e6ef75afcdac8b5b [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
Andrew Walbrand33f47a2020-04-30 11:25:55 +010052# Returns true if the environment contains Jenkins build variables.
53function is_jenkins_build() {
54 [ -v JENKINS_HOME ]
55 return $?
56}
57
David Brazdil39fb8662020-01-09 10:20:37 +000058# Returns true if the `.repo_manifest` folder exists. The folder should contain
59# the manifest and be present in all Repo builds. Eventually this should be
60# obsolete as we switch exclusively to Repo.
61function is_repo_build() {
62 [ -d "${ROOT_DIR}/.repo_manifest" ]
63 return $?
64}
65
66# Returns absolute path to the source file that called this function.
67function get_script_path() {
68 abs_path "${BASH_SOURCE[1]}"
69}
70
71# Returns absolute path to the directory containing the source file that called
72# this function.
73function get_script_dir() {
74 local caller_path="${BASH_SOURCE[1]}"
75 local caller_abs_path="$(abs_path $caller_path)"
76 dirname "$caller_abs_path"
77}
78
79# Assigns value (second arg) of a variable (first arg) if it is not set already.
80function default_value {
81 local var_name=$1
82 local value=$2
83 export ${var_name}=${!var_name:-${value}}
84}
85
86# Returns true if `git status` reports uncommitted changes in the source tree.
87# Runs on all projects if Repo is detected.
88function is_repo_dirty() {
89 local cmd=(git status --porcelain=v1)
David Brazdil3d85bb12020-03-25 16:03:22 +000090
91 # Temporary workaround for b/152398137 (all symlinks dirty on Kokoro).
92 # All affected files are in submodules, skip them.
93 if [ "${HAFNIUM_IGNORE_SUBMODULE_STATUS:-false}" == "true" ]
94 then
95 cmd+=(--ignore-submodules)
96 fi
97
David Brazdil39fb8662020-01-09 10:20:37 +000098 if is_repo_build
99 then
100 # This source tree was checked out using `repo`. Check the
101 # status of all projects.
102 cmd=(${REPO} forall -c "${cmd[@]}")
103 fi
104 ! (u="$(${cmd[@]})" && test -z "$u")
105 return $?
106}
107
108# Prepares the source directory for building. This includes setting global
109# variables and workaronuds for different build environments.
110function init_build() {
111 ##
112 ## Find Hafnium's root directory.
113 ##
114 ROOT_DIR="$(abs_path $(get_script_dir)/../..)"
115 # Temporary workaround for Repo builds. Check if there is a project
116 # folder specific to Repo builds in a parent directory. If it is, the
117 # root is further one level up.
118 if [ -d "${ROOT_DIR}/../.repo_manifest" ]
119 then
120 ROOT_DIR="$(dirname ${ROOT_DIR})"
121 fi
122
123 ##
124 ## Paths to tools
125 ##
126 CLANG="${ROOT_DIR}/prebuilts/linux-x64/clang/bin/clang"
127 REPO="${ROOT_DIR}/prebuilts/generic/repo/repo"
128
129 ##
130 ## Workarounds for Kokoro+Repo builds.
131 ##
132 if is_kokoro_build && is_repo_build
133 then
134 # Kokoro does not correctly initialize the `.repo` folder which
135 # causes `is_repo_dirty` checks to fail. We check out the
136 # manifest as one of the projects and use it to initialize the
137 # `.repo` folder here.
138 (cd "${ROOT_DIR}/.repo_manifest" && git branch master)
139 (cd "${ROOT_DIR}" && "${REPO}" init -u .repo_manifest)
140 # Kokoro does not support '<linkfile>' manifest tags. Parse the
141 # manifest and symlink files here.
142 "$(get_script_dir)/symlink_repo.py" "${ROOT_DIR}"
143 fi
David Brazdil3d85bb12020-03-25 16:03:22 +0000144
145 ##
146 ## Temporary workaround for b/152398137 (all symlinks dirty on Kokoro).
147 ## The `is_kokoro_build` check does not work inside container builds.
148 ## Use an environment variable with HAFNIUM_ prefix to pass that
149 ## information into the container (run_in_container.sh propagates them).
150 ##
151 if is_kokoro_build
152 then
153 export HAFNIUM_IGNORE_SUBMODULE_STATUS=true
154 fi
David Brazdil39fb8662020-01-09 10:20:37 +0000155}