blob: 6cc8c9e485a5031066f550a74b778b956d45573f [file] [log] [blame]
Joe Onorato344e4042022-12-05 15:15:36 -08001# Copyright (C) 2022 The Android Open Source Project
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# http://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
15function gettop
16{
17 local TOPFILE=build/make/core/envsetup.mk
18 # The ${TOP-} expansion allows this to work even with set -u
19 if [ -n "${TOP:-}" -a -f "${TOP:-}/$TOPFILE" ] ; then
20 # The following circumlocution ensures we remove symlinks from TOP.
21 (cd "$TOP"; PWD= /bin/pwd)
22 else
23 if [ -f $TOPFILE ] ; then
24 # The following circumlocution (repeated below as well) ensures
25 # that we record the true directory name and not one that is
26 # faked up with symlink names.
27 PWD= /bin/pwd
28 else
29 local HERE=$PWD
30 local T=
31 while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do
32 \cd ..
33 T=`PWD= /bin/pwd -P`
34 done
35 \cd "$HERE"
36 if [ -f "$T/$TOPFILE" ]; then
37 echo "$T"
38 fi
39 fi
40 fi
41}
42
Joe Onorato23124752024-05-14 15:06:48 -070043# Asserts that the root of the tree can be found.
Joe Onorato344e4042022-12-05 15:15:36 -080044if [ -z "${IMPORTING_ENVSETUP:-}" ] ; then
45function require_top
46{
47 TOP=$(gettop)
48 if [[ ! $TOP ]] ; then
Joe Onorato1b9ab292024-05-17 12:16:43 -070049 echo "Can not locate root of source tree. $(basename $0) must be run from within the Android source tree or TOP must be set." >&2
Joe Onorato344e4042022-12-05 15:15:36 -080050 exit 1
51 fi
52}
53fi
54
Joe Onorato23124752024-05-14 15:06:48 -070055# Asserts that the lunch variables have been set
56if [ -z "${IMPORTING_ENVSETUP:-}" ] ; then
57function require_lunch
58{
59 if [[ ! $TARGET_PRODUCT || ! $TARGET_RELEASE || ! $TARGET_BUILD_VARIANT ]] ; then
60 echo "Please run lunch and try again." >&2
61 exit 1
62 fi
63}
64fi
65
Sam Lewis2404c4a2024-09-23 20:06:02 +000066# This function sets up the build environment to be appropriate for Cog.
67function setup_cog_env_if_needed() {
68 local top=$(gettop)
69
70 # return early if not in a cog workspace
71 if [[ ! "$top" =~ ^/google/cog ]]; then
72 return 0
73 fi
74
75 setup_cog_symlink
76
77 export ANDROID_BUILD_ENVIRONMENT_CONFIG="googler-cog"
78
79 # Running repo command within Cog workspaces is not supported, so override
80 # it with this function. If the user is running repo within a Cog workspace,
81 # we'll fail with an error, otherwise, we run the original repo command with
82 # the given args.
83 if ! ORIG_REPO_PATH=`which repo`; then
84 return 0
85 fi
86 function repo {
87 if [[ "${PWD}" == /google/cog/* ]]; then
88 echo -e "\e[01;31mERROR:\e[0mrepo command is disallowed within Cog workspaces."
89 kill -INT $$ # exits the script without exiting the user's shell
90 fi
91 ${ORIG_REPO_PATH} "$@"
92 }
93}
94
95# creates a symlink for the out/ dir when inside a cog workspace.
96function setup_cog_symlink() {
97 local out_dir=$(getoutdir)
98 local top=$(gettop)
99
100 # return early if out dir is already a symlink
101 if [[ -L "$out_dir" ]]; then
102 return 0
103 fi
104
105 # return early if out dir is not in the workspace
106 if [[ ! "$out_dir" =~ ^$top/ ]]; then
107 return 0
108 fi
109
110 local link_destination="${HOME}/.cog/android-build-out"
111
112 # if there's a local out/ dir, prompt the user to remove it.
113 # fail out if they say no.
114 local answer
115 if [[ -d "$out_dir" ]]; then
116 echo "Detected existing out/ directory in the Cog workspace which is not supported. Can we repair your workspace by removing it and creating the symlink to ~/.cog/android-build-out instead? (y/N): "
117 read -r answer
118 if [[ $answer =~ ^[Yy]$ ]]; then
119 rm -rf "$out_dir"
120 else
121 echo "Exiting due to unsupported out/ directory."
122 kill -INT $$ # exits the script without exiting the user's shell
123 fi
124 fi
125
126 # create symlink
127 echo "Creating symlink: $out_dir -> $link_destination"
128 mkdir -p ${link_destination}
129 if ! ln -s "$link_destination" "$out_dir"; then
130 echo "Failed to create cog symlink: $out_dir -> $link_destination" >&2
131 kill -INT $$ # exits the script without exiting the user's shell
132 fi
133}
134
Joe Onorato344e4042022-12-05 15:15:36 -0800135function getoutdir
136{
137 local top=$(gettop)
138 local out_dir="${OUT_DIR:-}"
139 if [[ -z "${out_dir}" ]]; then
140 if [[ -n "${OUT_DIR_COMMON_BASE:-}" && -n "${top}" ]]; then
141 out_dir="${OUT_DIR_COMMON_BASE}/$(basename ${top})"
142 else
143 out_dir="out"
144 fi
145 fi
146 if [[ "${out_dir}" != /* ]]; then
147 out_dir="${top}/${out_dir}"
148 fi
149 echo "${out_dir}"
150}
151
Joe Onorato1b9ab292024-05-17 12:16:43 -0700152# Pretty print the build status and duration
153function _wrap_build()
154{
155 if [[ "${ANDROID_QUIET_BUILD:-}" == true ]]; then
156 "$@"
157 return $?
158 fi
159 local start_time=$(date +"%s")
160 "$@"
161 local ret=$?
162 local end_time=$(date +"%s")
163 local tdiff=$(($end_time-$start_time))
164 local hours=$(($tdiff / 3600 ))
165 local mins=$((($tdiff % 3600) / 60))
166 local secs=$(($tdiff % 60))
167 local ncolors=$(tput colors 2>/dev/null)
168 if [ -n "$ncolors" ] && [ $ncolors -ge 8 ]; then
169 color_failed=$'\E'"[0;31m"
170 color_success=$'\E'"[0;32m"
171 color_warning=$'\E'"[0;33m"
172 color_reset=$'\E'"[00m"
173 else
174 color_failed=""
175 color_success=""
176 color_reset=""
177 fi
178
179 echo
180 if [ $ret -eq 0 ] ; then
181 echo -n "${color_success}#### build completed successfully "
182 else
183 echo -n "${color_failed}#### failed to build some targets "
184 fi
185 if [ $hours -gt 0 ] ; then
186 printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
187 elif [ $mins -gt 0 ] ; then
188 printf "(%02g:%02g (mm:ss))" $mins $secs
189 elif [ $secs -gt 0 ] ; then
190 printf "(%s seconds)" $secs
191 fi
192 echo " ####${color_reset}"
193 echo
194 return $ret
195}
196
197
Joe Onorato1f6eddb2024-05-31 15:04:44 -0700198function log_tool_invocation()
199{
200 if [[ -z $ANDROID_TOOL_LOGGER ]]; then
201 return
202 fi
203
204 LOG_TOOL_TAG=$1
205 LOG_START_TIME=$(date +%s.%N)
206 trap '
207 exit_code=$?;
208 # Remove the trap to prevent duplicate log.
209 trap - EXIT;
210 $ANDROID_TOOL_LOGGER \
211 --tool_tag="${LOG_TOOL_TAG}" \
212 --start_timestamp="${LOG_START_TIME}" \
213 --end_timestamp="$(date +%s.%N)" \
214 --tool_args="$*" \
215 --exit_code="${exit_code}" \
216 ${ANDROID_TOOL_LOGGER_EXTRA_ARGS} \
217 > /dev/null 2>&1 &
218 exit ${exit_code}
219 ' SIGINT SIGTERM SIGQUIT EXIT
220}
Joe Onorato344e4042022-12-05 15:15:36 -0800221