Rebootstrap for changes to any bootstrap go binaries

The final build.ninja can't contain a rules to build the primary
builder or it will create a circular dependency, so rules for all
bootstrap go packages and binaries are replaced with phony targets
that trigger a re-bootstrap to rebuild.  Add all bootstrap go
binaries to the dependencies that trigger a rebuild so that they
all get rebuilt.

Also fix the phony rule generation to include rules for the
intermediate files of bootstrap go binaries so that they don't
get deleted by the cleanup phase when the primary builder runs.

Change-Id: Id0cc287789f74cc4b21b062086723712b57ee051
diff --git a/blueprint/bootstrap/bootstrap.go b/blueprint/bootstrap/bootstrap.go
index 99d6177..2d979a8 100644
--- a/blueprint/bootstrap/bootstrap.go
+++ b/blueprint/bootstrap/bootstrap.go
@@ -180,7 +180,7 @@
 		buildGoPackage(ctx, g.pkgRoot, g.properties.PkgPath, g.archiveFile,
 			g.properties.Srcs)
 	} else {
-		phonyGoTarget(ctx, g.archiveFile, g.properties.Srcs)
+		phonyGoTarget(ctx, g.archiveFile, g.properties.Srcs, nil)
 	}
 }
 
@@ -241,7 +241,8 @@
 			Inputs:  []string{aoutFile},
 		})
 	} else {
-		phonyGoTarget(ctx, binaryFile, g.properties.Srcs)
+		intermediates := []string{aoutFile, archiveFile}
+		phonyGoTarget(ctx, binaryFile, g.properties.Srcs, intermediates)
 	}
 }
 
@@ -279,7 +280,9 @@
 	})
 }
 
-func phonyGoTarget(ctx blueprint.ModuleContext, target string, srcs []string) {
+func phonyGoTarget(ctx blueprint.ModuleContext, target string, srcs []string,
+	intermediates []string) {
+
 	var depTargets []string
 	ctx.VisitDepsDepthFirstIf(isGoPackageProducer,
 		func(module blueprint.Module) {
@@ -309,6 +312,17 @@
 			Outputs: []string{src},
 		})
 	}
+
+	// If there is no rule to build the intermediate files of a bootstrap go package
+	// the cleanup phase of the primary builder will delete the intermediate files,
+	// forcing an unnecessary rebuild.  Add phony rules for all of them.
+	for _, intermediate := range intermediates {
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:    blueprint.Phony,
+			Outputs: []string{intermediate},
+		})
+	}
+
 }
 
 type singleton struct{}
@@ -448,23 +462,22 @@
 	} else {
 		// We're generating a non-bootstrapper Ninja file, so we need to set it
 		// up to depend on the bootstrapper Ninja file.  The build.ninja target
-		// also has an implicit dependency on the primary builder, which will
-		// have a phony dependency on all its sources.  This will cause any
-		// changes to the primary builder's sources to trigger a re-bootstrap
-		// operation, which will rebuild the primary builder.
+		// also has an implicit dependency on the primary builder and all other
+		// bootstrap go binaries, which will have phony dependencies on all of
+		// their sources.  This will cause any changes to a bootstrap binary's
+		// sources to trigger a re-bootstrap operation, which will rebuild the
+		// binary.
 		//
 		// On top of that we need to use the depfile generated by the bigbp
 		// rule.  We do this by depending on that file and then setting up a
 		// phony rule to generate it that uses the depfile.
+		buildNinjaDeps := []string{"$bootstrapCmd", mainNinjaFile}
+		buildNinjaDeps = append(buildNinjaDeps, allGoBinaries...)
 		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:    rebootstrap,
-			Outputs: []string{"build.ninja"},
-			Inputs:  []string{"$bootstrapManifest"},
-			Implicits: []string{
-				"$bootstrapCmd",
-				primaryBuilderFile,
-				mainNinjaFile,
-			},
+			Rule:      rebootstrap,
+			Outputs:   []string{"build.ninja"},
+			Inputs:    []string{"$bootstrapManifest"},
+			Implicits: buildNinjaDeps,
 		})
 
 		ctx.Build(pctx, blueprint.BuildParams{