Merge "Correctly set date and mode fields for MANIFEST.MF"
diff --git a/android/paths.go b/android/paths.go
index 4a49d55..69a7b0d 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -247,6 +247,9 @@
 // each string.
 func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string) Paths {
 	prefix := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir()) + "/"
+	if prefix == "./" {
+		prefix = ""
+	}
 	ret := make(Paths, 0, len(paths))
 	for _, p := range paths {
 		path := filepath.Clean(p)
diff --git a/java/builder.go b/java/builder.go
index b8332ad..95345d4 100644
--- a/java/builder.go
+++ b/java/builder.go
@@ -129,14 +129,6 @@
 	javaVersion   string
 }
 
-type jarSpec struct {
-	fileList, dir android.Path
-}
-
-func (j jarSpec) soongJarArgs() string {
-	return "-C " + j.dir.String() + " -l " + j.fileList.String()
-}
-
 func TransformJavaToClasses(ctx android.ModuleContext, srcFiles, srcFileLists android.Paths,
 	flags javaBuilderFlags, deps android.Paths) android.ModuleOutPath {
 
@@ -206,18 +198,11 @@
 	return classFileList
 }
 
-func TransformResourcesToJar(ctx android.ModuleContext, resources []jarSpec,
+func TransformResourcesToJar(ctx android.ModuleContext, jarArgs []string,
 	deps android.Paths) android.Path {
 
 	outputFile := android.PathForModuleOut(ctx, "res.jar")
 
-	jarArgs := []string{}
-
-	for _, j := range resources {
-		deps = append(deps, j.fileList)
-		jarArgs = append(jarArgs, j.soongJarArgs())
-	}
-
 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
 		Rule:        jar,
 		Description: "jar",
diff --git a/java/java.go b/java/java.go
index 5b82fb3..dbb755b 100644
--- a/java/java.go
+++ b/java/java.go
@@ -75,6 +75,12 @@
 	// list of directories that should be excluded from java_resource_dirs
 	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,
 	// ext, and framework for device targets)
 	No_standard_libs *bool
@@ -100,6 +106,9 @@
 	// If set to false, don't allow this module to be installed.  Defaults to true.
 	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
 	Annotation_processors []string
 
@@ -275,6 +284,7 @@
 	ctx.AddDependency(ctx.Module(), libTag, j.properties.Annotation_processors...)
 
 	android.ExtractSourcesDeps(ctx, j.properties.Srcs)
+	android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
 }
 
 func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
@@ -377,8 +387,6 @@
 		flags.javaVersion = "${config.DefaultJavaVersion}"
 	}
 
-	var extraDeps android.Paths
-
 	flags.bootClasspath.AddPaths(deps.bootClasspath)
 	flags.classpath.AddPaths(deps.classpath)
 
@@ -405,35 +413,50 @@
 
 	deps.srcFileLists = append(deps.srcFileLists, j.ExtraSrcLists...)
 
-	var extraJarDeps android.Paths
-
 	var jars android.Paths
 
 	if len(srcFiles) > 0 {
-		// Compile java sources into .class files
-		classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraDeps)
-		if ctx.Failed() {
-			return
-		}
-
+		var extraJarDeps android.Paths
 		if ctx.AConfig().IsEnvTrue("RUN_ERROR_PRONE") {
 			// If error-prone is enabled, add an additional rule to compile the java files into
 			// a separate set of classes (so that they don't overwrite the normal ones and require
-			// a rebuild when error-prone is turned off).  Add the classes as a dependency to
-			// the jar command so the two compiles can run in parallel.
+			// a rebuild when error-prone is turned off).
 			// TODO(ccross): Once we always compile with javac9 we may be able to conditionally
 			//    enable error-prone without affecting the output class files.
-			errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, extraDeps)
+			errorprone := RunErrorProne(ctx, srcFiles, deps.srcFileLists, flags, nil)
 			extraJarDeps = append(extraJarDeps, errorprone)
 		}
 
+		// Compile java sources into .class files
+		classes := TransformJavaToClasses(ctx, srcFiles, deps.srcFileLists, flags, extraJarDeps)
+		if ctx.Failed() {
+			return
+		}
+
 		jars = append(jars, classes)
 	}
 
-	resourceJarSpecs := ResourceDirsToJarSpecs(ctx, j.properties.Java_resource_dirs, j.properties.Exclude_java_resource_dirs)
-	if len(resourceJarSpecs) > 0 {
+	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 {
 		// Combine classes + resources into classes-full-debug.jar
-		resourceJar := TransformResourcesToJar(ctx, resourceJarSpecs, extraJarDeps)
+		resourceJar := TransformResourcesToJar(ctx, resArgs, resDeps)
 		if ctx.Failed() {
 			return
 		}
@@ -452,12 +475,12 @@
 	manifest := android.OptionalPathForModuleSrc(ctx, j.properties.Manifest)
 
 	// Combine the classes built from sources, any manifests, and any static libraries into
-	// classes-combined.jar.  If there is only one input jar this step will be skipped.
+	// classes.jar.  If there is only one input jar this step will be skipped.
 	outputFile := TransformJarsToJar(ctx, "classes.jar", jars, manifest, false)
 
 	if j.properties.Jarjar_rules != nil {
 		jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
-		// Transform classes-combined.jar into classes-jarjar.jar
+		// Transform classes.jar into classes-jarjar.jar
 		outputFile = TransformJarJar(ctx, outputFile, jarjar_rules)
 		if ctx.Failed() {
 			return
@@ -467,7 +490,7 @@
 	j.classpathFile = outputFile
 
 	// TODO(ccross): handle hostdex
-	if ctx.Device() && len(srcFiles) > 0 {
+	if ctx.Device() && len(srcFiles) > 0 && j.installable() {
 		dxFlags := j.deviceProperties.Dxflags
 		if false /* emma enabled */ {
 			// If you instrument class files that have local variable debug information in
@@ -520,10 +543,6 @@
 			return
 		}
 
-		// TODO(ccross): For now, use the desugared jar as the classpath file.  Eventually this
-		// might cause problems because desugar wants non-desugared jars in its class path.
-		j.classpathFile = desugarJar
-
 		// Compile classes.jar into classes.dex
 		dexJarFile := TransformClassesJarToDexJar(ctx, desugarJar, flags)
 		if ctx.Failed() {
@@ -541,6 +560,10 @@
 	j.outputFile = outputFile
 }
 
+func (j *Module) installable() bool {
+	return j.properties.Installable == nil || *j.properties.Installable
+}
+
 var _ Dependency = (*Library)(nil)
 
 func (j *Module) ClasspathFiles() android.Paths {
@@ -572,7 +595,7 @@
 func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	j.compile(ctx)
 
-	if j.properties.Installable == nil || *j.properties.Installable == true {
+	if j.installable() {
 		j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
 			ctx.ModuleName()+".jar", j.outputFile)
 	}
diff --git a/java/java_test.go b/java/java_test.go
index 7159e3f..7154f5e 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -16,6 +16,7 @@
 
 import (
 	"android/soong/android"
+	"android/soong/genrule"
 	"fmt"
 	"io/ioutil"
 	"os"
@@ -59,6 +60,7 @@
 	ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
 	ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
 	ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
+	ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
 	ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
 	ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
 	ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
@@ -92,6 +94,8 @@
 		"c.java":     nil,
 		"a.jar":      nil,
 		"b.jar":      nil,
+		"res/a":      nil,
+		"res/b":      nil,
 		"prebuilts/sdk/14/android.jar":    nil,
 		"prebuilts/sdk/14/framework.aidl": nil,
 	})
@@ -111,7 +115,7 @@
 	case strings.HasSuffix(name, ".jar"):
 		return name
 	default:
-		return filepath.Join(buildDir, ".intermediates", name, "android_common", "classes-desugar.jar")
+		return filepath.Join(buildDir, ".intermediates", name, "android_common", "classes-compiled.jar")
 	}
 }
 
@@ -142,8 +146,8 @@
 		t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
 	}
 
-	bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-desugar.jar")
-	baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-desugar.jar")
+	bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar")
+	baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar")
 
 	if !strings.Contains(javac.Args["classpath"], bar) {
 		t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
@@ -349,17 +353,87 @@
 		t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
 	}
 
-	bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-desugar.jar")
+	bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar")
 	if !strings.Contains(javac.Args["classpath"], bar) {
 		t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
 	}
 
-	baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-desugar.jar")
+	baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar")
 	if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
 		t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
 	}
 }
 
+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) {
 	if len(errs) > 0 {
 		for _, err := range errs {
diff --git a/java/resources.go b/java/resources.go
index 60dc934..9f5e5e0 100644
--- a/java/resources.go
+++ b/java/resources.go
@@ -15,7 +15,9 @@
 package java
 
 import (
+	"fmt"
 	"path/filepath"
+	"strings"
 
 	"github.com/google/blueprint/bootstrap"
 
@@ -40,7 +42,8 @@
 	return false
 }
 
-func ResourceDirsToJarSpecs(ctx android.ModuleContext, resourceDirs, excludeDirs []string) []jarSpec {
+func ResourceDirsToJarArgs(ctx android.ModuleContext,
+	resourceDirs, excludeDirs []string) (args []string, deps android.Paths) {
 	var excludes []string
 
 	for _, exclude := range excludeDirs {
@@ -49,8 +52,6 @@
 
 	excludes = append(excludes, resourceExcludes...)
 
-	var jarSpecs []jarSpec
-
 	for _, resourceDir := range resourceDirs {
 		if isStringInSlice(resourceDir, excludeDirs) {
 			continue
@@ -63,9 +64,29 @@
 
 			pattern := filepath.Join(dir.String(), "**/*")
 			bootstrap.GlobFile(ctx, pattern, excludes, fileListFile.String(), depFile)
-			jarSpecs = append(jarSpecs, jarSpec{fileListFile, dir})
+			args = append(args,
+				"-C", dir.String(),
+				"-l", fileListFile.String())
+			deps = append(deps, fileListFile)
 		}
 	}
 
-	return jarSpecs
+	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
 }