Add DistPath to reference the dist folder

Instead of open-coding the logic of whether there is one, or where to
find it.

Bug: 76168832
Test: diff out/soong/build.ninja without dist
Test: diff out/soong/build.ninja with dist specified
Change-Id: Ia3f1ef335e2d6e2175343338d04867d778a50300
Merged-In: Ia3f1ef335e2d6e2175343338d04867d778a50300
(cherry picked from commit bc0c5092671cbe7e58ab6a1f886414f864af3024)
diff --git a/android/paths.go b/android/paths.go
index 3605dcf..dc89bfd 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -693,6 +693,46 @@
 	return PathForOutput(ctx, ".intermediates", path)
 }
 
+// DistPath is a Path representing a file path rooted from the dist directory
+type DistPath struct {
+	basePath
+}
+
+func (p DistPath) withRel(rel string) DistPath {
+	p.basePath = p.basePath.withRel(rel)
+	return p
+}
+
+var _ Path = DistPath{}
+
+// PathForDist joins the provided paths and returns a DistPath that is
+// validated to not escape the dist dir.
+// On error, it will return a usable, but invalid DistPath, and report a ModuleError.
+func PathForDist(ctx PathContext, pathComponents ...string) DistPath {
+	path, err := validatePath(pathComponents...)
+	if err != nil {
+		reportPathError(ctx, err)
+	}
+	return DistPath{basePath{path, ctx.Config(), ""}}
+}
+
+func (p DistPath) writablePath() {}
+
+func (p DistPath) Valid() bool {
+	return p.config.ProductVariables.DistDir != nil && *p.config.ProductVariables.DistDir != ""
+}
+
+func (p DistPath) String() string {
+	if !p.Valid() {
+		panic("Requesting an invalid path")
+	}
+	return filepath.Join(*p.config.ProductVariables.DistDir, p.path)
+}
+
+func (p DistPath) RelPathString() string {
+	return p.path
+}
+
 // ModuleSrcPath is a Path representing a file rooted from a module's local source dir
 type ModuleSrcPath struct {
 	SourcePath
diff --git a/cc/builder.go b/cc/builder.go
index d438add..73a9168 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -199,11 +199,10 @@
 		func(ctx android.PackageRuleContext) blueprint.RuleParams {
 
 			commandStr := "($sAbiDiffer $allowFlags -lib $libName -arch $arch -check-all-apis -o ${out} -new $in -old $referenceDump)"
-			distDir := ctx.Config().ProductVariables.DistDir
-			commandStr += " || (echo ' ---- Please update abi references by running platform/development/vndk/tools/header-checker/utils/create_reference_dumps.py -l ${libName} ----'"
-			if distDir != nil && *distDir != "" {
-				distAbiDiffDir := *distDir + "/abidiffs/"
-				commandStr += " && (mkdir -p " + distAbiDiffDir + " && cp ${out} " + distAbiDiffDir + ")"
+			distAbiDiffDir := android.PathForDist(ctx, "abidiffs")
+			commandStr += "|| (echo ' ---- Please update abi references by running platform/development/vndk/tools/header-checker/utils/create_reference_dumps.py -l ${libName} ----'"
+			if distAbiDiffDir.Valid() {
+				commandStr += " && (mkdir -p " + distAbiDiffDir.String() + " && cp ${out} " + distAbiDiffDir.String() + ")"
 			}
 			commandStr += " && exit 1)"
 			return blueprint.RuleParams{