[dev.cc] cmd/dist: adjust for build process without cmd/cc

[This CL is part of the removal of C code from package runtime.
See golang.org/s/dev.cc for an overview.]

- Remove references to C compiler directories.
- Remove generation of special header files.
- Remove generation of Go source files from C declarations.

- Compile Go sources before rest of package (was after),
  so that Go compiler can write go_asm.h for use in assembly.

- Move TLS information from cmd/dist (was embedding in output)
  to src/runtime/go_tls.h, which it can be maintained directly.

LGTM=r
R=r, dave
CC=austin, golang-codereviews, iant, khr
https://golang.org/cl/172960043
diff --git a/src/cmd/dist/build.c b/src/cmd/dist/build.c
index d638ae4..62dec05 100644
--- a/src/cmd/dist/build.c
+++ b/src/cmd/dist/build.c
@@ -522,10 +522,6 @@
 		"anames6.c",
 		"anames8.c",
 	}},
-	{"cmd/cc", {
-		"-pgen.c",
-		"-pswt.c",
-	}},
 	{"cmd/gc", {
 		"-cplx.c",
 		"-pgen.c",
@@ -534,21 +530,6 @@
 		"-y1.tab.c",  // makefile dreg
 		"opnames.h",
 	}},
-	{"cmd/5c", {
-		"../cc/pgen.c",
-		"../cc/pswt.c",
-		"$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libcc.a",
-	}},
-	{"cmd/6c", {
-		"../cc/pgen.c",
-		"../cc/pswt.c",
-		"$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libcc.a",
-	}},
-	{"cmd/8c", {
-		"../cc/pgen.c",
-		"../cc/pswt.c",
-		"$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libcc.a",
-	}},
 	{"cmd/5g", {
 		"../gc/cplx.c",
 		"../gc/pgen.c",
@@ -591,12 +572,10 @@
 		"$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/lib9.a",
 	}},
 	{"runtime", {
-		"zaexperiment.h", // must sort above zasm
-		"zasm_$GOOS_$GOARCH.h",
+		"zaexperiment.h",
 		"zsys_$GOOS_$GOARCH.s",
 		"zgoarch_$GOARCH.go",
 		"zgoos_$GOOS.go",
-		"zruntime_defs_$GOOS_$GOARCH.go",
 		"zversion.go",
 	}},
 };
@@ -618,12 +597,10 @@
 	{"anames5.c", mkanames},
 	{"anames6.c", mkanames},
 	{"anames8.c", mkanames},
-	{"zasm_", mkzasm},
 	{"zdefaultcc.go", mkzdefaultcc},
 	{"zsys_", mkzsys},
 	{"zgoarch_", mkzgoarch},
 	{"zgoos_", mkzgoos},
-	{"zruntime_defs_", mkzruntimedefs},
 	{"zversion.go", mkzversion},
 	{"zaexperiment.h", mkzexperiment},
 
@@ -638,7 +615,7 @@
 {
 	char *name, *p, *elem, *prefix, *exe;
 	bool islib, ispkg, isgo, stale, ispackcmd;
-	Buf b, b1, path, final_path, final_name;
+	Buf b, b1, path, final_path, final_name, archive;
 	Vec compile, files, link, go, missing, clean, lib, extra;
 	Time ttarg, t;
 	int i, j, k, n, doclean, targ;
@@ -655,6 +632,7 @@
 	binit(&path);
 	binit(&final_path);
 	binit(&final_name);
+	binit(&archive);
 	vinit(&compile);
 	vinit(&files);
 	vinit(&link);
@@ -698,7 +676,7 @@
 		splitfields(&ldargs, bstr(&b));
 	}
 
-	islib = hasprefix(dir, "lib") || streq(dir, "cmd/cc") || streq(dir, "cmd/gc");
+	islib = hasprefix(dir, "lib") || streq(dir, "cmd/gc");
 	ispkg = !islib && !hasprefix(dir, "cmd/");
 	isgo = ispkg || streq(dir, "cmd/go") || streq(dir, "cmd/cgo");
 
@@ -877,17 +855,6 @@
 
 	// For package runtime, copy some files into the work space.
 	if(streq(dir, "runtime")) {
-		copyfile(bpathf(&b, "%s/arch_GOARCH.h", workdir),
-			bpathf(&b1, "%s/arch_%s.h", bstr(&path), goarch), 0);
-		copyfile(bpathf(&b, "%s/defs_GOOS_GOARCH.h", workdir),
-			bpathf(&b1, "%s/defs_%s_%s.h", bstr(&path), goos, goarch), 0);
-		p = bpathf(&b1, "%s/signal_%s_%s.h", bstr(&path), goos, goarch);
-		if(isfile(p))
-			copyfile(bpathf(&b, "%s/signal_GOOS_GOARCH.h", workdir), p, 0);
-		copyfile(bpathf(&b, "%s/os_GOOS.h", workdir),
-			bpathf(&b1, "%s/os_%s.h", bstr(&path), goos), 0);
-		copyfile(bpathf(&b, "%s/signals_GOOS.h", workdir),
-			bpathf(&b1, "%s/signals_%s.h", bstr(&path), goos), 0);
 		copyfile(bpathf(&b, "%s/pkg/%s_%s/textflag.h", goroot, goos, goarch),
 			bpathf(&b1, "%s/src/cmd/ld/textflag.h", goroot), 0);
 		copyfile(bpathf(&b, "%s/pkg/%s_%s/funcdata.h", goroot, goos, goarch),
@@ -921,14 +888,6 @@
 	built:;
 	}
 
-	// One more copy for package runtime.
-	// The last batch was required for the generators.
-	// This one is generated.
-	if(streq(dir, "runtime")) {
-		copyfile(bpathf(&b, "%s/zasm_GOOS_GOARCH.h", workdir),
-			bpathf(&b1, "%s/zasm_%s_%s.h", bstr(&path), goos, goarch), 0);
-	}
-
 	if((!streq(goos, gohostos) || !streq(goarch, gohostarch)) && isgo) {
 		// We've generated the right files; the go command can do the build.
 		if(vflag > 1)
@@ -936,6 +895,42 @@
 		goto nobuild;
 	}
 
+	if(isgo) {
+		// The next loop will compile individual non-Go files.
+		// Hand the Go files to the compiler en masse.
+		// For package runtime, this writes go_asm.h, which
+		// the assembly files will need.
+		vreset(&compile);
+		vadd(&compile, bpathf(&b, "%s/%sg", tooldir, gochar));
+
+		bpathf(&b, "%s/_go_.a", workdir);
+		vadd(&compile, "-pack");
+		vadd(&compile, "-o");
+		vadd(&compile, bstr(&b));
+		vadd(&clean, bstr(&b));
+		if(!ispackcmd)
+			vadd(&link, bstr(&b));
+		else
+			bwriteb(&archive, &b);
+
+		vadd(&compile, "-p");
+		if(hasprefix(dir, "cmd/"))
+			vadd(&compile, "main");
+		else
+			vadd(&compile, dir);
+
+		if(streq(dir, "runtime")) {
+			vadd(&compile, "-+");
+			vadd(&compile, "-asmhdr");
+			bpathf(&b1, "%s/go_asm.h", workdir);
+			vadd(&compile, bstr(&b1));
+		}
+
+		vcopy(&compile, go.p, go.len);
+
+		runv(nil, bstr(&path), CheckExit, &compile);
+	}
+
 	// Compile the files.
 	for(i=0; i<files.len; i++) {
 		if(!hassuffix(files.p[i], ".c") && !hassuffix(files.p[i], ".s"))
@@ -1049,38 +1044,10 @@
 	}
 	bgwait();
 
-	if(isgo) {
-		// The last loop was compiling individual files.
-		// Hand the Go files to the compiler en masse.
-		vreset(&compile);
-		vadd(&compile, bpathf(&b, "%s/%sg", tooldir, gochar));
-
-		bpathf(&b, "%s/_go_.a", workdir);
-		vadd(&compile, "-pack");
-		vadd(&compile, "-o");
-		vadd(&compile, bstr(&b));
-		vadd(&clean, bstr(&b));
-		if(!ispackcmd)
-			vadd(&link, bstr(&b));
-
-		vadd(&compile, "-p");
-		if(hasprefix(dir, "pkg/"))
-			vadd(&compile, dir+4);
-		else
-			vadd(&compile, "main");
-
-		if(streq(dir, "runtime"))
-			vadd(&compile, "-+");
-
-		vcopy(&compile, go.p, go.len);
-
-		runv(nil, bstr(&path), CheckExit, &compile);
-
-		if(ispackcmd) {
-			xremove(link.p[targ]);
-			dopack(link.p[targ], bstr(&b), &link.p[targ+1], link.len - (targ+1));
-			goto nobuild;
-		}
+	if(isgo && ispackcmd) {
+		xremove(link.p[targ]);
+		dopack(link.p[targ], bstr(&archive), &link.p[targ+1], link.len - (targ+1));
+		goto nobuild;
 	}
 
 	if(!islib && !isgo) {
@@ -1094,17 +1061,7 @@
 	xremove(link.p[targ]);
 
 	runv(nil, nil, CheckExit, &link);
-
 nobuild:
-	// In package runtime, we install runtime.h and cgocall.h too,
-	// for use by cgo compilation.
-	if(streq(dir, "runtime")) {
-		copyfile(bpathf(&b, "%s/pkg/%s_%s/cgocall.h", goroot, goos, goarch),
-			bpathf(&b1, "%s/src/runtime/cgocall.h", goroot), 0);
-		copyfile(bpathf(&b, "%s/pkg/%s_%s/runtime.h", goroot, goos, goarch),
-			bpathf(&b1, "%s/src/runtime/runtime.h", goroot), 0);
-	}
-
 
 out:
 	for(i=0; i<clean.len; i++)
@@ -1113,6 +1070,7 @@
 	bfree(&b);
 	bfree(&b1);
 	bfree(&path);
+	bfree(&archive);
 	vfree(&compile);
 	vfree(&files);
 	vfree(&link);
@@ -1286,11 +1244,9 @@
 	"libbio",
 	"liblink",
 
-	"cmd/cc",  // must be before c
 	"cmd/gc",  // must be before g
-	"cmd/%sl",  // must be before a, c, g
+	"cmd/%sl",  // must be before a, g
 	"cmd/%sa",
-	"cmd/%sc",
 	"cmd/%sg",
 
 	// The dependency order here was copied from a buildscript
@@ -1347,18 +1303,14 @@
 static char *cleantab[] = {
 	// Commands and C libraries.
 	"cmd/5a",
-	"cmd/5c",
 	"cmd/5g",
 	"cmd/5l",
 	"cmd/6a",
-	"cmd/6c",
 	"cmd/6g",
 	"cmd/6l",
 	"cmd/8a",
-	"cmd/8c",
 	"cmd/8g",
 	"cmd/8l",
-	"cmd/cc",
 	"cmd/gc",
 	"cmd/go",	
 	"lib9",
diff --git a/src/cmd/dist/buildruntime.c b/src/cmd/dist/buildruntime.c
index bb774e0..e561937 100644
--- a/src/cmd/dist/buildruntime.c
+++ b/src/cmd/dist/buildruntime.c
@@ -127,167 +127,8 @@
 	bfree(&out);
 }
 
-static struct {
-	char *goarch;
-	char *goos;
-	char *hdr;
-} zasmhdr[] = {
-	{"386", "",
-		"#define	get_tls(r)	MOVL TLS, r\n"
-		"#define	g(r)	0(r)(TLS*1)\n"
-	},
-	{"amd64p32", "",
-		"#define	get_tls(r)	MOVL TLS, r\n"
-		"#define	g(r)	0(r)(TLS*1)\n"
-	},
-	{"amd64", "",
-		"#define	get_tls(r)	MOVQ TLS, r\n"
-		"#define	g(r)	0(r)(TLS*1)\n"
-	},	
-
-	{"arm", "",
-	"#define	LR	R14\n"
-	},
-};
-
 #define MAXWINCB 2000 /* maximum number of windows callbacks allowed */
 
-// mkzasm writes zasm_$GOOS_$GOARCH.h,
-// which contains struct offsets for use by
-// assembly files.  It also writes a copy to the work space
-// under the name zasm_GOOS_GOARCH.h (no expansion).
-// 
-void
-mkzasm(char *dir, char *file)
-{
-	int i, n;
-	char *aggr, *p;
-	Buf in, b, b1, out, exp;
-	Vec argv, lines, fields;
-
-	binit(&in);
-	binit(&b);
-	binit(&b1);
-	binit(&out);
-	binit(&exp);
-	vinit(&argv);
-	vinit(&lines);
-	vinit(&fields);
-	
-	bwritestr(&out, "// auto generated by go tool dist\n\n");
-	if(streq(goos, "linux")) {
-		bwritestr(&out, "// +build !android\n\n");
-	}
-	
-	for(i=0; i<nelem(zasmhdr); i++) {
-		if(hasprefix(goarch, zasmhdr[i].goarch) && hasprefix(goos, zasmhdr[i].goos)) {
-			bwritestr(&out, zasmhdr[i].hdr);
-			goto ok;
-		}
-	}
-	fatal("unknown $GOOS/$GOARCH in mkzasm");
-ok:
-
-	copyfile(bpathf(&b, "%s/pkg/%s_%s/textflag.h", goroot, goos, goarch),
-		bpathf(&b1, "%s/src/cmd/ld/textflag.h", goroot), 0);
-
-	// Run 6c -D GOOS_goos -D GOARCH_goarch -I workdir -a -n -o workdir/proc.acid proc.c
-	// to get acid [sic] output. Run once without the -a -o workdir/proc.acid in order to
-	// report compilation failures (the -o redirects all messages, unfortunately).
-	vreset(&argv);
-	vadd(&argv, bpathf(&b, "%s/%sc", tooldir, gochar));
-	vadd(&argv, "-D");
-	vadd(&argv, bprintf(&b, "GOOS_%s", goos));
-	vadd(&argv, "-D");
-	vadd(&argv, bprintf(&b, "GOARCH_%s", goarch));
-	vadd(&argv, "-I");
-	vadd(&argv, bprintf(&b, "%s", workdir));
-	vadd(&argv, "-I");
-	vadd(&argv, bprintf(&b, "%s/pkg/%s_%s", goroot, goos, goarch));
-	vadd(&argv, "-n");
-	vadd(&argv, "-a");
-	vadd(&argv, "-o");
-	vadd(&argv, bpathf(&b, "%s/proc.acid", workdir));
-	vadd(&argv, "proc.c");
-	runv(nil, dir, CheckExit, &argv);
-	readfile(&in, bpathf(&b, "%s/proc.acid", workdir));
-	
-	// Convert input like
-	//	aggr G
-	//	{
-	//		Gobuf 24 sched;
-	//		'Y' 48 stack0;
-	//	}
-	//	StackMin = 128;
-	// into output like
-	//	#define g_sched 24
-	//	#define g_stack0 48
-	//	#define const_StackMin 128
-	aggr = nil;
-	splitlines(&lines, bstr(&in));
-	for(i=0; i<lines.len; i++) {
-		splitfields(&fields, lines.p[i]);
-		if(fields.len == 2 && streq(fields.p[0], "aggr")) {
-			if(streq(fields.p[1], "G"))
-				aggr = "g";
-			else if(streq(fields.p[1], "M"))
-				aggr = "m";
-			else if(streq(fields.p[1], "P"))
-				aggr = "p";
-			else if(streq(fields.p[1], "Gobuf"))
-				aggr = "gobuf";
-			else if(streq(fields.p[1], "LibCall"))
-				aggr = "libcall";
-			else if(streq(fields.p[1], "WinCallbackContext"))
-				aggr = "cbctxt";
-			else if(streq(fields.p[1], "SEH"))
-				aggr = "seh";
-			else if(streq(fields.p[1], "Alg"))
-				aggr = "alg";
-			else if(streq(fields.p[1], "Panic"))
-				aggr = "panic";
-			else if(streq(fields.p[1], "Stack"))
-				aggr = "stack";
-		}
-		if(hasprefix(lines.p[i], "}"))
-			aggr = nil;
-		if(aggr && hasprefix(lines.p[i], "\t") && fields.len >= 2) {
-			n = fields.len;
-			p = fields.p[n-1];
-			if(p[xstrlen(p)-1] == ';')
-				p[xstrlen(p)-1] = '\0';
-			bwritestr(&out, bprintf(&b, "#define %s_%s %s\n", aggr, fields.p[n-1], fields.p[n-2]));
-		}
-		if(fields.len == 3 && streq(fields.p[1], "=")) { // generated from enumerated constants
-			p = fields.p[2];
-			if(p[xstrlen(p)-1] == ';')
-				p[xstrlen(p)-1] = '\0';
-			bwritestr(&out, bprintf(&b, "#define const_%s %s\n", fields.p[0], p));
-		}
-	}
-
-	// Some #defines that are used for .c files.
-	if(streq(goos, "windows")) {
-		bwritestr(&out, bprintf(&b, "#define cb_max %d\n", MAXWINCB));
-	}
-	
-	xgetenv(&exp, "GOEXPERIMENT");
-	bwritestr(&out, bprintf(&b, "#define GOEXPERIMENT \"%s\"\n", bstr(&exp)));
-	
-	// Write both to file and to workdir/zasm_GOOS_GOARCH.h.
-	writefile(&out, file, 0);
-	writefile(&out, bprintf(&b, "%s/zasm_GOOS_GOARCH.h", workdir), 0);
-
-	bfree(&in);
-	bfree(&b);
-	bfree(&b1);
-	bfree(&out);
-	bfree(&exp);
-	vfree(&argv);
-	vfree(&lines);
-	vfree(&fields);
-}
-
 // mkzsys writes zsys_$GOOS_$GOARCH.s,
 // which contains arch or os specific asm code.
 // 
@@ -326,143 +167,3 @@
 	
 	bfree(&out);
 }
-
-static char *runtimedefs[] = {
-	"defs.c",
-	"malloc.c",
-	"mcache.c",
-	"mgc0.c",
-	"proc.c",
-	"parfor.c",
-	"stack.c",
-};
-
-// mkzruntimedefs writes zruntime_defs_$GOOS_$GOARCH.h,
-// which contains Go struct definitions equivalent to the C ones.
-// Mostly we just write the output of 6c -q to the file.
-// However, we run it on multiple files, so we have to delete
-// the duplicated definitions, and we don't care about the funcs,
-// so we delete those too.
-// 
-void
-mkzruntimedefs(char *dir, char *file)
-{
-	int i, skip;
-	char *p;
-	Buf in, b, b1, out;
-	Vec argv, lines, fields, seen;
-	
-	binit(&in);
-	binit(&b);
-	binit(&b1);
-	binit(&out);
-	vinit(&argv);
-	vinit(&lines);
-	vinit(&fields);
-	vinit(&seen);
-	
-	bwritestr(&out, "// auto generated by go tool dist\n"
-		"\n");
-
-	if(streq(goos, "linux")) {
-		bwritestr(&out, "// +build !android\n\n");
-	}
-	
-	bwritestr(&out,
-		"package runtime\n"
-		"import \"unsafe\"\n"
-		"var _ unsafe.Pointer\n"
-		"\n"
-	);
-
-	// Do not emit definitions for these.
-	vadd(&seen, "true");
-	vadd(&seen, "false");
-	vadd(&seen, "raceenabled");
-	vadd(&seen, "allgs");
-	
-	// Run 6c -D GOOS_goos -D GOARCH_goarch -I workdir -q -n -o workdir/runtimedefs
-	// on each of the runtimedefs C files.
-	vadd(&argv, bpathf(&b, "%s/%sc", tooldir, gochar));
-	vadd(&argv, "-D");
-	vadd(&argv, bprintf(&b, "GOOS_%s", goos));
-	vadd(&argv, "-D");
-	vadd(&argv, bprintf(&b, "GOARCH_%s", goarch));
-	vadd(&argv, "-I");
-	vadd(&argv, bprintf(&b, "%s", workdir));
-	vadd(&argv, "-I");
-	vadd(&argv, bprintf(&b, "%s/pkg/%s_%s", goroot, goos, goarch));
-	vadd(&argv, "-q");
-	vadd(&argv, "-n");
-	vadd(&argv, "-o");
-	vadd(&argv, bpathf(&b, "%s/runtimedefs", workdir));
-	vadd(&argv, "");
-	p = argv.p[argv.len-1];
-	for(i=0; i<nelem(runtimedefs); i++) {
-		argv.p[argv.len-1] = runtimedefs[i];
-		runv(nil, dir, CheckExit, &argv);
-		readfile(&b, bpathf(&b1, "%s/runtimedefs", workdir));
-		bwriteb(&in, &b);
-	}
-	argv.p[argv.len-1] = p;
-		
-	// Process the aggregate output.
-	skip = 0;
-	splitlines(&lines, bstr(&in));
-	for(i=0; i<lines.len; i++) {
-		p = lines.p[i];
-		// Drop comment and func lines.
-		if(hasprefix(p, "//") || hasprefix(p, "func"))
-			continue;
-		
-		// Note beginning of type or var decl, which can be multiline.
-		// Remove duplicates.  The linear check of seen here makes the
-		// whole processing quadratic in aggregate, but there are only
-		// about 100 declarations, so this is okay (and simple).
-		if(hasprefix(p, "type ") || hasprefix(p, "var ") || hasprefix(p, "const ")) {
-			splitfields(&fields, p);
-			if(fields.len < 2)
-				continue;
-			if(find(fields.p[1], seen.p, seen.len) >= 0) {
-				if(streq(fields.p[fields.len-1], "{"))
-					skip = 1;  // skip until }
-				continue;
-			}
-			vadd(&seen, fields.p[1]);
-		}
-
-		// Const lines are printed in original case (usually upper). Add a leading _ as needed.
-		if(hasprefix(p, "const ")) {
-			if('A' <= p[6] && p[6] <= 'Z')
-				bwritestr(&out, "const _");
-			else
-				bwritestr(&out, "const ");
-			bwritestr(&out, p+6);
-			continue;
-		}
-
-		if(skip) {
-			if(hasprefix(p, "}"))
-				skip = 0;
-			continue;
-		}
-		
-		bwritestr(&out, p);
-	}
-
-	// Some windows specific const.
-	if(streq(goos, "windows")) {
-		bwritestr(&out, bprintf(&b, "const cb_max = %d\n", MAXWINCB));
-	}
-	
-	writefile(&out, file, 0);
-
-	bfree(&in);
-	bfree(&b);
-	bfree(&b1);
-	bfree(&out);
-	vfree(&argv);
-	vfree(&lines);
-	vfree(&fields);
-	vfree(&seen);
-}
diff --git a/src/runtime/defs.c b/src/runtime/defs.c
deleted file mode 100644
index b0a9b20..0000000
--- a/src/runtime/defs.c
+++ /dev/null
@@ -1,15 +0,0 @@
-// Copyright 2013 The Go Authors.  All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// This file is compiled by cmd/dist to obtain debug information
-// about the given header files.
-
-#include "runtime.h"
-#include "arch_GOARCH.h"
-#include "malloc.h"
-#include "type.h"
-#include "race.h"
-#include "chan.h"
-#include "defs_GOOS_GOARCH.h"
-#include "os_GOOS.h"
diff --git a/src/runtime/go_tls.h b/src/runtime/go_tls.h
new file mode 100644
index 0000000..6a707cf
--- /dev/null
+++ b/src/runtime/go_tls.h
@@ -0,0 +1,22 @@
+// Copyright 2014 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#ifdef GOARCH_arm
+#define LR R14
+#endif
+
+#ifdef GOARCH_amd64
+#define	get_tls(r)	MOVQ TLS, r
+#define	g(r)	0(r)(TLS*1)
+#endif
+
+#ifdef GOARCH_amd64p32
+#define	get_tls(r)	MOVL TLS, r
+#define	g(r)	0(r)(TLS*1)
+#endif
+
+#ifdef GOARCH_386
+#define	get_tls(r)	MOVL TLS, r
+#define	g(r)	0(r)(TLS*1)
+#endif