Add per-directory build targets

Build a map of blueprint directory to modules built from that
directory, and then add phony rules to build.ninja that emulate
the behavior of mma in the current build system.

Also fixes checkbuild to depend on checkbuild files and installable
files, but not installed files.

Change-Id: I8bad6e93387940df7439dbd4554f6d79f924c65f
diff --git a/common/module.go b/common/module.go
index ef7c63a..327aeca 100644
--- a/common/module.go
+++ b/common/module.go
@@ -194,6 +194,12 @@
 	noAddressSanitizer bool
 	installFiles       []string
 	checkbuildFiles    []string
+
+	// Used by buildTargetSingleton to create checkbuild and per-directory build targets
+	// Only set on the final variant of each module
+	installTarget    string
+	checkbuildTarget string
+	blueprintDir     string
 }
 
 func (a *AndroidModuleBase) base() *AndroidModuleBase {
@@ -273,6 +279,7 @@
 			Implicits: allInstalledFiles,
 		})
 		deps = append(deps, name)
+		a.installTarget = name
 	}
 
 	if len(allCheckbuildFiles) > 0 {
@@ -284,6 +291,7 @@
 			Optional:  true,
 		})
 		deps = append(deps, name)
+		a.checkbuildTarget = name
 	}
 
 	if len(deps) > 0 {
@@ -293,6 +301,8 @@
 			Implicits: deps,
 			Optional:  true,
 		})
+
+		a.blueprintDir = ctx.ModuleDir()
 	}
 }
 
@@ -424,6 +434,7 @@
 	})
 
 	a.installFiles = append(a.installFiles, fullInstallPath)
+	a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
 	return fullInstallPath
 }
 
@@ -467,3 +478,52 @@
 	srcFiles = expandGlobs(ctx, srcFiles)
 	return srcFiles
 }
+
+func BuildTargetSingleton() blueprint.Singleton {
+	return &buildTargetSingleton{}
+}
+
+type buildTargetSingleton struct{}
+
+func (c *buildTargetSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
+	checkbuildDeps := []string{}
+
+	dirModules := make(map[string][]string)
+
+	ctx.VisitAllModules(func(module blueprint.Module) {
+		if a, ok := module.(AndroidModule); ok {
+			blueprintDir := a.base().blueprintDir
+			installTarget := a.base().installTarget
+			checkbuildTarget := a.base().checkbuildTarget
+
+			if checkbuildTarget != "" {
+				checkbuildDeps = append(checkbuildDeps, checkbuildTarget)
+				dirModules[blueprintDir] = append(dirModules[blueprintDir], checkbuildTarget)
+			}
+
+			if installTarget != "" {
+				dirModules[blueprintDir] = append(dirModules[blueprintDir], installTarget)
+			}
+		}
+	})
+
+	// Create a top-level checkbuild target that depends on all modules
+	ctx.Build(pctx, blueprint.BuildParams{
+		Rule:      blueprint.Phony,
+		Outputs:   []string{"checkbuild"},
+		Implicits: checkbuildDeps,
+		// HACK: checkbuild should be an optional build, but force it enabled for now
+		//Optional:  true,
+	})
+
+	// Create a mm/<directory> target that depends on all modules in a directory
+	dirs := sortedKeys(dirModules)
+	for _, dir := range dirs {
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:      blueprint.Phony,
+			Outputs:   []string{filepath.Join("mm", dir)},
+			Implicits: dirModules[dir],
+			Optional:  true,
+		})
+	}
+}