Merge "support for multiple filegroups and resource_dirs" into main
This commit is contained in:
@@ -907,6 +907,9 @@ var (
|
|||||||
"hal_unit_tests",
|
"hal_unit_tests",
|
||||||
|
|
||||||
"merge_annotation_zips_test",
|
"merge_annotation_zips_test",
|
||||||
|
|
||||||
|
// java_resources with multiple resource_dirs
|
||||||
|
"emma",
|
||||||
}
|
}
|
||||||
|
|
||||||
Bp2buildModuleTypeAlwaysConvertList = []string{
|
Bp2buildModuleTypeAlwaysConvertList = []string{
|
||||||
|
@@ -391,18 +391,56 @@ func TestJavaLibraryResourcesExcludeFile(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJavaLibraryResourcesFailsWithMultipleDirs(t *testing.T) {
|
func TestJavaLibraryResourcesWithMultipleDirs(t *testing.T) {
|
||||||
runJavaLibraryTestCase(t, Bp2buildTestCase{
|
runJavaLibraryTestCase(t, Bp2buildTestCase{
|
||||||
Filesystem: map[string]string{
|
Filesystem: map[string]string{
|
||||||
"res/a.res": "",
|
"res/a.res": "",
|
||||||
"res1/a.res": "",
|
"res1/b.res": "",
|
||||||
},
|
},
|
||||||
Blueprint: `java_library {
|
Blueprint: `java_library {
|
||||||
name: "java-lib-1",
|
name: "java-lib-1",
|
||||||
java_resource_dirs: ["res", "res1"],
|
java_resource_dirs: ["res", "res1"],
|
||||||
}`,
|
}`,
|
||||||
ExpectedErr: fmt.Errorf("bp2build does not support more than one directory in java_resource_dirs (b/226423379)"),
|
ExpectedBazelTargets: []string{
|
||||||
ExpectedBazelTargets: []string{},
|
MakeBazelTarget("java_resources", "java-lib-1_resource_dir_res1", AttrNameToString{
|
||||||
|
"resource_strip_prefix": `"res1"`,
|
||||||
|
"resources": `["res1/b.res"]`,
|
||||||
|
}),
|
||||||
|
MakeBazelTarget("java_library", "java-lib-1", AttrNameToString{
|
||||||
|
"additional_resources": `["java-lib-1_resource_dir_res1"]`,
|
||||||
|
"resources": `["res/a.res"]`,
|
||||||
|
"resource_strip_prefix": `"res"`,
|
||||||
|
}),
|
||||||
|
MakeNeverlinkDuplicateTarget("java_library", "java-lib-1"),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJavaLibraryJavaResourcesAndResourceDirs(t *testing.T) {
|
||||||
|
runJavaLibraryTestCase(t, Bp2buildTestCase{
|
||||||
|
Filesystem: map[string]string{
|
||||||
|
"resdir/a.res": "",
|
||||||
|
},
|
||||||
|
Blueprint: `java_library {
|
||||||
|
name: "java-lib-1",
|
||||||
|
java_resources: ["res1", "res2"],
|
||||||
|
java_resource_dirs: ["resdir"],
|
||||||
|
}`,
|
||||||
|
ExpectedBazelTargets: []string{
|
||||||
|
MakeBazelTarget("java_resources", "java-lib-1_resource_dir_resdir", AttrNameToString{
|
||||||
|
"resource_strip_prefix": `"resdir"`,
|
||||||
|
"resources": `["resdir/a.res"]`,
|
||||||
|
}),
|
||||||
|
MakeBazelTarget("java_library", "java-lib-1", AttrNameToString{
|
||||||
|
"additional_resources": `["java-lib-1_resource_dir_resdir"]`,
|
||||||
|
"resource_strip_prefix": `"."`,
|
||||||
|
"resources": `[
|
||||||
|
"res1",
|
||||||
|
"res2",
|
||||||
|
]`,
|
||||||
|
}),
|
||||||
|
MakeNeverlinkDuplicateTarget("java_library", "java-lib-1"),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -878,3 +916,57 @@ filegroup {
|
|||||||
ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
|
ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJavaLibraryJavaResourcesMultipleFilegroup(t *testing.T) {
|
||||||
|
runJavaLibraryTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{
|
||||||
|
Filesystem: map[string]string{
|
||||||
|
"a.res": "",
|
||||||
|
},
|
||||||
|
Description: "with java_resources that has multiple filegroups",
|
||||||
|
Blueprint: `java_library {
|
||||||
|
name: "java-lib-1",
|
||||||
|
srcs: ["a.java"],
|
||||||
|
java_resources: ["a.res", ":filegroup1", ":filegroup2"],
|
||||||
|
bazel_module: { bp2build_available: true },
|
||||||
|
}
|
||||||
|
|
||||||
|
filegroup {
|
||||||
|
name: "filegroup1",
|
||||||
|
path: "foo",
|
||||||
|
srcs: ["foo/a"],
|
||||||
|
}
|
||||||
|
|
||||||
|
filegroup {
|
||||||
|
name: "filegroup2",
|
||||||
|
path: "bar",
|
||||||
|
srcs: ["bar/a"],
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
ExpectedBazelTargets: []string{
|
||||||
|
MakeBazelTarget("java_resources", "java-lib-1_filegroup_resources_filegroup1", AttrNameToString{
|
||||||
|
"resource_strip_prefix": `"foo"`,
|
||||||
|
"resources": `[":filegroup1"]`,
|
||||||
|
}),
|
||||||
|
MakeBazelTarget("java_resources", "java-lib-1_filegroup_resources_filegroup2", AttrNameToString{
|
||||||
|
"resource_strip_prefix": `"bar"`,
|
||||||
|
"resources": `[":filegroup2"]`,
|
||||||
|
}),
|
||||||
|
MakeBazelTarget("java_library", "java-lib-1", AttrNameToString{
|
||||||
|
"srcs": `["a.java"]`,
|
||||||
|
"resources": `["a.res"]`,
|
||||||
|
"resource_strip_prefix": `"."`,
|
||||||
|
"additional_resources": `[
|
||||||
|
"java-lib-1_filegroup_resources_filegroup1",
|
||||||
|
"java-lib-1_filegroup_resources_filegroup2",
|
||||||
|
]`,
|
||||||
|
}),
|
||||||
|
MakeNeverlinkDuplicateTarget("java_library", "java-lib-1"),
|
||||||
|
MakeBazelTargetNoRestrictions("filegroup", "filegroup1", AttrNameToString{
|
||||||
|
"srcs": `["foo/a"]`}),
|
||||||
|
MakeBazelTargetNoRestrictions("filegroup", "filegroup2", AttrNameToString{
|
||||||
|
"srcs": `["bar/a"]`}),
|
||||||
|
},
|
||||||
|
}, func(ctx android.RegistrationContext) {
|
||||||
|
ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
72
java/java.go
72
java/java.go
@@ -21,6 +21,7 @@ package java
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"android/soong/bazel"
|
"android/soong/bazel"
|
||||||
@@ -2756,32 +2757,41 @@ func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
|
|||||||
type javaResourcesAttributes struct {
|
type javaResourcesAttributes struct {
|
||||||
Resources bazel.LabelListAttribute
|
Resources bazel.LabelListAttribute
|
||||||
Resource_strip_prefix *string
|
Resource_strip_prefix *string
|
||||||
|
Additional_resources bazel.LabelListAttribute
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Library) javaResourcesGetSingleFilegroupStripPrefix(ctx android.TopDownMutatorContext) (string, bool) {
|
func (m *Library) getResourceFilegroupStripPrefix(ctx android.TopDownMutatorContext, resourceFilegroup string) (*string, bool) {
|
||||||
if otherM, ok := ctx.ModuleFromName(m.properties.Java_resources[0]); ok && len(m.properties.Java_resources) == 1 {
|
if otherM, ok := ctx.ModuleFromName(resourceFilegroup); ok {
|
||||||
if fg, isFilegroup := otherM.(android.FileGroupPath); isFilegroup {
|
if fg, isFilegroup := otherM.(android.FileGroupPath); isFilegroup {
|
||||||
return filepath.Join(ctx.OtherModuleDir(otherM), fg.GetPath(ctx)), true
|
return proptools.StringPtr(filepath.Join(ctx.OtherModuleDir(otherM), fg.GetPath(ctx))), true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", false
|
return proptools.StringPtr(""), false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorContext) *javaResourcesAttributes {
|
func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorContext) *javaResourcesAttributes {
|
||||||
var resources bazel.LabelList
|
var resources bazel.LabelList
|
||||||
var resourceStripPrefix *string
|
var resourceStripPrefix *string
|
||||||
|
|
||||||
if m.properties.Java_resources != nil && len(m.properties.Java_resource_dirs) > 0 {
|
additionalJavaResourcesMap := make(map[string]*javaResourcesAttributes)
|
||||||
ctx.ModuleErrorf("bp2build doesn't support both java_resources and java_resource_dirs being set on the same module.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.properties.Java_resources != nil {
|
if m.properties.Java_resources != nil {
|
||||||
if prefix, ok := m.javaResourcesGetSingleFilegroupStripPrefix(ctx); ok {
|
for _, res := range m.properties.Java_resources {
|
||||||
resourceStripPrefix = proptools.StringPtr(prefix)
|
if prefix, isFilegroup := m.getResourceFilegroupStripPrefix(ctx, res); isFilegroup {
|
||||||
|
otherM, _ := ctx.ModuleFromName(res)
|
||||||
|
resourcesTargetName := ctx.ModuleName() + "_filegroup_resources_" + otherM.Name()
|
||||||
|
additionalJavaResourcesMap[resourcesTargetName] = &javaResourcesAttributes{
|
||||||
|
Resources: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, []string{res})),
|
||||||
|
Resource_strip_prefix: prefix,
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
resources.Append(android.BazelLabelForModuleSrc(ctx, []string{res}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !resources.IsEmpty() {
|
||||||
resourceStripPrefix = proptools.StringPtr(ctx.ModuleDir())
|
resourceStripPrefix = proptools.StringPtr(ctx.ModuleDir())
|
||||||
}
|
}
|
||||||
resources.Append(android.BazelLabelForModuleSrc(ctx, m.properties.Java_resources))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO(b/179889880) handle case where glob includes files outside package
|
//TODO(b/179889880) handle case where glob includes files outside package
|
||||||
@@ -2792,23 +2802,51 @@ func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorConte
|
|||||||
m.properties.Exclude_java_resources,
|
m.properties.Exclude_java_resources,
|
||||||
)
|
)
|
||||||
|
|
||||||
for i, resDep := range resDeps {
|
for _, resDep := range resDeps {
|
||||||
dir, files := resDep.dir, resDep.files
|
dir, files := resDep.dir, resDep.files
|
||||||
|
|
||||||
resources.Append(bazel.MakeLabelList(android.RootToModuleRelativePaths(ctx, files)))
|
|
||||||
|
|
||||||
// Bazel includes the relative path from the WORKSPACE root when placing the resource
|
// Bazel includes the relative path from the WORKSPACE root when placing the resource
|
||||||
// inside the JAR file, so we need to remove that prefix
|
// inside the JAR file, so we need to remove that prefix
|
||||||
resourceStripPrefix = proptools.StringPtr(dir.String())
|
prefix := proptools.StringPtr(dir.String())
|
||||||
if i > 0 {
|
resourcesTargetName := ctx.ModuleName() + "_resource_dir_" + dir.String()
|
||||||
// TODO(b/226423379) allow multiple resource prefixes
|
additionalJavaResourcesMap[resourcesTargetName] = &javaResourcesAttributes{
|
||||||
ctx.ModuleErrorf("bp2build does not support more than one directory in java_resource_dirs (b/226423379)")
|
Resources: bazel.MakeLabelListAttribute(bazel.MakeLabelList(android.RootToModuleRelativePaths(ctx, files))),
|
||||||
|
Resource_strip_prefix: prefix,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var additionalResourceLabels bazel.LabelList
|
||||||
|
if len(additionalJavaResourcesMap) > 0 {
|
||||||
|
var additionalResources []string
|
||||||
|
for resName, _ := range additionalJavaResourcesMap {
|
||||||
|
additionalResources = append(additionalResources, resName)
|
||||||
|
}
|
||||||
|
sort.Strings(additionalResources)
|
||||||
|
|
||||||
|
for i, resName := range additionalResources {
|
||||||
|
resAttr := additionalJavaResourcesMap[resName]
|
||||||
|
if resourceStripPrefix == nil && i == 0 {
|
||||||
|
resourceStripPrefix = resAttr.Resource_strip_prefix
|
||||||
|
resources = resAttr.Resources.Value
|
||||||
|
} else {
|
||||||
|
ctx.CreateBazelTargetModule(
|
||||||
|
bazel.BazelTargetModuleProperties{
|
||||||
|
Rule_class: "java_resources",
|
||||||
|
Bzl_load_location: "//build/bazel/rules/java:java_resources.bzl",
|
||||||
|
},
|
||||||
|
android.CommonAttributes{Name: resName},
|
||||||
|
resAttr,
|
||||||
|
)
|
||||||
|
additionalResourceLabels.Append(android.BazelLabelForModuleSrc(ctx, []string{resName}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return &javaResourcesAttributes{
|
return &javaResourcesAttributes{
|
||||||
Resources: bazel.MakeLabelListAttribute(resources),
|
Resources: bazel.MakeLabelListAttribute(resources),
|
||||||
Resource_strip_prefix: resourceStripPrefix,
|
Resource_strip_prefix: resourceStripPrefix,
|
||||||
|
Additional_resources: bazel.MakeLabelListAttribute(additionalResourceLabels),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user