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 [[ ! -z "$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 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 $(ls "$REAL_TOP/$dir"); do 70 local target="$MOCK_TOP/$dir/$i" 71 local source="$REAL_TOP/$dir/$i" 72 73 if [[ -e "$target" ]]; then 74 if [[ ! -d "$source" || ! -d "$target" ]]; then 75 fail "Trying to symlink $dir twice" 76 fi 77 else 78 ln -s "$REAL_TOP/$dir/$i" "$MOCK_TOP/$dir/$i"; 79 fi 80 done 81} 82 83function create_mock_soong { 84 copy_directory build/blueprint 85 copy_directory build/soong 86 87 symlink_directory prebuilts/go 88 symlink_directory prebuilts/build-tools 89 symlink_directory external/golang-protobuf 90 91 touch "$MOCK_TOP/Android.bp" 92} 93 94function setup() { 95 cleanup_mock_top 96 mkdir -p "$MOCK_TOP" 97 98 echo 99 echo ---------------------------------------------------------------------------- 100 info "Running test case \e[96;1m${FUNCNAME[1]}\e[0m" 101 cd "$MOCK_TOP" 102 103 tar xzf "$WARMED_UP_MOCK_TOP" 104} 105 106function run_soong() { 107 build/soong/soong_ui.bash --make-mode --skip-ninja --skip-make --skip-soong-tests "$@" 108} 109 110function create_mock_bazel() { 111 copy_directory build/bazel 112 113 symlink_directory prebuilts/bazel 114 symlink_directory prebuilts/jdk 115 116 symlink_file WORKSPACE 117 symlink_file tools/bazel 118} 119 120run_bazel() { 121 tools/bazel "$@" 122} 123 124run_bp2build() { 125 GENERATE_BAZEL_FILES=true build/soong/soong_ui.bash --make-mode --skip-ninja --skip-make --skip-soong-tests nothing 126} 127 128info "Starting Soong integration test suite $(basename $0)" 129info "Mock top: $MOCK_TOP" 130 131 132export ALLOW_MISSING_DEPENDENCIES=true 133warmup_mock_top 134