2021-04-21 13:10:09 +02:00
#!/bin/bash -eu
set -o pipefail
# Test that bp2build and Bazel can play nicely together
source " $( dirname " $0 " ) /lib.sh "
2021-05-18 13:47:15 +02:00
readonly GENERATED_BUILD_FILE_NAME = "BUILD.bazel"
2022-12-08 07:29:21 +01:00
function test_bp2build_null_build {
2021-08-12 11:37:14 +02:00
setup
2021-09-02 17:23:06 +02:00
run_soong bp2build
2022-06-09 18:22:36 +02:00
local -r output_mtime1 = $( stat -c "%y" out/soong/bp2build_workspace_marker)
2021-08-12 11:37:14 +02:00
2021-09-02 17:23:06 +02:00
run_soong bp2build
2022-06-09 18:22:36 +02:00
local -r output_mtime2 = $( stat -c "%y" out/soong/bp2build_workspace_marker)
2021-08-12 11:37:14 +02:00
if [ [ " $output_mtime1 " != " $output_mtime2 " ] ] ; then
fail "Output bp2build marker file changed on null build"
fi
}
2023-02-09 23:54:00 +01:00
# Tests that, if bp2build reruns due to a blueprint file changing, that
# BUILD files whose contents are unchanged are not regenerated.
function test_bp2build_unchanged {
setup
mkdir -p pkg
touch pkg/x.txt
cat > pkg/Android.bp <<'EOF'
filegroup {
name: "x" ,
srcs: [ "x.txt" ] ,
bazel_module: { bp2build_available: true} ,
}
EOF
run_soong bp2build
local -r buildfile_mtime1 = $( stat -c "%y" out/soong/bp2build/pkg/BUILD.bazel)
local -r marker_mtime1 = $( stat -c "%y" out/soong/bp2build_workspace_marker)
# Force bp2build to rerun by updating the timestamp of a blueprint file.
touch pkg/Android.bp
run_soong bp2build
local -r buildfile_mtime2 = $( stat -c "%y" out/soong/bp2build/pkg/BUILD.bazel)
local -r marker_mtime2 = $( stat -c "%y" out/soong/bp2build_workspace_marker)
if [ [ " $marker_mtime1 " = = " $marker_mtime2 " ] ] ; then
fail "Expected bp2build marker file to change"
fi
if [ [ " $buildfile_mtime1 " != " $buildfile_mtime2 " ] ] ; then
fail "BUILD.bazel was updated even though contents are same"
fi
}
# Tests that blueprint files that are deleted are not present when the
# bp2build tree is regenerated.
function test_bp2build_deleted_blueprint {
setup
mkdir -p pkg
touch pkg/x.txt
cat > pkg/Android.bp <<'EOF'
filegroup {
name: "x" ,
srcs: [ "x.txt" ] ,
bazel_module: { bp2build_available: true} ,
}
EOF
run_soong bp2build
if [ [ ! -e "./out/soong/bp2build/pkg/BUILD.bazel" ] ] ; then
fail "Expected pkg/BUILD.bazel to be generated"
fi
rm pkg/Android.bp
run_soong bp2build
if [ [ -e "./out/soong/bp2build/pkg/BUILD.bazel" ] ] ; then
fail "Expected pkg/BUILD.bazel to be deleted"
fi
}
2022-12-08 07:29:21 +01:00
function test_bp2build_null_build_with_globs {
2021-08-12 11:37:14 +02:00
setup
mkdir -p foo/bar
cat > foo/bar/Android.bp <<'EOF'
filegroup {
name: "globs" ,
srcs: [ "*.txt" ] ,
}
EOF
touch foo/bar/a.txt foo/bar/b.txt
2021-09-02 17:23:06 +02:00
run_soong bp2build
2022-06-09 18:22:36 +02:00
local -r output_mtime1 = $( stat -c "%y" out/soong/bp2build_workspace_marker)
2021-08-12 11:37:14 +02:00
2021-09-02 17:23:06 +02:00
run_soong bp2build
2022-06-09 18:22:36 +02:00
local -r output_mtime2 = $( stat -c "%y" out/soong/bp2build_workspace_marker)
2021-08-12 11:37:14 +02:00
if [ [ " $output_mtime1 " != " $output_mtime2 " ] ] ; then
fail "Output bp2build marker file changed on null build"
fi
}
2022-10-13 11:25:38 +02:00
function test_different_relative_outdir {
setup
mkdir -p a
touch a/g.txt
cat > a/Android.bp <<'EOF'
filegroup {
name: "g" ,
srcs: [ "g.txt" ] ,
bazel_module: { bp2build_available: true} ,
}
EOF
# A directory under $MOCK_TOP
outdir = out2
trap " rm -rf $outdir " EXIT
# Modify OUT_DIR in a subshell so it doesn't affect the top level one.
2022-11-20 00:14:48 +01:00
( export OUT_DIR = $outdir ; run_soong bp2build && run_bazel build --config= bp2build --config= ci //a:g)
2022-10-13 11:25:38 +02:00
}
function test_different_absolute_outdir {
setup
mkdir -p a
touch a/g.txt
cat > a/Android.bp <<'EOF'
filegroup {
name: "g" ,
srcs: [ "g.txt" ] ,
bazel_module: { bp2build_available: true} ,
}
EOF
# A directory under /tmp/...
outdir = $( mktemp -t -d st.XXXXX)
trap 'rm -rf $outdir' EXIT
# Modify OUT_DIR in a subshell so it doesn't affect the top level one.
2022-11-20 00:14:48 +01:00
( export OUT_DIR = $outdir ; run_soong bp2build && run_bazel build --config= bp2build --config= ci //a:g)
2022-10-13 11:25:38 +02:00
}
2022-12-08 07:29:21 +01:00
function _bp2build_generates_all_buildfiles {
2021-04-21 13:10:09 +02:00
setup
mkdir -p foo/convertible_soong_module
cat > foo/convertible_soong_module/Android.bp <<'EOF'
genrule {
name: "the_answer" ,
cmd: " echo '42' > $( out) " ,
out: [
"the_answer.txt" ,
] ,
bazel_module: {
bp2build_available: true,
} ,
}
EOF
mkdir -p foo/unconvertible_soong_module
cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
genrule {
name: "not_the_answer" ,
cmd: " echo '43' > $( out) " ,
out: [
"not_the_answer.txt" ,
] ,
bazel_module: {
bp2build_available: false,
} ,
}
EOF
2021-09-02 17:23:06 +02:00
run_soong bp2build
2021-04-21 13:10:09 +02:00
2021-05-18 13:47:15 +02:00
if [ [ ! -f " ./out/soong/workspace/foo/convertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } " ] ] ; then
fail " ./out/soong/workspace/foo/convertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } was not generated "
2021-04-21 13:10:09 +02:00
fi
2021-05-18 13:47:15 +02:00
if [ [ ! -f " ./out/soong/workspace/foo/unconvertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } " ] ] ; then
fail " ./out/soong/workspace/foo/unconvertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } was not generated "
2021-04-21 13:10:09 +02:00
fi
2021-05-18 13:47:15 +02:00
if ! grep "the_answer" " ./out/soong/workspace/foo/convertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } " ; then
fail " missing BUILD target the_answer in convertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } "
2021-04-21 13:10:09 +02:00
fi
2021-05-18 13:47:15 +02:00
if grep "not_the_answer" " ./out/soong/workspace/foo/unconvertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } " ; then
fail " found unexpected BUILD target not_the_answer in unconvertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } "
2021-04-21 13:10:09 +02:00
fi
2021-05-18 13:47:15 +02:00
if ! grep "filegroup" " ./out/soong/workspace/foo/unconvertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } " ; then
fail " missing filegroup in unconvertible_soong_module/ ${ GENERATED_BUILD_FILE_NAME } "
2021-04-21 13:10:09 +02:00
fi
# NOTE: We don't actually use the extra BUILD file for anything here
2022-11-20 00:14:48 +01:00
run_bazel build --config= android --config= bp2build --config= ci //foo/...
2021-04-21 13:10:09 +02:00
reduce forest generation to be incremental
Previously, symlink forest generation involved removing the entire
symlink forest and recreating it from scratch. With this change,
a) symlinks which need not change are untouched,
b) symlinks pointing to the wrong location are fixed, and
c) symlinks which should no longer exist are removed.
On AOSP on my local machine, this reduces the symlink forest generation
step from 2.5s to 1.1s clean, and 0.6s when a single file is added to
a source directory.
Bug: 257528847
Test: m bp2build, touch `fakefile` under the forest, remove a file
from the source tree, rerun m bp2build. Manually verify the new forest
does not retain the link to the deleted source file, and that fakefile
no longer exists in the forest.
Change-Id: I481371ae487e9419af6a3a4370c552578b07d650
2022-11-09 00:31:14 +01:00
local -r the_answer_file = " $( find -L bazel-out -name the_answer.txt) "
2021-04-21 13:10:09 +02:00
if [ [ ! -f " ${ the_answer_file } " ] ] ; then
2022-11-09 03:14:01 +01:00
fail "Expected the_answer.txt to be generated, but was missing"
2021-04-21 13:10:09 +02:00
fi
if ! grep 42 " ${ the_answer_file } " ; then
fail " Expected to find 42 in ' ${ the_answer_file } ' "
fi
}
2022-12-08 07:29:21 +01:00
function test_bp2build_generates_all_buildfiles {
_save_trap = $( trap -p EXIT)
trap '[[ $? -ne 0 ]] && echo Are you running this locally? Try changing --sandbox_tmpfs_path to something other than /tmp/ in build/bazel/linux.bazelrc.' EXIT
_bp2build_generates_all_buildfiles
eval " ${ _save_trap } "
}
2022-05-02 10:13:19 +02:00
reduce forest generation to be incremental
Previously, symlink forest generation involved removing the entire
symlink forest and recreating it from scratch. With this change,
a) symlinks which need not change are untouched,
b) symlinks pointing to the wrong location are fixed, and
c) symlinks which should no longer exist are removed.
On AOSP on my local machine, this reduces the symlink forest generation
step from 2.5s to 1.1s clean, and 0.6s when a single file is added to
a source directory.
Bug: 257528847
Test: m bp2build, touch `fakefile` under the forest, remove a file
from the source tree, rerun m bp2build. Manually verify the new forest
does not retain the link to the deleted source file, and that fakefile
no longer exists in the forest.
Change-Id: I481371ae487e9419af6a3a4370c552578b07d650
2022-11-09 00:31:14 +01:00
function test_bp2build_symlinks_files {
setup
mkdir -p foo
touch foo/BLANK1
touch foo/BLANK2
touch foo/F2D
touch foo/BUILD
run_soong bp2build
if [ [ -e "./out/soong/workspace/foo/BUILD" ] ] ; then
fail "./out/soong/workspace/foo/BUILD should be omitted"
fi
for file in BLANK1 BLANK2 F2D
do
if [ [ ! -L " ./out/soong/workspace/foo/ $file " ] ] ; then
fail " ./out/soong/workspace/foo/ $file should exist "
fi
done
local -r BLANK1_BEFORE = $( stat -c %y "./out/soong/workspace/foo/BLANK1" )
rm foo/BLANK2
rm foo/F2D
mkdir foo/F2D
touch foo/F2D/BUILD
run_soong bp2build
if [ [ -e "./out/soong/workspace/foo/BUILD" ] ] ; then
fail "./out/soong/workspace/foo/BUILD should be omitted"
fi
local -r BLANK1_AFTER = $( stat -c %y "./out/soong/workspace/foo/BLANK1" )
if [ [ " $BLANK1_AFTER " != " $BLANK1_BEFORE " ] ] ; then
fail "./out/soong/workspace/foo/BLANK1 should be untouched"
fi
if [ [ -e "./out/soong/workspace/foo/BLANK2" ] ] ; then
fail "./out/soong/workspace/foo/BLANK2 should be removed"
fi
if [ [ -L "./out/soong/workspace/foo/F2D" ] ] || [ [ ! -d "./out/soong/workspace/foo/F2D" ] ] ; then
fail "./out/soong/workspace/foo/F2D should be a dir"
fi
}
2022-05-02 10:13:19 +02:00
function test_cc_correctness {
setup
mkdir -p a
cat > a/Android.bp <<EOF
cc_object {
name: "qq" ,
srcs: [ "qq.cc" ] ,
bazel_module: {
bp2build_available: true,
} ,
stl: "none" ,
system_shared_libs: [ ] ,
}
EOF
cat > a/qq.cc <<EOF
#include "qq.h"
int qq( ) {
return QQ;
}
EOF
cat > a/qq.h <<EOF
#define QQ 1
EOF
run_soong bp2build
2022-11-20 00:14:48 +01:00
run_bazel build --config= android --config= bp2build --config= ci //a:qq
2022-06-09 18:22:36 +02:00
local -r output_mtime1 = $( stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
2022-05-02 10:13:19 +02:00
2022-11-20 00:14:48 +01:00
run_bazel build --config= android --config= bp2build --config= ci //a:qq
2022-06-09 18:22:36 +02:00
local -r output_mtime2 = $( stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
2022-05-02 10:13:19 +02:00
if [ [ " $output_mtime1 " != " $output_mtime2 " ] ] ; then
fail "output changed on null build"
fi
cat > a/qq.h <<EOF
#define QQ 2
EOF
2022-11-20 00:14:48 +01:00
run_bazel build --config= android --config= bp2build --config= ci //a:qq
2022-06-09 18:22:36 +02:00
local -r output_mtime3 = $( stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
2022-05-02 10:13:19 +02:00
if [ [ " $output_mtime1 " = = " $output_mtime3 " ] ] ; then
fail "output not changed when included header changed"
fi
}
2022-05-12 13:08:03 +02:00
# Regression test for the following failure during symlink forest creation:
#
# Cannot stat '/tmp/st.rr054/foo/bar/unresolved_symlink': stat /tmp/st.rr054/foo/bar/unresolved_symlink: no such file or directory
#
function test_bp2build_null_build_with_unresolved_symlink_in_source( ) {
setup
mkdir -p foo/bar
ln -s /tmp/non-existent foo/bar/unresolved_symlink
cat > foo/bar/Android.bp <<'EOF'
filegroup {
name: "fg" ,
srcs: [ "unresolved_symlink/non-existent-file.txt" ] ,
}
EOF
run_soong bp2build
dest = $( readlink -f out/soong/workspace/foo/bar/unresolved_symlink)
if [ [ " $dest " != "/tmp/non-existent" ] ] ; then
fail "expected to plant an unresolved symlink out/soong/workspace/foo/bar/unresolved_symlink that resolves to /tmp/non-existent"
fi
}
2023-01-11 04:05:24 +01:00
# Smoke test to verify api_bp2build worksapce does not contain any errors
function test_api_bp2build_empty_build( ) {
setup
run_soong api_bp2build
run_bazel build --config= android --config= api_bp2build //:empty
}
2022-12-08 07:29:21 +01:00
scan_and_run_tests