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