xref: /aosp_15_r20/build/bazel/bin/b (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker#!/bin/bash -e
2*7594170eSAndroid Build Coastguard Worker
3*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2022 The Android Open Source Project
4*7594170eSAndroid Build Coastguard Worker#
5*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*7594170eSAndroid Build Coastguard Worker#
9*7594170eSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*7594170eSAndroid Build Coastguard Worker#
11*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*7594170eSAndroid Build Coastguard Worker# limitations under the License.
16*7594170eSAndroid Build Coastguard Worker
17*7594170eSAndroid Build Coastguard Worker# Common script utilities
18*7594170eSAndroid Build Coastguard Workersource $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../../make/shell_utils.sh
19*7594170eSAndroid Build Coastguard Workersource $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../lib.sh
20*7594170eSAndroid Build Coastguard Worker
21*7594170eSAndroid Build Coastguard Workerrequire_top
22*7594170eSAndroid Build Coastguard Worker
23*7594170eSAndroid Build Coastguard Worker# Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present
24*7594170eSAndroid Build Coastguard Workerbazel_args=""
25*7594170eSAndroid Build Coastguard Workerskip_tests="--skip-soong-tests"
26*7594170eSAndroid Build Coastguard Workerfor i in $@; do
27*7594170eSAndroid Build Coastguard Worker    if [[ $i != "--run-soong-tests" ]]; then
28*7594170eSAndroid Build Coastguard Worker        bazel_args+="$i "
29*7594170eSAndroid Build Coastguard Worker    else
30*7594170eSAndroid Build Coastguard Worker        skip_tests=""
31*7594170eSAndroid Build Coastguard Worker    fi
32*7594170eSAndroid Build Coastguard Workerdone
33*7594170eSAndroid Build Coastguard Worker
34*7594170eSAndroid Build Coastguard Worker# Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
35*7594170eSAndroid Build Coastguard Worker# RBE is disabled because it's not used with b builds and adds overhead: b/251441524
36*7594170eSAndroid Build Coastguard Worker# TODO(b/262904551) - this is Darwin incompatible and should eventually be updated.
37*7594170eSAndroid Build Coastguard WorkerBUILD_STARTED_TIME=`date +%s%3N`
38*7594170eSAndroid Build Coastguard WorkerB_ARGS=$*
39*7594170eSAndroid Build Coastguard Workerset +e
40*7594170eSAndroid Build Coastguard Worker
41*7594170eSAndroid Build Coastguard Worker"$TOP/build/soong/soong_ui.bash" --build-mode --all-modules --dir="$(pwd)" $skip_tests bp2build USE_BAZEL_ANALYSIS= --build-command="b ${B_ARGS}" --skip-metrics-upload --build-started-time-unix-millis=$BUILD_STARTED_TIME
42*7594170eSAndroid Build Coastguard Workerexit_code=$?
43*7594170eSAndroid Build Coastguard Workerif [[ $exit_code -ne 0 ]]; then
44*7594170eSAndroid Build Coastguard Worker   exit $exit_code
45*7594170eSAndroid Build Coastguard Workerfi
46*7594170eSAndroid Build Coastguard Workerset -e
47*7594170eSAndroid Build Coastguard Worker
48*7594170eSAndroid Build Coastguard Worker# Then, run Bazel using the synthetic workspace as the --package_path.
49*7594170eSAndroid Build Coastguard Workerif [[ -z "$bazel_args" ]]; then
50*7594170eSAndroid Build Coastguard Worker    # If there are no args, show help and exit.
51*7594170eSAndroid Build Coastguard Worker    "$TOP/build/bazel/bin/bazel" help
52*7594170eSAndroid Build Coastguard Workerelse
53*7594170eSAndroid Build Coastguard Worker    PROFILE_OUT=$(get_profile_out_dir)
54*7594170eSAndroid Build Coastguard Worker    mkdir -p $PROFILE_OUT
55*7594170eSAndroid Build Coastguard Worker    bazel_args_with_config=$(formulate_b_args $bazel_args)
56*7594170eSAndroid Build Coastguard Worker    # Call Bazel.
57*7594170eSAndroid Build Coastguard Worker    set +e
58*7594170eSAndroid Build Coastguard Worker    "$TOP/build/bazel/bin/bazel" ${bazel_args_with_config[@]}
59*7594170eSAndroid Build Coastguard Worker    bazel_exit_code=$?
60*7594170eSAndroid Build Coastguard Worker    set -e
61*7594170eSAndroid Build Coastguard Worker    bazel_exit_code_string="--bazel-exit-code=$bazel_exit_code"
62*7594170eSAndroid Build Coastguard Worker    "$TOP/build/bazel/bin/bazel" analyze-profile $PROFILE_OUT/bazel_metrics-profile 1> $PROFILE_OUT/analyzed_bazel_profile.txt 2>/dev/null
63*7594170eSAndroid Build Coastguard Worker    rm $PROFILE_OUT/bazel_metrics-profile
64*7594170eSAndroid Build Coastguard Worker    exit $bazel_exit_code
65*7594170eSAndroid Build Coastguard Workerfi
66