cmd/toolstash: don't pass -N when compiling runtime

The runtime cannot be compiled with optimizations disabled.
This lead to very confusing error messages
when toolstash -cmp failed.

Change-Id: Ie341d633ff9b26693b475957309591ff0757f1ab
Reviewed-on: https://go-review.googlesource.com/38378
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
diff --git a/cmd/toolstash/main.go b/cmd/toolstash/main.go
index d447dd7..e1bed36 100644
--- a/cmd/toolstash/main.go
+++ b/cmd/toolstash/main.go
@@ -275,14 +275,26 @@
 		log.Fatalf("unknown tool %s", tool)
 
 	case tool == "compile" || strings.HasSuffix(tool, "g"): // compiler
-		cmdN := append([]string{cmd[0], "-N"}, cmd[1:]...)
+		useDashN := true
+		for _, s := range cmd {
+			if s == "-+" {
+				// Compiling runtime. Don't use -N.
+				useDashN = false
+				break
+			}
+		}
+		cmdN := injectflags(cmd, nil, useDashN)
 		_, ok := cmpRun(false, cmdN)
 		if !ok {
-			log.Printf("compiler output differs, even with optimizers disabled (-N)")
-			cmd = append([]string{cmd[0], "-v", "-N", "-m=2"}, cmd[1:]...)
+			if useDashN {
+				log.Printf("compiler output differs, with optimizers disabled (-N)")
+			} else {
+				log.Printf("compiler output differs")
+			}
+			cmd = injectflags(cmd, []string{"-v", "-m=2"}, useDashN)
 			break
 		}
-		cmd = append([]string{cmd[0], "-v", "-m=2"}, cmd[1:]...)
+		cmd = injectflags(cmd, []string{"-v", "-m=2"}, false)
 		log.Printf("compiler output differs, only with optimizers enabled")
 
 	case tool == "asm" || strings.HasSuffix(tool, "a"): // assembler
@@ -293,13 +305,23 @@
 		extra = "-v=2"
 	}
 
-	cmdS := append([]string{cmd[0], extra}, cmd[1:]...)
+	cmdS := injectflags(cmd, []string{extra}, false)
 	outfile, _ = cmpRun(true, cmdS)
 
 	fmt.Fprintf(os.Stderr, "\n%s\n", compareLogs(outfile))
 	os.Exit(2)
 }
 
+func injectflags(cmd []string, extra []string, addDashN bool) []string {
+	x := []string{cmd[0]}
+	if addDashN {
+		x = append(x, "-N")
+	}
+	x = append(x, extra...)
+	x = append(x, cmd[1:]...)
+	return x
+}
+
 func cmpRun(keepLog bool, cmd []string) (outfile string, match bool) {
 	cmdStash := make([]string, len(cmd))
 	copy(cmdStash, cmd)