xref: /aosp_15_r20/build/soong/tests/lib.sh (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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
11function make_mock_top {
12  mock=$(mktemp -t -d st.XXXXX)
13  echo "$mock"
14}
15
16if [[ -n "$HARDWIRED_MOCK_TOP" ]]; then
17  MOCK_TOP="$HARDWIRED_MOCK_TOP"
18else
19  MOCK_TOP=$(make_mock_top)
20  trap cleanup_mock_top EXIT
21fi
22
23WARMED_UP_MOCK_TOP=$(mktemp -t soong_integration_tests_warmup.XXXXXX.tar.gz)
24trap 'rm -f "$WARMED_UP_MOCK_TOP"' EXIT
25
26function warmup_mock_top {
27  info "Warming up mock top ..."
28  info "Mock top warmup archive: $WARMED_UP_MOCK_TOP"
29  cleanup_mock_top
30  mkdir -p "$MOCK_TOP"
31  cd "$MOCK_TOP"
32
33  create_mock_soong
34  run_soong
35  tar czf "$WARMED_UP_MOCK_TOP" *
36}
37
38function cleanup_mock_top {
39  cd /
40  rm -fr "$MOCK_TOP"
41}
42
43function info {
44  echo -e "\e[92;1m[TEST HARNESS INFO]\e[0m" "$*"
45}
46
47function fail {
48  echo -e "\e[91;1mFAILED:\e[0m" "$*"
49  exit 1
50}
51
52function copy_directory {
53  local dir="$1"
54  local -r parent="$(dirname "$dir")"
55
56  mkdir -p "$MOCK_TOP/$parent"
57  cp -R "$REAL_TOP/$dir" "$MOCK_TOP/$parent"
58}
59
60function delete_directory {
61  rm -rf "$MOCK_TOP/$1"
62}
63
64function symlink_file {
65  local file="$1"
66
67  mkdir -p "$MOCK_TOP/$(dirname "$file")"
68  ln -s "$REAL_TOP/$file" "$MOCK_TOP/$file"
69}
70
71function symlink_directory {
72  local dir="$1"
73
74  mkdir -p "$MOCK_TOP/$dir"
75  # We need to symlink the contents of the directory individually instead of
76  # using one symlink for the whole directory because finder.go doesn't follow
77  # symlinks when looking for Android.bp files
78  for i in "$REAL_TOP/$dir"/*; do
79    i=$(basename "$i")
80    local target="$MOCK_TOP/$dir/$i"
81    local source="$REAL_TOP/$dir/$i"
82
83    if [[ -e "$target" ]]; then
84      if [[ ! -d "$source" || ! -d "$target" ]]; then
85        fail "Trying to symlink $dir twice"
86      fi
87    else
88      ln -s "$REAL_TOP/$dir/$i" "$MOCK_TOP/$dir/$i";
89    fi
90  done
91}
92
93function create_mock_soong {
94  copy_directory build/blueprint
95  copy_directory build/soong
96  copy_directory build/make
97
98  symlink_directory prebuilts/sdk
99  symlink_directory prebuilts/go
100  symlink_directory prebuilts/build-tools
101  symlink_directory prebuilts/clang/host
102  symlink_directory external/compiler-rt
103  symlink_directory external/go-cmp
104  symlink_directory external/golang-protobuf
105  symlink_directory external/licenseclassifier
106  symlink_directory external/starlark-go
107  symlink_directory external/python
108  symlink_directory external/sqlite
109  symlink_directory external/spdx-tools
110  symlink_directory libcore
111
112  # TODO: b/286872909 - Remove these when the blocking bug is completed
113  symlink_directory external/libavc
114  symlink_directory external/libaom
115  symlink_directory external/libvpx
116  symlink_directory frameworks/base/libs/androidfw
117  symlink_directory external/libhevc
118  symlink_directory external/libexif
119  symlink_directory external/libopus
120  symlink_directory external/libmpeg2
121  symlink_directory external/expat
122  symlink_directory external/flac
123  symlink_directory system/extras/toolchain-extras
124
125  touch "$MOCK_TOP/Android.bp"
126}
127
128function setup {
129  cleanup_mock_top
130  mkdir -p "$MOCK_TOP"
131
132  echo
133  echo ----------------------------------------------------------------------------
134  info "Running test case \e[96;1m${FUNCNAME[1]}\e[0m"
135  cd "$MOCK_TOP"
136
137  tar xzf "$WARMED_UP_MOCK_TOP" --warning=no-timestamp
138}
139
140# shellcheck disable=SC2120
141function run_soong {
142  USE_RBE=false TARGET_PRODUCT=aosp_arm TARGET_RELEASE=trunk_staging TARGET_BUILD_VARIANT=userdebug build/soong/soong_ui.bash --make-mode --skip-ninja --skip-config --soong-only --skip-soong-tests "$@"
143}
144
145function run_ninja {
146  build/soong/soong_ui.bash --make-mode --skip-config --soong-only --skip-soong-tests "$@"
147}
148
149info "Starting Soong integration test suite $(basename "$0")"
150info "Mock top: $MOCK_TOP"
151
152
153export ALLOW_MISSING_DEPENDENCIES=true
154export ALLOW_BP_UNDER_SYMLINKS=true
155warmup_mock_top
156
157function scan_and_run_tests {
158  # find all test_ functions
159  # NB "declare -F" output is sorted, hence test order is deterministic
160  readarray -t test_fns < <(declare -F | sed -n -e 's/^declare -f \(test_.*\)$/\1/p')
161  info "Found ${#test_fns[*]} tests"
162  if [[ ${#test_fns[*]} -eq 0 ]]; then
163    fail "No tests found"
164  fi
165  for f in ${test_fns[*]}; do
166    $f
167    info "Completed test case \e[96;1m$f\e[0m"
168  done
169}
170
171function move_mock_top {
172  MOCK_TOP2=$(make_mock_top)
173  rm -rf $MOCK_TOP2
174  mv $MOCK_TOP $MOCK_TOP2
175  MOCK_TOP=$MOCK_TOP2
176  trap cleanup_mock_top EXIT
177}
178