Merge branch 'master' into danw/multistagebootstrap

Pull in the latest travis config

Change-Id: I71b5900237378877520a68d639260819d28d1b25
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..de99854
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+out.test
diff --git a/.travis.yml b/.travis.yml
index 0d736e8..1004a71 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -13,9 +13,9 @@
 
 script:
     - go test ./...
-    - cp build.ninja.in build.ninja.in.orig
     - mkdir stage
     - cd stage 
     - ../bootstrap.bash
     - ninja
-    - diff -us ../build.ninja.in ../build.ninja.in.orig
+    - diff -us ../build.ninja.in .bootstrap/bootstrap.ninja.in
+    - ../tests/test.sh
diff --git a/Blueprints b/Blueprints
index 46b5c84..173017a 100644
--- a/Blueprints
+++ b/Blueprints
@@ -98,7 +98,7 @@
     ],
 )
 
-bootstrap_go_binary(
+bootstrap_core_go_binary(
     name = "minibp",
     deps = [
         "blueprint",
@@ -119,7 +119,12 @@
     srcs = ["bpmodify/bpmodify.go"],
 )
 
-bootstrap_go_binary(
+bootstrap_core_go_binary(
     name = "gotestmain",
     srcs = ["gotestmain/gotestmain.go"],
 )
+
+bootstrap_core_go_binary(
+    name = "choosestage",
+    srcs = ["choosestage/choosestage.go"],
+)
diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go
index c07f863..7ed2a44 100644
--- a/bootstrap/bootstrap.go
+++ b/bootstrap/bootstrap.go
@@ -16,9 +16,7 @@
 
 import (
 	"fmt"
-	"os"
 	"path/filepath"
-	"strconv"
 	"strings"
 
 	"github.com/google/blueprint"
@@ -26,35 +24,15 @@
 )
 
 const bootstrapDir = ".bootstrap"
+const miniBootstrapDir = ".minibootstrap"
 
 var (
 	pctx = blueprint.NewPackageContext("github.com/google/blueprint/bootstrap")
 
-	gcCmd         = pctx.StaticVariable("gcCmd", "$goToolDir/${goChar}g")
-	linkCmd       = pctx.StaticVariable("linkCmd", "$goToolDir/${goChar}l")
-	goTestMainCmd = pctx.StaticVariable("goTestMainCmd", filepath.Join(bootstrapDir, "bin", "gotestmain"))
-
-	// Ninja only reinvokes itself once when it regenerates a .ninja file. For
-	// the re-bootstrap process we need that to happen more than once, so we
-	// invoke an additional Ninja process from the rebootstrap rule.
-	// Unfortunately this seems to cause "warning: bad deps log signature or
-	// version; starting over" messages from Ninja. This warning can be
-	// avoided by having the bootstrap and non-bootstrap build manifests have
-	// a different builddir (so they use different log files).
-	//
-	// This workaround can be avoided entirely by making a simple change to
-	// Ninja that would allow it to rebuild the manifest multiple times rather
-	// than just once.  If the Ninja being used is capable of this, then the
-	// workaround we're doing can be disabled by setting the
-	// BLUEPRINT_NINJA_HAS_MULTIPASS environment variable to a true value.
-	runChildNinja = pctx.VariableFunc("runChildNinja",
-		func(config interface{}) (string, error) {
-			if ninjaHasMultipass(config) {
-				return "", nil
-			} else {
-				return " && ninja", nil
-			}
-		})
+	gcCmd          = pctx.StaticVariable("gcCmd", "$goToolDir/${goChar}g")
+	linkCmd        = pctx.StaticVariable("linkCmd", "$goToolDir/${goChar}l")
+	goTestMainCmd  = pctx.StaticVariable("goTestMainCmd", filepath.Join(bootstrapDir, "bin", "gotestmain"))
+	chooseStageCmd = pctx.StaticVariable("chooseStageCmd", filepath.Join(bootstrapDir, "bin", "choosestage"))
 
 	gc = pctx.StaticRule("gc",
 		blueprint.RuleParams{
@@ -99,12 +77,19 @@
 			Generator:   true,
 		})
 
-	rebootstrap = pctx.StaticRule("rebootstrap",
+	chooseStage = pctx.StaticRule("chooseStage",
 		blueprint.RuleParams{
-			Command:     "$bootstrapCmd -i $in$runChildNinja",
-			Description: "re-bootstrap $in",
-			Generator:   true,
-		})
+			Command:     "$chooseStageCmd --current $current --bootstrap $bootstrapManifest -o $out $in",
+			Description: "choosing next stage",
+		},
+		"current", "generator")
+
+	touch = pctx.StaticRule("touch",
+		blueprint.RuleParams{
+			Command:     "touch $out",
+			Description: "touch $out",
+		},
+		"depfile", "generator")
 
 	// Work around a Ninja issue.  See https://github.com/martine/ninja/pull/634
 	phony = pctx.StaticRule("phony",
@@ -121,6 +106,23 @@
 	docsDir = filepath.Join(bootstrapDir, "docs")
 )
 
+type bootstrapGoCore interface {
+	BuildStage() Stage
+	SetBuildStage(Stage)
+}
+
+func propagateStageBootstrap(mctx blueprint.TopDownMutatorContext) {
+	if mod, ok := mctx.Module().(bootstrapGoCore); !ok || mod.BuildStage() != StageBootstrap {
+		return
+	}
+
+	mctx.VisitDirectDeps(func (mod blueprint.Module) {
+			if m, ok := mod.(bootstrapGoCore); ok {
+				m.SetBuildStage(StageBootstrap)
+			}
+		})
+}
+
 type goPackageProducer interface {
 	GoPkgRoot() string
 	GoPackageTarget() string
@@ -133,6 +135,7 @@
 
 type goTestProducer interface {
 	GoTestTarget() string
+	BuildStage() Stage
 }
 
 func isGoTestProducer(module blueprint.Module) bool {
@@ -151,17 +154,6 @@
 	return isBinary
 }
 
-// ninjaHasMultipass returns true if Ninja will perform multiple passes
-// that can regenerate the build manifest.
-func ninjaHasMultipass(config interface{}) bool {
-	envString := os.Getenv("BLUEPRINT_NINJA_HAS_MULTIPASS")
-	envValue, err := strconv.ParseBool(envString)
-	if err != nil {
-		return false
-	}
-	return envValue
-}
-
 // A goPackage is a module for building Go packages.
 type goPackage struct {
 	properties struct {
@@ -182,6 +174,9 @@
 
 	// The bootstrap Config
 	config *Config
+
+	// The stage in which this module should be built
+	buildStage Stage
 }
 
 var _ goPackageProducer = (*goPackage)(nil)
@@ -207,6 +202,14 @@
 	return g.testArchiveFile
 }
 
+func (g *goPackage) BuildStage() Stage {
+	return g.buildStage
+}
+
+func (g *goPackage) SetBuildStage(buildStage Stage) {
+	g.buildStage = buildStage
+}
+
 func (g *goPackage) GenerateBuildActions(ctx blueprint.ModuleContext) {
 	name := ctx.ModuleName()
 
@@ -228,7 +231,7 @@
 	// the circular dependence that occurs when the builder requires a new Ninja
 	// file to be built, but building a new ninja file requires the builder to
 	// be built.
-	if g.config.generatingBootstrapper {
+	if g.config.stage == g.BuildStage() {
 		var deps []string
 
 		if g.config.runGoTests {
@@ -239,7 +242,7 @@
 
 		buildGoPackage(ctx, g.pkgRoot, g.properties.PkgPath, g.archiveFile,
 			g.properties.Srcs, deps)
-	} else {
+	} else if g.config.stage != StageBootstrap {
 		if len(g.properties.TestSrcs) > 0 && g.config.runGoTests {
 			phonyGoTarget(ctx, g.testArchiveFile, g.properties.TestSrcs, nil)
 		}
@@ -260,12 +263,16 @@
 
 	// The bootstrap Config
 	config *Config
+
+	// The stage in which this module should be built
+	buildStage Stage
 }
 
-func newGoBinaryModuleFactory(config *Config) func() (blueprint.Module, []interface{}) {
+func newGoBinaryModuleFactory(config *Config, buildStage Stage) func() (blueprint.Module, []interface{}) {
 	return func() (blueprint.Module, []interface{}) {
 		module := &goBinary{
-			config: config,
+			config:     config,
+			buildStage: buildStage,
 		}
 		return module, []interface{}{&module.properties}
 	}
@@ -275,6 +282,14 @@
 	return g.testArchiveFile
 }
 
+func (g *goBinary) BuildStage() Stage {
+	return g.buildStage
+}
+
+func (g *goBinary) SetBuildStage(buildStage Stage) {
+	g.buildStage = buildStage
+}
+
 func (g *goBinary) GenerateBuildActions(ctx blueprint.ModuleContext) {
 	var (
 		name        = ctx.ModuleName()
@@ -293,7 +308,7 @@
 	// the circular dependence that occurs when the builder requires a new Ninja
 	// file to be built, but building a new ninja file requires the builder to
 	// be built.
-	if g.config.generatingBootstrapper {
+	if g.config.stage == g.BuildStage() {
 		var deps []string
 
 		if g.config.runGoTests {
@@ -329,7 +344,7 @@
 			Outputs: []string{binaryFile},
 			Inputs:  []string{aoutFile},
 		})
-	} else {
+	} else if g.config.stage != StageBootstrap {
 		if len(g.properties.TestSrcs) > 0 && g.config.runGoTests {
 			phonyGoTarget(ctx, g.testArchiveFile, g.properties.TestSrcs, nil)
 		}
@@ -508,13 +523,21 @@
 	// creating the binary that we'll use to generate the non-bootstrap
 	// build.ninja file.
 	var primaryBuilders []*goBinary
+	// rebootstrapDeps contains modules that will be built in StageBootstrap
 	var rebootstrapDeps []string
+	// primaryRebootstrapDeps contains modules that will be built in StagePrimary
+	var primaryRebootstrapDeps []string
 	ctx.VisitAllModulesIf(isBootstrapBinaryModule,
 		func(module blueprint.Module) {
 			binaryModule := module.(*goBinary)
 			binaryModuleName := ctx.ModuleName(binaryModule)
 			binaryModulePath := filepath.Join(BinDir, binaryModuleName)
-			rebootstrapDeps = append(rebootstrapDeps, binaryModulePath)
+
+			if binaryModule.BuildStage() == StageBootstrap {
+				rebootstrapDeps = append(rebootstrapDeps, binaryModulePath)
+			} else {
+				primaryRebootstrapDeps = append(primaryRebootstrapDeps, binaryModulePath)
+			}
 			if binaryModule.properties.PrimaryBuilder {
 				primaryBuilders = append(primaryBuilders, binaryModule)
 			}
@@ -548,28 +571,166 @@
 	}
 
 	// Get the filename of the top-level Blueprints file to pass to minibp.
-	// This comes stored in a global variable that's set by Main.
 	topLevelBlueprints := filepath.Join("$srcDir",
 		filepath.Base(s.config.topLevelBlueprintsFile))
 
+	rebootstrapDeps = append(rebootstrapDeps, topLevelBlueprints)
+	primaryRebootstrapDeps = append(primaryRebootstrapDeps, topLevelBlueprints)
+
 	mainNinjaFile := filepath.Join(bootstrapDir, "main.ninja.in")
-	mainNinjaDepFile := mainNinjaFile + ".d"
+	mainNinjaTimestampFile := mainNinjaFile + ".timestamp"
+	mainNinjaTimestampDepFile := mainNinjaTimestampFile + ".d"
+	primaryBuilderNinjaFile := filepath.Join(bootstrapDir, "primary.ninja.in")
+	primaryBuilderNinjaTimestampFile := primaryBuilderNinjaFile + ".timestamp"
+	primaryBuilderNinjaTimestampDepFile := primaryBuilderNinjaTimestampFile + ".d"
 	bootstrapNinjaFile := filepath.Join(bootstrapDir, "bootstrap.ninja.in")
 	docsFile := filepath.Join(docsDir, primaryBuilderName+".html")
 
-	rebootstrapDeps = append(rebootstrapDeps, docsFile)
+	primaryRebootstrapDeps = append(primaryRebootstrapDeps, docsFile)
 
-	if s.config.generatingBootstrapper {
+	// If the tests change, be sure to re-run them. These need to be
+	// dependencies for the ninja file so that it's updated after these
+	// run. Otherwise we'd never leave the bootstrap stage, since the
+	// timestamp file would be newer than the ninja file.
+	ctx.VisitAllModulesIf(isGoTestProducer,
+		func(module blueprint.Module) {
+			testModule := module.(goTestProducer)
+			target := testModule.GoTestTarget()
+			if target != "" {
+				if testModule.BuildStage() == StageBootstrap {
+					rebootstrapDeps = append(rebootstrapDeps, target)
+				} else {
+					primaryRebootstrapDeps = append(primaryRebootstrapDeps, target)
+				}
+			}
+		})
+
+	switch s.config.stage {
+	case StageBootstrap:
 		// We're generating a bootstrapper Ninja file, so we need to set things
 		// up to rebuild the build.ninja file using the primary builder.
 
-		// Because the non-bootstrap build.ninja file manually re-invokes Ninja,
-		// its builddir must be different than that of the bootstrap build.ninja
-		// file.  Otherwise we occasionally get "warning: bad deps log signature
-		// or version; starting over" messages from Ninja, presumably because
-		// two Ninja processes try to write to the same log concurrently.
+		// BuildDir must be different between the three stages, otherwise the
+		// cleanup process will remove files from the other builds.
+		ctx.SetBuildDir(pctx, miniBootstrapDir)
+
+		// Generate the Ninja file to build the primary builder. Save the
+		// timestamps and deps, so that we can come back to this stage if
+		// it needs to be regenerated.
+		primarybp := ctx.Rule(pctx, "primarybp",
+			blueprint.RuleParams{
+				Command: fmt.Sprintf("%s --build-primary $runTests -m $bootstrapManifest "+
+					"--timestamp $timestamp --timestampdep $timestampdep "+
+					"-d $outfile.d -o $outfile $in", minibpFile),
+				Description: "minibp $outfile",
+				Depfile:     "$outfile.d",
+			},
+			"runTests", "timestamp", "timestampdep", "outfile")
+
+		args := map[string]string{
+			"outfile":      primaryBuilderNinjaFile,
+			"timestamp":    primaryBuilderNinjaTimestampFile,
+			"timestampdep": primaryBuilderNinjaTimestampDepFile,
+		}
+
+		if s.config.runGoTests {
+			args["runTests"] = "-t"
+		}
+
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:      primarybp,
+			Outputs:   []string{primaryBuilderNinjaFile, primaryBuilderNinjaTimestampFile},
+			Inputs:    []string{topLevelBlueprints},
+			Implicits: rebootstrapDeps,
+			Args:      args,
+		})
+
+		// Rebuild the bootstrap Ninja file using the minibp that we just built.
+		// If this produces a difference, choosestage will retrigger this stage.
+		minibp := ctx.Rule(pctx, "minibp",
+			blueprint.RuleParams{
+				Command: fmt.Sprintf("%s $runTests -m $bootstrapManifest "+
+					"-d $out.d -o $out $in", minibpFile),
+				Description: "minibp $out",
+				Generator:   true,
+				Depfile:     "$out.d",
+			},
+			"runTests")
+
+		args = map[string]string{}
+
+		if s.config.runGoTests {
+			args["runTests"] = "-t"
+		}
+
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:    minibp,
+			Outputs: []string{bootstrapNinjaFile},
+			Inputs:  []string{topLevelBlueprints},
+			// $bootstrapManifest is here so that when it is updated, we
+			// force a rebuild of bootstrap.ninja.in. chooseStage should
+			// have already copied the new version over, but kept the old
+			// timestamps to force this regeneration.
+			Implicits: []string{"$bootstrapManifest", minibpFile},
+			Args:      args,
+		})
+
+		// When the current build.ninja file is a bootstrapper, we always want
+		// to have it replace itself with a non-bootstrapper build.ninja.  To
+		// accomplish that we depend on a file that should never exist and
+		// "build" it using Ninja's built-in phony rule.
+		notAFile := filepath.Join(bootstrapDir, "notAFile")
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:    blueprint.Phony,
+			Outputs: []string{notAFile},
+		})
+
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:      chooseStage,
+			Outputs:   []string{filepath.Join(bootstrapDir, "build.ninja.in")},
+			Inputs:    []string{bootstrapNinjaFile, primaryBuilderNinjaFile},
+			Implicits: []string{"$chooseStageCmd", "$bootstrapManifest", notAFile},
+			Args: map[string]string{
+				"current": bootstrapNinjaFile,
+			},
+		})
+
+	case StagePrimary:
+		// We're generating a bootstrapper Ninja file, so we need to set things
+		// up to rebuild the build.ninja file using the primary builder.
+
+		// BuildDir must be different between the three stages, otherwise the
+		// cleanup process will remove files from the other builds.
 		ctx.SetBuildDir(pctx, bootstrapDir)
 
+		// We generate the depfile here that includes the dependencies for all
+		// the Blueprints files that contribute to generating the big build
+		// manifest (build.ninja file).  This depfile will be used by the non-
+		// bootstrap build manifest to determine whether it should touch the
+		// timestamp file to trigger a re-bootstrap.
+		bigbp := ctx.Rule(pctx, "bigbp",
+			blueprint.RuleParams{
+				Command: fmt.Sprintf("%s %s -m $bootstrapManifest "+
+					"--timestamp $timestamp --timestampdep $timestampdep "+
+					"-d $outfile.d -o $outfile $in", primaryBuilderFile,
+					primaryBuilderExtraFlags),
+				Description: fmt.Sprintf("%s $outfile", primaryBuilderName),
+				Depfile:     "$outfile.d",
+			},
+			"timestamp", "timestampdep", "outfile")
+
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:      bigbp,
+			Outputs:   []string{mainNinjaFile, mainNinjaTimestampFile},
+			Inputs:    []string{topLevelBlueprints},
+			Implicits: primaryRebootstrapDeps,
+			Args: map[string]string{
+				"timestamp":    mainNinjaTimestampFile,
+				"timestampdep": mainNinjaTimestampDepFile,
+				"outfile":      mainNinjaFile,
+			},
+		})
+
 		// Generate build system docs for the primary builder.  Generating docs reads the source
 		// files used to build the primary builder, but that dependency will be picked up through
 		// the dependency on the primary builder itself.  There are no dependencies on the
@@ -588,36 +749,24 @@
 			Implicits: []string{primaryBuilderFile},
 		})
 
-		// We generate the depfile here that includes the dependencies for all
-		// the Blueprints files that contribute to generating the big build
-		// manifest (build.ninja file).  This depfile will be used by the non-
-		// bootstrap build manifest to determine whether it should trigger a re-
-		// bootstrap.  Because the re-bootstrap rule's output is "build.ninja"
-		// we need to force the depfile to have that as its "make target"
-		// (recall that depfiles use a subset of the Makefile syntax).
-		bigbp := ctx.Rule(pctx, "bigbp",
-			blueprint.RuleParams{
-				Command: fmt.Sprintf("%s %s -d %s -m $bootstrapManifest "+
-					"-o $out $in", primaryBuilderFile,
-					primaryBuilderExtraFlags, mainNinjaDepFile),
-				Description: fmt.Sprintf("%s $out", primaryBuilderName),
-				Depfile:     mainNinjaDepFile,
-			})
-
+		// Detect whether we need to rebuild the primary stage by going back to
+		// the bootstrapper. If this is newer than the primaryBuilderNinjaFile,
+		// then chooseStage will trigger a rebuild of primaryBuilderNinjaFile by
+		// returning to the bootstrap stage.
 		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:      bigbp,
-			Outputs:   []string{mainNinjaFile},
-			Inputs:    []string{topLevelBlueprints},
+			Rule:      touch,
+			Outputs:   []string{primaryBuilderNinjaTimestampFile},
 			Implicits: rebootstrapDeps,
+			Args: map[string]string{
+				"depfile":   primaryBuilderNinjaTimestampDepFile,
+				"generator": "true",
+			},
 		})
 
 		// When the current build.ninja file is a bootstrapper, we always want
 		// to have it replace itself with a non-bootstrapper build.ninja.  To
 		// accomplish that we depend on a file that should never exist and
 		// "build" it using Ninja's built-in phony rule.
-		//
-		// We also need to add an implicit dependency on bootstrapNinjaFile so
-		// that it gets generated as part of the bootstrap process.
 		notAFile := filepath.Join(bootstrapDir, "notAFile")
 		ctx.Build(pctx, blueprint.BuildParams{
 			Rule:    blueprint.Phony,
@@ -625,103 +774,71 @@
 		})
 
 		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:      bootstrap,
-			Outputs:   []string{"build.ninja"},
-			Inputs:    []string{mainNinjaFile},
-			Implicits: []string{"$bootstrapCmd", notAFile, bootstrapNinjaFile},
-		})
-
-		// Rebuild the bootstrap Ninja file using the minibp that we just built.
-		// The checkFile tells minibp to compare the new bootstrap file to the
-		// current one.  If the files are the same then minibp sets the new
-		// file's mtime to match that of the current one.  If they're different
-		// then the new file will have a newer timestamp than the current one
-		// and it will trigger a reboostrap by the non-boostrap build manifest.
-		minibp := ctx.Rule(pctx, "minibp",
-			blueprint.RuleParams{
-				Command: fmt.Sprintf("%s $runTests -c $checkFile -m $bootstrapManifest "+
-					"-d $out.d -o $out $in", minibpFile),
-				Description: "minibp $out",
-				Generator:   true,
-				Depfile:     "$out.d",
+			Rule:      chooseStage,
+			Outputs:   []string{filepath.Join(bootstrapDir, "build.ninja.in")},
+			Inputs:    []string{bootstrapNinjaFile, primaryBuilderNinjaFile, mainNinjaFile},
+			Implicits: []string{"$chooseStageCmd", "$bootstrapManifest", notAFile, primaryBuilderNinjaTimestampFile},
+			Args: map[string]string{
+				"current": primaryBuilderNinjaFile,
 			},
-			"checkFile", "runTests")
-
-		args := map[string]string{
-			"checkFile": "$bootstrapManifest",
-		}
-
-		if s.config.runGoTests {
-			args["runTests"] = "-t"
-		}
-
-		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:      minibp,
-			Outputs:   []string{bootstrapNinjaFile},
-			Inputs:    []string{topLevelBlueprints},
-			Implicits: []string{minibpFile},
-			Args:      args,
 		})
-	} else {
-		ctx.VisitAllModulesIf(isGoTestProducer,
-			func(module blueprint.Module) {
-				testModule := module.(goTestProducer)
-				target := testModule.GoTestTarget()
-				if target != "" {
-					rebootstrapDeps = append(rebootstrapDeps, target)
-				}
-			})
 
+		// Create this phony rule so that upgrades don't delete these during
+		// cleanup
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:    blueprint.Phony,
+			Outputs: []string{bootstrapNinjaFile},
+		})
+
+	case StageMain:
 		// 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 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.
+		// up to re-bootstrap if necessary. We do this by making build.ninja.in
+		// depend on the various Ninja files, the source build.ninja.in, and
+		// on the timestamp files.
 		//
-		// 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, rebootstrapDeps...)
-
+		// The timestamp files themselves are set up with the same dependencies
+		// as their Ninja files, including their own depfile. If any of the
+		// dependencies need to be updated, we'll touch the timestamp file,
+		// which will tell choosestage to switch to the stage that rebuilds
+		// that Ninja file.
 		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:      rebootstrap,
-			Outputs:   []string{"build.ninja"},
-			Inputs:    []string{"$bootstrapManifest"},
-			Implicits: buildNinjaDeps,
-		})
-
-		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:    phony,
-			Outputs: []string{mainNinjaFile},
-			Inputs:  []string{topLevelBlueprints},
+			Rule:      touch,
+			Outputs:   []string{primaryBuilderNinjaTimestampFile},
+			Implicits: rebootstrapDeps,
 			Args: map[string]string{
-				"depfile": mainNinjaDepFile,
-			},
-		})
-
-		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:      phony,
-			Outputs:   []string{docsFile},
-			Implicits: []string{primaryBuilderFile},
-		})
-
-		// If the bootstrap Ninja invocation caused a new bootstrapNinjaFile to be
-		// generated then that means we need to rebootstrap using it instead of
-		// the current bootstrap manifest.  We enable the Ninja "generator"
-		// behavior so that Ninja doesn't invoke this build just because it's
-		// missing a command line log entry for the bootstrap manifest.
-		ctx.Build(pctx, blueprint.BuildParams{
-			Rule:    cp,
-			Outputs: []string{"$bootstrapManifest"},
-			Inputs:  []string{bootstrapNinjaFile},
-			Args: map[string]string{
+				"depfile":   primaryBuilderNinjaTimestampDepFile,
 				"generator": "true",
 			},
 		})
 
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:      touch,
+			Outputs:   []string{mainNinjaTimestampFile},
+			Implicits: primaryRebootstrapDeps,
+			Args: map[string]string{
+				"depfile":   mainNinjaTimestampDepFile,
+				"generator": "true",
+			},
+		})
+
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:      chooseStage,
+			Outputs:   []string{filepath.Join(bootstrapDir, "build.ninja.in")},
+			Inputs:    []string{bootstrapNinjaFile, primaryBuilderNinjaFile, mainNinjaFile},
+			Implicits: []string{"$chooseStageCmd", "$bootstrapManifest", primaryBuilderNinjaTimestampFile, mainNinjaTimestampFile},
+			Args: map[string]string{
+				"current":   mainNinjaFile,
+				"generator": "true",
+			},
+		})
+
+		// Create this phony rule so that upgrades don't delete these during
+		// cleanup
+		ctx.Build(pctx, blueprint.BuildParams{
+			Rule:    blueprint.Phony,
+			Outputs: []string{mainNinjaFile, docsFile},
+		})
+
 		if primaryBuilderName == "minibp" {
 			// This is a standalone Blueprint build, so we copy the minibp
 			// binary to the "bin" directory to make it easier to find.
@@ -733,6 +850,13 @@
 			})
 		}
 	}
+
+	ctx.Build(pctx, blueprint.BuildParams{
+		Rule:      bootstrap,
+		Outputs:   []string{"build.ninja"},
+		Inputs:    []string{filepath.Join(bootstrapDir, "build.ninja.in")},
+		Implicits: []string{"$bootstrapCmd"},
+	})
 }
 
 // packageRoot returns the module-specific package root directory path.  This
diff --git a/bootstrap/cleanup.go b/bootstrap/cleanup.go
index f3cb634..dd698f4 100644
--- a/bootstrap/cleanup.go
+++ b/bootstrap/cleanup.go
@@ -33,7 +33,10 @@
 	srcDir, manifestFile string) error {
 
 	buildDir := "."
-	if config.generatingBootstrapper {
+	switch config.stage {
+	case StageBootstrap:
+		buildDir = miniBootstrapDir
+	case StagePrimary:
 		buildDir = bootstrapDir
 	}
 
diff --git a/bootstrap/command.go b/bootstrap/command.go
index 932cfd7..33c4227 100644
--- a/bootstrap/command.go
+++ b/bootstrap/command.go
@@ -29,19 +29,21 @@
 )
 
 var (
-	outFile      string
-	depFile      string
-	checkFile    string
-	manifestFile string
-	docFile      string
-	cpuprofile   string
-	runGoTests   bool
+	outFile          string
+	depFile          string
+	timestampFile    string
+	timestampDepFile string
+	manifestFile     string
+	docFile          string
+	cpuprofile       string
+	runGoTests       bool
 )
 
 func init() {
 	flag.StringVar(&outFile, "o", "build.ninja.in", "the Ninja file to output")
 	flag.StringVar(&depFile, "d", "", "the dependency file to output")
-	flag.StringVar(&checkFile, "c", "", "the existing file to check against")
+	flag.StringVar(&timestampFile, "timestamp", "", "file to write before the output file")
+	flag.StringVar(&timestampDepFile, "timestampdep", "", "the dependency file for the timestamp file")
 	flag.StringVar(&manifestFile, "m", "", "the bootstrap manifest file")
 	flag.StringVar(&docFile, "docs", "", "build documentation file to output")
 	flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
@@ -69,19 +71,26 @@
 		fatalf("no Blueprints file specified")
 	}
 
-	generatingBootstrapper := false
+	stage := StageMain
 	if c, ok := config.(ConfigInterface); ok {
-		generatingBootstrapper = c.GeneratingBootstrapper()
+		if c.GeneratingBootstrapper() {
+			stage = StageBootstrap
+		}
+		if c.GeneratingPrimaryBuilder() {
+			stage = StagePrimary
+		}
 	}
 
 	bootstrapConfig := &Config{
-		generatingBootstrapper: generatingBootstrapper,
+		stage: stage,
 		topLevelBlueprintsFile: flag.Arg(0),
 		runGoTests:             runGoTests,
 	}
 
 	ctx.RegisterModuleType("bootstrap_go_package", newGoPackageModuleFactory(bootstrapConfig))
-	ctx.RegisterModuleType("bootstrap_go_binary", newGoBinaryModuleFactory(bootstrapConfig))
+	ctx.RegisterModuleType("bootstrap_core_go_binary", newGoBinaryModuleFactory(bootstrapConfig, StageBootstrap))
+	ctx.RegisterModuleType("bootstrap_go_binary", newGoBinaryModuleFactory(bootstrapConfig, StagePrimary))
+	ctx.RegisterTopDownMutator("bootstrap_stage", propagateStageBootstrap)
 	ctx.RegisterSingletonType("bootstrap", newSingletonFactory(bootstrapConfig))
 
 	deps, errs := ctx.ParseBlueprintsFiles(bootstrapConfig.topLevelBlueprintsFile)
@@ -118,48 +127,34 @@
 	}
 
 	const outFilePermissions = 0666
+	if timestampFile != "" {
+		err := ioutil.WriteFile(timestampFile, []byte{}, outFilePermissions)
+		if err != nil {
+			fatalf("error writing %s: %s", timestampFile, err)
+		}
+
+		if timestampDepFile != "" {
+			err := deptools.WriteDepFile(timestampDepFile, timestampFile, deps)
+			if err != nil {
+				fatalf("error writing depfile: %s", err)
+			}
+		}
+	}
+
 	err = ioutil.WriteFile(outFile, buf.Bytes(), outFilePermissions)
 	if err != nil {
 		fatalf("error writing %s: %s", outFile, err)
 	}
 
-	if checkFile != "" {
-		checkData, err := ioutil.ReadFile(checkFile)
-		if err != nil {
-			fatalf("error reading %s: %s", checkFile, err)
-		}
-
-		matches := buf.Len() == len(checkData)
-		if matches {
-			for i, value := range buf.Bytes() {
-				if value != checkData[i] {
-					matches = false
-					break
-				}
-			}
-		}
-
-		if matches {
-			// The new file content matches the check-file content, so we set
-			// the new file's mtime and atime to match that of the check-file.
-			checkFileInfo, err := os.Stat(checkFile)
-			if err != nil {
-				fatalf("error stat'ing %s: %s", checkFile, err)
-			}
-
-			time := checkFileInfo.ModTime()
-			err = os.Chtimes(outFile, time, time)
-			if err != nil {
-				fatalf("error setting timestamps for %s: %s", outFile, err)
-			}
-		}
-	}
-
 	if depFile != "" {
 		err := deptools.WriteDepFile(depFile, outFile, deps)
 		if err != nil {
 			fatalf("error writing depfile: %s", err)
 		}
+		err = deptools.WriteDepFile(depFile+".timestamp", outFile+".timestamp", deps)
+		if err != nil {
+			fatalf("error writing depfile: %s", err)
+		}
 	}
 
 	srcDir := filepath.Dir(bootstrapConfig.topLevelBlueprintsFile)
diff --git a/bootstrap/config.go b/bootstrap/config.go
index ce96318..b236cc7 100644
--- a/bootstrap/config.go
+++ b/bootstrap/config.go
@@ -36,13 +36,21 @@
 	// creating a build.ninja.in file to be used in a build bootstrapping
 	// sequence.
 	GeneratingBootstrapper() bool
+	// GeneratingPrimaryBuilder should return true if this build invocation is
+	// creating a build.ninja.in file to be used to build the primary builder
+	GeneratingPrimaryBuilder() bool
 }
 
+type Stage int
+
+const (
+	StageBootstrap Stage = iota
+	StagePrimary
+	StageMain
+)
+
 type Config struct {
-	// generatingBootstrapper should be true if this build invocation is
-	// creating a build.ninja.in file to be used in a build bootstrapping
-	// sequence.
-	generatingBootstrapper bool
+	stage Stage
 
 	topLevelBlueprintsFile string
 
diff --git a/bootstrap/minibp/main.go b/bootstrap/minibp/main.go
index ad36c29..4e25bc8 100644
--- a/bootstrap/minibp/main.go
+++ b/bootstrap/minibp/main.go
@@ -21,15 +21,24 @@
 )
 
 var runAsPrimaryBuilder bool
+var buildPrimaryBuilder bool
 
 func init() {
 	flag.BoolVar(&runAsPrimaryBuilder, "p", false, "run as a primary builder")
+	flag.BoolVar(&buildPrimaryBuilder, "build-primary", false, "build the primary builder")
 }
 
-type Config bool
+type Config struct {
+	generatingBootstrapper   bool
+	generatingPrimaryBuilder bool
+}
 
 func (c Config) GeneratingBootstrapper() bool {
-	return bool(c)
+	return c.generatingBootstrapper
+}
+
+func (c Config) GeneratingPrimaryBuilder() bool {
+	return c.generatingPrimaryBuilder
 }
 
 func main() {
@@ -40,7 +49,10 @@
 		ctx.SetIgnoreUnknownModuleTypes(true)
 	}
 
-	config := Config(!runAsPrimaryBuilder)
+	config := Config{
+		generatingBootstrapper:   !runAsPrimaryBuilder && !buildPrimaryBuilder,
+		generatingPrimaryBuilder: !runAsPrimaryBuilder && buildPrimaryBuilder,
+	}
 
 	bootstrap.Main(ctx, config)
 }
diff --git a/build.ninja.in b/build.ninja.in
index 3f43e21..a5a353a 100644
--- a/build.ninja.in
+++ b/build.ninja.in
@@ -7,12 +7,14 @@
 #
 #     bootstrap [from Go package github.com/google/blueprint/bootstrap]
 #
-ninja_required_version = 1.1.0
+ninja_required_version = 1.6.0
 
 g.bootstrap.bootstrapCmd = @@Bootstrap@@
 
 g.bootstrap.bootstrapManifest = @@BootstrapManifest@@
 
+g.bootstrap.chooseStageCmd = .bootstrap/bin/choosestage
+
 g.bootstrap.goRoot = @@GoRoot@@
 
 g.bootstrap.goOS = @@GoOS@@
@@ -29,13 +31,17 @@
 
 g.bootstrap.srcDir = @@SrcDir@@
 
-builddir = .bootstrap
+builddir = .minibootstrap
 
 rule g.bootstrap.bootstrap
     command = ${g.bootstrap.bootstrapCmd} -i ${in}
     description = bootstrap ${in}
     generator = true
 
+rule g.bootstrap.chooseStage
+    command = ${g.bootstrap.chooseStageCmd} --current ${current} --bootstrap ${g.bootstrap.bootstrapManifest} -o ${out} ${in}
+    description = choosing next stage
+
 rule g.bootstrap.cp
     command = cp ${in} ${out}
     description = cp ${out}
@@ -177,53 +183,29 @@
         .bootstrap/blueprint-proptools/pkg/github.com/google/blueprint/proptools.a
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
-# Module:  bpfmt
+# Module:  choosestage
 # Variant:
-# Type:    bootstrap_go_binary
+# Type:    bootstrap_core_go_binary
 # Factory: github.com/google/blueprint/bootstrap.func·003
-# Defined: Blueprints:110:1
+# Defined: Blueprints:127:1
 
-build .bootstrap/bpfmt/obj/bpfmt.a: g.bootstrap.gc $
-        ${g.bootstrap.srcDir}/bpfmt/bpfmt.go | ${g.bootstrap.gcCmd} $
-        .bootstrap/blueprint-parser/pkg/github.com/google/blueprint/parser.a
-    incFlags = -I .bootstrap/blueprint-parser/pkg
-    pkgPath = bpfmt
-default .bootstrap/bpfmt/obj/bpfmt.a
+build .bootstrap/choosestage/obj/choosestage.a: g.bootstrap.gc $
+        ${g.bootstrap.srcDir}/choosestage/choosestage.go | $
+        ${g.bootstrap.gcCmd}
+    pkgPath = choosestage
+default .bootstrap/choosestage/obj/choosestage.a
 
-build .bootstrap/bpfmt/obj/a.out: g.bootstrap.link $
-        .bootstrap/bpfmt/obj/bpfmt.a | ${g.bootstrap.linkCmd}
-    libDirFlags = -L .bootstrap/blueprint-parser/pkg
-default .bootstrap/bpfmt/obj/a.out
-
-build .bootstrap/bin/bpfmt: g.bootstrap.cp .bootstrap/bpfmt/obj/a.out
-default .bootstrap/bin/bpfmt
-
-# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
-# Module:  bpmodify
-# Variant:
-# Type:    bootstrap_go_binary
-# Factory: github.com/google/blueprint/bootstrap.func·003
-# Defined: Blueprints:116:1
-
-build .bootstrap/bpmodify/obj/bpmodify.a: g.bootstrap.gc $
-        ${g.bootstrap.srcDir}/bpmodify/bpmodify.go | ${g.bootstrap.gcCmd} $
-        .bootstrap/blueprint-parser/pkg/github.com/google/blueprint/parser.a
-    incFlags = -I .bootstrap/blueprint-parser/pkg
-    pkgPath = bpmodify
-default .bootstrap/bpmodify/obj/bpmodify.a
-
-build .bootstrap/bpmodify/obj/a.out: g.bootstrap.link $
-        .bootstrap/bpmodify/obj/bpmodify.a | ${g.bootstrap.linkCmd}
-    libDirFlags = -L .bootstrap/blueprint-parser/pkg
-default .bootstrap/bpmodify/obj/a.out
-
-build .bootstrap/bin/bpmodify: g.bootstrap.cp .bootstrap/bpmodify/obj/a.out
-default .bootstrap/bin/bpmodify
+build .bootstrap/choosestage/obj/a.out: g.bootstrap.link $
+        .bootstrap/choosestage/obj/choosestage.a | ${g.bootstrap.linkCmd}
+default .bootstrap/choosestage/obj/a.out
+build .bootstrap/bin/choosestage: g.bootstrap.cp $
+        .bootstrap/choosestage/obj/a.out
+default .bootstrap/bin/choosestage
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 # Module:  gotestmain
 # Variant:
-# Type:    bootstrap_go_binary
+# Type:    bootstrap_core_go_binary
 # Factory: github.com/google/blueprint/bootstrap.func·003
 # Defined: Blueprints:122:1
 
@@ -242,7 +224,7 @@
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 # Module:  minibp
 # Variant:
-# Type:    bootstrap_go_binary
+# Type:    bootstrap_core_go_binary
 # Factory: github.com/google/blueprint/bootstrap.func·003
 # Defined: Blueprints:101:1
 
@@ -271,37 +253,40 @@
 # Singleton: bootstrap
 # Factory:   github.com/google/blueprint/bootstrap.func·008
 
-rule s.bootstrap.bigbpDocs
-    command = .bootstrap/bin/minibp -p --docs ${out} ${g.bootstrap.srcDir}/Blueprints
-    description = minibp docs ${out}
-
-rule s.bootstrap.bigbp
-    command = .bootstrap/bin/minibp -p -d .bootstrap/main.ninja.in.d -m ${g.bootstrap.bootstrapManifest} -o ${out} ${in}
-    depfile = .bootstrap/main.ninja.in.d
-    description = minibp ${out}
+rule s.bootstrap.primarybp
+    command = .bootstrap/bin/minibp --build-primary ${runTests} -m ${g.bootstrap.bootstrapManifest} --timestamp ${timestamp} --timestampdep ${timestampdep} -d ${outfile}.d -o ${outfile} ${in}
+    depfile = ${outfile}.d
+    description = minibp ${outfile}
 
 rule s.bootstrap.minibp
-    command = .bootstrap/bin/minibp ${runTests} -c ${checkFile} -m ${g.bootstrap.bootstrapManifest} -d ${out}.d -o ${out} ${in}
+    command = .bootstrap/bin/minibp ${runTests} -m ${g.bootstrap.bootstrapManifest} -d ${out}.d -o ${out} ${in}
     depfile = ${out}.d
     description = minibp ${out}
     generator = true
 
-build .bootstrap/docs/minibp.html: s.bootstrap.bigbpDocs | $
+build .bootstrap/primary.ninja.in .bootstrap/primary.ninja.in.timestamp: $
+        s.bootstrap.primarybp ${g.bootstrap.srcDir}/Blueprints | $
+        .bootstrap/bin/choosestage .bootstrap/bin/gotestmain $
+        .bootstrap/bin/minibp ${g.bootstrap.srcDir}/Blueprints
+    outfile = .bootstrap/primary.ninja.in
+    timestamp = .bootstrap/primary.ninja.in.timestamp
+    timestampdep = .bootstrap/primary.ninja.in.timestamp.d
+default .bootstrap/primary.ninja.in .bootstrap/primary.ninja.in.timestamp
+
+build .bootstrap/bootstrap.ninja.in: s.bootstrap.minibp $
+        ${g.bootstrap.srcDir}/Blueprints | ${g.bootstrap.bootstrapManifest} $
         .bootstrap/bin/minibp
-default .bootstrap/docs/minibp.html
-build .bootstrap/main.ninja.in: s.bootstrap.bigbp $
-        ${g.bootstrap.srcDir}/Blueprints | .bootstrap/bin/bpfmt $
-        .bootstrap/bin/bpmodify .bootstrap/bin/gotestmain $
-        .bootstrap/bin/minibp .bootstrap/docs/minibp.html
-default .bootstrap/main.ninja.in
+default .bootstrap/bootstrap.ninja.in
 build .bootstrap/notAFile: phony
 default .bootstrap/notAFile
-build build.ninja: g.bootstrap.bootstrap .bootstrap/main.ninja.in | $
-        ${g.bootstrap.bootstrapCmd} .bootstrap/notAFile $
-        .bootstrap/bootstrap.ninja.in
+build .bootstrap/build.ninja.in: g.bootstrap.chooseStage $
+        .bootstrap/bootstrap.ninja.in .bootstrap/primary.ninja.in | $
+        ${g.bootstrap.chooseStageCmd} ${g.bootstrap.bootstrapManifest} $
+        .bootstrap/notAFile
+    current = .bootstrap/bootstrap.ninja.in
+default .bootstrap/build.ninja.in
+
+build build.ninja: g.bootstrap.bootstrap .bootstrap/build.ninja.in | $
+        ${g.bootstrap.bootstrapCmd}
 default build.ninja
-build .bootstrap/bootstrap.ninja.in: s.bootstrap.minibp $
-        ${g.bootstrap.srcDir}/Blueprints | .bootstrap/bin/minibp
-    checkFile = ${g.bootstrap.bootstrapManifest}
-default .bootstrap/bootstrap.ninja.in
 
diff --git a/choosestage/choosestage.go b/choosestage/choosestage.go
new file mode 100644
index 0000000..e1445e0
--- /dev/null
+++ b/choosestage/choosestage.go
@@ -0,0 +1,194 @@
+// 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.
+
+// Choose which ninja file (stage) to run next
+//
+// In the common case, this program takes a list of ninja files, compares their
+// mtimes against their $file.timestamp mtimes, and picks the last up to date
+// ninja file to output. That stage is expected to rebuild the next file in the
+// list and call this program again. If none of the ninja files are considered
+// dirty, the last stage is output.
+//
+// One exception is if the current stage's ninja file was rewritten, it will be
+// run again.
+//
+// Another exception is if the source bootstrap file has been updated more
+// recently than the first stage, the source file will be copied to the first
+// stage, and output. This would be expected with a new source drop via git.
+// The timestamp of the first file is not updated so that it can be regenerated
+// with any local changes.
+
+package choosestage
+
+import (
+	"bytes"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"os"
+	"path/filepath"
+)
+
+var (
+	outputFile    string
+	currentFile   string
+	bootstrapFile string
+	verbose       bool
+)
+
+func init() {
+	flag.StringVar(&outputFile, "o", "", "Output file")
+	flag.StringVar(&currentFile, "current", "", "Current stage's file")
+	flag.StringVar(&bootstrapFile, "bootstrap", "", "Bootstrap file checked into source")
+	flag.BoolVar(&verbose, "v", false, "Verbose mode")
+}
+
+func compareFiles(a, b string) (bool, error) {
+	aData, err := ioutil.ReadFile(a)
+	if err != nil {
+		return false, err
+	}
+
+	bData, err := ioutil.ReadFile(b)
+	if err != nil {
+		return false, err
+	}
+
+	return bytes.Equal(aData, bData), nil
+}
+
+// If the source bootstrap reference file is newer, then we may have gotten
+// other source updates too. So we need to restart everything with the file
+// that was checked in instead of the bootstrap that we last built.
+func copyBootstrapIfNecessary(bootstrapFile, filename string) (bool, error) {
+	if bootstrapFile == "" {
+		return false, nil
+	}
+
+	bootstrapStat, err := os.Stat(bootstrapFile)
+	if err != nil {
+		return false, err
+	}
+
+	fileStat, err := os.Stat(filename)
+	if err != nil {
+		return false, err
+	}
+
+	time := fileStat.ModTime()
+	if !bootstrapStat.ModTime().After(time) {
+		return false, nil
+	}
+
+	fmt.Printf("Newer source version of %s. Copying to %s\n", filepath.Base(bootstrapFile), filepath.Base(filename))
+	if verbose {
+		fmt.Printf("Source: %s\nBuilt:  %s\n", bootstrapStat.ModTime(), time)
+	}
+
+	data, err := ioutil.ReadFile(bootstrapFile)
+	if err != nil {
+		return false, err
+	}
+
+	err = ioutil.WriteFile(filename, data, 0666)
+	if err != nil {
+		return false, err
+	}
+
+	// Restore timestamp to force regeneration of the bootstrap.ninja.in
+	err = os.Chtimes(filename, time, time)
+	return true, err
+}
+
+func main() {
+	flag.Parse()
+
+	if flag.NArg() == 0 {
+		fmt.Fprintf(os.Stderr, "Must specify at least one ninja file\n")
+		os.Exit(1)
+	}
+
+	if outputFile == "" {
+		fmt.Fprintf(os.Stderr, "Must specify an output file\n")
+		os.Exit(1)
+	}
+
+	gotoFile := flag.Arg(0)
+	if copied, err := copyBootstrapIfNecessary(bootstrapFile, flag.Arg(0)); err != nil {
+		fmt.Fprintf(os.Stderr, "Failed to copy bootstrap ninja file: %s\n", err)
+		os.Exit(1)
+	} else if !copied {
+		for _, fileName := range flag.Args() {
+			timestampName := fileName + ".timestamp"
+
+			// If we're currently running this stage, and the build.ninja.in
+			// file differs from the current stage file, then it has been rebuilt.
+			// Restart the stage.
+			if currentFile == fileName {
+				if _, err := os.Stat(outputFile); !os.IsNotExist(err) {
+					if ok, err := compareFiles(fileName, outputFile); err != nil {
+						fmt.Fprintf(os.Stderr, "Failure when comparing files: %s\n", err)
+						os.Exit(1)
+					} else if !ok {
+						fmt.Printf("Stage %s has changed, restarting\n", filepath.Base(fileName))
+						gotoFile = fileName
+						break
+					}
+				}
+			}
+
+			fileStat, err := os.Stat(fileName)
+			if err != nil {
+				// Regenerate this stage on error
+				break
+			}
+
+			timestampStat, err := os.Stat(timestampName)
+			if err != nil {
+				// This file may not exist. There's no point for
+				// the first stage to have one, as it should be
+				// a subset of the second stage dependencies,
+				// and both will return to the first stage.
+				continue
+			}
+
+			if verbose {
+				fmt.Printf("For %s:\n  file: %s\n  time: %s\n", fileName, fileStat.ModTime(), timestampStat.ModTime())
+			}
+
+			// If the timestamp file has a later modification time, that
+			// means that this stage needs to be regenerated. Break, so
+			// that we run the last found stage.
+			if timestampStat.ModTime().After(fileStat.ModTime()) {
+				break
+			}
+
+			gotoFile = fileName
+		}
+	}
+
+	fmt.Printf("Choosing %s for next stage\n", filepath.Base(gotoFile))
+
+	data, err := ioutil.ReadFile(gotoFile)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "Can't read file: %s", err)
+		os.Exit(1)
+	}
+
+	err = ioutil.WriteFile(outputFile, data, 0666)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "Can't write file: %s", err)
+		os.Exit(1)
+	}
+}
diff --git a/context.go b/context.go
index b077623..0109dd3 100644
--- a/context.go
+++ b/context.go
@@ -1631,7 +1631,7 @@
 func (c *Context) initSpecialVariables() {
 	c.buildDir = nil
 	c.requiredNinjaMajor = 1
-	c.requiredNinjaMinor = 1
+	c.requiredNinjaMinor = 6
 	c.requiredNinjaMicro = 0
 }
 
@@ -2429,6 +2429,10 @@
 	buf := bytes.NewBuffer(nil)
 
 	for _, module := range modules {
+		if len(module.actionDefs.variables)+len(module.actionDefs.rules)+len(module.actionDefs.buildDefs) == 0 {
+			continue
+		}
+
 		buf.Reset()
 
 		// In order to make the bootstrap build manifest independent of the
@@ -2497,6 +2501,10 @@
 	for _, name := range singletonNames {
 		info := c.singletonInfo[name]
 
+		if len(info.actionDefs.variables)+len(info.actionDefs.rules)+len(info.actionDefs.buildDefs) == 0 {
+			continue
+		}
+
 		// Get the name of the factory function for the module.
 		factory := info.factory
 		factoryFunc := runtime.FuncForPC(reflect.ValueOf(factory).Pointer())
diff --git a/tests/bootstrap.bash b/tests/bootstrap.bash
new file mode 100755
index 0000000..4b58b19
--- /dev/null
+++ b/tests/bootstrap.bash
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+export BOOTSTRAP="${BASH_SOURCE[0]}"
+export SRCDIR=".."
+export BOOTSTRAP_MANIFEST="src.build.ninja.in"
+
+../bootstrap.bash "$@"
diff --git a/tests/expected_all b/tests/expected_all
new file mode 100644
index 0000000..b16fc78
--- /dev/null
+++ b/tests/expected_all
@@ -0,0 +1,3 @@
+Choosing bootstrap.ninja.in for next stage
+Choosing primary.ninja.in for next stage
+Choosing main.ninja.in for next stage
diff --git a/tests/expected_manifest b/tests/expected_manifest
new file mode 100644
index 0000000..3970edb
--- /dev/null
+++ b/tests/expected_manifest
@@ -0,0 +1,4 @@
+Newer source version of build.ninja.in. Copying to bootstrap.ninja.in
+Choosing bootstrap.ninja.in for next stage
+Choosing primary.ninja.in for next stage
+Choosing main.ninja.in for next stage
diff --git a/tests/expected_none b/tests/expected_none
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/expected_none
diff --git a/tests/expected_primary b/tests/expected_primary
new file mode 100644
index 0000000..43f2d35
--- /dev/null
+++ b/tests/expected_primary
@@ -0,0 +1,2 @@
+Choosing primary.ninja.in for next stage
+Choosing main.ninja.in for next stage
diff --git a/tests/expected_rebuild_test b/tests/expected_rebuild_test
new file mode 100644
index 0000000..b16fc78
--- /dev/null
+++ b/tests/expected_rebuild_test
@@ -0,0 +1,3 @@
+Choosing bootstrap.ninja.in for next stage
+Choosing primary.ninja.in for next stage
+Choosing main.ninja.in for next stage
diff --git a/tests/expected_regen b/tests/expected_regen
new file mode 100644
index 0000000..e2e10b8
--- /dev/null
+++ b/tests/expected_regen
@@ -0,0 +1,6 @@
+Newer source version of src.build.ninja.in. Copying to bootstrap.ninja.in
+Choosing bootstrap.ninja.in for next stage
+Stage bootstrap.ninja.in has changed, restarting
+Choosing bootstrap.ninja.in for next stage
+Choosing primary.ninja.in for next stage
+Choosing main.ninja.in for next stage
diff --git a/tests/expected_start b/tests/expected_start
new file mode 100644
index 0000000..43f2d35
--- /dev/null
+++ b/tests/expected_start
@@ -0,0 +1,2 @@
+Choosing primary.ninja.in for next stage
+Choosing main.ninja.in for next stage
diff --git a/tests/expected_start2 b/tests/expected_start2
new file mode 100644
index 0000000..4c339e2
--- /dev/null
+++ b/tests/expected_start2
@@ -0,0 +1,4 @@
+Stage bootstrap.ninja.in has changed, restarting
+Choosing bootstrap.ninja.in for next stage
+Choosing primary.ninja.in for next stage
+Choosing main.ninja.in for next stage
diff --git a/tests/expected_start_add_tests b/tests/expected_start_add_tests
new file mode 100644
index 0000000..4c339e2
--- /dev/null
+++ b/tests/expected_start_add_tests
@@ -0,0 +1,4 @@
+Stage bootstrap.ninja.in has changed, restarting
+Choosing bootstrap.ninja.in for next stage
+Choosing primary.ninja.in for next stage
+Choosing main.ninja.in for next stage
diff --git a/tests/test.sh b/tests/test.sh
new file mode 100755
index 0000000..e27ae97
--- /dev/null
+++ b/tests/test.sh
@@ -0,0 +1,94 @@
+#!/bin/bash
+
+# Go to srcdir
+cd $(dirname ${BASH_SOURCE[0]})/..
+
+rm -rf out.test
+mkdir out.test
+cd out.test
+../bootstrap.bash
+
+# Run ninja, filter the output, and compare against expectations
+# $1: Name of test
+function testcase()
+{
+  echo -n "Running $1..."
+  if ! ninja -v -d explain >log_$1 2>&1; then
+    echo " Failed."
+    echo "Test $1 Failed:" >>failed
+    tail log_$1 >>failed
+    return
+  fi
+  grep -E "^(Choosing|Newer|Stage)" log_$1 >test_$1
+  if ! cmp -s test_$1 ../tests/expected_$1; then
+    echo " Failed."
+    echo "Test $1 Failed:" >>failed
+    diff -u ../tests/expected_$1 test_$1 >>failed
+  else
+    echo " Passed."
+  fi
+}
+
+
+
+
+testcase start
+
+# The 2 second sleeps are needed until ninja understands sub-second timestamps
+# https://github.com/martine/ninja/issues/371
+
+# This test affects all bootstrap stages
+sleep 2
+touch ../Blueprints
+testcase all
+
+# This test affects only the primary bootstrap stage
+sleep 2
+touch ../bpmodify/bpmodify.go
+testcase primary
+
+# This test affects nothing, nothing should be done
+sleep 2
+testcase none
+
+# This test will cause the source build.ninja.in to be copied into the first
+# stage.
+sleep 2
+touch ../build.ninja.in
+testcase manifest
+
+# From now on, we're going to be modifying the build.ninja.in, so let's make our
+# own copy
+sleep 2
+../tests/bootstrap.bash -r
+
+sleep 2
+testcase start2
+
+# This is similar to the last test, but incorporates a change into the source
+# build.ninja.in, so that we'll restart into the new version created by the
+# build.
+sleep 2
+echo "# test" >>src.build.ninja.in
+testcase regen
+
+# Add tests to our build by using '-t'
+sleep 2
+../tests/bootstrap.bash -r -t
+
+sleep 2
+testcase start_add_tests
+
+# Make sure that updating a test file causes us to go back to the bootstrap
+# stage
+sleep 2
+touch ../parser/parser_test.go
+testcase rebuild_test
+
+
+
+
+if [ -f failed ]; then
+  cat failed
+  exit 1
+fi