1#!/bin/bash -e 2 3# Copyright (C) 2023 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# This contains some utilities used by b 18# They were moved separately to facilitate testing 19source $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../make/shell_utils.sh 20 21function get_profile_out_dir { 22 require_top 23 if [[ -z ${OUT_DIR+x} ]]; then 24 PROFILE_OUT=$TOP/out 25 else 26 PROFILE_OUT=$OUT_DIR 27 fi 28 29 echo $PROFILE_OUT 30} 31 32function is_command { 33 arg=$1 34 BAZEL_COMMAND_LIST="analyze-profile aquery build canonicalize-flags clean config coverage cquery dump fetch help info license mobile-install mod print_action query run shutdown sync test version" 35 if echo "$BAZEL_COMMAND_LIST" | "grep" -ws -e "$arg"; then 36 true 37 else 38 false 39 fi 40} 41 42function formulate_b_args { 43 # Always run with the bp2build configuration, which sets Bazel's package path to 44 # the synthetic workspace. 45 # Add the --config=bp2build after the first argument. That should be the bazel command 46 # (build, test, run, etc) If the --config was added at the end, it wouldn't work 47 # with commands like: b run //foo -- --args-for-foo 48 # This function will create a UUID for BES purposes if not already set to the ENV var 49 # "BES_UUID". Likewise, the bazel profile file will be written to the dir set as "PROFILE_OUT" 50 # or default to $TOP/out or out if not specified. 51 52 # Represent the args as an array, not a string. 53 bazel_args_with_config=() 54 command_set=0 55 PROFILE_OUT=${PROFILE_OUT:-`get_profile_out_dir`} 56 57 for arg in $@; do 58 bazel_args_with_config+=("$arg ") 59 arg_is_command=$(is_command $arg) 60 # Add the default configs after the first argument, which should be the command, e.g. build/test 61 if [[ $arg_is_command && $command_set == 0 ]]; then 62 bazel_args_with_config+=("--profile=$PROFILE_OUT/bazel_metrics-profile --config=bp2build ") 63 command_set=1 64 fi 65 done 66 echo ${bazel_args_with_config[@]} 67} 68