Separate dist processing from make specific output
Previously, the GetDistForGoals(Module) func combined the processing
of the dist properties with generating the make specific rules for
generating that dist. That has a couple of problems:
1. It combines two pieces of functionality into one method which is
bad practice.
2. It makes it hard to test because the make specific output ends up
containing absolute paths to temporary directories created by the
test.
3. It makes switching to a non-make output difficult and fragile as
changing the output will also require changing the tests.
This change adds an intermediate data structure to contain the result
of the dist processing. That processing is done by the new method
getDistContributions(Module) which returns the new intermediate
structure. It also adds generateDistContributionsForMake(..) to
generate the make output. The GetDistForGoals(Module) func uses them to
implement the previous behavior.
It adds identical tests to those in TestGetDistForGoals() but leaves
those tests alone to show that this refactoring does not change the
behavior. Follow up changes will clean up TestGetDistForGoals(). It
also adds a test for generateDistContributionsForMake(..).
Bug: 174226317
Test: m nothing
m dist sdk - before and after this change, compare result to
make sure that there are no significant differences.
Change-Id: I458b7c8e4485bf66d3498f50df85a8d65fc2ee00
diff --git a/android/androidmk.go b/android/androidmk.go
index 4adbb22..7afc9de 100644
--- a/android/androidmk.go
+++ b/android/androidmk.go
@@ -177,13 +177,51 @@
a.EntryMap[name] = append(a.EntryMap[name], value...)
}
-// Compute the list of Make strings to declare phone goals and dist-for-goals
-// calls from the module's dist and dists properties.
-func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
+// The contributions to the dist.
+type distContributions struct {
+ // List of goals and the dist copy instructions.
+ copiesForGoals []*copiesForGoals
+}
+
+// getCopiesForGoals returns a copiesForGoals into which copy instructions that
+// must be processed when building one or more of those goals can be added.
+func (d *distContributions) getCopiesForGoals(goals string) *copiesForGoals {
+ copiesForGoals := &copiesForGoals{goals: goals}
+ d.copiesForGoals = append(d.copiesForGoals, copiesForGoals)
+ return copiesForGoals
+}
+
+// Associates a list of dist copy instructions with a set of goals for which they
+// should be run.
+type copiesForGoals struct {
+ // goals are a space separated list of build targets that will trigger the
+ // copy instructions.
+ goals string
+
+ // A list of instructions to copy a module's output files to somewhere in the
+ // dist directory.
+ copies []distCopy
+}
+
+// Adds a copy instruction.
+func (d *copiesForGoals) addCopyInstruction(from Path, dest string) {
+ d.copies = append(d.copies, distCopy{from, dest})
+}
+
+// Instruction on a path that must be copied into the dist.
+type distCopy struct {
+ // The path to copy from.
+ from Path
+
+ // The destination within the dist directory to copy to.
+ dest string
+}
+
+// Compute the contributions that the module makes to the dist.
+func (a *AndroidMkEntries) getDistContributions(mod blueprint.Module) *distContributions {
amod := mod.(Module).base()
name := amod.BaseModuleName()
- var ret []string
var availableTaggedDists TaggedDistFiles
if a.DistFiles != nil {
@@ -195,6 +233,9 @@
return nil
}
+ // Collate the contributions this module makes to the dist.
+ distContributions := &distContributions{}
+
// Iterate over this module's dist structs, merged from the dist and dists properties.
for _, dist := range amod.Dists() {
// Get the list of goals this dist should be enabled for. e.g. sdk, droidcore
@@ -224,9 +265,9 @@
panic(fmt.Errorf(errorMessage, goals, name, tagPaths))
}
- ret = append(ret, fmt.Sprintf(".PHONY: %s\n", goals))
+ copiesForGoals := distContributions.getCopiesForGoals(goals)
- // Create dist-for-goals calls for each path in the dist'd files.
+ // Iterate over each path adding a copy instruction to copiesForGoals
for _, path := range tagPaths {
// It's possible that the Path is nil from errant modules. Be defensive here.
if path == nil {
@@ -261,15 +302,41 @@
}
}
+ copiesForGoals.addCopyInstruction(path, dest)
+ }
+ }
+
+ return distContributions
+}
+
+// generateDistContributionsForMake generates make rules that will generate the
+// dist according to the instructions in the supplied distContribution.
+func generateDistContributionsForMake(distContributions *distContributions) []string {
+ var ret []string
+ for _, d := range distContributions.copiesForGoals {
+ ret = append(ret, fmt.Sprintf(".PHONY: %s\n", d.goals))
+ // Create dist-for-goals calls for each of the copy instructions.
+ for _, c := range d.copies {
ret = append(
ret,
- fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", goals, path.String(), dest))
+ fmt.Sprintf("$(call dist-for-goals,%s,%s:%s)\n", d.goals, c.from.String(), c.dest))
}
}
return ret
}
+// Compute the list of Make strings to declare phony goals and dist-for-goals
+// calls from the module's dist and dists properties.
+func (a *AndroidMkEntries) GetDistForGoals(mod blueprint.Module) []string {
+ distContributions := a.getDistContributions(mod)
+ if distContributions == nil {
+ return nil
+ }
+
+ return generateDistContributionsForMake(distContributions)
+}
+
func (a *AndroidMkEntries) fillInEntries(config Config, bpPath string, mod blueprint.Module) {
a.EntryMap = make(map[string][]string)
amod := mod.(Module).base()