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 19require_top 20 21if [[ -z ${OUT_DIR+x} ]]; then 22 PROFILE_OUT=$TOP/out 23else 24 PROFILE_OUT=$OUT_DIR 25fi 26mkdir -p $PROFILE_OUT 27 28# Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present 29bazel_args="" 30skip_tests="--skip-soong-tests" 31for i in $@; do 32 if [[ $i != "--run-soong-tests" ]]; then 33 bazel_args+="$i " 34 else 35 skip_tests="" 36 fi 37done 38 39# Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace). 40# RBE is disabled because it's not used with b builds and adds overhead: b/251441524 41# TODO(b/262904551) - this is Darwin incompatible and should eventually be updated. 42BUILD_STARTED_TIME=`date +%s%3N` 43B_ARGS=$* 44USE_RBE=false "$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 || exit 1 45 46 47# Then, run Bazel using the synthetic workspace as the --package_path. 48if [[ -z "$bazel_args" ]]; then 49 # If there are no args, show help and exit. 50 "$TOP/build/bazel/bin/bazel" help 51else 52 # Else, always run with the bp2build configuration, which sets Bazel's package path to 53 # the synthetic workspace. 54 # Add the --config=bp2build after the first argument that doesn't start with a dash. That 55 # should be the bazel 56 # command. (build, test, run, ect) If the --config was added at the end, it wouldn't work 57 # with commands like: b run //foo -- --args-for-foo 58 config_set=0 59 60 # Represent the args as an array, not a string. 61 bazel_args_with_config=() 62 for arg in $bazel_args; do 63 if [[ $arg == "--" && $config_set -ne 1 ]]; # if we find --, insert config argument here 64 then 65 bazel_args_with_config+=("--profile=$PROFILE_OUT/bazel_metrics-profile --config=bp2build -- ") 66 config_set=1 67 else 68 bazel_args_with_config+=("$arg ") 69 fi 70 done 71 if [[ $config_set -ne 1 ]]; then 72 bazel_args_with_config+=("--profile=$PROFILE_OUT/bazel_metrics-profile --config=bp2build ") 73 fi 74 75 # Call Bazel. 76 "$TOP/build/bazel/bin/bazel" ${bazel_args_with_config[@]} 77 "$TOP/build/bazel/bin/bazel" analyze-profile $PROFILE_OUT/bazel_metrics-profile > $PROFILE_OUT/analyzed_bazel_profile.txt 78 rm $PROFILE_OUT/bazel_metrics-profile 79 "$TOP/build/soong/soong_ui.bash" --upload-metrics-only 80fi 81