Support handling ModuleMakeVarsProvider and SingletonMakeVarsProvider in soong only dist

This change allows modules that set dist goals via `MakeVars(...)` to be
properly handled in collecting dist contributions in Soong only build.

Test: compare dist directories with/without change in soong+make build and soong only build
Bug: 394365683
Change-Id: I8a81ab17195b687297961dc6f1ee7b4b97cec24c
diff --git a/android/androidmk.go b/android/androidmk.go
index f862a96..cf1589d 100644
--- a/android/androidmk.go
+++ b/android/androidmk.go
@@ -333,6 +333,25 @@
 	dest string
 }
 
+func (d *distCopy) String() string {
+	if len(d.dest) == 0 {
+		return d.from.String()
+	}
+	return fmt.Sprintf("%s:%s", d.from.String(), d.dest)
+}
+
+type distCopies []distCopy
+
+func (d *distCopies) Strings() (ret []string) {
+	if d == nil {
+		return
+	}
+	for _, dist := range *d {
+		ret = append(ret, dist.String())
+	}
+	return
+}
+
 // Compute the contributions that the module makes to the dist.
 func (a *AndroidMkEntries) getDistContributions(mod blueprint.Module) *distContributions {
 	amod := mod.(Module).base()
@@ -821,6 +840,26 @@
 	}
 }
 
+func getMakeVarsDistContributions(mctx *makeVarsContext) *distContributions {
+	if len(mctx.dists) == 0 {
+		return nil
+	}
+
+	copyGoals := []*copiesForGoals{}
+	for _, dist := range mctx.dists {
+		for _, goal := range dist.goals {
+			copy := &copiesForGoals{}
+			copy.goals = goal
+			copy.copies = dist.paths
+			copyGoals = append(copyGoals, copy)
+		}
+	}
+
+	contribution := &distContributions{}
+	contribution.copiesForGoals = copyGoals
+	return contribution
+}
+
 // getSoongOnlyDataFromMods gathers data from the given modules needed in soong-only builds.
 // Currently, this is the dist contributions, and the module-info.json contents.
 func getSoongOnlyDataFromMods(ctx fillInEntriesContext, mods []blueprint.Module) ([]distContributions, []*ModuleInfoJSON) {
@@ -853,6 +892,11 @@
 				}
 			}
 		} else {
+			mctx := &makeVarsContext{
+				SingletonContext: ctx.(SingletonContext),
+				config:           ctx.Config(),
+				pctx:             pctx,
+			}
 			switch x := mod.(type) {
 			case AndroidMkDataProvider:
 				data := x.AndroidMk()
@@ -885,6 +929,21 @@
 						allDistContributions = append(allDistContributions, *contribution)
 					}
 				}
+			case ModuleMakeVarsProvider:
+				if !x.Enabled(ctx) {
+					continue
+				}
+				x.MakeVars(mctx)
+				if contribution := getMakeVarsDistContributions(mctx); contribution != nil {
+					allDistContributions = append(allDistContributions, *contribution)
+				}
+
+			case SingletonMakeVarsProvider:
+				x.MakeVars(mctx)
+				if contribution := getMakeVarsDistContributions(mctx); contribution != nil {
+					allDistContributions = append(allDistContributions, *contribution)
+				}
+
 			default:
 				// Not exported to make so no make variables to set.
 			}
diff --git a/android/makevars.go b/android/makevars.go
index 8305d8e..d4389cf 100644
--- a/android/makevars.go
+++ b/android/makevars.go
@@ -220,7 +220,7 @@
 
 type dist struct {
 	goals []string
-	paths []string
+	paths distCopies
 }
 
 func (s *makeVarsSingleton) GenerateBuildActions(ctx SingletonContext) {
@@ -330,7 +330,7 @@
 		return len(a) < len(b)
 	}
 	sort.Slice(dists, func(i, j int) bool {
-		return lessArr(dists[i].goals, dists[j].goals) || lessArr(dists[i].paths, dists[j].paths)
+		return lessArr(dists[i].goals, dists[j].goals) || lessArr(dists[i].paths.Strings(), dists[j].paths.Strings())
 	})
 
 	outBytes := s.writeVars(vars)
@@ -458,7 +458,7 @@
 	for _, dist := range dists {
 		fmt.Fprintf(buf, ".PHONY: %s\n", strings.Join(dist.goals, " "))
 		fmt.Fprintf(buf, "$(call dist-for-goals,%s,%s)\n",
-			strings.Join(dist.goals, " "), strings.Join(dist.paths, " "))
+			strings.Join(dist.goals, " "), strings.Join(dist.paths.Strings(), " "))
 	}
 
 	return buf.Bytes()
@@ -607,7 +607,7 @@
 	c.phonies = append(c.phonies, phony{name, deps})
 }
 
-func (c *makeVarsContext) addDist(goals []string, paths []string) {
+func (c *makeVarsContext) addDist(goals []string, paths []distCopy) {
 	c.dists = append(c.dists, dist{
 		goals: goals,
 		paths: paths,
@@ -647,9 +647,15 @@
 }
 
 func (c *makeVarsContext) DistForGoals(goals []string, paths ...Path) {
-	c.addDist(goals, Paths(paths).Strings())
+	var copies distCopies
+	for _, path := range paths {
+		copies = append(copies, distCopy{
+			from: path,
+		})
+	}
+	c.addDist(goals, copies)
 }
 
 func (c *makeVarsContext) DistForGoalsWithFilename(goals []string, path Path, filename string) {
-	c.addDist(goals, []string{path.String() + ":" + filename})
+	c.addDist(goals, distCopies{{from: path, dest: filename}})
 }