xref: /aosp_15_r20/build/bazel/ci/build_with_bazel.sh (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1#!/bin/bash -eux
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# Verifies mixed builds does not run if neither --bazel-mode-dev nor --bazel-mode
18# is set.
19# This verification script is designed to be used for continuous integration
20# tests, though may also be used for manual developer verification.
21
22STARTUP_FLAGS=(
23  # Keep the Bazel server alive, package cache hot and reduce excessive I/O
24  # and wall time by ensuring that max_idle_secs is longer than bp2build which
25  # runs in every loop. bp2build takes ~20 seconds to run, so set this to a
26  # minute to account for resource contention, but still ensure that the bazel
27  # server doesn't stick around after.
28  --max_idle_secs=60
29)
30
31# Before you add flags to this list, cosnider adding it to the "ci" bazelrc
32# config instead of this list so that flags are not duplicated between scripts
33# and bazelrc, and bazelrc is the Bazel-native way of organizing flags.
34FLAGS=(
35  --config=bp2build
36  --config=ci
37  --keep_going
38)
39
40function build_for_device() {
41  local -n build_targets=$1
42  local -n test_targets=$2
43  ###########
44  # Iterate over various products supported in the platform build.
45  ###########
46  product_prefix="aosp_"
47  for arch in arm arm64 x86 x86_64; do
48    # Re-run product config and bp2build for every TARGET_PRODUCT. This is
49    # necessary as long as bp2build workspaces are not product independent.
50    product=${product_prefix}${arch}
51    "${SOURCE_ROOT}/build/soong/soong_ui.bash" --make-mode BP2BUILD_VERBOSE=1 TARGET_PRODUCT=${product} --skip-soong-tests bp2build dist
52    # Remove the ninja_build output marker file to communicate to buildbot that this is not a regular Ninja build, and its
53    # output should not be parsed as such.
54    rm -f out/ninja_build
55
56    # Dist the entire workspace of generated BUILD files, rooted from
57    # out/soong/bp2build. This is done early so it's available even if
58    # builds/tests fail. Currently the generated BUILD files can be different
59    # between products due to Soong plugins and non-deterministic codegeneration.
60    # We tar and gzip in separate steps because when using tar -z, you can't tell it to not include
61    # a timestamp in the gzip header.
62    tar c --mtime='1970-01-01' -C out/soong/bp2build . | gzip -n > "${DIST_DIR}/bp2build_generated_workspace_${product}.tar.gz"
63
64    local device_startup_flags=(
65      # Unique output bases per product to help with incremental builds across
66      # invocations of this script.
67      # e.g. the second invocation of this script for aosp_x86 would use the output_base
68      # of aosp_x86 from the first invocation.
69      --output_base="${OUT_DIR}/bazel/test_output_bases/${product}"
70    )
71    device_startup_flags+=( "${STARTUP_FLAGS[@]}" )
72
73    # Use a loop to prevent unnecessarily switching --platforms because that drops
74    # the Bazel analysis cache.
75    #
76    # 1. Build every target in $BUILD_TARGETS
77    build/bazel/bin/bazel ${device_startup_flags[@]} \
78      build ${FLAGS[@]} --config=android -- \
79      ${build_targets[@]}
80
81    # 2. Test every target that is compatible with an android target platform (e.g. analysis_tests, sh_tests, diff_tests).
82    build/bazel/bin/bazel ${device_startup_flags[@]} \
83      test ${FLAGS[@]} --build_tests_only --config=android -- \
84      ${test_targets[@]}
85
86    # 3. Dist mainline modules.
87    build/bazel/bin/bazel ${device_startup_flags[@]} \
88      run //build/bazel/ci/dist:mainline_modules ${FLAGS[@]} \
89      --config=android -- \
90      --dist_dir="${DIST_DIR}/mainline_modules_${arch}"
91  done
92}
93
94function build_and_test_for_host() {
95  targets=("$@")
96  # We can safely build and test all targets on the host linux config, and rely on
97  # incompatible target skipping for tests that cannot run on the host.
98  build/bazel/bin/bazel \
99    "${STARTUP_FLAGS[@]}" test ${FLAGS[@]} \
100    --build_tests_only=false \
101    --test_lang_filters=-tradefed_device_driven,-tradefed_host_driven_device,-tradefed_deviceless \
102    -- ${targets[@]}
103}
104