Merge "convert java_resources with bp2build" am: 2737c25e9e

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2042845

Change-Id: I917f7114637e0439f22be9d9d6c2521ed9c1d2aa
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot
2022-05-11 20:44:12 +00:00
committed by Automerger Merge Worker
4 changed files with 182 additions and 12 deletions

View File

@@ -108,6 +108,7 @@ var (
"external/icu": Bp2BuildDefaultTrueRecursively, "external/icu": Bp2BuildDefaultTrueRecursively,
"external/icu/android_icu4j": Bp2BuildDefaultFalse, // java rules incomplete "external/icu/android_icu4j": Bp2BuildDefaultFalse, // java rules incomplete
"external/icu/icu4j": Bp2BuildDefaultFalse, // java rules incomplete "external/icu/icu4j": Bp2BuildDefaultFalse, // java rules incomplete
"external/jarjar": Bp2BuildDefaultTrueRecursively,
"external/javapoet": Bp2BuildDefaultTrueRecursively, "external/javapoet": Bp2BuildDefaultTrueRecursively,
"external/jemalloc_new": Bp2BuildDefaultTrueRecursively, "external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
"external/jsoncpp": Bp2BuildDefaultTrueRecursively, "external/jsoncpp": Bp2BuildDefaultTrueRecursively,
@@ -308,6 +309,7 @@ var (
"libprotobuf-internal-python-srcs", // TODO(b/210751803), we don't handle path property for filegroups "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-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 "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: // go deps:
"analyze_bcpf", // depends on bpmodify a blueprint_go_binary. "analyze_bcpf", // depends on bpmodify a blueprint_go_binary.

View File

@@ -267,3 +267,108 @@ func TestJavaLibraryLogTags(t *testing.T) {
}), }),
}}) }})
} }
func TestJavaLibraryResources(t *testing.T) {
runJavaLibraryTestCase(t, bp2buildTestCase{
filesystem: map[string]string{
"res/a.res": "",
"res/b.res": "",
"res/dir1/b.res": "",
},
blueprint: `java_library {
name: "java-lib-1",
java_resources: ["res/a.res", "res/b.res"],
}`,
expectedBazelTargets: []string{
makeBazelTarget("java_library", "java-lib-1", attrNameToString{
"resources": `[
"res/a.res",
"res/b.res",
]`,
}),
},
})
}
func TestJavaLibraryResourceDirs(t *testing.T) {
runJavaLibraryTestCase(t, bp2buildTestCase{
filesystem: map[string]string{
"res/a.res": "",
"res/b.res": "",
"res/dir1/b.res": "",
},
blueprint: `java_library {
name: "java-lib-1",
java_resource_dirs: ["res"],
}`,
expectedBazelTargets: []string{
makeBazelTarget("java_library", "java-lib-1", attrNameToString{
"resource_strip_prefix": `"res"`,
"resources": `[
"res/a.res",
"res/b.res",
"res/dir1/b.res",
]`,
}),
},
})
}
func TestJavaLibraryResourcesExcludeDir(t *testing.T) {
runJavaLibraryTestCase(t, bp2buildTestCase{
filesystem: map[string]string{
"res/a.res": "",
"res/exclude/b.res": "",
},
blueprint: `java_library {
name: "java-lib-1",
java_resource_dirs: ["res"],
exclude_java_resource_dirs: ["res/exclude"],
}`,
expectedBazelTargets: []string{
makeBazelTarget("java_library", "java-lib-1", attrNameToString{
"resource_strip_prefix": `"res"`,
"resources": `["res/a.res"]`,
}),
},
})
}
func TestJavaLibraryResourcesExcludeFile(t *testing.T) {
runJavaLibraryTestCase(t, bp2buildTestCase{
filesystem: map[string]string{
"res/a.res": "",
"res/dir1/b.res": "",
"res/dir1/exclude.res": "",
},
blueprint: `java_library {
name: "java-lib-1",
java_resource_dirs: ["res"],
exclude_java_resources: ["res/dir1/exclude.res"],
}`,
expectedBazelTargets: []string{
makeBazelTarget("java_library", "java-lib-1", attrNameToString{
"resource_strip_prefix": `"res"`,
"resources": `[
"res/a.res",
"res/dir1/b.res",
]`,
}),
},
})
}
func TestJavaLibraryResourcesFailsWithMultipleDirs(t *testing.T) {
runJavaLibraryTestCase(t, bp2buildTestCase{
filesystem: map[string]string{
"res/a.res": "",
"res1/a.res": "",
},
blueprint: `java_library {
name: "java-lib-1",
java_resource_dirs: ["res", "res1"],
}`,
expectedErr: fmt.Errorf("bp2build does not support more than one directory in java_resource_dirs (b/226423379)"),
expectedBazelTargets: []string{},
})
}

View File

@@ -2018,7 +2018,49 @@ func addCLCFromDep(ctx android.ModuleContext, depModule android.Module,
} }
} }
type javaResourcesAttributes struct {
Resources bazel.LabelListAttribute
Resource_strip_prefix *string
}
func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorContext) *javaResourcesAttributes {
var resources bazel.LabelList
var resourceStripPrefix *string
if m.properties.Java_resources != nil {
resources.Append(android.BazelLabelForModuleSrc(ctx, m.properties.Java_resources))
}
//TODO(b/179889880) handle case where glob includes files outside package
resDeps := ResourceDirsToFiles(
ctx,
m.properties.Java_resource_dirs,
m.properties.Exclude_java_resource_dirs,
m.properties.Exclude_java_resources,
)
for i, resDep := range resDeps {
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
// inside the JAR file, so we need to remove that prefix
resourceStripPrefix = proptools.StringPtr(dir.String())
if i > 0 {
// TODO(b/226423379) allow multiple resource prefixes
ctx.ModuleErrorf("bp2build does not support more than one directory in java_resource_dirs (b/226423379)")
}
}
return &javaResourcesAttributes{
Resources: bazel.MakeLabelListAttribute(resources),
Resource_strip_prefix: resourceStripPrefix,
}
}
type javaCommonAttributes struct { type javaCommonAttributes struct {
*javaResourcesAttributes
Srcs bazel.LabelListAttribute Srcs bazel.LabelListAttribute
Plugins bazel.LabelListAttribute Plugins bazel.LabelListAttribute
Javacopts bazel.StringListAttribute Javacopts bazel.StringListAttribute
@@ -2096,6 +2138,7 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)
commonAttrs := &javaCommonAttributes{ commonAttrs := &javaCommonAttributes{
Srcs: javaSrcs, Srcs: javaSrcs,
javaResourcesAttributes: m.convertJavaResourcesAttributes(ctx),
Plugins: bazel.MakeLabelListAttribute( Plugins: bazel.MakeLabelListAttribute(
android.BazelLabelForModuleDeps(ctx, m.properties.Plugins), android.BazelLabelForModuleDeps(ctx, m.properties.Plugins),
), ),

View File

@@ -33,8 +33,13 @@ var resourceExcludes = []string{
"**/*~", "**/*~",
} }
func ResourceDirsToJarArgs(ctx android.ModuleContext, type resourceDeps struct {
resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) { dir android.Path
files android.Paths
}
func ResourceDirsToFiles(ctx android.BaseModuleContext,
resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (deps []resourceDeps) {
var excludeDirs []string var excludeDirs []string
var excludeFiles []string var excludeFiles []string
@@ -55,11 +60,26 @@ func ResourceDirsToJarArgs(ctx android.ModuleContext,
dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, resourceDir).String(), excludeDirs) dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, resourceDir).String(), excludeDirs)
for _, dir := range dirs { for _, dir := range dirs {
files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles) files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles)
deps = append(deps, resourceDeps{
dir: dir,
files: files,
})
}
}
deps = append(deps, files...) return deps
}
func ResourceDirsToJarArgs(ctx android.ModuleContext,
resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) {
resDeps := ResourceDirsToFiles(ctx, resourceDirs, excludeResourceDirs, excludeResourceFiles)
for _, resDep := range resDeps {
dir, files := resDep.dir, resDep.files
if len(files) > 0 { if len(files) > 0 {
args = append(args, "-C", dir.String()) args = append(args, "-C", dir.String())
deps = append(deps, files...)
for _, f := range files { for _, f := range files {
path := f.String() path := f.String()
@@ -69,7 +89,7 @@ func ResourceDirsToJarArgs(ctx android.ModuleContext,
args = append(args, "-f", pathtools.MatchEscape(path)) args = append(args, "-f", pathtools.MatchEscape(path))
} }
} }
}
} }
return args, deps return args, deps