Add support for checkbuild target

Modules can choose to add "installed files", which are files that
a product can depend on to cause the module to build, and "checkbuild
files", which will only be built if another module that needs to build
depends on them.  For every target, add a module-install and a
module-checkbuild target that create dependencies on those files,
and then add a global checkbuild target that depends on all the
module-checkbuild targets.  Also add a module target for each module
that depends on module-install and module-checkbuild.

Change-Id: I801389ad6ab9806b71d92cd327a0f9cb7582e0df
diff --git a/Blueprints b/Blueprints
index 057d27c..3c0755f 100644
--- a/Blueprints
+++ b/Blueprints
@@ -56,6 +56,7 @@
     ],
     srcs: [
         "common/arch.go",
+        "common/checkbuild.go",
         "common/defs.go",
         "common/glob.go",
         "common/module.go",
diff --git a/build.ninja.in b/build.ninja.in
index b3447c7..016e625 100644
--- a/build.ninja.in
+++ b/build.ninja.in
@@ -53,7 +53,7 @@
 # Variant:
 # Type:    bootstrap_go_binary
 # Factory: blueprint/bootstrap.newGoBinaryModule
-# Defined: build/soong/Blueprints:106:1
+# Defined: build/soong/Blueprints:107:1
 
 build .bootstrap/androidmk/obj/androidmk.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/androidmk/cmd/androidmk/android.go $
@@ -79,7 +79,7 @@
 # Variant:
 # Type:    bootstrap_go_package
 # Factory: blueprint/bootstrap.newGoPackageModule
-# Defined: build/soong/Blueprints:119:1
+# Defined: build/soong/Blueprints:120:1
 
 build .bootstrap/androidmk-parser/pkg/android/soong/androidmk/parser.a: $
         g.bootstrap.gc $
@@ -274,7 +274,7 @@
 # Variant:
 # Type:    bootstrap_go_package
 # Factory: blueprint/bootstrap.newGoPackageModule
-# Defined: build/soong/Blueprints:79:1
+# Defined: build/soong/Blueprints:80:1
 
 build .bootstrap/soong-cc/pkg/android/soong/cc.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/cc/builder.go $
@@ -308,6 +308,7 @@
 
 build .bootstrap/soong-common/pkg/android/soong/common.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/common/arch.go $
+        ${g.bootstrap.srcDir}/build/soong/common/checkbuild.go $
         ${g.bootstrap.srcDir}/build/soong/common/defs.go $
         ${g.bootstrap.srcDir}/build/soong/common/glob.go $
         ${g.bootstrap.srcDir}/build/soong/common/module.go $
@@ -329,7 +330,7 @@
 # Variant:
 # Type:    bootstrap_go_package
 # Factory: blueprint/bootstrap.newGoPackageModule
-# Defined: build/soong/Blueprints:66:1
+# Defined: build/soong/Blueprints:67:1
 
 build .bootstrap/soong-config/pkg/android/soong/config.a: g.bootstrap.gc $
         ${g.bootstrap.srcDir}/build/soong/config/config.go | $
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index c1999fe..5c954a9 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -54,6 +54,7 @@
 	ctx.RegisterEarlyMutator("link", cc.LinkageMutator)
 
 	// Singletons
+	ctx.RegisterSingletonType("checkbuild", common.CheckbuildSingleton)
 
 	configuration, err := config.New(srcDir)
 	if err != nil {
diff --git a/common/checkbuild.go b/common/checkbuild.go
new file mode 100644
index 0000000..daa70a7
--- /dev/null
+++ b/common/checkbuild.go
@@ -0,0 +1,43 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package common
+
+import (
+	"blueprint"
+)
+
+func CheckbuildSingleton() blueprint.Singleton {
+	return &checkbuildSingleton{}
+}
+
+type checkbuildSingleton struct{}
+
+func (c *checkbuildSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
+	deps := []string{}
+	ctx.VisitAllModules(func(module blueprint.Module) {
+		if a, ok := module.(AndroidModule); ok {
+			if len(a.base().checkbuildFiles) > 0 {
+				deps = append(deps, ctx.ModuleName(module)+"-checkbuild")
+			}
+		}
+	})
+
+	ctx.Build(pctx, blueprint.BuildParams{
+		Rule:      blueprint.Phony,
+		Outputs:   []string{"checkbuild"},
+		Implicits: deps,
+		Optional:  true,
+	})
+}
diff --git a/common/module.go b/common/module.go
index 0cbe4b0..515327a 100644
--- a/common/module.go
+++ b/common/module.go
@@ -212,18 +212,45 @@
 	}
 
 	allInstalledFiles := []string{}
+	allCheckbuildFiles := []string{}
 	ctx.VisitAllModuleVariants(func(module blueprint.Module) {
 		if androidModule, ok := module.(AndroidModule); ok {
 			files := androidModule.base().installFiles
 			allInstalledFiles = append(allInstalledFiles, files...)
+			files = androidModule.base().checkbuildFiles
+			allCheckbuildFiles = append(allCheckbuildFiles, files...)
 		}
 	})
 
+	deps := []string{}
+
 	if len(allInstalledFiles) > 0 {
+		name := ctx.ModuleName() + "-install"
 		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:    blueprint.Phony,
-			Outputs: []string{ctx.ModuleName()},
-			Inputs:  allInstalledFiles,
+			Rule:      blueprint.Phony,
+			Outputs:   []string{name},
+			Implicits: allInstalledFiles,
+		})
+		deps = append(deps, name)
+	}
+
+	if len(allCheckbuildFiles) > 0 {
+		name := ctx.ModuleName() + "-checkbuild"
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:      blueprint.Phony,
+			Outputs:   []string{name},
+			Implicits: allCheckbuildFiles,
+			Optional:  true,
+		})
+		deps = append(deps, name)
+	}
+
+	if len(deps) > 0 {
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:      blueprint.Phony,
+			Outputs:   []string{ctx.ModuleName()},
+			Implicits: deps,
+			Optional:  true,
 		})
 	}
 }
@@ -262,6 +289,9 @@
 	if ctx.Failed() {
 		return
 	}
+
+	a.installFiles = append(a.installFiles, androidCtx.installFiles...)
+	a.checkbuildFiles = append(a.checkbuildFiles, androidCtx.checkbuildFiles...)
 }
 
 type androidModuleContext struct {
@@ -300,7 +330,6 @@
 	})
 
 	a.installFiles = append(a.installFiles, fullInstallPath)
-	a.checkbuildFiles = append(a.checkbuildFiles, srcPath)
 }
 
 func (a *androidModuleContext) CheckbuildFile(srcPath string) {