1#!/bin/bash -eu 2 3set -o pipefail 4 5HARDWIRED_MOCK_TOP= 6# Uncomment this to be able to view the source tree after a test is run 7# HARDWIRED_MOCK_TOP=/tmp/td 8 9REAL_TOP="$(readlink -f "$(dirname "$0")"/../../..)" 10 11if [[ -n "$HARDWIRED_MOCK_TOP" ]]; then 12 MOCK_TOP="$HARDWIRED_MOCK_TOP" 13else 14 MOCK_TOP=$(mktemp -t -d st.XXXXX) 15 trap cleanup_mock_top EXIT 16fi 17 18WARMED_UP_MOCK_TOP=$(mktemp -t soong_integration_tests_warmup.XXXXXX.tar.gz) 19trap 'rm -f "$WARMED_UP_MOCK_TOP"' EXIT 20 21function warmup_mock_top { 22 info "Warming up mock top ..." 23 info "Mock top warmup archive: $WARMED_UP_MOCK_TOP" 24 cleanup_mock_top 25 mkdir -p "$MOCK_TOP" 26 cd "$MOCK_TOP" 27 28 create_mock_soong 29 run_soong 30 tar czf "$WARMED_UP_MOCK_TOP" * 31} 32 33function cleanup_mock_top { 34 cd / 35 rm -fr "$MOCK_TOP" 36} 37 38function info { 39 echo -e "\e[92;1m[TEST HARNESS INFO]\e[0m" "$*" 40} 41 42function fail { 43 echo -e "\e[91;1mFAILED:\e[0m" "$*" 44 exit 1 45} 46 47function copy_directory { 48 local dir="$1" 49 local -r parent="$(dirname "$dir")" 50 51 mkdir -p "$MOCK_TOP/$parent" 52 cp -R "$REAL_TOP/$dir" "$MOCK_TOP/$parent" 53} 54 55function symlink_file { 56 local file="$1" 57 58 mkdir -p "$MOCK_TOP/$(dirname "$file")" 59 ln -s "$REAL_TOP/$file" "$MOCK_TOP/$file" 60} 61 62function symlink_directory { 63 local dir="$1" 64 65 mkdir -p "$MOCK_TOP/$dir" 66 # We need to symlink the contents of the directory individually instead of 67 # using one symlink for the whole directory because finder.go doesn't follow 68 # symlinks when looking for Android.bp files 69 for i in "$REAL_TOP/$dir"/*; do 70 i=$(basename "$i") 71 local target="$MOCK_TOP/$dir/$i" 72 local source="$REAL_TOP/$dir/$i" 73 74 if [[ -e "$target" ]]; then 75 if [[ ! -d "$source" || ! -d "$target" ]]; then 76 fail "Trying to symlink $dir twice" 77 fi 78 else 79 ln -s "$REAL_TOP/$dir/$i" "$MOCK_TOP/$dir/$i"; 80 fi 81 done 82} 83 84function create_mock_soong { 85 create_mock_bazel 86 copy_directory build/blueprint 87 copy_directory build/soong 88 copy_directory build/make 89 90 symlink_directory prebuilts/sdk 91 symlink_directory prebuilts/go 92 symlink_directory prebuilts/build-tools 93 symlink_directory prebuilts/clang/host 94 symlink_directory external/compiler-rt 95 symlink_directory external/go-cmp 96 symlink_directory external/golang-protobuf 97 symlink_directory external/licenseclassifier 98 symlink_directory external/starlark-go 99 symlink_directory external/python 100 symlink_directory external/sqlite 101 symlink_directory external/spdx-tools 102 103 touch "$MOCK_TOP/Android.bp" 104} 105 106function setup { 107 cleanup_mock_top 108 mkdir -p "$MOCK_TOP" 109 110 echo 111 echo ---------------------------------------------------------------------------- 112 info "Running test case \e[96;1m${FUNCNAME[1]}\e[0m" 113 cd "$MOCK_TOP" 114 115 tar xzf "$WARMED_UP_MOCK_TOP" --warning=no-timestamp 116} 117 118# shellcheck disable=SC2120 119function run_soong { 120 USE_RBE=false build/soong/soong_ui.bash --make-mode --skip-ninja --skip-config --soong-only --skip-soong-tests "$@" 121} 122 123function create_mock_bazel { 124 copy_directory build/bazel 125 copy_directory build/bazel_common_rules 126 127 symlink_directory packages/modules/common/build 128 symlink_directory prebuilts/bazel 129 symlink_directory prebuilts/clang 130 symlink_directory prebuilts/jdk 131 symlink_directory external/bazel-skylib 132 symlink_directory external/bazelbuild-rules_android 133 symlink_directory external/bazelbuild-rules_license 134 symlink_directory external/bazelbuild-kotlin-rules 135 136 symlink_file WORKSPACE 137 symlink_file BUILD 138} 139 140function run_bazel { 141 # Remove the ninja_build output marker file to communicate to buildbot that this is not a regular Ninja build, and its 142 # output should not be parsed as such. 143 rm -rf out/ninja_build 144 145 build/bazel/bin/bazel "$@" 146} 147 148function run_ninja { 149 build/soong/soong_ui.bash --make-mode --skip-config --soong-only --skip-soong-tests "$@" 150} 151 152info "Starting Soong integration test suite $(basename "$0")" 153info "Mock top: $MOCK_TOP" 154 155 156export ALLOW_MISSING_DEPENDENCIES=true 157export ALLOW_BP_UNDER_SYMLINKS=true 158warmup_mock_top 159 160function scan_and_run_tests { 161 # find all test_ functions 162 # NB "declare -F" output is sorted, hence test order is deterministic 163 readarray -t test_fns < <(declare -F | sed -n -e 's/^declare -f \(test_.*\)$/\1/p') 164 info "Found ${#test_fns[*]} tests" 165 if [[ ${#test_fns[*]} -eq 0 ]]; then 166 fail "No tests found" 167 fi 168 for f in ${test_fns[*]}; do 169 $f 170 info "Completed test case \e[96;1m$f\e[0m" 171 done 172} 173