| #!/bin/bash |
| |
| # Copyright (C) 2022 The Android Open Source Project |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| function gettop |
| { |
| local TOPFILE=build/make/core/envsetup.mk |
| if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then |
| # The following circumlocution ensures we remove symlinks from TOP. |
| (cd "$TOP"; PWD= /bin/pwd) |
| else |
| if [ -f $TOPFILE ] ; then |
| # The following circumlocution (repeated below as well) ensures |
| # that we record the true directory name and not one that is |
| # faked up with symlink names. |
| PWD= /bin/pwd |
| else |
| local HERE=$PWD |
| local T= |
| while [ \( ! \( -f $TOPFILE \) \) -a \( "$PWD" != "/" \) ]; do |
| \cd .. |
| T=`PWD= /bin/pwd -P` |
| done |
| \cd "$HERE" |
| if [ -f "$T/$TOPFILE" ]; then |
| echo "$T" |
| fi |
| fi |
| fi |
| } |
| |
| T=$(gettop) |
| if [[ ! $T ]] ; then |
| echo "Can not locate root of source tree. b must be run from within Android source tree." >&2 |
| exit 1 |
| fi |
| |
| # Look for the --run-soong-tests flag and skip passing --skip-soong-tests to Soong if present |
| bazel_args="" |
| skip_tests="--skip-soong-tests" |
| for i in $@; do |
| if [[ $i != "--run-soong-tests" ]]; then |
| bazel_args+="$i " |
| else |
| skip_tests="" |
| fi |
| done |
| |
| # Generate BUILD, bzl files into the synthetic Bazel workspace (out/soong/workspace). |
| # RBE is disabled because it's not used with b builds and adds overhead: b/251441524 |
| USE_RBE=false "$T/build/soong/soong_ui.bash" --build-mode --all-modules --dir="$(pwd)" $skip_tests bp2build USE_BAZEL_ANALYSIS= || exit 1 |
| |
| |
| # Then, run Bazel using the synthetic workspace as the --package_path. |
| if [[ -z "$bazel_args" ]]; then |
| # If there are no args, show help and exit. |
| "$T/build/bazel/bin/bazel" help |
| else |
| # Else, always run with the bp2build configuration, which sets Bazel's package path to |
| # the synthetic workspace. |
| # Add the --config=bp2build after the first argument that doesn't start with a dash. That |
| # should be the bazel |
| # command. (build, test, run, ect) If the --config was added at the end, it wouldn't work |
| # with commands like: b run //foo -- --args-for-foo |
| config_set=0 |
| |
| # Represent the args as an array, not a string. |
| bazel_args_with_config=() |
| for arg in $bazel_args; do |
| if [[ $arg == "--" && $config_set -ne 1 ]]; # if we find --, insert config argument here |
| then |
| bazel_args_with_config+=("--config=bp2build -- ") |
| config_set=1 |
| else |
| bazel_args_with_config+=("$arg ") |
| fi |
| done |
| if [[ $config_set -ne 1 ]]; then |
| bazel_args_with_config+=("--config=bp2build ") |
| fi |
| |
| # Call Bazel. |
| "$T/build/bazel/bin/bazel" ${bazel_args_with_config[@]} |
| fi |
| |