Add Path.RelativeToTop

Makes Path.RelativeToTop the source of truth as to what `relative to
top` means.

Uses it in PathRelativeToTop so that it will be tested by the existing
tests that use AssertPathRelativeToTop.

Also adds RelativeToTop() to WritablePaths and Paths.

Bug: 183650682
Test: m droid
Change-Id: I88dc56afd1314e51b4e41795a2448cab4ce8a899
diff --git a/android/paths.go b/android/paths.go
index b3cf804..1278961 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -174,8 +174,35 @@
 	// example, Rel on a PathsForModuleSrc would return the path relative to the module source
 	// directory, and OutputPath.Join("foo").Rel() would return "foo".
 	Rel() string
+
+	// RelativeToTop returns a new path relative to the top, it is provided solely for use in tests.
+	//
+	// It is guaranteed to always return the same type as it is called on, e.g. if called on an
+	// InstallPath then the returned value can be converted to an InstallPath.
+	//
+	// A standard build has the following structure:
+	//   ../top/
+	//          out/ - make install files go here.
+	//          out/soong - this is the buildDir passed to NewTestConfig()
+	//          ... - the source files
+	//
+	// This function converts a path so that it appears relative to the ../top/ directory, i.e.
+	// * Make install paths, which have the pattern "buildDir/../<path>" are converted into the top
+	//   relative path "out/<path>"
+	// * Soong install paths and other writable paths, which have the pattern "buildDir/<path>" are
+	//   converted into the top relative path "out/soong/<path>".
+	// * Source paths are already relative to the top.
+	// * Phony paths are not relative to anything.
+	// * toolDepPath have an absolute but known value in so don't need making relative to anything in
+	//   order to test.
+	RelativeToTop() Path
 }
 
+const (
+	OutDir      = "out"
+	OutSoongDir = OutDir + "/soong"
+)
+
 // WritablePath is a type of path that can be used as an output for build rules.
 type WritablePath interface {
 	Path
@@ -271,6 +298,20 @@
 // Paths is a slice of Path objects, with helpers to operate on the collection.
 type Paths []Path
 
+// RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each
+// item in this slice.
+func (p Paths) RelativeToTop() Paths {
+	ensureTestOnly()
+	if p == nil {
+		return p
+	}
+	ret := make(Paths, len(p))
+	for i, path := range p {
+		ret[i] = path.RelativeToTop()
+	}
+	return ret
+}
+
 func (paths Paths) containsPath(path Path) bool {
 	for _, p := range paths {
 		if p == path {
@@ -910,6 +951,20 @@
 // WritablePaths is a slice of WritablePath, used for multiple outputs.
 type WritablePaths []WritablePath
 
+// RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on
+// each item in this slice.
+func (p WritablePaths) RelativeToTop() WritablePaths {
+	ensureTestOnly()
+	if p == nil {
+		return p
+	}
+	ret := make(WritablePaths, len(p))
+	for i, path := range p {
+		ret[i] = path.RelativeToTop().(WritablePath)
+	}
+	return ret
+}
+
 // Strings returns the string forms of the writable paths.
 func (p WritablePaths) Strings() []string {
 	if p == nil {
@@ -972,6 +1027,11 @@
 	srcDir string
 }
 
+func (p SourcePath) RelativeToTop() Path {
+	ensureTestOnly()
+	return p
+}
+
 var _ Path = SourcePath{}
 
 func (p SourcePath) withRel(rel string) SourcePath {
@@ -1167,6 +1227,16 @@
 	return p.buildDir
 }
 
+func (p OutputPath) RelativeToTop() Path {
+	return p.outputPathRelativeToTop()
+}
+
+func (p OutputPath) outputPathRelativeToTop() OutputPath {
+	p.fullPath = StringPathRelativeToTop(p.buildDir, p.fullPath)
+	p.buildDir = OutSoongDir
+	return p
+}
+
 func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
 	return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
 }
@@ -1180,6 +1250,11 @@
 	basePath
 }
 
+func (t toolDepPath) RelativeToTop() Path {
+	ensureTestOnly()
+	return t
+}
+
 var _ Path = toolDepPath{}
 
 // pathForBuildToolDep returns a toolDepPath representing the given path string.
@@ -1357,7 +1432,13 @@
 	OutputPath
 }
 
+func (p ModuleOutPath) RelativeToTop() Path {
+	p.OutputPath = p.outputPathRelativeToTop()
+	return p
+}
+
 var _ Path = ModuleOutPath{}
+var _ WritablePath = ModuleOutPath{}
 
 func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
 	return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
@@ -1462,7 +1543,13 @@
 	ModuleOutPath
 }
 
+func (p ModuleGenPath) RelativeToTop() Path {
+	p.OutputPath = p.outputPathRelativeToTop()
+	return p
+}
+
 var _ Path = ModuleGenPath{}
+var _ WritablePath = ModuleGenPath{}
 var _ genPathProvider = ModuleGenPath{}
 var _ objPathProvider = ModuleGenPath{}
 
@@ -1495,7 +1582,13 @@
 	ModuleOutPath
 }
 
+func (p ModuleObjPath) RelativeToTop() Path {
+	p.OutputPath = p.outputPathRelativeToTop()
+	return p
+}
+
 var _ Path = ModuleObjPath{}
+var _ WritablePath = ModuleObjPath{}
 
 // PathForModuleObj returns a Path representing the paths... under the module's
 // 'obj' directory.
@@ -1513,7 +1606,13 @@
 	ModuleOutPath
 }
 
+func (p ModuleResPath) RelativeToTop() Path {
+	p.OutputPath = p.outputPathRelativeToTop()
+	return p
+}
+
 var _ Path = ModuleResPath{}
+var _ WritablePath = ModuleResPath{}
 
 // PathForModuleRes returns a Path representing the paths... under the module's
 // 'res' directory.
@@ -1541,6 +1640,26 @@
 	makePath bool
 }
 
+// Will panic if called from outside a test environment.
+func ensureTestOnly() {
+	// Normal soong test environment
+	if InList("-test.short", os.Args) {
+		return
+	}
+	// IntelliJ test environment
+	if InList("-test.v", os.Args) {
+		return
+	}
+
+	panic(fmt.Errorf("Not in test\n%s", strings.Join(os.Args, "\n")))
+}
+
+func (p InstallPath) RelativeToTop() Path {
+	ensureTestOnly()
+	p.buildDir = OutSoongDir
+	return p
+}
+
 func (p InstallPath) getBuildDir() string {
 	return p.buildDir
 }
@@ -1814,6 +1933,13 @@
 	return ""
 }
 
+func (p PhonyPath) RelativeToTop() Path {
+	ensureTestOnly()
+	// A phony path cannot contain any / so does not have a build directory so switching to a new
+	// build directory has no effect so just return this path.
+	return p
+}
+
 func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
 	panic("Not implemented")
 }
@@ -1825,10 +1951,17 @@
 	basePath
 }
 
+func (p testPath) RelativeToTop() Path {
+	ensureTestOnly()
+	return p
+}
+
 func (p testPath) String() string {
 	return p.path
 }
 
+var _ Path = testPath{}
+
 // PathForTesting returns a Path constructed from joining the elements of paths with '/'.  It should only be used from
 // within tests.
 func PathForTesting(paths ...string) Path {