• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2
3# Copyright (C) 2022 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# A script to generate an Atest Bazel workspace for execution on the Android CI.
18
19# Exit immediately on failures and disallow undefined variables.
20set -euo pipefail
21# List commands as they are executed. This helps debug the error
22# if the script exits mid-way through.
23set -x
24
25function check_env_var()
26{
27  if [ ! -n "${!1}" ] ; then
28    echo "Necessary environment variable ${1} missing, exiting."
29    exit 1
30  fi
31}
32
33# Check for necessary environment variables.
34check_env_var "ANDROID_BUILD_TOP"
35check_env_var "TARGET_PRODUCT"
36check_env_var "TARGET_BUILD_VARIANT"
37
38function get_build_var()
39{
40  (${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --dumpvar-mode --abs $1)
41}
42
43out=$(get_build_var PRODUCT_OUT)
44
45# ANDROID_BUILD_TOP is deprecated, so don't use it throughout the script.
46# But if someone sets it, we'll respect it.
47cd ${ANDROID_BUILD_TOP:-.}
48
49# Use the versioned Python binaries in prebuilts/ for a reproducible
50# build with minimal reliance on host tools.
51export PATH=`pwd`/prebuilts/build-tools/path/linux-x86:${PATH}
52
53export \
54  ANDROID_PRODUCT_OUT=${out} \
55  OUT=${out} \
56  ANDROID_HOST_OUT=$(get_build_var HOST_OUT) \
57  ANDROID_TARGET_OUT_TESTCASES=$(get_build_var TARGET_OUT_TESTCASES)
58
59if [ ! -n "${OUT_DIR:-}" ] ; then
60  OUT_DIR=$(get_build_var "OUT_DIR")
61fi
62
63if [ ! -n "${DIST_DIR:-}" ] ; then
64  echo "dist dir not defined, defaulting to OUT_DIR/dist."
65  export DIST_DIR=${OUT_DIR}/dist
66fi
67
68# Build:
69#  - Atest from source to pick up the latest changes
70#  - Bazel test suite needed by BazelTest
71#  - EXTRA_TARGETS requested on the commandline (used by git_master.gcl)
72targets="atest dist empty-bazel-test-suite ${EXTRA_TARGETS:-}"
73build/soong/soong_ui.bash --make-mode $targets
74
75# TODO(b/277656887): Fix the underlying atest issue that causes the workspace to not be
76# regenerated.
77rm -rf ${OUT_DIR}/atest_bazel_workspace
78
79# Generate the initial workspace via Atest Bazel mode.
80${OUT_DIR}/host/linux-x86/bin/atest-dev \
81  --bazel-mode \
82  --host-unit-test-only \
83  --host \
84  -c \
85  -b # Builds dependencies without running tests.
86
87
88# TODO(b/201242197): Create a stub workspace for the remote_coverage_tools
89# package so that Bazel does not attempt to fetch resources online which is not
90# allowed on build bots.
91mkdir -p ${OUT_DIR}/atest_bazel_workspace/remote_coverage_tools
92touch ${OUT_DIR}/atest_bazel_workspace/remote_coverage_tools/WORKSPACE
93cat << EOF > ${OUT_DIR}/atest_bazel_workspace/remote_coverage_tools/BUILD
94package(default_visibility = ["//visibility:public"])
95
96filegroup(
97    name = "coverage_report_generator",
98    srcs = ["coverage_report_generator.sh"],
99)
100EOF
101
102# Create the workspace archive.
103prebuilts/build-tools/linux-x86/bin/soong_zip \
104  -o ${DIST_DIR}/atest_bazel_workspace.zip \
105  -P android-bazel-suite/ \
106  -D out/atest_bazel_workspace/ \
107  -f "out/atest_bazel_workspace/**/.*" \
108  -symlinks=false  `# Follow symlinks and store the referenced files.` \
109  -sha256  `# Store SHA256 checksum for each file to enable CAS.` \
110  `# Avoid failing for dangling symlinks since these are expected` \
111  `# because we don't build all targets.` \
112  -ignore_missing_files
113
114# Merge the workspace into bazel-test-suite.
115prebuilts/build-tools/linux-x86/bin/merge_zips \
116  ${DIST_DIR}/bazel-test-suite.zip \
117  ${DIST_DIR}/empty-bazel-test-suite.zip \
118  ${DIST_DIR}/atest_bazel_workspace.zip
119
120# Remove the old archives we no longer need
121rm -f \
122  ${DIST_DIR}/atest_bazel_workspace.zip \
123  ${DIST_DIR}/empty-bazel-test-suite.zip
124