Skip to content

Commit

Permalink
Have bp2build generate BUILD.bazel instead of generating BUILD.
Browse files Browse the repository at this point in the history
Test: TH
Change-Id: I465d29da96dd77c432890a38a56203e291b8ceed
  • Loading branch information
rupertks committed May 18, 2021
1 parent f1d8819 commit 413a7a9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
5 changes: 4 additions & 1 deletion bp2build/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ var (
// be preferred for use within a Bazel build.

// The file name used for automatically generated files.
GeneratedBuildFileName = "BUILD"
GeneratedBuildFileName = "BUILD.bazel"

// The file name used for hand-crafted build targets.
// NOTE: It is okay that this matches GeneratedBuildFileName, since we generate BUILD files in a different directory to source files
// FIXME: Because there are hundreds of existing BUILD.bazel files in the AOSP tree, we should pick another name here, like BUILD.android
HandcraftedBuildFileName = "BUILD.bazel"
)
6 changes: 3 additions & 3 deletions bp2build/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
expectedFilePaths := []bazelFilepath{
{
dir: "",
basename: "BUILD",
basename: "BUILD.bazel",
},
{
dir: "",
basename: "WORKSPACE",
},
{
dir: bazelRulesSubDir,
basename: "BUILD",
basename: "BUILD.bazel",
},
{
dir: bazelRulesSubDir,
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {

if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
} else if actualFile.Basename == "BUILD" || actualFile.Basename == "WORKSPACE" {
} else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" {
if actualFile.Contents != "" {
t.Errorf("Expected %s to have no content.", actualFile)
}
Expand Down
34 changes: 18 additions & 16 deletions tests/bootstrap_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ set -o pipefail

source "$(dirname "$0")/lib.sh"

readonly GENERATED_BUILD_FILE_NAME="BUILD.bazel"

function test_smoke {
setup
run_soong
Expand Down Expand Up @@ -505,8 +507,8 @@ filegroup {
EOF

GENERATE_BAZEL_FILES=1 run_soong
[[ -e out/soong/bp2build/a/BUILD ]] || fail "a/BUILD not created"
[[ -L out/soong/workspace/a/BUILD ]] || fail "a/BUILD not symlinked"
[[ -e out/soong/bp2build/a/${GENERATED_BUILD_FILE_NAME} ]] || fail "a/${GENERATED_BUILD_FILE_NAME} not created"
[[ -L out/soong/workspace/a/${GENERATED_BUILD_FILE_NAME} ]] || fail "a/${GENERATED_BUILD_FILE_NAME} not symlinked"

mkdir -p b
touch b/b.txt
Expand All @@ -519,8 +521,8 @@ filegroup {
EOF

GENERATE_BAZEL_FILES=1 run_soong
[[ -e out/soong/bp2build/b/BUILD ]] || fail "a/BUILD not created"
[[ -L out/soong/workspace/b/BUILD ]] || fail "a/BUILD not symlinked"
[[ -e out/soong/bp2build/b/${GENERATED_BUILD_FILE_NAME} ]] || fail "a/${GENERATED_BUILD_FILE_NAME} not created"
[[ -L out/soong/workspace/b/${GENERATED_BUILD_FILE_NAME} ]] || fail "a/${GENERATED_BUILD_FILE_NAME} not symlinked"
}

function test_bp2build_null_build {
Expand Down Expand Up @@ -551,11 +553,11 @@ filegroup {
EOF

GENERATE_BAZEL_FILES=1 run_soong
grep -q a1.txt out/soong/bp2build/a/BUILD || fail "a1.txt not in BUILD file"
grep -q a1.txt "out/soong/bp2build/a/${GENERATED_BUILD_FILE_NAME}" || fail "a1.txt not in ${GENERATED_BUILD_FILE_NAME} file"

touch a/a2.txt
GENERATE_BAZEL_FILES=1 run_soong
grep -q a2.txt out/soong/bp2build/a/BUILD || fail "a2.txt not in BUILD file"
grep -q a2.txt "out/soong/bp2build/a/${GENERATED_BUILD_FILE_NAME}" || fail "a2.txt not in ${GENERATED_BUILD_FILE_NAME} file"
}

function test_dump_json_module_graph() {
Expand Down Expand Up @@ -583,8 +585,8 @@ EOF
GENERATE_BAZEL_FILES=1 run_soong
[[ -e out/soong/workspace ]] || fail "Bazel workspace not created"
[[ -d out/soong/workspace/a/b ]] || fail "module directory not a directory"
[[ -L out/soong/workspace/a/b/BUILD ]] || fail "BUILD file not symlinked"
[[ "$(readlink -f out/soong/workspace/a/b/BUILD)" =~ bp2build/a/b/BUILD$ ]] \
[[ -L "out/soong/workspace/a/b/${GENERATED_BUILD_FILE_NAME}" ]] || fail "${GENERATED_BUILD_FILE_NAME} file not symlinked"
[[ "$(readlink -f out/soong/workspace/a/b/${GENERATED_BUILD_FILE_NAME})" =~ "bp2build/a/b/${GENERATED_BUILD_FILE_NAME}"$ ]] \
|| fail "BUILD files symlinked at the wrong place"
[[ -L out/soong/workspace/a/b/b.txt ]] || fail "a/b/b.txt not symlinked"
[[ -L out/soong/workspace/a/a.txt ]] || fail "a/b/a.txt not symlinked"
Expand Down Expand Up @@ -616,7 +618,7 @@ function test_bp2build_build_file_precedence {

mkdir -p a
touch a/a.txt
touch a/BUILD
touch a/${GENERATED_BUILD_FILE_NAME}
cat > a/Android.bp <<EOF
filegroup {
name: "a",
Expand All @@ -626,15 +628,15 @@ filegroup {
EOF

GENERATE_BAZEL_FILES=1 run_soong
[[ -L out/soong/workspace/a/BUILD ]] || fail "BUILD file not symlinked"
[[ "$(readlink -f out/soong/workspace/a/BUILD)" =~ bp2build/a/BUILD$ ]] \
|| fail "BUILD files symlinked to the wrong place"
[[ -L "out/soong/workspace/a/${GENERATED_BUILD_FILE_NAME}" ]] || fail "${GENERATED_BUILD_FILE_NAME} file not symlinked"
[[ "$(readlink -f out/soong/workspace/a/${GENERATED_BUILD_FILE_NAME})" =~ "bp2build/a/${GENERATED_BUILD_FILE_NAME}"$ ]] \
|| fail "${GENERATED_BUILD_FILE_NAME} files symlinked to the wrong place"
}

function test_bp2build_reports_multiple_errors {
setup

mkdir -p a/BUILD
mkdir -p "a/${GENERATED_BUILD_FILE_NAME}"
touch a/a.txt
cat > a/Android.bp <<EOF
filegroup {
Expand All @@ -644,7 +646,7 @@ filegroup {
}
EOF

mkdir -p b/BUILD
mkdir -p "b/${GENERATED_BUILD_FILE_NAME}"
touch b/b.txt
cat > b/Android.bp <<EOF
filegroup {
Expand All @@ -658,8 +660,8 @@ EOF
fail "Build should have failed"
fi

grep -q "a/BUILD' exist" "$MOCK_TOP/errors" || fail "Error for a/BUILD not found"
grep -q "b/BUILD' exist" "$MOCK_TOP/errors" || fail "Error for b/BUILD not found"
grep -q "a/${GENERATED_BUILD_FILE_NAME}' exist" "$MOCK_TOP/errors" || fail "Error for a/${GENERATED_BUILD_FILE_NAME} not found"
grep -q "b/${GENERATED_BUILD_FILE_NAME}' exist" "$MOCK_TOP/errors" || fail "Error for b/${GENERATED_BUILD_FILE_NAME} not found"
}

test_smoke
Expand Down
22 changes: 12 additions & 10 deletions tests/bp2build_bazel_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ set -o pipefail

source "$(dirname "$0")/lib.sh"

readonly GENERATED_BUILD_FILE_NAME="BUILD.bazel"

function test_bp2build_generates_all_buildfiles {
setup
create_mock_bazel
Expand Down Expand Up @@ -40,24 +42,24 @@ EOF

run_bp2build

if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/BUILD" ]]; then
fail "./out/soong/workspace/foo/convertible_soong_module/BUILD was not generated"
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"
fi

if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/BUILD" ]]; then
fail "./out/soong/workspace/foo/unconvertible_soong_module/BUILD was not generated"
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"
fi

if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/BUILD"; then
fail "missing BUILD target the_answer in convertible_soong_module/BUILD"
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}"
fi

if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/BUILD"; then
fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/BUILD"
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}"
fi

if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/BUILD"; then
fail "missing filegroup in unconvertible_soong_module/BUILD"
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}"
fi

# NOTE: We don't actually use the extra BUILD file for anything here
Expand Down

0 comments on commit 413a7a9

Please sign in to comment.