Files
build_soong/tests/lib.sh
Lukacs T. Berki a1b9372ef7 Make running soong_build in alternate modes nicer.
This includes the JSON graph generator and bp2build.

Before:
GENERATE_BAZEL_FILES=1 m nothing
GENERATE_JSON_MODULE_GRAPH=1 m nothing

Now:
m json-module-graph
m bp2build

They can now also be combined with other targets or each other.

The longer-term goal is to run "m queryview" and "m soong_docs" using
the same infrastructure. There are two alternate approaches:

1. Call soong_build from within the main Ninja invocation. This requires
two sequential soong_build invocations and is thus slower.
2. Do everything requested in the same soong_build invocation. This
would be faster, but one AFAIU can't tell Ninja that multiple possible
actions can build the same output so that doesn't work.

(1) is somewhat more desirable because soong_docs seems to be built
from build/make/core/main.mk ; I assume that that can be worked around
although I haven't checked where the output of "m soong_docs" goes.

Test: Presubmits.
Change-Id: If5ba36490d9f3f60733e6d6be9286eb2b67c3ff5
2021-09-06 16:03:51 +02:00

137 lines
2.9 KiB
Bash

#!/bin/bash -eu
set -o pipefail
HARDWIRED_MOCK_TOP=
# Uncomment this to be able to view the source tree after a test is run
# HARDWIRED_MOCK_TOP=/tmp/td
REAL_TOP="$(readlink -f "$(dirname "$0")"/../../..)"
if [[ ! -z "$HARDWIRED_MOCK_TOP" ]]; then
MOCK_TOP="$HARDWIRED_MOCK_TOP"
else
MOCK_TOP=$(mktemp -t -d st.XXXXX)
trap cleanup_mock_top EXIT
fi
WARMED_UP_MOCK_TOP=$(mktemp -t soong_integration_tests_warmup.XXXXXX.tar.gz)
trap 'rm -f "$WARMED_UP_MOCK_TOP"' EXIT
function warmup_mock_top {
info "Warming up mock top ..."
info "Mock top warmup archive: $WARMED_UP_MOCK_TOP"
cleanup_mock_top
mkdir -p "$MOCK_TOP"
cd "$MOCK_TOP"
create_mock_soong
run_soong
tar czf "$WARMED_UP_MOCK_TOP" *
}
function cleanup_mock_top {
cd /
rm -fr "$MOCK_TOP"
}
function info {
echo -e "\e[92;1m[TEST HARNESS INFO]\e[0m" $*
}
function fail {
echo -e "\e[91;1mFAILED:\e[0m" $*
exit 1
}
function copy_directory() {
local dir="$1"
local parent="$(dirname "$dir")"
mkdir -p "$MOCK_TOP/$parent"
cp -R "$REAL_TOP/$dir" "$MOCK_TOP/$parent"
}
function symlink_file() {
local file="$1"
mkdir -p "$MOCK_TOP/$(dirname "$file")"
ln -s "$REAL_TOP/$file" "$MOCK_TOP/$file"
}
function symlink_directory() {
local dir="$1"
mkdir -p "$MOCK_TOP/$dir"
# We need to symlink the contents of the directory individually instead of
# using one symlink for the whole directory because finder.go doesn't follow
# symlinks when looking for Android.bp files
for i in $(ls "$REAL_TOP/$dir"); do
local target="$MOCK_TOP/$dir/$i"
local source="$REAL_TOP/$dir/$i"
if [[ -e "$target" ]]; then
if [[ ! -d "$source" || ! -d "$target" ]]; then
fail "Trying to symlink $dir twice"
fi
else
ln -s "$REAL_TOP/$dir/$i" "$MOCK_TOP/$dir/$i";
fi
done
}
function create_mock_soong {
copy_directory build/blueprint
copy_directory build/soong
symlink_directory prebuilts/go
symlink_directory prebuilts/build-tools
symlink_directory external/go-cmp
symlink_directory external/golang-protobuf
touch "$MOCK_TOP/Android.bp"
}
function setup() {
cleanup_mock_top
mkdir -p "$MOCK_TOP"
echo
echo ----------------------------------------------------------------------------
info "Running test case \e[96;1m${FUNCNAME[1]}\e[0m"
cd "$MOCK_TOP"
tar xzf "$WARMED_UP_MOCK_TOP"
}
function run_soong() {
build/soong/soong_ui.bash --make-mode --skip-ninja --skip-make --skip-soong-tests "$@"
}
function create_mock_bazel() {
copy_directory build/bazel
symlink_directory prebuilts/bazel
symlink_directory prebuilts/jdk
symlink_directory external/bazel-skylib
symlink_file WORKSPACE
symlink_file BUILD
symlink_file tools/bazel
}
run_bazel() {
tools/bazel "$@"
}
run_ninja() {
build/soong/soong_ui.bash --make-mode --skip-make --skip-soong-tests "$@"
}
info "Starting Soong integration test suite $(basename $0)"
info "Mock top: $MOCK_TOP"
export ALLOW_MISSING_DEPENDENCIES=true
warmup_mock_top