Bp2build Java libs for java_binary -> java_import edge

Since Bazel's java_import requires a jars attribute to be specified,
the generated neverlink-duplicated module is of type java_library

Change-Id: I14a866dfc583507a9462add50d95060cbfe540c5
Bug: 244210934
Test: m bp2build, go test ./bp2build, manual inspection of generated Build and jar files
This commit is contained in:
Alix
2022-09-27 15:36:01 +00:00
parent 9b019134c2
commit b4e09a0ada
4 changed files with 99 additions and 6 deletions

View File

@@ -272,6 +272,9 @@ var (
"prebuilts/tools": Bp2BuildDefaultTrue, "prebuilts/tools": Bp2BuildDefaultTrue,
"prebuilts/tools/common/m2": Bp2BuildDefaultTrue, "prebuilts/tools/common/m2": Bp2BuildDefaultTrue,
"sdk/eventanalyzer": Bp2BuildDefaultTrue,
"sdk/dumpeventlog": Bp2BuildDefaultTrue,
"system/apex": Bp2BuildDefaultFalse, // TODO(b/207466993): flaky failures "system/apex": Bp2BuildDefaultFalse, // TODO(b/207466993): flaky failures
"system/apex/apexer": Bp2BuildDefaultTrue, "system/apex/apexer": Bp2BuildDefaultTrue,
"system/apex/libs": Bp2BuildDefaultTrueRecursively, "system/apex/libs": Bp2BuildDefaultTrueRecursively,

View File

@@ -29,6 +29,7 @@ func runJavaBinaryHostTestCase(t *testing.T, tc Bp2buildTestCase) {
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
ctx.RegisterModuleType("cc_library_host_shared", cc.LibraryHostSharedFactory) ctx.RegisterModuleType("cc_library_host_shared", cc.LibraryHostSharedFactory)
ctx.RegisterModuleType("java_library", java.LibraryFactory) ctx.RegisterModuleType("java_library", java.LibraryFactory)
ctx.RegisterModuleType("java_import_host", java.ImportFactory)
}, tc) }, tc)
} }
@@ -102,3 +103,34 @@ java_library {
}, },
}) })
} }
func TestJavaBinaryHostLibs(t *testing.T) {
runJavaBinaryHostTestCase(t, Bp2buildTestCase{
Description: "java_binary_host with srcs, libs.",
Filesystem: fs,
Blueprint: `java_binary_host {
name: "java-binary-host-libs",
libs: ["java-lib-dep-1"],
manifest: "test.mf",
srcs: ["a.java"],
}
java_import_host{
name: "java-lib-dep-1",
jars: ["foo.jar"],
bazel_module: { bp2build_available: false },
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("java_binary", "java-binary-host-libs", AttrNameToString{
"main_class": `"com.android.test.MainClass"`,
"srcs": `["a.java"]`,
"deps": `[":java-lib-dep-1-neverlink"]`,
"target_compatible_with": `select({
"//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
"//conditions:default": [],
})`,
}),
},
})
}

View File

@@ -48,6 +48,10 @@ java_import {
MakeBazelTarget("java_import", "example_import", AttrNameToString{ MakeBazelTarget("java_import", "example_import", AttrNameToString{
"jars": `["import.jar"]`, "jars": `["import.jar"]`,
}), }),
MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
"exports": `[":example_import"]`,
"neverlink": `True`,
}),
}}) }})
} }
@@ -81,5 +85,35 @@ java_import {
"//conditions:default": [], "//conditions:default": [],
})`, })`,
}), }),
MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
"exports": `[":example_import"]`,
"neverlink": `True`,
}),
}})
}
func TestJavaImportHost(t *testing.T) {
runJavaImportTestCase(t, Bp2buildTestCase{
Description: "Java import host- simple example",
ModuleTypeUnderTest: "java_import_host",
ModuleTypeUnderTestFactory: java.ImportFactory,
Filesystem: map[string]string{
"import.jar": "",
},
Blueprint: `
java_import_host {
name: "example_import",
jars: ["import.jar"],
bazel_module: { bp2build_available: true },
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("java_import", "example_import", AttrNameToString{
"jars": `["import.jar"]`,
}),
MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
"exports": `[":example_import"]`,
"neverlink": `True`,
}),
}}) }})
} }

View File

@@ -2384,7 +2384,18 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
} }
if m.properties.Libs != nil { if m.properties.Libs != nil {
deps.Append(android.BazelLabelForModuleDeps(ctx, android.LastUniqueStrings(android.CopyOf(m.properties.Libs))))
// TODO 244210934 ALIX Check if this else statement breaks presubmits get rid of it if it doesn't
if strings.HasPrefix(ctx.ModuleType(), "java_binary") {
for _, d := range m.properties.Libs {
neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d)
neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink"
deps.Add(&neverlinkLabel)
}
} else {
deps.Append(android.BazelLabelForModuleDeps(ctx, android.LastUniqueStrings(android.CopyOf(m.properties.Libs))))
}
} }
if m.properties.Static_libs != nil { if m.properties.Static_libs != nil {
@@ -2409,8 +2420,9 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
type javaLibraryAttributes struct { type javaLibraryAttributes struct {
*javaCommonAttributes *javaCommonAttributes
Deps bazel.LabelListAttribute Deps bazel.LabelListAttribute
Exports bazel.LabelListAttribute Exports bazel.LabelListAttribute
Neverlink bazel.BoolAttribute
} }
func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
@@ -2440,7 +2452,8 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
Bzl_load_location: "//build/bazel/rules/java:library.bzl", Bzl_load_location: "//build/bazel/rules/java:library.bzl",
} }
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) name := m.Name()
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
} }
type javaBinaryHostAttributes struct { type javaBinaryHostAttributes struct {
@@ -2522,7 +2535,8 @@ func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
} }
type bazelJavaImportAttributes struct { type bazelJavaImportAttributes struct {
Jars bazel.LabelListAttribute Jars bazel.LabelListAttribute
Exports bazel.LabelListAttribute
} }
// java_import bp2Build converter. // java_import bp2Build converter.
@@ -2543,7 +2557,17 @@ func (i *Import) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
} }
props := bazel.BazelTargetModuleProperties{Rule_class: "java_import"} props := bazel.BazelTargetModuleProperties{Rule_class: "java_import"}
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: android.RemoveOptionalPrebuiltPrefix(i.Name())}, attrs) name := android.RemoveOptionalPrebuiltPrefix(i.Name())
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
neverlink := true
neverlinkAttrs := &javaLibraryAttributes{
Neverlink: bazel.BoolAttribute{Value: &neverlink},
Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}),
}
ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{Rule_class: "java_library"}, android.CommonAttributes{Name: name + "-neverlink"}, neverlinkAttrs)
} }
var _ android.MixedBuildBuildable = (*Import)(nil) var _ android.MixedBuildBuildable = (*Import)(nil)