Respect package boundaries in bp2build conversion of go modules
bp2build's codegen context does not implement
BazelPathConversionContext. To reuse the utility function
transformPackagePaths, update its signature
(Also make deps of go_library unique to make the conversion resilient)
Test: go test ./bp2build
Change-Id: I126b1057d2b26bc6c7d3be2780f1b62d28323cf0
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index 9921a7f..a817386 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -302,21 +302,25 @@
// helper function to return labels for srcs used in bootstrap_go_package and bootstrap_go_binary
// this function has the following limitations which make it unsuitable for widespread use
-// 1. wildcard patterns in srcs
-// 2. package boundary violations
-// (1) is ok for go since build/blueprint does not support it. (2) _might_ be ok too.
+// - wildcard patterns in srcs
+// This is ok for go since build/blueprint does not support it.
//
// Prefer to use `BazelLabelForModuleSrc` instead
-func goSrcLabels(srcs []string, linuxSrcs, darwinSrcs []string) bazel.LabelListAttribute {
+func goSrcLabels(cfg android.Config, moduleDir string, srcs []string, linuxSrcs, darwinSrcs []string) bazel.LabelListAttribute {
labels := func(srcs []string) bazel.LabelList {
ret := []bazel.Label{}
for _, src := range srcs {
srcLabel := bazel.Label{
- Label: ":" + src, // TODO - b/284483729: Fix for possible package boundary violations
+ Label: src,
}
ret = append(ret, srcLabel)
}
- return bazel.MakeLabelList(ret)
+ // Respect package boundaries
+ return android.TransformSubpackagePaths(
+ cfg,
+ moduleDir,
+ bazel.MakeLabelList(ret),
+ )
}
ret := bazel.LabelListAttribute{}
@@ -371,8 +375,11 @@
Importpath: bazel.StringAttribute{
Value: proptools.StringPtr(g.GoPkgPath()),
},
- Srcs: goSrcLabels(g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()),
- Deps: goDepLabels(transitiveDeps, goModulesMap),
+ Srcs: goSrcLabels(ctx.Config(), ctx.ModuleDir(g), g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()),
+ Deps: goDepLabels(
+ android.FirstUniqueStrings(transitiveDeps),
+ goModulesMap,
+ ),
Target_compatible_with: targetNotCompatibleWithAndroid(),
}
@@ -444,7 +451,7 @@
transitiveDeps := transitiveGoDeps(g.Deps(), goModulesMap)
ga := goAttributes{
- Srcs: goSrcLabels(g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()),
+ Srcs: goSrcLabels(ctx.Config(), ctx.ModuleDir(g), g.Srcs(), g.LinuxSrcs(), g.DarwinSrcs()),
Deps: goDepLabels(transitiveDeps, goModulesMap),
Target_compatible_with: targetNotCompatibleWithAndroid(),
}
diff --git a/bp2build/go_conversion_test.go b/bp2build/go_conversion_test.go
index a872fc7..507fbf0 100644
--- a/bp2build/go_conversion_test.go
+++ b/bp2build/go_conversion_test.go
@@ -75,11 +75,11 @@
"deps": `["//bar:bar"]`,
"importpath": `"android/foo"`,
"srcs": `[
- ":foo1.go",
- ":foo2.go",
+ "foo1.go",
+ "foo2.go",
] + select({
- "//build/bazel/platforms/os:darwin": [":foo_darwin.go"],
- "//build/bazel/platforms/os:linux_glibc": [":foo_linux.go"],
+ "//build/bazel/platforms/os:darwin": ["foo_darwin.go"],
+ "//build/bazel/platforms/os:linux_glibc": ["foo_linux.go"],
"//conditions:default": [],
})`,
},
@@ -118,7 +118,31 @@
"//bar:bar",
"//bar:baz",
]`,
- "srcs": `[":main.go"]`,
+ "srcs": `["main.go"]`,
+ },
+ android.HostSupported,
+ )},
+ })
+}
+
+func TestConvertGoBinaryWithSrcInDifferentPackage(t *testing.T) {
+ bp := `
+blueprint_go_binary {
+ name: "foo",
+ srcs: ["subdir/main.go"],
+}
+`
+ t.Parallel()
+ runGoTests(t, Bp2buildTestCase{
+ Description: "Convert blueprint_go_binary with src in different package",
+ Blueprint: bp,
+ Filesystem: map[string]string{
+ "subdir/Android.bp": "",
+ },
+ ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_binary", "foo",
+ AttrNameToString{
+ "deps": `[]`,
+ "srcs": `["//subdir:main.go"]`,
},
android.HostSupported,
)},