Use correct module name for java_import in shouldConvertWithBp2build verification

This change makes sure that prebuilt module has correct module name
prefix("prebuilt_") inside method shouldConvertWithBp2build, to avoid
the bp2build conflict issue when a normal module and a prebuilt module
with the same name are both allowlisted.

Test: CI, added unit tests and the steps to reproduce in b/303376793

Bug: 303376793
Bug: 303868211
Change-Id: I4f1126182ec1f52ae6a08046a77204939230edef
This commit is contained in:
Zi Wang
2023-10-06 11:25:06 -07:00
parent 008f2e55c7
commit 734266b633
3 changed files with 70 additions and 0 deletions

View File

@@ -544,7 +544,17 @@ func (b *BazelModuleBase) shouldConvertWithBp2build(ctx shouldConvertModuleConte
} }
moduleName := moduleNameWithPossibleOverride(ctx, module, p.moduleName) moduleName := moduleNameWithPossibleOverride(ctx, module, p.moduleName)
// use "prebuilt_" + original module name as the java_import(_host) module name,
// to avoid the failure that a normal module and a prebuilt module with
// the same name are both allowlisted. This cannot be applied to all the *_import
// module types. For example, android_library_import has to use original module
// name here otherwise the *-nodeps targets cannot be handled correctly.
if strings.HasPrefix(p.moduleType, "java_import") {
moduleName = module.Name()
}
allowlist := ctx.Config().Bp2buildPackageConfig allowlist := ctx.Config().Bp2buildPackageConfig
moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName] moduleNameAllowed := allowlist.moduleAlwaysConvert[moduleName]
moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[p.moduleType] moduleTypeAllowed := allowlist.moduleTypeAlwaysConvert[p.moduleType]
allowlistConvert := moduleNameAllowed || moduleTypeAllowed allowlistConvert := moduleNameAllowed || moduleTypeAllowed

View File

@@ -21,6 +21,13 @@ import (
"testing" "testing"
) )
func runJavaImportTestCaseWithRegistrationCtxFunc(t *testing.T, tc Bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) {
t.Helper()
(&tc).ModuleTypeUnderTest = "java_import"
(&tc).ModuleTypeUnderTestFactory = java.ImportFactory
RunBp2BuildTestCase(t, registrationCtxFunc, tc)
}
func runJavaImportTestCase(t *testing.T, tc Bp2buildTestCase) { func runJavaImportTestCase(t *testing.T, tc Bp2buildTestCase) {
t.Helper() t.Helper()
RunBp2BuildTestCase(t, registerJavaImportModuleTypes, tc) RunBp2BuildTestCase(t, registerJavaImportModuleTypes, tc)
@@ -120,3 +127,31 @@ java_import_host {
}), }),
}}) }})
} }
func TestJavaImportSameNameAsJavaLibrary(t *testing.T) {
runJavaImportTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{
Description: "java_import has the same name as other package java_library's",
Filesystem: map[string]string{
"foo/bar/Android.bp": simpleModule("java_library", "test_lib"),
"test.jar": "",
},
Blueprint: `java_import {
name: "test_lib",
jars: ["test.jar"],
bazel_module: { bp2build_available: true },
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("java_import", "test_lib", AttrNameToString{
"jars": `["test.jar"]`,
}),
MakeBazelTarget("java_library", "test_lib-neverlink", AttrNameToString{
"exports": `[":test_lib"]`,
"neverlink": `True`,
"sdk_version": `"none"`,
}),
},
}, func(ctx android.RegistrationContext) {
ctx.RegisterModuleType("java_library", java.LibraryFactory)
})
}

View File

@@ -1041,3 +1041,28 @@ filegroup {
ctx.RegisterModuleType("filegroup", android.FileGroupFactory) ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
}) })
} }
func TestJavaLibrarySameNameAsPrebuilt(t *testing.T) {
runJavaLibraryTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{
Description: "java_library and prebuilt module have the same name",
Filesystem: map[string]string{
"foo/bar/Android.bp": simpleModule("java_import", "test_lib"),
},
Blueprint: `java_library {
name: "test_lib",
srcs: ["a.java"],
sdk_version: "current",
bazel_module: { bp2build_available: true },
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("java_library", "test_lib", AttrNameToString{
"srcs": `["a.java"]`,
"sdk_version": `"current"`,
}),
MakeNeverlinkDuplicateTarget("java_library", "test_lib"),
},
}, func(ctx android.RegistrationContext) {
ctx.RegisterModuleType("java_import", java.ImportFactory)
})
}