Add java file resources and flag to include sources
Add a properties to allow including files as resources, including support for filegroups. Also add a flag that causes module sources to be included in the final jar. Test: java_test.go TestResources Change-Id: Ida8ee59b28df9fe66952170f46470d3a09fd5d65
This commit is contained in:
parent
8649b2653c
commit
0f37af0c15
|
@ -247,6 +247,9 @@ func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths {
|
||||||
// each string.
|
// each string.
|
||||||
func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string) Paths {
|
func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string) Paths {
|
||||||
prefix := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir()) + "/"
|
prefix := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir()) + "/"
|
||||||
|
if prefix == "./" {
|
||||||
|
prefix = ""
|
||||||
|
}
|
||||||
ret := make(Paths, 0, len(paths))
|
ret := make(Paths, 0, len(paths))
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
path := filepath.Clean(p)
|
path := filepath.Clean(p)
|
||||||
|
|
28
java/java.go
28
java/java.go
|
@ -75,6 +75,12 @@ type CompilerProperties struct {
|
||||||
// list of directories that should be excluded from java_resource_dirs
|
// list of directories that should be excluded from java_resource_dirs
|
||||||
Exclude_java_resource_dirs []string `android:"arch_variant"`
|
Exclude_java_resource_dirs []string `android:"arch_variant"`
|
||||||
|
|
||||||
|
// list of files to use as Java resources
|
||||||
|
Java_resources []string `android:"arch_variant"`
|
||||||
|
|
||||||
|
// list of files that should be excluded from java_resources
|
||||||
|
Exclude_java_resources []string `android:"arch_variant"`
|
||||||
|
|
||||||
// don't build against the default libraries (legacy-test, core-junit,
|
// don't build against the default libraries (legacy-test, core-junit,
|
||||||
// ext, and framework for device targets)
|
// ext, and framework for device targets)
|
||||||
No_standard_libs *bool
|
No_standard_libs *bool
|
||||||
|
@ -100,6 +106,9 @@ type CompilerProperties struct {
|
||||||
// If set to false, don't allow this module to be installed. Defaults to true.
|
// If set to false, don't allow this module to be installed. Defaults to true.
|
||||||
Installable *bool
|
Installable *bool
|
||||||
|
|
||||||
|
// If set to true, include sources used to compile the module in to the final jar
|
||||||
|
Include_srcs *bool
|
||||||
|
|
||||||
// List of modules to use as annotation processors
|
// List of modules to use as annotation processors
|
||||||
Annotation_processors []string
|
Annotation_processors []string
|
||||||
|
|
||||||
|
@ -275,6 +284,7 @@ func (j *Module) deps(ctx android.BottomUpMutatorContext) {
|
||||||
ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
|
ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
|
||||||
|
|
||||||
android.ExtractSourcesDeps(ctx, j.properties.Srcs)
|
android.ExtractSourcesDeps(ctx, j.properties.Srcs)
|
||||||
|
android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
|
func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
|
||||||
|
@ -426,7 +436,23 @@ func (j *Module) compile(ctx android.ModuleContext) {
|
||||||
jars = append(jars, classes)
|
jars = append(jars, classes)
|
||||||
}
|
}
|
||||||
|
|
||||||
resArgs, resDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
|
dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
|
||||||
|
fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
|
||||||
|
|
||||||
|
var resArgs []string
|
||||||
|
var resDeps android.Paths
|
||||||
|
|
||||||
|
resArgs = append(resArgs, dirArgs...)
|
||||||
|
resDeps = append(resDeps, dirDeps...)
|
||||||
|
|
||||||
|
resArgs = append(resArgs, fileArgs...)
|
||||||
|
resDeps = append(resDeps, fileDeps...)
|
||||||
|
|
||||||
|
if proptools.Bool(j.properties.Include_srcs) {
|
||||||
|
srcArgs, srcDeps := ResourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
|
||||||
|
resArgs = append(resArgs, srcArgs...)
|
||||||
|
resDeps = append(resDeps, srcDeps...)
|
||||||
|
}
|
||||||
|
|
||||||
if len(resArgs) > 0 {
|
if len(resArgs) > 0 {
|
||||||
// Combine classes + resources into classes-full-debug.jar
|
// Combine classes + resources into classes-full-debug.jar
|
||||||
|
|
|
@ -16,6 +16,7 @@ package java
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"android/soong/android"
|
"android/soong/android"
|
||||||
|
"android/soong/genrule"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -59,6 +60,7 @@ func testJava(t *testing.T, bp string) *android.TestContext {
|
||||||
ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
|
ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
|
||||||
ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
|
ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
|
||||||
ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
|
ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
|
||||||
|
ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
|
||||||
ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
|
ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
|
||||||
ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
|
ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
|
||||||
ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
|
ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
|
||||||
|
@ -92,6 +94,8 @@ func testJava(t *testing.T, bp string) *android.TestContext {
|
||||||
"c.java": nil,
|
"c.java": nil,
|
||||||
"a.jar": nil,
|
"a.jar": nil,
|
||||||
"b.jar": nil,
|
"b.jar": nil,
|
||||||
|
"res/a": nil,
|
||||||
|
"res/b": nil,
|
||||||
"prebuilts/sdk/14/android.jar": nil,
|
"prebuilts/sdk/14/android.jar": nil,
|
||||||
"prebuilts/sdk/14/framework.aidl": nil,
|
"prebuilts/sdk/14/framework.aidl": nil,
|
||||||
})
|
})
|
||||||
|
@ -360,6 +364,76 @@ func TestDefaults(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResources(t *testing.T) {
|
||||||
|
var table = []struct {
|
||||||
|
name string
|
||||||
|
prop string
|
||||||
|
extra string
|
||||||
|
args string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// Test that a module with java_resource_dirs includes a file list file
|
||||||
|
name: "resource dirs",
|
||||||
|
prop: `java_resource_dirs: ["res"]`,
|
||||||
|
args: "-C res -l ",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Test that a module with java_resources includes the files
|
||||||
|
name: "resource files",
|
||||||
|
prop: `java_resources: ["res/a", "res/b"]`,
|
||||||
|
args: "-C . -f res/a -C . -f res/b",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Test that a module with a filegroup in java_resources includes the files with the
|
||||||
|
// path prefix
|
||||||
|
name: "resource filegroup",
|
||||||
|
prop: `java_resources: [":foo-res"]`,
|
||||||
|
extra: `
|
||||||
|
filegroup {
|
||||||
|
name: "foo-res",
|
||||||
|
path: "res",
|
||||||
|
srcs: ["res/a", "res/b"],
|
||||||
|
}`,
|
||||||
|
args: "-C res -f res/a -C res -f res/b",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Test that a module with "include_srcs: true" includes its source files in the resources jar
|
||||||
|
name: "include sources",
|
||||||
|
prop: `include_srcs: true`,
|
||||||
|
args: "-C . -f a.java -C . -f b.java -C . -f c.java",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range table {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
ctx := testJava(t, `
|
||||||
|
java_library {
|
||||||
|
name: "foo",
|
||||||
|
srcs: [
|
||||||
|
"a.java",
|
||||||
|
"b.java",
|
||||||
|
"c.java",
|
||||||
|
],
|
||||||
|
`+test.prop+`,
|
||||||
|
}
|
||||||
|
`+test.extra)
|
||||||
|
|
||||||
|
foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar")
|
||||||
|
fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
|
||||||
|
|
||||||
|
if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
|
||||||
|
t.Errorf("foo combined jars %v does not contain %q",
|
||||||
|
foo.Inputs.Strings(), fooRes.Output.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(fooRes.Args["jarArgs"], test.args) {
|
||||||
|
t.Errorf("foo resource jar args %q does not contain %q",
|
||||||
|
fooRes.Args["jarArgs"], test.args)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func fail(t *testing.T, errs []error) {
|
func fail(t *testing.T, errs []error) {
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
for _, err := range errs {
|
for _, err := range errs {
|
||||||
|
|
|
@ -15,7 +15,9 @@
|
||||||
package java
|
package java
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/google/blueprint/bootstrap"
|
"github.com/google/blueprint/bootstrap"
|
||||||
|
|
||||||
|
@ -71,3 +73,20 @@ func ResourceDirsToJarArgs(ctx android.ModuleContext,
|
||||||
|
|
||||||
return args, deps
|
return args, deps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ResourceFilesToJarArgs(ctx android.ModuleContext,
|
||||||
|
res, exclude []string) (args []string, deps android.Paths) {
|
||||||
|
files := ctx.ExpandSources(res, exclude)
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
rel := f.Rel()
|
||||||
|
path := f.String()
|
||||||
|
if !strings.HasSuffix(path, rel) {
|
||||||
|
panic(fmt.Errorf("path %q does not end with %q", path, rel))
|
||||||
|
}
|
||||||
|
path = filepath.Clean(strings.TrimSuffix(path, rel))
|
||||||
|
args = append(args, "-C", filepath.Clean(path), "-f", f.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return args, files
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue