Partial bp2build conversion of bootstratp_go_package
This module type does not implement android.Module, and therefore we
cannot write a conventional bp2build converter for this module type.
Instead this has been special-cased inside bp2build/build_conversion.go.
Because of the above, we also do not have access to useful functions
available in the ctx object of ConvertWithBp2build. This includes
1. Finding the package (directory) of a dep. This requires getting a
handle of the underlying module from its name (string). To solve, we
do a pre-visit to collect this information. This did not increase the
wall time. On my machine, `m bp2build --skip-soong-tests` takes ~14s
before and after this CL
2. Converting srcs to labels. This requires glob and package boundary
resolution. This CL introduces a partial implementation for this
function. (glob patterns are not used in go tools)
For (1), I considered creating a `ModuleFromName` on
`blueprint.Context` instead of a pre-run, but this increased the time to ~27s.
Test: unit tests
Test: TH
Bug: 284483729
Change-Id: Ifeb029103d14947352556dba295591dd7038b090
diff --git a/bp2build/go_conversion_test.go b/bp2build/go_conversion_test.go
new file mode 100644
index 0000000..13d2513
--- /dev/null
+++ b/bp2build/go_conversion_test.go
@@ -0,0 +1,89 @@
+// Copyright 2023 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package bp2build
+
+import (
+ "testing"
+
+ "github.com/google/blueprint/bootstrap"
+
+ "android/soong/android"
+)
+
+func runGoTests(t *testing.T, tc Bp2buildTestCase) {
+ RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
+ tCtx := ctx.(*android.TestContext)
+ bootstrap.RegisterGoModuleTypes(tCtx.Context.Context) // android.TestContext --> android.Context --> blueprint.Context
+ }, tc)
+}
+
+func TestConvertGoPackage(t *testing.T) {
+ bp := `
+bootstrap_go_package {
+ name: "foo",
+ pkgPath: "android/foo",
+ deps: [
+ "bar",
+ ],
+ srcs: [
+ "foo1.go",
+ "foo2.go",
+ ],
+ linux: {
+ srcs: [
+ "foo_linux.go",
+ ],
+ },
+ darwin: {
+ srcs: [
+ "foo_darwin.go",
+ ],
+ },
+ testSrcs: [
+ "foo1_test.go",
+ "foo2_test.go",
+ ],
+}
+`
+ depBp := `
+bootstrap_go_package {
+ name: "bar",
+}
+`
+ t.Parallel()
+ runGoTests(t, Bp2buildTestCase{
+ Description: "Convert bootstrap_go_package to go_library",
+ ModuleTypeUnderTest: "bootrstap_go_package",
+ Blueprint: bp,
+ Filesystem: map[string]string{
+ "bar/Android.bp": depBp, // Put dep in Android.bp to reduce boilerplate in ExpectedBazelTargets
+ },
+ ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_library", "foo",
+ AttrNameToString{
+ "deps": `["//bar:bar"]`,
+ "importpath": `"android/foo"`,
+ "srcs": `[
+ ":foo1.go",
+ ":foo2.go",
+ ] + select({
+ "//build/bazel/platforms/os:darwin": [":foo_darwin.go"],
+ "//build/bazel/platforms/os:linux_glibc": [":foo_linux.go"],
+ "//conditions:default": [],
+ })`,
+ },
+ android.HostSupported,
+ )},
+ })
+}