aboutsummaryrefslogtreecommitdiff
path: root/ci/bp2build.sh
blob: 5c6c824b53616a458eda43febf8306958e61904e (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash -eux
# Verifies that bp2build-generated BUILD files result in successful Bazel
# builds.
#
# This verification script is designed to be used for continuous integration
# tests, though may also be used for manual developer verification.

#######
# Setup
#######

# Set the test output directories.
AOSP_ROOT="$(dirname $0)/../../.."
OUT_DIR=$(realpath ${OUT_DIR:-${AOSP_ROOT}/out})
if [[ -z ${DIST_DIR+x} ]]; then
  DIST_DIR="${OUT_DIR}/dist"
  echo "DIST_DIR not set. Using ${OUT_DIR}/dist. This should only be used for manual developer testing."
fi

# 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_LIST=(
  --config=bp2build
  --config=ci
)
FLAGS="${FLAGS_LIST[@]}"

source "$(dirname $0)/target_lists.sh"

###############
# Build and test targets for device target platform.
###############

###########
# 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 .

  STARTUP_FLAGS=(
    --max_idle_secs=5
    # 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}"
  )

  # 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 ${STARTUP_FLAGS[@]} build ${FLAGS} --config=android -k -- ${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 ${STARTUP_FLAGS[@]} test ${FLAGS} --build_tests_only --config=android -k -- ${TEST_TARGETS}
  # 3. Dist mainline modules.
  build/bazel/bin/bazel ${STARTUP_FLAGS[@]} run //build/bazel/ci/dist:mainline_modules ${FLAGS} --config=android -- --dist_dir="${DIST_DIR}/mainline_modules_${arch}"
done

#########
# Host-only builds and tests
#########

# 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 --max_idle_secs=5 test ${FLAGS} --build_tests_only=false -k \
  -- ${BUILD_TARGETS} ${TEST_TARGETS} "${HOST_INCOMPATIBLE_TARGETS[@]}"

# Run some tests under the linux_bionic_x86_64 configuration as well
build/bazel/bin/bazel --max_idle_secs=5 test ${FLAGS} --config=linux_bionic_x86_64 --build_tests_only=false -k \
  -- //prebuilts/clang/host/linux-x86:cc_toolchain_features_ubsan_tests

###################
# bp2build-progress
###################

function get_soong_names_from_queryview() {
  names=$( build/bazel/bin/bazel query --config=ci --config=queryview --output=xml "${@}" \
    | awk -F'"' '$2 ~ /soong_module_name/ { print $4 }' \
    | sort -u )
  echo "${names[@]}"
}

# Generate bp2build progress reports and graphs for these modules into the dist
# dir so that they can be downloaded from the CI artifact list.
BP2BUILD_PROGRESS_MODULES=(
  NetworkStackNext  # not updatable but will be
  build-tools  # host sdk
  com.android.runtime  # not updatable but will be
  platform-tools  # host sdk
)

# Query for some module types of interest so that we don't have to hardcode the
# lists
"${AOSP_ROOT}/build/soong/soong_ui.bash" --make-mode BP2BUILD_VERBOSE=1 --skip-soong-tests queryview
rm -f out/ninja_build

# Only apexes/apps that specify updatable=1 are mainline modules, the other are
# "just" apexes/apps. Often this is not specified in the process of becoming a
# mainline module as enables a number of validations.
# Ignore defaults and test rules.
APEX_QUERY='attr(updatable, 1, //...) - kind("_defaults rule", //...) - kind("apex_test_ rule", //...)'
APEX_VNDK_QUERY="kind(\"apex_vndk rule\", //...)"

BP2BUILD_PROGRESS_MODULES+=( $(get_soong_names_from_queryview "${APEX_QUERY}"" + ""${APEX_VNDK_QUERY}" ) )

bp2build_progress_script="//build/bazel/scripts/bp2build-progress:bp2build-progress"
bp2build_progress_output_dir="${DIST_DIR}/bp2build-progress"
mkdir -p "${bp2build_progress_output_dir}"

report_args=""
for m in "${BP2BUILD_PROGRESS_MODULES[@]}"; do
  report_args="$report_args -m ""${m}"
  if [[ "${m}" =~ (media.swcodec|neuralnetworks)$ ]]; then
    build/bazel/bin/bazel run ${FLAGS} --config=linux_x86_64 "${bp2build_progress_script}" -- graph  -m "${m}" > "${bp2build_progress_output_dir}/${m}_graph.dot"
  fi
done

build/bazel/bin/bazel run ${FLAGS} --config=linux_x86_64 "${bp2build_progress_script}" -- \
  report ${report_args} \
  --proto-file=$( realpath "${bp2build_progress_output_dir}" )"/bp2build-progress.pb" \
  > "${bp2build_progress_output_dir}/progress_report.txt"