Support data properties for test binaries

Allow tests to specify a data property that lists files or filegroup
modules that will be packaged alongside the test.  Also add a path
property to filegroup modules to allow shifting the path of the
packaged files, and add ExpandSourcesSubDir to expand the filegroup
sources while including a shifted relative path in the Paths objects.

Test: soong tests, manually adding data to a module
Change-Id: I52a48942660e12755d313ef13279313361b4fc35
diff --git a/android/module.go b/android/module.go
index 430563d..b5de1ad 100644
--- a/android/module.go
+++ b/android/module.go
@@ -77,6 +77,7 @@
 	ModuleBuild(pctx blueprint.PackageContext, params ModuleBuildParams)
 
 	ExpandSources(srcFiles, excludes []string) Paths
+	ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths
 	Glob(globPattern string, excludes []string) Paths
 
 	InstallFile(installPath OutputPath, srcPath Path, deps ...Path) OutputPath
@@ -742,9 +743,14 @@
 }
 
 // Returns a list of paths expanded from globs and modules referenced using ":module" syntax.
-// ExpandSourceDeps must have already been called during the dependency resolution phase.
+// ExtractSourcesDeps must have already been called during the dependency resolution phase.
 func (ctx *androidModuleContext) ExpandSources(srcFiles, excludes []string) Paths {
+	return ctx.ExpandSourcesSubDir(srcFiles, excludes, "")
+}
+
+func (ctx *androidModuleContext) ExpandSourcesSubDir(srcFiles, excludes []string, subDir string) Paths {
 	prefix := PathForModuleSrc(ctx).String()
+
 	for i, e := range excludes {
 		j := findStringInSlice(e, srcFiles)
 		if j != -1 {
@@ -754,23 +760,28 @@
 		excludes[i] = filepath.Join(prefix, e)
 	}
 
-	globbedSrcFiles := make(Paths, 0, len(srcFiles))
+	expandedSrcFiles := make(Paths, 0, len(srcFiles))
 	for _, s := range srcFiles {
 		if m := SrcIsModule(s); m != "" {
 			module := ctx.GetDirectDepWithTag(m, SourceDepTag)
 			if srcProducer, ok := module.(SourceFileProducer); ok {
-				globbedSrcFiles = append(globbedSrcFiles, srcProducer.Srcs()...)
+				expandedSrcFiles = append(expandedSrcFiles, srcProducer.Srcs()...)
 			} else {
 				ctx.ModuleErrorf("srcs dependency %q is not a source file producing module", m)
 			}
 		} else if pathtools.IsGlob(s) {
-			globbedSrcFiles = append(globbedSrcFiles, ctx.Glob(filepath.Join(prefix, s), excludes)...)
+			globbedSrcFiles := ctx.Glob(filepath.Join(prefix, s), excludes)
+			expandedSrcFiles = append(expandedSrcFiles, globbedSrcFiles...)
+			for i, s := range expandedSrcFiles {
+				expandedSrcFiles[i] = s.(ModuleSrcPath).WithSubDir(ctx, subDir)
+			}
 		} else {
-			globbedSrcFiles = append(globbedSrcFiles, PathForModuleSrc(ctx, s))
+			s := PathForModuleSrc(ctx, s).WithSubDir(ctx, subDir)
+			expandedSrcFiles = append(expandedSrcFiles, s)
 		}
 	}
 
-	return globbedSrcFiles
+	return expandedSrcFiles
 }
 
 func (ctx *androidModuleContext) Glob(globPattern string, excludes []string) Paths {
diff --git a/android/paths.go b/android/paths.go
index ac7d81e..037c98d 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -86,6 +86,11 @@
 
 	// Base returns the last element of the path
 	Base() string
+
+	// Rel returns the portion of the path relative to the directory it was created from.  For
+	// example, Rel on a PathsForModuleSrc would return the path relative to the module source
+	// directory.
+	Rel() string
 }
 
 // WritablePath is a type of path that can be used as an output for build rules.
@@ -283,6 +288,7 @@
 type basePath struct {
 	path   string
 	config Config
+	rel    string
 }
 
 func (p basePath) Ext() string {
@@ -293,6 +299,13 @@
 	return filepath.Base(p.path)
 }
 
+func (p basePath) Rel() string {
+	if p.rel != "" {
+		return p.rel
+	}
+	return p.path
+}
+
 // SourcePath is a Path representing a file path rooted from SrcDir
 type SourcePath struct {
 	basePath
@@ -304,7 +317,7 @@
 // code that is embedding ninja variables in paths
 func safePathForSource(ctx PathContext, path string) SourcePath {
 	p := validateSafePath(ctx, path)
-	ret := SourcePath{basePath{p, pathConfig(ctx)}}
+	ret := SourcePath{basePath{p, pathConfig(ctx), ""}}
 
 	abs, err := filepath.Abs(ret.String())
 	if err != nil {
@@ -330,7 +343,7 @@
 // will return a usable, but invalid SourcePath, and report a ModuleError.
 func PathForSource(ctx PathContext, paths ...string) SourcePath {
 	p := validatePath(ctx, paths...)
-	ret := SourcePath{basePath{p, pathConfig(ctx)}}
+	ret := SourcePath{basePath{p, pathConfig(ctx), ""}}
 
 	abs, err := filepath.Abs(ret.String())
 	if err != nil {
@@ -365,7 +378,7 @@
 	}
 
 	p := validatePath(ctx, paths...)
-	path := SourcePath{basePath{p, pathConfig(ctx)}}
+	path := SourcePath{basePath{p, pathConfig(ctx), ""}}
 
 	abs, err := filepath.Abs(path.String())
 	if err != nil {
@@ -476,7 +489,7 @@
 // OutputPath, and report a ModuleError.
 func PathForOutput(ctx PathContext, paths ...string) OutputPath {
 	path := validatePath(ctx, paths...)
-	return OutputPath{basePath{path, pathConfig(ctx)}}
+	return OutputPath{basePath{path, pathConfig(ctx), ""}}
 }
 
 func (p OutputPath) writablePath() {}
@@ -516,8 +529,10 @@
 // PathForModuleSrc returns a ModuleSrcPath representing the paths... under the
 // module's local source directory.
 func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath {
-	path := validatePath(ctx, paths...)
-	return ModuleSrcPath{PathForSource(ctx, ctx.ModuleDir(), path)}
+	p := validatePath(ctx, paths...)
+	path := ModuleSrcPath{PathForSource(ctx, ctx.ModuleDir(), p)}
+	path.basePath.rel = p
+	return path
 }
 
 // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
@@ -542,6 +557,18 @@
 	return PathForModuleRes(ctx, p.path, name)
 }
 
+func (p ModuleSrcPath) WithSubDir(ctx ModuleContext, subdir string) ModuleSrcPath {
+	subdir = PathForModuleSrc(ctx, subdir).String()
+	var err error
+	rel, err := filepath.Rel(subdir, p.path)
+	if err != nil {
+		ctx.ModuleErrorf("source file %q is not under path %q", p.path, subdir)
+		return p
+	}
+	p.rel = rel
+	return p
+}
+
 // ModuleOutPath is a Path representing a module's output directory.
 type ModuleOutPath struct {
 	OutputPath