aboutsummaryrefslogtreecommitdiff
path: root/ci/build_with_bazel.sh
blob: 84d43f8dc6bd4e19d6f5fb1aa753240d8b05e15b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash -eux

# Copyright (C) 2023 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.
#
# Verifies mixed builds does not run if neither --bazel-mode-dev nor --bazel-mode
# is set.
# This verification script is designed to be used for continuous integration
# tests, though may also be used for manual developer verification.

STARTUP_FLAGS=(
  --max_idle_secs=5
)

# Before you add flags to this list, cosnider adding it to the "ci" bazelrc
# config instead of this list so that flags are not duplicated between scripts
# and bazelrc, and bazelrc is the Bazel-native way of organizing flags.
FLAGS=(
  --config=bp2build
  --config=ci
  --keep_going
)

function build_for_device() {
  local -n build_targets=$1
  local -n test_targets=$2
  ###########
  # Iterate over various products supported in the platform build.
  ###########
  product_prefix="aosp_"
  for arch in arm arm64 x86 x86_64; do
    # Re-run product config and bp2build for every TARGET_PRODUCT.
    product=${product_prefix}${arch}
    "${AOSP_ROOT}/build/soong/soong_ui.bash" --make-mode BP2BUILD_VERBOSE=1 TARGET_PRODUCT=${product} --skip-soong-tests bp2build dist
    # Remove the ninja_build output marker file to communicate to buildbot that this is not a regular Ninja build, and its
    # output should not be parsed as such.
    rm -f out/ninja_build

    # Dist the entire workspace of generated BUILD files, rooted from
    # out/soong/bp2build. This is done early so it's available even if
    # builds/tests fail. Currently the generated BUILD files can be different
    # between products due to Soong plugins and non-deterministic codegeneration.
    tar --mtime='1970-01-01' -czf "${DIST_DIR}/bp2build_generated_workspace_${product}.tar.gz" -C out/soong/bp2build .

    local device_startup_flags=(
      # Unique output bases per product to help with incremental builds across
      # invocations of this script.
      # e.g. the second invocation of this script for aosp_x86 would use the output_base
      # of aosp_x86 from the first invocation.
      --output_base="${OUT_DIR}/bazel/test_output_bases/${product}"
    )
    device_startup_flags+=( "${STARTUP_FLAGS[@]}" )

    # Use a loop to prevent unnecessarily switching --platforms because that drops
    # the Bazel analysis cache.
    #
    # 1. Build every target in $BUILD_TARGETS
    build/bazel/bin/bazel ${device_startup_flags[@]} \
      build ${FLAGS[@]} --config=android -- \
      ${build_targets[@]}

    # 2. Test every target that is compatible with an android target platform (e.g. analysis_tests, sh_tests, diff_tests).
    build/bazel/bin/bazel ${device_startup_flags[@]} \
      test ${FLAGS[@]} --build_tests_only --config=android -- \
      ${test_targets[@]}

    # 3. Dist mainline modules.
    build/bazel/bin/bazel ${device_startup_flags[@]} \
      run //build/bazel/ci/dist:mainline_modules ${FLAGS[@]} \
      --config=android -- \
      --dist_dir="${DIST_DIR}/mainline_modules_${arch}"
  done
}

function build_for_host() {
  targets=("$@")
  # We can safely build and test all targets on the host linux config, and rely on
  # incompatible target skipping for tests that cannot run on the host.
  build/bazel/bin/bazel \
    "${STARTUP_FLAGS[@]}" test ${FLAGS[@]} --build_tests_only=false \
    -- ${targets[@]}
}