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 WRAPPER_TOOL=atest $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 --no-metrics \ 82 --bazel-mode \ 83 --host-unit-test-only \ 84 --host \ 85 -c \ 86 -b # Builds dependencies without running tests. 87 88 89# TODO(b/201242197): Create a stub workspace for the remote_coverage_tools 90# package so that Bazel does not attempt to fetch resources online which is not 91# allowed on build bots. 92mkdir -p ${OUT_DIR}/atest_bazel_workspace/remote_coverage_tools 93touch ${OUT_DIR}/atest_bazel_workspace/remote_coverage_tools/WORKSPACE 94cat << EOF > ${OUT_DIR}/atest_bazel_workspace/remote_coverage_tools/BUILD 95package(default_visibility = ["//visibility:public"]) 96 97filegroup( 98 name = "coverage_report_generator", 99 srcs = ["coverage_report_generator.sh"], 100) 101EOF 102 103# Create the workspace archive. 104prebuilts/build-tools/linux-x86/bin/soong_zip \ 105 -o ${DIST_DIR}/atest_bazel_workspace.zip \ 106 -P android-bazel-suite/ \ 107 -D out/atest_bazel_workspace/ \ 108 -f "out/atest_bazel_workspace/**/.*" \ 109 -symlinks=false `# Follow symlinks and store the referenced files.` \ 110 -sha256 `# Store SHA256 checksum for each file to enable CAS.` \ 111 `# Avoid failing for dangling symlinks since these are expected` \ 112 `# because we don't build all targets.` \ 113 -ignore_missing_files 114 115# Merge the workspace into bazel-test-suite. 116prebuilts/build-tools/linux-x86/bin/merge_zips \ 117 ${DIST_DIR}/bazel-test-suite.zip \ 118 ${DIST_DIR}/empty-bazel-test-suite.zip \ 119 ${DIST_DIR}/atest_bazel_workspace.zip 120 121# Remove the old archives we no longer need 122rm -f \ 123 ${DIST_DIR}/atest_bazel_workspace.zip \ 124 ${DIST_DIR}/empty-bazel-test-suite.zip 125