bp2build for java_library with .kt srcs or common_srcs

java_library modules with .kt srcs or common_srcs will be converted into
module type kt_jvm_library

Test: m bp2build, manually inspected build files for allowlisted modules
Bug: 258688914
Change-Id: I8293a11c8247b4b76358d0991f82c6b61b58adc3
This commit is contained in:
Alix
2022-11-14 21:38:07 +00:00
parent 877336cc3a
commit 8062f4dee7
6 changed files with 99 additions and 17 deletions

View File

@@ -647,6 +647,10 @@ var (
"libcodec2_soft_avcenc", "libcodec2_soft_avcenc",
"libcodec2_soft_aacdec", "libcodec2_soft_aacdec",
"libcodec2_soft_common", "libcodec2_soft_common",
// kotlin srcs in java libs
"CtsPkgInstallerConstants",
"kotlinx_atomicfu",
} }
Bp2buildModuleTypeAlwaysConvertList = []string{ Bp2buildModuleTypeAlwaysConvertList = []string{

View File

@@ -649,3 +649,46 @@ android_library {
}), }),
}}) }})
} }
func TestJavaLibraryKotlinSrcs(t *testing.T) {
runJavaLibraryTestCase(t, Bp2buildTestCase{
Description: "java_library with kotlin srcs",
Blueprint: `java_library {
name: "java-lib-1",
srcs: ["a.java", "b.java", "c.kt"],
bazel_module: { bp2build_available: true },
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("kt_jvm_library", "java-lib-1", AttrNameToString{
"srcs": `[
"a.java",
"b.java",
"c.kt",
]`,
}),
},
})
}
func TestJavaLibraryKotlinCommonSrcs(t *testing.T) {
runJavaLibraryTestCase(t, Bp2buildTestCase{
Description: "java_library with kotlin common_srcs",
Blueprint: `java_library {
name: "java-lib-1",
srcs: ["a.java", "b.java"],
common_srcs: ["c.kt"],
bazel_module: { bp2build_available: true },
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("kt_jvm_library", "java-lib-1", AttrNameToString{
"srcs": `[
"a.java",
"b.java",
]`,
"common_srcs": `["c.kt"]`,
}),
},
})
}

View File

@@ -1046,7 +1046,8 @@ func (a *AARImport) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
} }
func (a *AndroidLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) { func (a *AndroidLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
commonAttrs, depLabels := a.convertLibraryAttrsBp2Build(ctx) commonAttrs, bp2buildInfo := a.convertLibraryAttrsBp2Build(ctx)
depLabels := bp2buildInfo.DepLabels
deps := depLabels.Deps deps := depLabels.Deps
if !commonAttrs.Srcs.IsEmpty() { if !commonAttrs.Srcs.IsEmpty() {

View File

@@ -1492,7 +1492,8 @@ type bazelAndroidAppAttributes struct {
// ConvertWithBp2build is used to convert android_app to Bazel. // ConvertWithBp2build is used to convert android_app to Bazel.
func (a *AndroidApp) ConvertWithBp2build(ctx android.TopDownMutatorContext) { func (a *AndroidApp) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
commonAttrs, depLabels := a.convertLibraryAttrsBp2Build(ctx) commonAttrs, bp2BuildInfo := a.convertLibraryAttrsBp2Build(ctx)
depLabels := bp2BuildInfo.DepLabels
deps := depLabels.Deps deps := depLabels.Deps
deps.Append(depLabels.StaticDeps) deps.Append(depLabels.StaticDeps)

View File

@@ -2307,11 +2307,21 @@ type javaAidlLibraryAttributes struct {
Deps bazel.LabelListAttribute Deps bazel.LabelListAttribute
} }
// convertLibraryAttrsBp2Build converts a few shared attributes from java_* modules // bp2BuildJavaInfo has information needed for the conversion of java*_modules
// and also separates dependencies into dynamic dependencies and static dependencies. // that is needed bor Bp2Build conversion but that requires different handling
// Each corresponding Bazel target type, can have a different method for handling // depending on the module type.
// dynamic vs. static dependencies, and so these are returned to the calling function. type bp2BuildJavaInfo struct {
func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) (*javaCommonAttributes, *javaDependencyLabels) { // separates dependencies into dynamic dependencies and static dependencies.
DepLabels *javaDependencyLabels
hasKotlinSrcs bool
}
// convertLibraryAttrsBp2Build returns a javaCommonAttributes struct with
// converted attributes shared across java_* modules and a bp2BuildJavaInfo struct
// which has other non-attribute information needed for bp2build conversion
// that needs different handling depending on the module types, and thus needs
// to be returned to the calling function.
func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) (*javaCommonAttributes, *bp2BuildJavaInfo) {
var srcs bazel.LabelListAttribute var srcs bazel.LabelListAttribute
var deps bazel.LabelList var deps bazel.LabelList
var staticDeps bazel.LabelList var staticDeps bazel.LabelList
@@ -2330,14 +2340,18 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
protoSrcPartition := "proto" protoSrcPartition := "proto"
logtagSrcPartition := "logtag" logtagSrcPartition := "logtag"
aidlSrcPartition := "aidl" aidlSrcPartition := "aidl"
kotlinPartition := "kotlin"
srcPartitions := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{ srcPartitions := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{
javaSrcPartition: bazel.LabelPartition{Extensions: []string{".java"}, Keep_remainder: true}, javaSrcPartition: bazel.LabelPartition{Extensions: []string{".java"}, Keep_remainder: true},
logtagSrcPartition: bazel.LabelPartition{Extensions: []string{".logtags", ".logtag"}}, logtagSrcPartition: bazel.LabelPartition{Extensions: []string{".logtags", ".logtag"}},
protoSrcPartition: android.ProtoSrcLabelPartition, protoSrcPartition: android.ProtoSrcLabelPartition,
aidlSrcPartition: android.AidlSrcLabelPartition, aidlSrcPartition: android.AidlSrcLabelPartition,
kotlinPartition: bazel.LabelPartition{Extensions: []string{".kt"}},
}) })
javaSrcs := srcPartitions[javaSrcPartition] javaSrcs := srcPartitions[javaSrcPartition]
kotlinSrcs := srcPartitions[kotlinPartition]
javaSrcs.Append(kotlinSrcs)
if !srcPartitions[logtagSrcPartition].IsEmpty() { if !srcPartitions[logtagSrcPartition].IsEmpty() {
logtagsLibName := m.Name() + "_logtags" logtagsLibName := m.Name() + "_logtags"
@@ -2447,18 +2461,25 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
depLabels.Deps = bazel.MakeLabelListAttribute(deps) depLabels.Deps = bazel.MakeLabelListAttribute(deps)
depLabels.StaticDeps = bazel.MakeLabelListAttribute(staticDeps) depLabels.StaticDeps = bazel.MakeLabelListAttribute(staticDeps)
return commonAttrs, depLabels bp2BuildInfo := &bp2BuildJavaInfo{
DepLabels: depLabels,
hasKotlinSrcs: !kotlinSrcs.IsEmpty(),
}
return commonAttrs, bp2BuildInfo
} }
type javaLibraryAttributes struct { type javaLibraryAttributes struct {
*javaCommonAttributes *javaCommonAttributes
Deps bazel.LabelListAttribute Deps bazel.LabelListAttribute
Exports bazel.LabelListAttribute Exports bazel.LabelListAttribute
Neverlink bazel.BoolAttribute Neverlink bazel.BoolAttribute
Common_srcs bazel.LabelListAttribute
} }
func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
commonAttrs, depLabels := m.convertLibraryAttrsBp2Build(ctx) commonAttrs, bp2BuildInfo := m.convertLibraryAttrsBp2Build(ctx)
depLabels := bp2BuildInfo.DepLabels
deps := depLabels.Deps deps := depLabels.Deps
if !commonAttrs.Srcs.IsEmpty() { if !commonAttrs.Srcs.IsEmpty() {
@@ -2473,15 +2494,25 @@ func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) {
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
attrs := &javaLibraryAttributes{ attrs := &javaLibraryAttributes{
javaCommonAttributes: commonAttrs, javaCommonAttributes: commonAttrs,
Deps: deps, Deps: deps,
Exports: depLabels.StaticDeps, Exports: depLabels.StaticDeps,
} }
props := bazel.BazelTargetModuleProperties{ if !bp2BuildInfo.hasKotlinSrcs && len(m.properties.Common_srcs) == 0 {
Rule_class: "java_library", props = bazel.BazelTargetModuleProperties{
Bzl_load_location: "//build/bazel/rules/java:library.bzl", Rule_class: "java_library",
Bzl_load_location: "//build/bazel/rules/java:library.bzl",
}
} else {
attrs.Common_srcs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Common_srcs))
props = bazel.BazelTargetModuleProperties{
Rule_class: "kt_jvm_library",
Bzl_load_location: "@rules_kotlin//kotlin:jvm_library.bzl",
}
} }
name := m.Name() name := m.Name()
@@ -2498,7 +2529,8 @@ type javaBinaryHostAttributes struct {
// JavaBinaryHostBp2Build is for java_binary_host bp2build. // JavaBinaryHostBp2Build is for java_binary_host bp2build.
func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) { func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {
commonAttrs, depLabels := m.convertLibraryAttrsBp2Build(ctx) commonAttrs, bp2BuildInfo := m.convertLibraryAttrsBp2Build(ctx)
depLabels := bp2BuildInfo.DepLabels
deps := depLabels.Deps deps := depLabels.Deps
deps.Append(depLabels.StaticDeps) deps.Append(depLabels.StaticDeps)

View File

@@ -66,7 +66,8 @@ type pluginAttributes struct {
// ConvertWithBp2build is used to convert android_app to Bazel. // ConvertWithBp2build is used to convert android_app to Bazel.
func (p *Plugin) ConvertWithBp2build(ctx android.TopDownMutatorContext) { func (p *Plugin) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
pluginName := p.Name() pluginName := p.Name()
commonAttrs, depLabels := p.convertLibraryAttrsBp2Build(ctx) commonAttrs, bp2BuildInfo := p.convertLibraryAttrsBp2Build(ctx)
depLabels := bp2BuildInfo.DepLabels
deps := depLabels.Deps deps := depLabels.Deps
deps.Append(depLabels.StaticDeps) deps.Append(depLabels.StaticDeps)