xref: /aosp_15_r20/build/bazel/bin/b (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1#!/bin/bash -e
2
3# Copyright (C) 2022 The Android Open Source Project
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#      http://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
17# Common script utilities
18source $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../../make/shell_utils.sh
19source $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../lib.sh
20
21require_top
22
23# Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present
24bazel_args=""
25skip_tests="--skip-soong-tests"
26for i in $@; do
27    if [[ $i != "--run-soong-tests" ]]; then
28        bazel_args+="$i "
29    else
30        skip_tests=""
31    fi
32done
33
34# Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace).
35# RBE is disabled because it's not used with b builds and adds overhead: b/251441524
36# TODO(b/262904551) - this is Darwin incompatible and should eventually be updated.
37BUILD_STARTED_TIME=`date +%s%3N`
38B_ARGS=$*
39set +e
40
41"$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
42exit_code=$?
43if [[ $exit_code -ne 0 ]]; then
44   exit $exit_code
45fi
46set -e
47
48# Then, run Bazel using the synthetic workspace as the --package_path.
49if [[ -z "$bazel_args" ]]; then
50    # If there are no args, show help and exit.
51    "$TOP/build/bazel/bin/bazel" help
52else
53    PROFILE_OUT=$(get_profile_out_dir)
54    mkdir -p $PROFILE_OUT
55    bazel_args_with_config=$(formulate_b_args $bazel_args)
56    # Call Bazel.
57    set +e
58    "$TOP/build/bazel/bin/bazel" ${bazel_args_with_config[@]}
59    bazel_exit_code=$?
60    set -e
61    bazel_exit_code_string="--bazel-exit-code=$bazel_exit_code"
62    "$TOP/build/bazel/bin/bazel" analyze-profile $PROFILE_OUT/bazel_metrics-profile 1> $PROFILE_OUT/analyzed_bazel_profile.txt 2>/dev/null
63    rm $PROFILE_OUT/bazel_metrics-profile
64    exit $bazel_exit_code
65fi
66