blob: 4d7bd730bbd2e76e05077897e7a5d4125f22b98b [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)
84 if is_repo_build
85 then
86 # This source tree was checked out using `repo`. Check the
87 # status of all projects.
88 cmd=(${REPO} forall -c "${cmd[@]}")
89 fi
90 ! (u="$(${cmd[@]})" && test -z "$u")
91 return $?
92}
93
94# Prepares the source directory for building. This includes setting global
95# variables and workaronuds for different build environments.
96function init_build() {
97 ##
98 ## Find Hafnium's root directory.
99 ##
100 ROOT_DIR="$(abs_path $(get_script_dir)/../..)"
101 # Temporary workaround for Repo builds. Check if there is a project
102 # folder specific to Repo builds in a parent directory. If it is, the
103 # root is further one level up.
104 if [ -d "${ROOT_DIR}/../.repo_manifest" ]
105 then
106 ROOT_DIR="$(dirname ${ROOT_DIR})"
107 fi
108
109 ##
110 ## Paths to tools
111 ##
112 CLANG="${ROOT_DIR}/prebuilts/linux-x64/clang/bin/clang"
113 REPO="${ROOT_DIR}/prebuilts/generic/repo/repo"
114
115 ##
116 ## Workarounds for Kokoro+Repo builds.
117 ##
118 if is_kokoro_build && is_repo_build
119 then
120 # Kokoro does not correctly initialize the `.repo` folder which
121 # causes `is_repo_dirty` checks to fail. We check out the
122 # manifest as one of the projects and use it to initialize the
123 # `.repo` folder here.
124 (cd "${ROOT_DIR}/.repo_manifest" && git branch master)
125 (cd "${ROOT_DIR}" && "${REPO}" init -u .repo_manifest)
126 # Kokoro does not support '<linkfile>' manifest tags. Parse the
127 # manifest and symlink files here.
128 "$(get_script_dir)/symlink_repo.py" "${ROOT_DIR}"
129 fi
130}