From b29a3cd2aa7d415f4d5f27a775cfb2e02cbe0ba0 Mon Sep 17 00:00:00 2001 From: Alix Date: Tue, 9 May 2023 20:37:49 +0000 Subject: [PATCH] bp2build java_resources that only contain a filegroup supports filegroup that specifies path property Bug: 280860624 Test: built libauto_value_plugin Change-Id: I9ed0b13e055beb92ba8090f6b5e88b9873c9ce61 --- android/allowlists/allowlists.go | 6 +++- android/filegroup.go | 13 ++++++++ bp2build/java_library_conversion_test.go | 40 ++++++++++++++++++++++++ java/java.go | 15 ++++++++- 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/android/allowlists/allowlists.go b/android/allowlists/allowlists.go index 751a4cb83..6405e9fe9 100644 --- a/android/allowlists/allowlists.go +++ b/android/allowlists/allowlists.go @@ -761,6 +761,11 @@ var ( // aidl "aidl", "libaidl-common", + + // java_resources containing only a single filegroup + "libauto_value_plugin", + "auto_value_plugin_resources", + "auto_value_extension", } Bp2buildModuleTypeAlwaysConvertList = []string{ @@ -835,7 +840,6 @@ var ( "libprotobuf-internal-python-srcs", // TODO(b/210751803), we don't handle path property for filegroups "libprotobuf-java-full", // TODO(b/210751803), we don't handle path property for filegroups "libprotobuf-java-util-full", // TODO(b/210751803), we don't handle path property for filegroups - "auto_value_plugin_resources", // TODO(b/210751803), we don't handle path property for filegroups // go deps: "analyze_bcpf", // depends on bpmodify a blueprint_go_binary. diff --git a/android/filegroup.go b/android/filegroup.go index f30ee5143..c042ff18e 100644 --- a/android/filegroup.go +++ b/android/filegroup.go @@ -177,6 +177,17 @@ func (fg *fileGroup) ConvertWithBp2build(ctx TopDownMutatorContext) { } } +type FileGroupPath interface { + GetPath(ctx TopDownMutatorContext) string +} + +func (fg *fileGroup) GetPath(ctx TopDownMutatorContext) string { + if fg.properties.Path != nil { + return *fg.properties.Path + } + return "" +} + type fileGroupProperties struct { // srcs lists files that will be included in this filegroup Srcs []string `android:"path"` @@ -207,6 +218,7 @@ type fileGroup struct { BazelModuleBase DefaultableModuleBase FileGroupAsLibrary + FileGroupPath properties fileGroupProperties srcs Paths } @@ -214,6 +226,7 @@ type fileGroup struct { var _ MixedBuildBuildable = (*fileGroup)(nil) var _ SourceFileProducer = (*fileGroup)(nil) var _ FileGroupAsLibrary = (*fileGroup)(nil) +var _ FileGroupPath = (*fileGroup)(nil) // filegroup contains a list of files that are referenced by other modules // properties (such as "srcs") using the syntax ":". filegroup are diff --git a/bp2build/java_library_conversion_test.go b/bp2build/java_library_conversion_test.go index 60766f42a..fd92e95c6 100644 --- a/bp2build/java_library_conversion_test.go +++ b/bp2build/java_library_conversion_test.go @@ -826,3 +826,43 @@ func TestJavaLibraryArchVariantSrcsWithExcludes(t *testing.T) { }, }) } + +func TestJavaLibraryJavaResourcesSingleFilegroup(t *testing.T) { + runJavaLibraryTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{ + Filesystem: map[string]string{ + "res/a.res": "", + "res/b.res": "", + "res/dir1/b.res": "", + }, + Description: "java_library", + Blueprint: `java_library { + name: "java-lib-1", + srcs: ["a.java"], + java_resources: [":filegroup1"], + bazel_module: { bp2build_available: true }, +} + +filegroup { + name: "filegroup1", + path: "foo", + srcs: ["foo/a", "foo/b"], +} + +`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("java_library", "java-lib-1", AttrNameToString{ + "srcs": `["a.java"]`, + "resources": `[":filegroup1"]`, + "resource_strip_prefix": `"foo"`, + }), + MakeNeverlinkDuplicateTarget("java_library", "java-lib-1"), + MakeBazelTargetNoRestrictions("filegroup", "filegroup1", AttrNameToString{ + "srcs": `[ + "foo/a", + "foo/b", + ]`}), + }, + }, func(ctx android.RegistrationContext) { + ctx.RegisterModuleType("filegroup", android.FileGroupFactory) + }) +} diff --git a/java/java.go b/java/java.go index a23526aee..4dcc9b582 100644 --- a/java/java.go +++ b/java/java.go @@ -2704,6 +2704,15 @@ type javaResourcesAttributes struct { Resource_strip_prefix *string } +func (m *Library) javaResourcesGetSingleFilegroupStripPrefix(ctx android.TopDownMutatorContext) (string, bool) { + if otherM, ok := ctx.ModuleFromName(m.properties.Java_resources[0]); ok && len(m.properties.Java_resources) == 1 { + if fg, isFilegroup := otherM.(android.FileGroupPath); isFilegroup { + return filepath.Join(ctx.OtherModuleDir(otherM), fg.GetPath(ctx)), true + } + } + return "", false +} + func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorContext) *javaResourcesAttributes { var resources bazel.LabelList var resourceStripPrefix *string @@ -2713,8 +2722,12 @@ func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorConte } if m.properties.Java_resources != nil { + if prefix, ok := m.javaResourcesGetSingleFilegroupStripPrefix(ctx); ok { + resourceStripPrefix = proptools.StringPtr(prefix) + } else { + resourceStripPrefix = proptools.StringPtr(ctx.ModuleDir()) + } resources.Append(android.BazelLabelForModuleSrc(ctx, m.properties.Java_resources)) - resourceStripPrefix = proptools.StringPtr(ctx.ModuleDir()) } //TODO(b/179889880) handle case where glob includes files outside package