From 98cb85624c44e2e6b31f55ea18e9f87163a7b788 Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Thu, 9 Mar 2023 23:05:47 +0000 Subject: [PATCH 1/2] Generate a BUILD file for every Android.bp file in api_bp2build workspace. This is necessary to solve bazel package boundary issues where the api file might exist in a different directory Test: m api_bp2build && build/bazel/bin/bazel build --config=android --config=api_bp2build //build/orchestrator/apis:system Test: multitree_build system/nothing (in multitree) Change-Id: Id64085d65a1943bdb394ea80c875db96ca373839 --- cmd/soong_build/main.go | 25 +++++++++++++++++++++++-- cmd/soong_build/queryview.go | 4 ++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go index 5c187f6cf..9b5116022 100644 --- a/cmd/soong_build/main.go +++ b/cmd/soong_build/main.go @@ -136,7 +136,7 @@ func runQueryView(queryviewDir, queryviewMarker string, ctx *android.Context) { ctx.EventHandler.Begin("queryview") defer ctx.EventHandler.End("queryview") codegenContext := bp2build.NewCodegenContext(ctx.Config(), ctx, bp2build.QueryView, topDir) - err := createBazelWorkspace(codegenContext, shared.JoinPath(topDir, queryviewDir)) + err := createBazelWorkspace(codegenContext, shared.JoinPath(topDir, queryviewDir), false) maybeQuit(err, "") touch(shared.JoinPath(topDir, queryviewMarker)) } @@ -174,7 +174,28 @@ func runApiBp2build(ctx *android.Context, extraNinjaDeps []string) string { // Run codegen to generate BUILD files codegenContext := bp2build.NewCodegenContext(ctx.Config(), ctx, bp2build.ApiBp2build, topDir) absoluteApiBp2buildDir := shared.JoinPath(topDir, cmdlineArgs.BazelApiBp2buildDir) - err := createBazelWorkspace(codegenContext, absoluteApiBp2buildDir) + // Always generate bp2build_all_srcs filegroups in api_bp2build. + // This is necessary to force each Android.bp file to create an equivalent BUILD file + // and prevent package boundray issues. + // e.g. + // Source + // f/b/Android.bp + // java_library{ + // name: "foo", + // api: "api/current.txt", + // } + // + // f/b/api/Android.bp <- will cause package boundary issues + // + // Gen + // f/b/BUILD + // java_contribution{ + // name: "foo.contribution", + // api: "//f/b/api:current.txt", + // } + // + // If we don't generate f/b/api/BUILD, foo.contribution will be unbuildable. + err := createBazelWorkspace(codegenContext, absoluteApiBp2buildDir, true) maybeQuit(err, "") ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...) diff --git a/cmd/soong_build/queryview.go b/cmd/soong_build/queryview.go index 35ae009bf..ce3218498 100644 --- a/cmd/soong_build/queryview.go +++ b/cmd/soong_build/queryview.go @@ -25,11 +25,11 @@ import ( ) // A helper function to generate a Read-only Bazel workspace in outDir -func createBazelWorkspace(ctx *bp2build.CodegenContext, outDir string) error { +func createBazelWorkspace(ctx *bp2build.CodegenContext, outDir string, generateFilegroups bool) error { os.RemoveAll(outDir) ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories()) - res, err := bp2build.GenerateBazelTargets(ctx, false) + res, err := bp2build.GenerateBazelTargets(ctx, generateFilegroups) if err != nil { panic(err) } From 68bcbb588af08cd06a147ea6830af0c02e731a9f Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Fri, 10 Mar 2023 02:32:18 +0000 Subject: [PATCH 2/2] Add an integration test for API export from another bazel package This test ensures that API export works ok if the api file exists in a different directory (precisely, package) than the *_api_contribution target. Test: tests/run_integration_tests.sh Change-Id: I1ff171b93773b514a9a081f962606f4c28abe42e --- tests/bp2build_bazel_test.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/bp2build_bazel_test.sh b/tests/bp2build_bazel_test.sh index 1ff1b5bca..68d7f8d19 100755 --- a/tests/bp2build_bazel_test.sh +++ b/tests/bp2build_bazel_test.sh @@ -343,4 +343,29 @@ function test_api_bp2build_empty_build() { run_bazel build --config=android --config=api_bp2build //:empty } +# Verify that an *_api_contribution target can refer to an api file from +# another Bazel package. +function test_api_export_from_another_bazel_package() { + setup + # Parent dir Android.bp + mkdir -p foo + cat > foo/Android.bp << 'EOF' +cc_library { + name: "libfoo", + stubs: { + symbol_file: "api/libfoo.map.txt", + }, +} +EOF + # Child dir Android.bp + mkdir -p foo/api + cat > foo/api/Android.bp << 'EOF' +package{} +EOF + touch foo/api/libfoo.map.txt + # Run test + run_soong api_bp2build + run_bazel build --config=android --config=api_bp2build //foo:libfoo.contribution +} + scan_and_run_tests