Merge "Bp2build: handle the case where java_library has only proto srcs" into main am: c90e1ec784

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2748099

Change-Id: I61e88db98415997ea27b0d590e65f36dc8f1e153
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Zi Wang
2023-09-13 01:19:40 +00:00
committed by Automerger Merge Worker
2 changed files with 72 additions and 7 deletions

View File

@@ -137,3 +137,50 @@ func TestJavaProtoDefault(t *testing.T) {
}, },
}) })
} }
func TestJavaLibsAndOnlyProtoSrcs(t *testing.T) {
runJavaProtoTestCase(t, Bp2buildTestCase{
Description: "java_library that has only proto srcs",
Blueprint: `java_library_static {
name: "java-protos",
srcs: ["a.proto"],
libs: ["java-lib"],
java_version: "7",
sdk_version: "current",
}
java_library_static {
name: "java-lib",
bazel_module: { bp2build_available: false },
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("proto_library", "java-protos_proto", AttrNameToString{
"srcs": `["a.proto"]`,
}),
MakeBazelTarget(
"java_lite_proto_library",
"java-protos_java_proto_lite",
AttrNameToString{
"deps": `[":java-protos_proto"]`,
"java_version": `"7"`,
"sdk_version": `"current"`,
}),
MakeBazelTarget("java_library", "java-protos", AttrNameToString{
"exports": `[
":java-protos_java_proto_lite",
":java-lib-neverlink",
]`,
"java_version": `"7"`,
"sdk_version": `"current"`,
}),
MakeNeverlinkDuplicateTargetWithAttrs(
"java_library",
"java-protos",
AttrNameToString{
"java_version": `"7"`,
"sdk_version": `"current"`,
}),
},
})
}

View File

@@ -2887,6 +2887,7 @@ type bp2BuildJavaInfo struct {
// separates dependencies into dynamic dependencies and static dependencies. // separates dependencies into dynamic dependencies and static dependencies.
DepLabels *javaDependencyLabels DepLabels *javaDependencyLabels
hasKotlin bool hasKotlin bool
onlyProtoInSrcs bool
} }
func javaXsdTargetName(xsd android.XsdConfigBp2buildTargets) string { func javaXsdTargetName(xsd android.XsdConfigBp2buildTargets) string {
@@ -2949,6 +2950,9 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
staticDeps.Append(srcPartitions[xsdSrcPartition]) staticDeps.Append(srcPartitions[xsdSrcPartition])
_, protoInSrcs := srcPartitions[protoSrcPartition]
onlyProtoInSrcs := protoInSrcs && len(srcPartitions) == 1
if !srcPartitions[logtagSrcPartition].IsEmpty() { if !srcPartitions[logtagSrcPartition].IsEmpty() {
logtagsLibName := m.Name() + "_logtags" logtagsLibName := m.Name() + "_logtags"
ctx.CreateBazelTargetModule( ctx.CreateBazelTargetModule(
@@ -3088,6 +3092,7 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
bp2BuildInfo := &bp2BuildJavaInfo{ bp2BuildInfo := &bp2BuildJavaInfo{
DepLabels: depLabels, DepLabels: depLabels,
hasKotlin: hasKotlin, hasKotlin: hasKotlin,
onlyProtoInSrcs: onlyProtoInSrcs,
} }
return commonAttrs, bp2BuildInfo, true return commonAttrs, bp2BuildInfo, true
@@ -3127,16 +3132,29 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
depLabels := bp2BuildInfo.DepLabels depLabels := bp2BuildInfo.DepLabels
deps := depLabels.Deps deps := depLabels.Deps
exports := depLabels.StaticDeps
if !commonAttrs.Srcs.IsEmpty() { if !commonAttrs.Srcs.IsEmpty() {
deps.Append(depLabels.StaticDeps) // we should only append these if there are sources to use them deps.Append(exports) // we should only append these if there are sources to use them
} else if !deps.IsEmpty() { } else if !deps.IsEmpty() {
if bp2BuildInfo.onlyProtoInSrcs {
// java_library does not accept deps when there are no srcs because
// there is no compilation happening, but it accepts exports.
// bp2build converts this module to 2 java_libraries + java_xx_proto_library + proto_library
// the non-empty deps here are not necessary for compiling the protos, in which case
// they're unnecessary as deps on the java_library as well since they aren't
// being propagated to any dependencies.
// so we can put the deps to exports and drop deps here.
exports.Append(deps)
deps = bazel.LabelListAttribute{}
} else {
ctx.ModuleErrorf("Module has direct dependencies but no sources. Bazel will not allow this.") ctx.ModuleErrorf("Module has direct dependencies but no sources. Bazel will not allow this.")
} }
}
var props bazel.BazelTargetModuleProperties var props bazel.BazelTargetModuleProperties
attrs := &javaLibraryAttributes{ attrs := &javaLibraryAttributes{
javaCommonAttributes: commonAttrs, javaCommonAttributes: commonAttrs,
Deps: deps, Deps: deps,
Exports: depLabels.StaticDeps, Exports: exports,
} }
name := m.Name() name := m.Name()