xref: /aosp_15_r20/tools/treble/build/sandbox/build_android_target.sh (revision 105f628577ac4ba0e277a494fbb614ed8c12a994)
1#!/bin/bash
2
3# Copyright 2020 Google LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     https://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17readonly ANDROID_TARGET=$1
18readonly BUILD_DIR=$2
19shift
20shift
21readonly BUILD_COMMAND="$@"
22
23if [[ -z "${ANDROID_TARGET}" ]]; then
24  echo "error: Android target not set"
25  exit 1
26fi
27
28if [[ -z "${BUILD_DIR}" ]]; then
29  echo "error: Build directory not set"
30  exit 1
31fi
32
33if [[ -z "${BUILD_COMMAND}" ]]; then
34  echo "error: Build command not set"
35  exit 1
36fi
37
38# If there is an error, exit right away instead of continuing. For example,
39# lunch could fail. If so, there is no point in continuing the build.
40
41set -e
42
43BUILD_COMMAND_ARRAY=($BUILD_COMMAND)
44for i in ${BUILD_COMMAND_ARRAY[@]};
45do
46  if [[ $i =~ ^[A-Z_][A-Z0-9_]*= ]];
47  then
48    echo "build_android_target.sh: export $i";
49    export $i;
50  fi;
51done;
52
53echo "build_android_target.sh: source build/envsetup.sh"
54source build/envsetup.sh
55echo "build_android_target.sh: lunch $ANDROID_TARGET"
56lunch "$ANDROID_TARGET"
57echo "build_android_target.sh: cd $BUILD_DIR"
58cd "$BUILD_DIR"
59
60# However, the build command itself cannot use set -e. I haven't figured this
61# out yet, but something in the build command causes early exit for some
62# targets.
63
64set +e
65
66echo "build_android_target.sh: $BUILD_COMMAND"
67eval $BUILD_COMMAND
68BUILD_COMMAND_EXIT_VALUE=$?
69
70# Collect RBE metrics if enabled
71if [[ -n "${USE_RBE}" && -n "${RBE_DIR}" ]]; then
72  echo "build_android_target.sh: $RBE_DIR/dumpstats"
73  $RBE_DIR/dumpstats
74fi
75
76exit $BUILD_COMMAND_EXIT_VALUE
77