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 19function check_env_var() 20{ 21 if [ ! -n "${!1}" ] ; then 22 echo "Necessary environment variable ${1} missing, exiting." 23 exit 1 24 fi 25} 26 27# Check for necessary environment variables. 28check_env_var "ANDROID_BUILD_TOP" 29check_env_var "TARGET_PRODUCT" 30check_env_var "TARGET_BUILD_VARIANT" 31 32function get_build_var() 33{ 34 (${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --dumpvar-mode --abs $1) 35} 36 37out=$(get_build_var PRODUCT_OUT) 38JDK_PATH="${ANDROID_BUILD_TOP}/prebuilts/jdk/jdk11/linux-x86" 39BAZEL_BINARY="${ANDROID_BUILD_TOP}/prebuilts/bazel/linux-x86_64/bazel" 40 41# Use the versioned JDK and Python binaries in prebuilts/ for a reproducible 42# build with minimal reliance on host tools. 43export PATH=${ANDROID_BUILD_TOP}/prebuilts/build-tools/path/linux-x86:${ANDROID_BUILD_TOP}/prebuilts/jdk/jdk11/linux-x86/bin:${PATH} 44 45export \ 46 ANDROID_PRODUCT_OUT=${out} \ 47 OUT=${out} \ 48 ANDROID_HOST_OUT=$(get_build_var HOST_OUT) \ 49 ANDROID_TARGET_OUT_TESTCASES=$(get_build_var TARGET_OUT_TESTCASES) 50 51if [ ! -n "$OUT_DIR" ] ; then 52 OUT_DIR=$(get_build_var "OUT_DIR") 53fi 54 55if [ ! -n "$DIST_DIR" ] ; then 56 echo "dist dir not defined, defaulting to OUT_DIR/dist." 57 export DIST_DIR=${OUT_DIR}/dist 58fi 59 60# Build Atest from source to pick up the latest changes. 61${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode atest 62 63# Generate the initial workspace via Atest Bazel mode. 64${OUT_DIR}/host/linux-x86/bin/atest-dev --bazel-mode --dry-run -m 65 66# Copy over some needed dependencies. We need Bazel for querying dependencies 67# and actually running the test. The JDK is for the Tradefed test runner and 68# Java tests. 69cp -L ${BAZEL_BINARY} ${OUT_DIR}/atest_bazel_workspace/bazelbin 70mkdir ${OUT_DIR}/atest_bazel_workspace/prebuilts/jdk 71cp -a ${JDK_PATH}/* ${OUT_DIR}/atest_bazel_workspace/prebuilts/jdk/. 72 73pushd ${OUT_DIR}/atest_bazel_workspace 74 75# TODO(b/201242197): Create a stub workspace for the remote_coverage_tools 76# package so that Bazel does not attempt to fetch resources online which is not 77# allowed on build bots. 78mkdir remote_coverage_tools 79touch remote_coverage_tools/WORKSPACE 80cat << EOF > remote_coverage_tools/BUILD 81package(default_visibility = ["//visibility:public"]) 82 83filegroup( 84 name = "coverage_report_generator", 85 srcs = ["coverage_report_generator.sh"], 86) 87EOF 88 89# Make directories for temporary output. 90JAVA_TEMP_DIR=$(mktemp -d) 91trap "rm -rf ${JAVA_TEMP_DIR}" EXIT 92 93BAZEL_TEMP_DIR=$(mktemp -d) 94trap "rm -rf ${BAZEL_TEMP_DIR}" EXIT 95 96# Query the list of dependencies needed by the tests. 97# TODO(b/217658764): Consolidate Bazel query functions into a separate script 98# that other components can use. 99JAVA_HOME="${JDK_PATH}" \ 100 "${BAZEL_BINARY}" \ 101 --server_javabase="${JDK_PATH}" \ 102 --host_jvm_args=-Djava.io.tmpdir=${JAVA_TEMP_DIR} \ 103 --output_user_root=${BAZEL_TEMP_DIR} \ 104 --max_idle_secs=5 \ 105 cquery \ 106 --override_repository=remote_coverage_tools=${ANDROID_BUILD_TOP}/out/atest_bazel_workspace/remote_coverage_tools \ 107 --output=starlark \ 108 --starlark:file=${ANDROID_BUILD_TOP}/tools/asuite/atest/bazel/format_as_soong_module_name.cquery \ 109 "deps( $(${BAZEL_BINARY} \ 110 --server_javabase="${JDK_PATH}" \ 111 --host_jvm_args=-Djava.io.tmpdir=${JAVA_TEMP_DIR} \ 112 --output_user_root=${BAZEL_TEMP_DIR} \ 113 --max_idle_secs=5 query "tests(...)" | paste -sd "+" -) )" | \ 114 sed '/^$/d' | \ 115 sort -u \ 116> build_targets 117 118popd 119 120# Build all test dependencies. 121${ANDROID_BUILD_TOP}/build/soong/soong_ui.bash --make-mode $(cat $OUT_DIR/atest_bazel_workspace/build_targets) 122 123# Create the workspace archive which will be downloaded by the Tradefed hosts. 124tar zcfh ${DIST_DIR}/atest_bazel_workspace.tar.gz out/atest_bazel_workspace/ 125