From f038cde8df5623a11c0a198378650684e7388080 Mon Sep 17 00:00:00 2001 From: Zi Wang Date: Wed, 13 Sep 2023 10:06:54 -0700 Subject: [PATCH 1/2] Do not append deps to exports when java_library has only proto srcs This is to address the comment in aosp/2748099 Test: CI Bug: 285952385 Change-Id: I0bc8c76e2bc9ee113ced8642e0ab93a95799211c --- bp2build/java_proto_conversion_test.go | 5 +---- java/java.go | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/bp2build/java_proto_conversion_test.go b/bp2build/java_proto_conversion_test.go index dfef6973c..b25471048 100644 --- a/bp2build/java_proto_conversion_test.go +++ b/bp2build/java_proto_conversion_test.go @@ -167,10 +167,7 @@ java_library_static { "sdk_version": `"current"`, }), MakeBazelTarget("java_library", "java-protos", AttrNameToString{ - "exports": `[ - ":java-protos_java_proto_lite", - ":java-lib-neverlink", - ]`, + "exports": `[":java-protos_java_proto_lite"]`, "java_version": `"7"`, "sdk_version": `"current"`, }), diff --git a/java/java.go b/java/java.go index 270f45615..3567dbead 100644 --- a/java/java.go +++ b/java/java.go @@ -3143,9 +3143,7 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { // 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) + // being propagated to any dependencies. So we can drop deps here. deps = bazel.LabelListAttribute{} } else { ctx.ModuleErrorf("Module has direct dependencies but no sources. Bazel will not allow this.") From dd93636f1e642f75f334d178a13a665f11080900 Mon Sep 17 00:00:00 2001 From: Zi Wang Date: Fri, 15 Sep 2023 09:46:17 -0700 Subject: [PATCH 2/2] Always drop deps from java_library with no srcs This is the follow up from aosp/2750763 Test: CI and added unit test Bug: 285952385 Change-Id: I64703ee4fa742b718ffb43f2af1b6ab43ddeb606 --- bp2build/java_library_conversion_test.go | 45 +++++++++++++----------- java/java.go | 30 ++++++---------- 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/bp2build/java_library_conversion_test.go b/bp2build/java_library_conversion_test.go index 8c782172e..3260b1e9f 100644 --- a/bp2build/java_library_conversion_test.go +++ b/bp2build/java_library_conversion_test.go @@ -15,7 +15,6 @@ package bp2build import ( - "fmt" "testing" "android/soong/android" @@ -143,26 +142,6 @@ java_library { }) } -func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) { - runJavaLibraryTestCase(t, Bp2buildTestCase{ - ExpectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."), - Blueprint: `java_library { - name: "java-lib-1", - libs: ["java-lib-2"], - sdk_version: "current", - bazel_module: { bp2build_available: true }, -} - -java_library { - name: "java-lib-2", - srcs: ["a.java"], - sdk_version: "current", - bazel_module: { bp2build_available: false }, -}`, - ExpectedBazelTargets: []string{}, - }) -} - func TestJavaLibraryPlugins(t *testing.T) { runJavaLibraryTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{ Blueprint: `java_library { @@ -869,6 +848,30 @@ func TestJavaLibraryKotlinCommonSrcs(t *testing.T) { }) } +func TestJavaLibraryLibsWithNoSrcs(t *testing.T) { + runJavaLibraryTestCase(t, Bp2buildTestCase{ + Description: "java_library that has libs but no srcs", + Blueprint: `java_library { + name: "java-lib-1", + libs: ["java-lib-2"], + sdk_version: "current", + bazel_module: { bp2build_available: true }, +} + +java_library{ + name: "java-lib-2", + bazel_module: { bp2build_available: false }, +} +`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("java_library", "java-lib-1", AttrNameToString{ + "sdk_version": `"current"`, + }), + MakeNeverlinkDuplicateTarget("java_library", "java-lib-1"), + }, + }) +} + func TestJavaLibraryArchVariantDeps(t *testing.T) { runJavaLibraryTestCase(t, Bp2buildTestCase{ Description: "java_library with arch variant libs", diff --git a/java/java.go b/java/java.go index 3567dbead..48e13feb8 100644 --- a/java/java.go +++ b/java/java.go @@ -2886,9 +2886,8 @@ type javaAidlLibraryAttributes struct { // depending on the module type. type bp2BuildJavaInfo struct { // separates dependencies into dynamic dependencies and static dependencies. - DepLabels *javaDependencyLabels - hasKotlin bool - onlyProtoInSrcs bool + DepLabels *javaDependencyLabels + hasKotlin bool } func javaXsdTargetName(xsd android.XsdConfigBp2buildTargets) string { @@ -2951,9 +2950,6 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) staticDeps.Append(srcPartitions[xsdSrcPartition]) - _, protoInSrcs := srcPartitions[protoSrcPartition] - onlyProtoInSrcs := protoInSrcs && len(srcPartitions) == 1 - if !srcPartitions[logtagSrcPartition].IsEmpty() { logtagsLibName := m.Name() + "_logtags" ctx.CreateBazelTargetModule( @@ -3091,9 +3087,8 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) } bp2BuildInfo := &bp2BuildJavaInfo{ - DepLabels: depLabels, - hasKotlin: hasKotlin, - onlyProtoInSrcs: onlyProtoInSrcs, + DepLabels: depLabels, + hasKotlin: hasKotlin, } return commonAttrs, bp2BuildInfo, true @@ -3137,17 +3132,12 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { if !commonAttrs.Srcs.IsEmpty() { deps.Append(exports) // we should only append these if there are sources to use them } 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 drop deps here. - deps = bazel.LabelListAttribute{} - } else { - ctx.ModuleErrorf("Module has direct dependencies but no sources. Bazel will not allow this.") - } + // java_library does not accept deps when there are no srcs because + // there is no compilation happening, but it accepts exports. + // The non-empty deps here are unnecessary as deps on the java_library + // since they aren't being propagated to any dependencies. + // So we can drop deps here. + deps = bazel.LabelListAttribute{} } var props bazel.BazelTargetModuleProperties attrs := &javaLibraryAttributes{