build.c 40 KB
Newer Older
Russ Cox's avatar
Russ Cox committed
1 2 3 4 5
// Copyright 2012 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.

#include "a.h"
6
#include "arg.h"
Russ Cox's avatar
Russ Cox committed
7 8 9 10 11 12 13 14 15

/*
 * Initialization for any invocation.
 */

// The usual variables.
char *goarch;
char *gobin;
char *gohostarch;
Russ Cox's avatar
Russ Cox committed
16
char *gohostchar;
Russ Cox's avatar
Russ Cox committed
17 18
char *gohostos;
char *goos;
19
char *goarm;
20
char *go386;
21 22
char *goroot = GOROOT_FINAL;
char *goroot_final = GOROOT_FINAL;
23
char *goextlinkenabled = "";
Russ Cox's avatar
Russ Cox committed
24
char *workdir;
25
char *tooldir;
Russ Cox's avatar
Russ Cox committed
26
char *gochar;
27
char *goversion;
Russ Cox's avatar
Russ Cox committed
28
char *slash;	// / for unix, \ for windows
29
char *defaultcc;
30
char *defaultcxx;
31 32
bool	rebuildall;
bool defaultclang;
Russ Cox's avatar
Russ Cox committed
33

Russ Cox's avatar
Russ Cox committed
34
static bool shouldbuild(char*, char*);
35
static void copy(char*, char*, int);
36
static void dopack(char*, char*, char**, int);
37
static char *findgoversion(void);
Russ Cox's avatar
Russ Cox committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

// The known architecture letters.
static char *gochars = "568";

// The known architectures.
static char *okgoarch[] = {
	// same order as gochars
	"arm",
	"amd64",
	"386",
};

// The known operating systems.
static char *okgoos[] = {
	"darwin",
53
	"dragonfly",
Russ Cox's avatar
Russ Cox committed
54
	"linux",
55
	"solaris",
Russ Cox's avatar
Russ Cox committed
56 57 58 59 60 61 62 63 64 65
	"freebsd",
	"netbsd",
	"openbsd",
	"plan9",
	"windows",
};

static void rmworkdir(void);

// find reports the first index of p in l[0:n], or else -1.
66
int
Russ Cox's avatar
Russ Cox committed
67 68 69
find(char *p, char **l, int n)
{
	int i;
70

Russ Cox's avatar
Russ Cox committed
71 72 73 74 75 76 77 78 79 80 81 82 83
	for(i=0; i<n; i++)
		if(streq(p, l[i]))
			return i;
	return -1;
}

// init handles initialization of the various global state, like goroot and goarch.
void
init(void)
{
	char *p;
	int i;
	Buf b;
84

Russ Cox's avatar
Russ Cox committed
85 86 87
	binit(&b);

	xgetenv(&b, "GOROOT");
88 89 90 91
	if(b.len > 0) {
		// if not "/", then strip trailing path separator
		if(b.len >= 2 && b.p[b.len - 1] == slash[0])
			b.len--;
92
		goroot = btake(&b);
93
	}
Russ Cox's avatar
Russ Cox committed
94 95 96 97 98 99 100 101 102 103 104 105 106

	xgetenv(&b, "GOBIN");
	if(b.len == 0)
		bprintf(&b, "%s%sbin", goroot, slash);
	gobin = btake(&b);

	xgetenv(&b, "GOOS");
	if(b.len == 0)
		bwritestr(&b, gohostos);
	goos = btake(&b);
	if(find(goos, okgoos, nelem(okgoos)) < 0)
		fatal("unknown $GOOS %s", goos);

107 108 109 110 111
	xgetenv(&b, "GOARM");
	if(b.len == 0)
		bwritestr(&b, xgetgoarm());
	goarm = btake(&b);

112
	xgetenv(&b, "GO386");
Russ Cox's avatar
Russ Cox committed
113
	if(b.len == 0) {
114 115
		if(cansse2())
			bwritestr(&b, "sse2");
Russ Cox's avatar
Russ Cox committed
116 117 118
		else
			bwritestr(&b, "387");
	}
119 120
	go386 = btake(&b);

121
	p = bpathf(&b, "%s/include/u.h", goroot);
Russ Cox's avatar
Russ Cox committed
122 123 124 125 126
	if(!isfile(p)) {
		fatal("$GOROOT is not set correctly or not exported\n"
			"\tGOROOT=%s\n"
			"\t%s does not exist", goroot, p);
	}
127

Russ Cox's avatar
Russ Cox committed
128 129 130 131
	xgetenv(&b, "GOHOSTARCH");
	if(b.len > 0)
		gohostarch = btake(&b);

Russ Cox's avatar
Russ Cox committed
132 133
	i = find(gohostarch, okgoarch, nelem(okgoarch));
	if(i < 0)
Russ Cox's avatar
Russ Cox committed
134
		fatal("unknown $GOHOSTARCH %s", gohostarch);
Russ Cox's avatar
Russ Cox committed
135 136
	bprintf(&b, "%c", gochars[i]);
	gohostchar = btake(&b);
Russ Cox's avatar
Russ Cox committed
137 138 139 140 141

	xgetenv(&b, "GOARCH");
	if(b.len == 0)
		bwritestr(&b, gohostarch);
	goarch = btake(&b);
Russ Cox's avatar
Russ Cox committed
142 143
	i = find(goarch, okgoarch, nelem(okgoarch));
	if(i < 0)
Russ Cox's avatar
Russ Cox committed
144 145 146 147
		fatal("unknown $GOARCH %s", goarch);
	bprintf(&b, "%c", gochars[i]);
	gochar = btake(&b);

148 149 150 151 152 153
	xgetenv(&b, "GO_EXTLINK_ENABLED");
	if(b.len > 0) {
		goextlinkenabled = btake(&b);
		if(!streq(goextlinkenabled, "0") && !streq(goextlinkenabled, "1"))
			fatal("unknown $GO_EXTLINK_ENABLED %s", goextlinkenabled);
	}
154 155 156 157 158 159 160 161 162 163 164 165 166 167
	
	xgetenv(&b, "CC");
	if(b.len == 0) {
		// Use clang on OS X, because gcc is deprecated there.
		// Xcode for OS X 10.9 Mavericks will ship a fake "gcc" binary that
		// actually runs clang. We prepare different command
		// lines for the two binaries, so it matters what we call it.
		// See golang.org/issue/5822.
		if(defaultclang)
			bprintf(&b, "clang");
		else
			bprintf(&b, "gcc");
	}
	defaultcc = btake(&b);
168

169 170 171 172 173 174 175 176 177
	xgetenv(&b, "CXX");
	if(b.len == 0) {
		if(defaultclang)
			bprintf(&b, "clang++");
		else
			bprintf(&b, "g++");
	}
	defaultcxx = btake(&b);

Russ Cox's avatar
Russ Cox committed
178 179 180
	xsetenv("GOROOT", goroot);
	xsetenv("GOARCH", goarch);
	xsetenv("GOOS", goos);
181
	xsetenv("GOARM", goarm);
182
	xsetenv("GO386", go386);
183

Russ Cox's avatar
Russ Cox committed
184 185 186 187
	// Make the environment more predictable.
	xsetenv("LANG", "C");
	xsetenv("LANGUAGE", "en_US.UTF8");

188 189
	goversion = findgoversion();

Russ Cox's avatar
Russ Cox committed
190 191 192
	workdir = xworkdir();
	xatexit(rmworkdir);

Russ Cox's avatar
Russ Cox committed
193 194 195
	bpathf(&b, "%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch);
	tooldir = btake(&b);

Russ Cox's avatar
Russ Cox committed
196 197 198 199 200 201 202
	bfree(&b);
}

// rmworkdir deletes the work directory.
static void
rmworkdir(void)
{
203
	if(vflag > 1)
204
		errprintf("rm -rf %s\n", workdir);
Russ Cox's avatar
Russ Cox committed
205 206 207
	xremoveall(workdir);
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
// Remove trailing spaces.
static void
chomp(Buf *b)
{
	int c;

	while(b->len > 0 && ((c=b->p[b->len-1]) == ' ' || c == '\t' || c == '\r' || c == '\n'))
		b->len--;
}


// findgoversion determines the Go version to use in the version string.
static char*
findgoversion(void)
{
	char *tag, *rev, *p;
	int i, nrev;
	Buf b, path, bmore, branch;
	Vec tags;
227

228 229 230 231 232
	binit(&b);
	binit(&path);
	binit(&bmore);
	binit(&branch);
	vinit(&tags);
233

234 235 236 237 238 239
	// The $GOROOT/VERSION file takes priority, for distributions
	// without the Mercurial repo.
	bpathf(&path, "%s/VERSION", goroot);
	if(isfile(bstr(&path))) {
		readfile(&b, bstr(&path));
		chomp(&b);
240 241 242 243 244 245
		// Commands such as "dist version > VERSION" will cause
		// the shell to create an empty VERSION file and set dist's
		// stdout to its fd. dist in turn looks at VERSION and uses
		// its content if available, which is empty at this point.
		if(b.len > 0)
			goto done;
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	}

	// The $GOROOT/VERSION.cache file is a cache to avoid invoking
	// hg every time we run this command.  Unlike VERSION, it gets
	// deleted by the clean command.
	bpathf(&path, "%s/VERSION.cache", goroot);
	if(isfile(bstr(&path))) {
		readfile(&b, bstr(&path));
		chomp(&b);
		goto done;
	}

	// Otherwise, use Mercurial.
	// What is the current branch?
	run(&branch, goroot, CheckExit, "hg", "identify", "-b", nil);
	chomp(&branch);

	// What are the tags along the current branch?
264
	tag = "devel";
265
	rev = ".";
266
	run(&b, goroot, CheckExit, "hg", "log", "-b", bstr(&branch), "-r", ".:0", "--template", "{tags} + ", nil);
267 268 269 270 271 272
	splitfields(&tags, bstr(&b));
	nrev = 0;
	for(i=0; i<tags.len; i++) {
		p = tags.p[i];
		if(streq(p, "+"))
			nrev++;
273 274 275
		// NOTE: Can reenable the /* */ code when we want to
		// start reporting versions named 'weekly' again.
		if(/*hasprefix(p, "weekly.") ||*/ hasprefix(p, "go")) {
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
			tag = xstrdup(p);
			// If this tag matches the current checkout
			// exactly (no "+" yet), don't show extra
			// revision information.
			if(nrev == 0)
				rev = "";
			break;
		}
	}

	if(tag[0] == '\0') {
		// Did not find a tag; use branch name.
		bprintf(&b, "branch.%s", bstr(&branch));
		tag = btake(&b);
	}
291

292 293 294
	if(rev[0]) {
		// Tag is before the revision we're building.
		// Add extra information.
295
		run(&bmore, goroot, CheckExit, "hg", "log", "--template", " +{node|short} {date|date}", "-r", rev, nil);
296 297
		chomp(&bmore);
	}
298

299 300 301 302 303
	bprintf(&b, "%s", tag);
	if(bmore.len > 0)
		bwriteb(&b, &bmore);

	// Cache version.
304
	writefile(&b, bstr(&path), 0);
305 306 307

done:
	p = btake(&b);
308 309


310 311 312 313 314
	bfree(&b);
	bfree(&path);
	bfree(&bmore);
	bfree(&branch);
	vfree(&tags);
315

316 317 318
	return p;
}

Russ Cox's avatar
Russ Cox committed
319 320 321 322 323 324 325 326 327 328 329
/*
 * Initial tree setup.
 */

// The old tools that no longer live in $GOBIN or $GOROOT/bin.
static char *oldtool[] = {
	"5a", "5c", "5g", "5l",
	"6a", "6c", "6g", "6l",
	"8a", "8c", "8g", "8l",
	"6cov",
	"6nm",
Russ Cox's avatar
Russ Cox committed
330
	"6prof",
Russ Cox's avatar
Russ Cox committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	"cgo",
	"ebnflint",
	"goapi",
	"gofix",
	"goinstall",
	"gomake",
	"gopack",
	"gopprof",
	"gotest",
	"gotype",
	"govet",
	"goyacc",
	"quietgcc",
};

346 347 348 349 350 351 352
// Unreleased directories (relative to $GOROOT) that should
// not be in release branches.
static char *unreleased[] = {
	"src/cmd/prof",
	"src/pkg/old",
};

Russ Cox's avatar
Russ Cox committed
353 354 355 356 357 358 359 360 361 362
// setup sets up the tree for the initial build.
static void
setup(void)
{
	int i;
	Buf b;
	char *p;

	binit(&b);

Russ Cox's avatar
Russ Cox committed
363
	// Create bin directory.
364
	p = bpathf(&b, "%s/bin", goroot);
Russ Cox's avatar
Russ Cox committed
365 366 367 368
	if(!isdir(p))
		xmkdir(p);

	// Create package directory.
369
	p = bpathf(&b, "%s/pkg", goroot);
Russ Cox's avatar
Russ Cox committed
370 371
	if(!isdir(p))
		xmkdir(p);
Russ Cox's avatar
Russ Cox committed
372 373 374
	p = bpathf(&b, "%s/pkg/%s_%s", goroot, gohostos, gohostarch);
	if(rebuildall)
		xremoveall(p);
375
	xmkdirall(p);
Russ Cox's avatar
Russ Cox committed
376 377 378 379
	if(!streq(goos, gohostos) || !streq(goarch, gohostarch)) {
		p = bpathf(&b, "%s/pkg/%s_%s", goroot, goos, goarch);
		if(rebuildall)
			xremoveall(p);
380
		xmkdirall(p);
Russ Cox's avatar
Russ Cox committed
381
	}
382

383 384
	// Create object directory.
	// We keep it in pkg/ so that all the generated binaries
Russ Cox's avatar
Russ Cox committed
385 386 387 388 389 390 391 392 393 394 395 396 397
	// are in one tree.  If pkg/obj/libgc.a exists, it is a dreg from
	// before we used subdirectories of obj.  Delete all of obj
	// to clean up.
	bpathf(&b, "%s/pkg/obj/libgc.a", goroot);
	if(isfile(bstr(&b)))
		xremoveall(bpathf(&b, "%s/pkg/obj", goroot));
	p = bpathf(&b, "%s/pkg/obj/%s_%s", goroot, gohostos, gohostarch);
	if(rebuildall)
		xremoveall(p);
	xmkdirall(p);

	// Create tool directory.
	// We keep it in pkg/, just like the object directory above.
398 399
	if(rebuildall)
		xremoveall(tooldir);
Russ Cox's avatar
Russ Cox committed
400 401 402 403
	xmkdirall(tooldir);

	// Remove tool binaries from before the tool/gohostos_gohostarch
	xremoveall(bpathf(&b, "%s/bin/tool", goroot));
Russ Cox's avatar
Russ Cox committed
404 405 406

	// Remove old pre-tool binaries.
	for(i=0; i<nelem(oldtool); i++)
Russ Cox's avatar
Russ Cox committed
407 408
		xremove(bpathf(&b, "%s/bin/%s", goroot, oldtool[i]));

Russ Cox's avatar
Russ Cox committed
409 410 411 412 413 414 415 416 417
	// If $GOBIN is set and has a Go compiler, it must be cleaned.
	for(i=0; gochars[i]; i++) {
		if(isfile(bprintf(&b, "%s%s%c%s", gobin, slash, gochars[i], "g"))) {
			for(i=0; i<nelem(oldtool); i++)
				xremove(bprintf(&b, "%s%s%s", gobin, slash, oldtool[i]));
			break;
		}
	}

418
	// For release, make sure excluded things are excluded.
419
	if(hasprefix(goversion, "release.") || hasprefix(goversion, "go")) {
420 421 422 423 424
		for(i=0; i<nelem(unreleased); i++)
			if(isdir(bpathf(&b, "%s/%s", goroot, unreleased[i])))
				fatal("%s should not exist in release build", bstr(&b));
	}

Russ Cox's avatar
Russ Cox committed
425 426 427 428 429 430 431 432
	bfree(&b);
}

/*
 * C library and tool building
 */

// gccargs is the gcc command line to use for compiling a single C file.
433
static char *proto_gccargs[] = {
Russ Cox's avatar
Russ Cox committed
434
	"-Wall",
435 436 437
	// native Plan 9 compilers don't like non-standard prototypes
	// so let gcc catch them.
	"-Wstrict-prototypes",
438 439 440
	"-Wextra",
	"-Wunused",
	"-Wuninitialized",
Russ Cox's avatar
Russ Cox committed
441 442 443 444 445 446
	"-Wno-sign-compare",
	"-Wno-missing-braces",
	"-Wno-parentheses",
	"-Wno-unknown-pragmas",
	"-Wno-switch",
	"-Wno-comment",
447
	"-Wno-missing-field-initializers",
Russ Cox's avatar
Russ Cox committed
448 449
	"-Werror",
	"-fno-common",
450
	"-ggdb",
451
	"-pipe",
452 453 454 455 456
#if defined(__NetBSD__) && defined(__arm__)
	// GCC 4.5.4 (NetBSD nb1 20120916) on ARM is known to mis-optimize gc/mparith3.c
	// Fix available at http://patchwork.ozlabs.org/patch/64562/.
	"-O1",
#else
457
	"-O2",
458
#endif
Russ Cox's avatar
Russ Cox committed
459 460
};

461 462
static Vec gccargs;

Russ Cox's avatar
Russ Cox committed
463
// deptab lists changes to the default dependencies for a given prefix.
464
// deps ending in /* read the whole directory; deps beginning with -
Russ Cox's avatar
Russ Cox committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
// exclude files with that prefix.
static struct {
	char *prefix;  // prefix of target
	char *dep[20];  // dependency tweaks for targets with that prefix
} deptab[] = {
	{"lib9", {
		"$GOROOT/include/u.h",
		"$GOROOT/include/utf.h",
		"$GOROOT/include/fmt.h",
		"$GOROOT/include/libc.h",
		"fmt/*",
		"utf/*",
	}},
	{"libbio", {
		"$GOROOT/include/u.h",
		"$GOROOT/include/utf.h",
		"$GOROOT/include/fmt.h",
		"$GOROOT/include/libc.h",
		"$GOROOT/include/bio.h",
	}},
	{"libmach", {
		"$GOROOT/include/u.h",
		"$GOROOT/include/utf.h",
		"$GOROOT/include/fmt.h",
		"$GOROOT/include/libc.h",
		"$GOROOT/include/bio.h",
		"$GOROOT/include/ar.h",
		"$GOROOT/include/bootexec.h",
		"$GOROOT/include/mach.h",
		"$GOROOT/include/ureg_amd64.h",
		"$GOROOT/include/ureg_arm.h",
		"$GOROOT/include/ureg_x86.h",
	}},
498 499 500 501 502 503 504 505 506 507 508 509
	{"liblink", {
		"$GOROOT/include/u.h",
		"$GOROOT/include/utf.h",
		"$GOROOT/include/fmt.h",
		"$GOROOT/include/libc.h",
		"$GOROOT/include/bio.h",
		"$GOROOT/include/ar.h",
		"$GOROOT/include/link.h",
		"anames5.c",
		"anames6.c",
		"anames8.c",
	}},
Russ Cox's avatar
Russ Cox committed
510 511 512 513 514 515 516
	{"cmd/cc", {
		"-pgen.c",
		"-pswt.c",
	}},
	{"cmd/gc", {
		"-cplx.c",
		"-pgen.c",
517
		"-plive.c",
518
		"-popt.c",
Russ Cox's avatar
Russ Cox committed
519 520 521 522 523 524
		"-y1.tab.c",  // makefile dreg
		"opnames.h",
	}},
	{"cmd/5c", {
		"../cc/pgen.c",
		"../cc/pswt.c",
Russ Cox's avatar
Russ Cox committed
525
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/libcc.a",
Russ Cox's avatar
Russ Cox committed
526 527 528 529
	}},
	{"cmd/6c", {
		"../cc/pgen.c",
		"../cc/pswt.c",
Russ Cox's avatar
Russ Cox committed
530
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/libcc.a",
Russ Cox's avatar
Russ Cox committed
531 532 533 534
	}},
	{"cmd/8c", {
		"../cc/pgen.c",
		"../cc/pswt.c",
Russ Cox's avatar
Russ Cox committed
535
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/libcc.a",
Russ Cox's avatar
Russ Cox committed
536 537 538 539
	}},
	{"cmd/5g", {
		"../gc/cplx.c",
		"../gc/pgen.c",
540
		"../gc/plive.c",
541 542
		"../gc/popt.c",
		"../gc/popt.h",
Russ Cox's avatar
Russ Cox committed
543
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/libgc.a",
Russ Cox's avatar
Russ Cox committed
544 545 546 547
	}},
	{"cmd/6g", {
		"../gc/cplx.c",
		"../gc/pgen.c",
548
		"../gc/plive.c",
549 550
		"../gc/popt.c",
		"../gc/popt.h",
Russ Cox's avatar
Russ Cox committed
551
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/libgc.a",
Russ Cox's avatar
Russ Cox committed
552 553 554 555
	}},
	{"cmd/8g", {
		"../gc/cplx.c",
		"../gc/pgen.c",
556
		"../gc/plive.c",
557 558
		"../gc/popt.c",
		"../gc/popt.h",
Russ Cox's avatar
Russ Cox committed
559
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/libgc.a",
Russ Cox's avatar
Russ Cox committed
560 561
	}},
	{"cmd/5l", {
562
		"../ld/*",
Russ Cox's avatar
Russ Cox committed
563 564 565 566 567 568 569
	}},
	{"cmd/6l", {
		"../ld/*",
	}},
	{"cmd/8l", {
		"../ld/*",
	}},
570 571 572
	{"cmd/go", {
		"zdefaultcc.go",
	}},
Russ Cox's avatar
Russ Cox committed
573
	{"cmd/", {
574
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/liblink.a",
Russ Cox's avatar
Russ Cox committed
575 576 577
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/libmach.a",
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/libbio.a",
		"$GOROOT/pkg/obj/$GOOS_$GOARCH/lib9.a",
578 579
	}},
	{"pkg/runtime", {
580
		"zaexperiment.h", // must sort above zasm
581
		"zasm_$GOOS_$GOARCH.h",
582
		"zsys_$GOOS_$GOARCH.s",
583 584 585 586
		"zgoarch_$GOARCH.go",
		"zgoos_$GOOS.go",
		"zruntime_defs_$GOOS_$GOARCH.go",
		"zversion.go",
Russ Cox's avatar
Russ Cox committed
587 588 589 590 591 592 593 594 595
	}},
};

// depsuffix records the allowed suffixes for source files.
char *depsuffix[] = {
	".c",
	".h",
	".s",
	".go",
596
	".goc",
Russ Cox's avatar
Russ Cox committed
597 598 599 600
};

// gentab records how to generate some trivial files.
static struct {
601
	char *nameprefix;
Russ Cox's avatar
Russ Cox committed
602 603 604
	void (*gen)(char*, char*);
} gentab[] = {
	{"opnames.h", gcopnames},
605 606 607
	{"anames5.c", mkanames},
	{"anames6.c", mkanames},
	{"anames8.c", mkanames},
608
	{"zasm_", mkzasm},
609
	{"zdefaultcc.go", mkzdefaultcc},
610
	{"zsys_", mkzsys},
611 612 613 614
	{"zgoarch_", mkzgoarch},
	{"zgoos_", mkzgoos},
	{"zruntime_defs_", mkzruntimedefs},
	{"zversion.go", mkzversion},
615
	{"zaexperiment.h", mkzexperiment},
616 617 618

	// not generated anymore, but delete the file if we see it
	{"enam.c", nil},
Russ Cox's avatar
Russ Cox committed
619 620 621 622 623 624 625
};

// install installs the library, package, or binary associated with dir,
// which is relative to $GOROOT/src.
static void
install(char *dir)
{
626
	char *name, *p, *elem, *prefix, *exe;
627
	bool islib, ispkg, isgo, stale, ispackcmd;
Russ Cox's avatar
Russ Cox committed
628 629 630
	Buf b, b1, path;
	Vec compile, files, link, go, missing, clean, lib, extra;
	Time ttarg, t;
631
	int i, j, k, n, doclean, targ;
Russ Cox's avatar
Russ Cox committed
632

Russ Cox's avatar
Russ Cox committed
633 634
	if(vflag) {
		if(!streq(goos, gohostos) || !streq(goarch, gohostarch))
635
			errprintf("%s (%s/%s)\n", dir, goos, goarch);
Russ Cox's avatar
Russ Cox committed
636
		else
637
			errprintf("%s\n", dir);
Russ Cox's avatar
Russ Cox committed
638
	}
639

Russ Cox's avatar
Russ Cox committed
640 641 642 643 644 645 646 647 648 649 650
	binit(&b);
	binit(&b1);
	binit(&path);
	vinit(&compile);
	vinit(&files);
	vinit(&link);
	vinit(&go);
	vinit(&missing);
	vinit(&clean);
	vinit(&lib);
	vinit(&extra);
651

652

653 654 655 656 657 658 659
	// path = full path to dir.
	bpathf(&path, "%s/src/%s", goroot, dir);
	name = lastelem(dir);

	// For misc/prof, copy into the tool directory and we're done.
	if(hasprefix(dir, "misc/")) {
		copy(bpathf(&b, "%s/%s", tooldir, name),
660
			bpathf(&b1, "%s/misc/%s", goroot, name), 1);
661 662 663
		goto out;
	}

Rob Pike's avatar
Rob Pike committed
664 665
	// For release, cmd/prof is not included.
	if((streq(dir, "cmd/prof")) && !isdir(bstr(&path))) {
666
		if(vflag > 1)
667
			errprintf("skipping %s - does not exist\n", dir);
668 669 670
		goto out;
	}

671 672
	// set up gcc command line on first run.
	if(gccargs.len == 0) {
673
		bprintf(&b, "%s", defaultcc);
674 675 676
		splitfields(&gccargs, bstr(&b));
		for(i=0; i<nelem(proto_gccargs); i++)
			vadd(&gccargs, proto_gccargs[i]);
Russ Cox's avatar
Russ Cox committed
677
		if(contains(gccargs.p[0], "clang")) {
678 679
			// disable ASCII art in clang errors, if possible
			vadd(&gccargs, "-fno-caret-diagnostics");
680 681 682
			// clang is too smart about unused command-line arguments
			vadd(&gccargs, "-Qunused-arguments");
		}
683 684
		// disable word wrapping in error messages
		vadd(&gccargs, "-fmessage-length=0");
685 686 687 688
		if(streq(gohostos, "darwin")) {
			// golang.org/issue/5261
			vadd(&gccargs, "-mmacosx-version-min=10.6");
		}
689
	}
690

Russ Cox's avatar
Russ Cox committed
691 692
	islib = hasprefix(dir, "lib") || streq(dir, "cmd/cc") || streq(dir, "cmd/gc");
	ispkg = hasprefix(dir, "pkg");
Russ Cox's avatar
Russ Cox committed
693
	isgo = ispkg || streq(dir, "cmd/go") || streq(dir, "cmd/cgo");
Russ Cox's avatar
Russ Cox committed
694

695 696 697
	exe = "";
	if(streq(gohostos, "windows"))
		exe = ".exe";
698

Russ Cox's avatar
Russ Cox committed
699
	// Start final link command line.
Scott Lawrence's avatar
Scott Lawrence committed
700
	// Note: code below knows that link.p[targ] is the target.
701
	ispackcmd = 0;
Russ Cox's avatar
Russ Cox committed
702 703 704
	if(islib) {
		// C library.
		vadd(&link, "ar");
705 706 707 708
		if(streq(gohostos, "plan9"))
			vadd(&link, "rc");
		else
			vadd(&link, "rsc");
Russ Cox's avatar
Russ Cox committed
709 710 711
		prefix = "";
		if(!hasprefix(name, "lib"))
			prefix = "lib";
Scott Lawrence's avatar
Scott Lawrence committed
712
		targ = link.len;
Russ Cox's avatar
Russ Cox committed
713
		vadd(&link, bpathf(&b, "%s/pkg/obj/%s_%s/%s%s.a", goroot, gohostos, gohostarch, prefix, name));
Russ Cox's avatar
Russ Cox committed
714 715
	} else if(ispkg) {
		// Go library (package).
716 717
		ispackcmd = 1;
		vadd(&link, "pack"); // program name - unused here, but all the other cases record one
718
		p = bprintf(&b, "%s/pkg/%s_%s/%s", goroot, goos, goarch, dir+4);
Russ Cox's avatar
Russ Cox committed
719 720
		*xstrrchr(p, '/') = '\0';
		xmkdirall(p);
Scott Lawrence's avatar
Scott Lawrence committed
721
		targ = link.len;
722
		vadd(&link, bpathf(&b, "%s/pkg/%s_%s/%s.a", goroot, goos, goarch, dir+4));
Russ Cox's avatar
Russ Cox committed
723
	} else if(streq(dir, "cmd/go") || streq(dir, "cmd/cgo")) {
Russ Cox's avatar
Russ Cox committed
724
		// Go command.
Russ Cox's avatar
Russ Cox committed
725
		vadd(&link, bpathf(&b, "%s/%sl", tooldir, gochar));
Russ Cox's avatar
Russ Cox committed
726
		vadd(&link, "-o");
Russ Cox's avatar
Russ Cox committed
727 728 729
		elem = name;
		if(streq(elem, "go"))
			elem = "go_bootstrap";
Scott Lawrence's avatar
Scott Lawrence committed
730
		targ = link.len;
Russ Cox's avatar
Russ Cox committed
731
		vadd(&link, bpathf(&b, "%s/%s%s", tooldir, elem, exe));
Russ Cox's avatar
Russ Cox committed
732
	} else {
Scott Lawrence's avatar
Scott Lawrence committed
733
		// C command. Use gccargs.
734 735 736 737 738 739 740
		if(streq(gohostos, "plan9")) {
			vadd(&link, bprintf(&b, "%sl", gohostchar));
			vadd(&link, "-o");
			targ = link.len;
			vadd(&link, bpathf(&b, "%s/%s", tooldir, name));
		} else {
			vcopy(&link, gccargs.p, gccargs.len);
741 742
			if(sflag)
				vadd(&link, "-static");
743 744 745 746 747 748 749 750
			vadd(&link, "-o");
			targ = link.len;
			vadd(&link, bpathf(&b, "%s/%s%s", tooldir, name, exe));
			if(streq(gohostarch, "amd64"))
				vadd(&link, "-m64");
			else if(streq(gohostarch, "386"))
				vadd(&link, "-m32");
		}
Russ Cox's avatar
Russ Cox committed
751
	}
Scott Lawrence's avatar
Scott Lawrence committed
752
	ttarg = mtime(link.p[targ]);
Russ Cox's avatar
Russ Cox committed
753 754 755 756 757

	// Gather files that are sources for this target.
	// Everything in that directory, and any target-specific
	// additions.
	xreaddir(&files, bstr(&path));
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

	// Remove files beginning with . or _,
	// which are likely to be editor temporary files.
	// This is the same heuristic build.ScanDir uses.
	// There do exist real C files beginning with _,
	// so limit that check to just Go files.
	n = 0;
	for(i=0; i<files.len; i++) {
		p = files.p[i];
		if(hasprefix(p, ".") || (hasprefix(p, "_") && hassuffix(p, ".go")))
			xfree(p);
		else
			files.p[n++] = p;
	}
	files.len = n;

Russ Cox's avatar
Russ Cox committed
774 775 776
	for(i=0; i<nelem(deptab); i++) {
		if(hasprefix(dir, deptab[i].prefix)) {
			for(j=0; (p=deptab[i].dep[j])!=nil; j++) {
777 778 779 780 781 782
				breset(&b1);
				bwritestr(&b1, p);
				bsubst(&b1, "$GOROOT", goroot);
				bsubst(&b1, "$GOOS", goos);
				bsubst(&b1, "$GOARCH", goarch);
				p = bstr(&b1);
Russ Cox's avatar
Russ Cox committed
783
				if(hassuffix(p, ".a")) {
784
					vadd(&lib, bpathf(&b, "%s", p));
Russ Cox's avatar
Russ Cox committed
785 786 787
					continue;
				}
				if(hassuffix(p, "/*")) {
788
					bpathf(&b, "%s/%s", bstr(&path), p);
Russ Cox's avatar
Russ Cox committed
789 790 791 792
					b.len -= 2;
					xreaddir(&extra, bstr(&b));
					bprintf(&b, "%s", p);
					b.len -= 2;
793 794
					for(k=0; k<extra.len; k++)
						vadd(&files, bpathf(&b1, "%s/%s", bstr(&b), extra.p[k]));
Russ Cox's avatar
Russ Cox committed
795 796 797 798 799 800 801 802 803 804 805 806 807
					continue;
				}
				if(hasprefix(p, "-")) {
					p++;
					n = 0;
					for(k=0; k<files.len; k++) {
						if(hasprefix(files.p[k], p))
							xfree(files.p[k]);
						else
							files.p[n++] = files.p[k];
					}
					files.len = n;
					continue;
808
				}
Russ Cox's avatar
Russ Cox committed
809 810 811 812 813
				vadd(&files, p);
			}
		}
	}
	vuniq(&files);
814

Russ Cox's avatar
Russ Cox committed
815 816 817
	// Convert to absolute paths.
	for(i=0; i<files.len; i++) {
		if(!isabs(files.p[i])) {
818
			bpathf(&b, "%s/%s", bstr(&path), files.p[i]);
Russ Cox's avatar
Russ Cox committed
819 820 821 822 823 824
			xfree(files.p[i]);
			files.p[i] = btake(&b);
		}
	}

	// Is the target up-to-date?
Russ Cox's avatar
Russ Cox committed
825
	stale = rebuildall;
Russ Cox's avatar
Russ Cox committed
826 827 828 829 830 831 832 833 834 835
	n = 0;
	for(i=0; i<files.len; i++) {
		p = files.p[i];
		for(j=0; j<nelem(depsuffix); j++)
			if(hassuffix(p, depsuffix[j]))
				goto ok;
		xfree(files.p[i]);
		continue;
	ok:
		t = mtime(p);
836 837 838 839 840 841
		if(t != 0 && !hassuffix(p, ".a") && !shouldbuild(p, dir)) {
			xfree(files.p[i]);
			continue;
		}
		if(hassuffix(p, ".go"))
			vadd(&go, p);
Russ Cox's avatar
Russ Cox committed
842 843 844 845 846 847 848 849 850 851
		if(t > ttarg)
			stale = 1;
		if(t == 0) {
			vadd(&missing, p);
			files.p[n++] = files.p[i];
			continue;
		}
		files.p[n++] = files.p[i];
	}
	files.len = n;
852

853 854 855 856
	// If there are no files to compile, we're done.
	if(files.len == 0)
		goto out;
	
Russ Cox's avatar
Russ Cox committed
857 858 859
	for(i=0; i<lib.len && !stale; i++)
		if(mtime(lib.p[i]) > ttarg)
			stale = 1;
860

Russ Cox's avatar
Russ Cox committed
861 862 863
	if(!stale)
		goto out;

864 865 866
	// For package runtime, copy some files into the work space.
	if(streq(dir, "pkg/runtime")) {
		copy(bpathf(&b, "%s/arch_GOARCH.h", workdir),
867
			bpathf(&b1, "%s/arch_%s.h", bstr(&path), goarch), 0);
868
		copy(bpathf(&b, "%s/defs_GOOS_GOARCH.h", workdir),
869
			bpathf(&b1, "%s/defs_%s_%s.h", bstr(&path), goos, goarch), 0);
870 871 872
		p = bpathf(&b1, "%s/signal_%s_%s.h", bstr(&path), goos, goarch);
		if(isfile(p))
			copy(bpathf(&b, "%s/signal_GOOS_GOARCH.h", workdir), p, 0);
873
		copy(bpathf(&b, "%s/os_GOOS.h", workdir),
874
			bpathf(&b1, "%s/os_%s.h", bstr(&path), goos), 0);
875
		copy(bpathf(&b, "%s/signals_GOOS.h", workdir),
876
			bpathf(&b1, "%s/signals_%s.h", bstr(&path), goos), 0);
877 878 879 880 881
	}

	// Generate any missing files; regenerate existing ones.
	for(i=0; i<files.len; i++) {
		p = files.p[i];
Russ Cox's avatar
Russ Cox committed
882 883
		elem = lastelem(p);
		for(j=0; j<nelem(gentab); j++) {
884 885
			if(gentab[j].gen == nil)
				continue;
886 887
			if(hasprefix(elem, gentab[j].nameprefix)) {
				if(vflag > 1)
888
					errprintf("generate %s\n", p);
Russ Cox's avatar
Russ Cox committed
889
				gentab[j].gen(bstr(&path), p);
890 891 892 893 894 895 896
				// Do not add generated file to clean list.
				// In pkg/runtime, we want to be able to
				// build the package with the go tool,
				// and it assumes these generated files already
				// exist (it does not know how to build them).
				// The 'clean' command can remove
				// the generated files.
Russ Cox's avatar
Russ Cox committed
897 898 899
				goto built;
			}
		}
900 901 902
		// Did not rebuild p.
		if(find(p, missing.p, missing.len) >= 0)
			fatal("missing file %s", p);
Russ Cox's avatar
Russ Cox committed
903 904 905
	built:;
	}

906 907 908 909 910
	// One more copy for package runtime.
	// The last batch was required for the generators.
	// This one is generated.
	if(streq(dir, "pkg/runtime")) {
		copy(bpathf(&b, "%s/zasm_GOOS_GOARCH.h", workdir),
911
			bpathf(&b1, "%s/zasm_%s_%s.h", bstr(&path), goos, goarch), 0);
912
	}
913

914
	// Generate .c files from .goc files.
915
	if(streq(dir, "pkg/runtime")) {
916 917 918 919
		for(i=0; i<files.len; i++) {
			p = files.p[i];
			if(!hassuffix(p, ".goc"))
				continue;
920
			// b = path/zp but with _goos_goarch.c instead of .goc
921 922
			bprintf(&b, "%s%sz%s", bstr(&path), slash, lastelem(p));
			b.len -= 4;
923
			bwritef(&b, "_%s_%s.c", goos, goarch);
924 925 926 927 928
			goc2c(p, bstr(&b));
			vadd(&files, bstr(&b));
		}
		vuniq(&files);
	}
929

930
	if((!streq(goos, gohostos) || !streq(goarch, gohostarch)) && isgo) {
Russ Cox's avatar
Russ Cox committed
931 932
		// We've generated the right files; the go command can do the build.
		if(vflag > 1)
933
			errprintf("skip build for cross-compile %s\n", dir);
Russ Cox's avatar
Russ Cox committed
934 935
		goto nobuild;
	}
936

Russ Cox's avatar
Russ Cox committed
937 938 939 940 941 942 943 944 945
	// Compile the files.
	for(i=0; i<files.len; i++) {
		if(!hassuffix(files.p[i], ".c") && !hassuffix(files.p[i], ".s"))
			continue;
		name = lastelem(files.p[i]);

		vreset(&compile);
		if(!isgo) {
			// C library or tool.
946 947
			if(streq(gohostos, "plan9")) {
				vadd(&compile, bprintf(&b, "%sc", gohostchar));
948 949 950 951
				vadd(&compile, "-FTVwp");
				vadd(&compile, "-DPLAN9");
				vadd(&compile, "-D__STDC__=1");
				vadd(&compile, "-D__SIZE_TYPE__=ulong"); // for GNU Bison
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
				vadd(&compile, bpathf(&b, "-I%s/include/plan9", goroot));
				vadd(&compile, bpathf(&b, "-I%s/include/plan9/%s", goroot, gohostarch));
			} else {
				vcopy(&compile, gccargs.p, gccargs.len);
				vadd(&compile, "-c");
				if(streq(gohostarch, "amd64"))
					vadd(&compile, "-m64");
				else if(streq(gohostarch, "386"))
					vadd(&compile, "-m32");
				if(streq(dir, "lib9"))
					vadd(&compile, "-DPLAN9PORT");
	
				vadd(&compile, "-I");
				vadd(&compile, bpathf(&b, "%s/include", goroot));
			}
967

Russ Cox's avatar
Russ Cox committed
968 969
			vadd(&compile, "-I");
			vadd(&compile, bstr(&path));
970

Russ Cox's avatar
Russ Cox committed
971
			// lib9/goos.c gets the default constants hard-coded.
Russ Cox's avatar
Russ Cox committed
972
			if(streq(name, "goos.c")) {
973 974 975 976
				vadd(&compile, "-D");
				vadd(&compile, bprintf(&b, "GOOS=\"%s\"", goos));
				vadd(&compile, "-D");
				vadd(&compile, bprintf(&b, "GOARCH=\"%s\"", goarch));
977
				bprintf(&b1, "%s", goroot_final);
978
				bsubst(&b1, "\\", "\\\\");  // turn into C string
979 980 981 982 983 984 985 986
				vadd(&compile, "-D");
				vadd(&compile, bprintf(&b, "GOROOT=\"%s\"", bstr(&b1)));
				vadd(&compile, "-D");
				vadd(&compile, bprintf(&b, "GOVERSION=\"%s\"", goversion));
				vadd(&compile, "-D");
				vadd(&compile, bprintf(&b, "GOARM=\"%s\"", goarm));
				vadd(&compile, "-D");
				vadd(&compile, bprintf(&b, "GO386=\"%s\"", go386));
987 988
				vadd(&compile, "-D");
				vadd(&compile, bprintf(&b, "GO_EXTLINK_ENABLED=\"%s\"", goextlinkenabled));
Russ Cox's avatar
Russ Cox committed
989
			}
990

Russ Cox's avatar
Russ Cox committed
991 992 993
			// gc/lex.c records the GOEXPERIMENT setting used during the build.
			if(streq(name, "lex.c")) {
				xgetenv(&b, "GOEXPERIMENT");
994 995
				vadd(&compile, "-D");
				vadd(&compile, bprintf(&b1, "GOEXPERIMENT=\"%s\"", bstr(&b)));
Russ Cox's avatar
Russ Cox committed
996 997 998
			}
		} else {
			// Supporting files for a Go package.
999
			if(hassuffix(files.p[i], ".s"))
Russ Cox's avatar
Russ Cox committed
1000
				vadd(&compile, bpathf(&b, "%s/%sa", tooldir, gochar));
1001
			else {
Russ Cox's avatar
Russ Cox committed
1002
				vadd(&compile, bpathf(&b, "%s/%sc", tooldir, gochar));
1003 1004 1005
				vadd(&compile, "-F");
				vadd(&compile, "-V");
				vadd(&compile, "-w");
Russ Cox's avatar
Russ Cox committed
1006 1007 1008
			}
			vadd(&compile, "-I");
			vadd(&compile, workdir);
1009 1010
			vadd(&compile, "-I");
			vadd(&compile, bprintf(&b, "%s/pkg/%s_%s", goroot, goos, goarch));
1011 1012 1013 1014
			vadd(&compile, "-D");
			vadd(&compile, bprintf(&b, "GOOS_%s", goos));
			vadd(&compile, "-D");
			vadd(&compile, bprintf(&b, "GOARCH_%s", goarch));
Russ Cox's avatar
Russ Cox committed
1015 1016
			vadd(&compile, "-D");
			vadd(&compile, bprintf(&b, "GOOS_GOARCH_%s_%s", goos, goarch));
1017
		}
Russ Cox's avatar
Russ Cox committed
1018

Russ Cox's avatar
Russ Cox committed
1019 1020
		bpathf(&b, "%s/%s", workdir, lastelem(files.p[i]));
		doclean = 1;
1021 1022 1023 1024 1025 1026 1027 1028
		if(!isgo && streq(gohostos, "darwin")) {
			// To debug C programs on OS X, it is not enough to say -ggdb
			// on the command line.  You have to leave the object files
			// lying around too.  Leave them in pkg/obj/, which does not
			// get removed when this tool exits.
			bpathf(&b1, "%s/pkg/obj/%s", goroot, dir);
			xmkdirall(bstr(&b1));
			bpathf(&b, "%s/%s", bstr(&b1), lastelem(files.p[i]));
Russ Cox's avatar
Russ Cox committed
1029 1030
			doclean = 0;
		}
1031

1032 1033 1034 1035 1036
		// Change the last character of the output file (which was c or s).
		if(streq(gohostos, "plan9"))
			b.p[b.len-1] = gohostchar[0];
		else
			b.p[b.len-1] = 'o';
Russ Cox's avatar
Russ Cox committed
1037 1038 1039
		vadd(&compile, "-o");
		vadd(&compile, bstr(&b));
		vadd(&compile, files.p[i]);
1040
		bgrunv(bstr(&path), CheckExit, &compile);
Russ Cox's avatar
Russ Cox committed
1041

1042
		vadd(&link, bstr(&b));
Russ Cox's avatar
Russ Cox committed
1043 1044
		if(doclean)
			vadd(&clean, bstr(&b));
Russ Cox's avatar
Russ Cox committed
1045
	}
1046
	bgwait();
1047

Russ Cox's avatar
Russ Cox committed
1048 1049 1050 1051
	if(isgo) {
		// The last loop was compiling individual files.
		// Hand the Go files to the compiler en masse.
		vreset(&compile);
Russ Cox's avatar
Russ Cox committed
1052
		vadd(&compile, bpathf(&b, "%s/%sg", tooldir, gochar));
Russ Cox's avatar
Russ Cox committed
1053

1054 1055
		bpathf(&b, "%s/_go_.a", workdir);
		vadd(&compile, "-pack");
Russ Cox's avatar
Russ Cox committed
1056 1057 1058
		vadd(&compile, "-o");
		vadd(&compile, bstr(&b));
		vadd(&clean, bstr(&b));
1059 1060
		if(!ispackcmd)
			vadd(&link, bstr(&b));
1061

Russ Cox's avatar
Russ Cox committed
1062 1063 1064 1065 1066
		vadd(&compile, "-p");
		if(hasprefix(dir, "pkg/"))
			vadd(&compile, dir+4);
		else
			vadd(&compile, "main");
1067

Russ Cox's avatar
Russ Cox committed
1068 1069
		if(streq(dir, "pkg/runtime"))
			vadd(&compile, "-+");
1070

Russ Cox's avatar
Russ Cox committed
1071 1072 1073
		vcopy(&compile, go.p, go.len);

		runv(nil, bstr(&path), CheckExit, &compile);
1074 1075 1076 1077 1078 1079

		if(ispackcmd) {
			xremove(link.p[targ]);
			dopack(link.p[targ], bstr(&b), &link.p[targ+1], link.len - (targ+1));
			goto nobuild;
		}
Russ Cox's avatar
Russ Cox committed
1080 1081 1082 1083 1084
	}

	if(!islib && !isgo) {
		// C binaries need the libraries explicitly, and -lm.
		vcopy(&link, lib.p, lib.len);
1085 1086
		if(!streq(gohostos, "plan9"))
			vadd(&link, "-lm");
Russ Cox's avatar
Russ Cox committed
1087 1088 1089
	}

	// Remove target before writing it.
Scott Lawrence's avatar
Scott Lawrence committed
1090
	xremove(link.p[targ]);
Russ Cox's avatar
Russ Cox committed
1091 1092 1093

	runv(nil, nil, CheckExit, &link);

Russ Cox's avatar
Russ Cox committed
1094
nobuild:
1095 1096 1097 1098
	// In package runtime, we install runtime.h and cgocall.h too,
	// for use by cgo compilation.
	if(streq(dir, "pkg/runtime")) {
		copy(bpathf(&b, "%s/pkg/%s_%s/cgocall.h", goroot, goos, goarch),
1099
			bpathf(&b1, "%s/src/pkg/runtime/cgocall.h", goroot), 0);
1100
		copy(bpathf(&b, "%s/pkg/%s_%s/runtime.h", goroot, goos, goarch),
1101
			bpathf(&b1, "%s/src/pkg/runtime/runtime.h", goroot), 0);
1102 1103 1104
	}


Russ Cox's avatar
Russ Cox committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
out:
	for(i=0; i<clean.len; i++)
		xremove(clean.p[i]);

	bfree(&b);
	bfree(&b1);
	bfree(&path);
	vfree(&compile);
	vfree(&files);
	vfree(&link);
	vfree(&go);
	vfree(&missing);
	vfree(&clean);
	vfree(&lib);
	vfree(&extra);
}

// matchfield reports whether the field matches this build.
static bool
matchfield(char *f)
{
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	char *p;
	bool res;

	p = xstrrchr(f, ',');
	if(p == nil)
		return streq(f, goos) || streq(f, goarch) || streq(f, "cmd_go_bootstrap") || streq(f, "go1.1");
	*p = 0;
	res = matchfield(f) && matchfield(p+1);
	*p = ',';
	return res;
Russ Cox's avatar
Russ Cox committed
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
}

// shouldbuild reports whether we should build this file.
// It applies the same rules that are used with context tags
// in package go/build, except that the GOOS and GOARCH
// can appear anywhere in the file name, not just after _.
// In particular, they can be the entire file name (like windows.c).
// We also allow the special tag cmd_go_bootstrap.
// See ../go/bootstrap.go and package go/build.
static bool
shouldbuild(char *file, char *dir)
{
	char *name, *p;
1149
	int i, j, ret;
Russ Cox's avatar
Russ Cox committed
1150 1151
	Buf b;
	Vec lines, fields;
1152

1153 1154 1155 1156
	// On Plan 9, most of the libraries are already present.
	// The main exception is libmach which has been modified
	// in various places to support Go object files.
	if(streq(gohostos, "plan9")) {
1157 1158 1159 1160
		if(streq(dir, "lib9")) {
			name = lastelem(file);
			if(streq(name, "goos.c") || streq(name, "flag.c"))
				return 1;
1161 1162
			if(!contains(name, "plan9"))
				return 0;
1163
		}
1164 1165
	}
	
Russ Cox's avatar
Russ Cox committed
1166 1167 1168 1169 1170 1171 1172 1173
	// Check file name for GOOS or GOARCH.
	name = lastelem(file);
	for(i=0; i<nelem(okgoos); i++)
		if(contains(name, okgoos[i]) && !streq(okgoos[i], goos))
			return 0;
	for(i=0; i<nelem(okgoarch); i++)
		if(contains(name, okgoarch[i]) && !streq(okgoarch[i], goarch))
			return 0;
1174

Russ Cox's avatar
Russ Cox committed
1175 1176 1177
	// Omit test files.
	if(contains(name, "_test"))
		return 0;
1178

1179 1180 1181 1182 1183 1184
	// cmd/go/doc.go has a giant /* */ comment before
	// it gets to the important detail that it is not part of
	// package main.  We don't parse those comments,
	// so special case that file.
	if(hassuffix(file, "cmd/go/doc.go") || hassuffix(file, "cmd\\go\\doc.go"))
		return 0;
Russ Cox's avatar
Russ Cox committed
1185 1186
	if(hassuffix(file, "cmd/cgo/doc.go") || hassuffix(file, "cmd\\cgo\\doc.go"))
		return 0;
Russ Cox's avatar
Russ Cox committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205

	// Check file contents for // +build lines.
	binit(&b);
	vinit(&lines);
	vinit(&fields);

	ret = 1;
	readfile(&b, file);
	splitlines(&lines, bstr(&b));
	for(i=0; i<lines.len; i++) {
		p = lines.p[i];
		while(*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
			p++;
		if(*p == '\0')
			continue;
		if(contains(p, "package documentation")) {
			ret = 0;
			goto out;
		}
Russ Cox's avatar
Russ Cox committed
1206
		if(contains(p, "package main") && !streq(dir, "cmd/go") && !streq(dir, "cmd/cgo")) {
Russ Cox's avatar
Russ Cox committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
			ret = 0;
			goto out;
		}
		if(!hasprefix(p, "//"))
			break;
		if(!contains(p, "+build"))
			continue;
		splitfields(&fields, lines.p[i]);
		if(fields.len < 2 || !streq(fields.p[1], "+build"))
			continue;
		for(j=2; j<fields.len; j++) {
			p = fields.p[j];
			if((*p == '!' && !matchfield(p+1)) || matchfield(p))
				goto fieldmatch;
		}
		ret = 0;
		goto out;
	fieldmatch:;
	}

out:
	bfree(&b);
	vfree(&lines);
	vfree(&fields);
1231

Russ Cox's avatar
Russ Cox committed
1232 1233 1234 1235 1236
	return ret;
}

// copy copies the file src to dst, via memory (so only good for small files).
static void
1237
copy(char *dst, char *src, int exec)
Russ Cox's avatar
Russ Cox committed
1238 1239
{
	Buf b;
1240

1241
	if(vflag > 1)
1242
		errprintf("cp %s %s\n", src, dst);
1243

Russ Cox's avatar
Russ Cox committed
1244 1245
	binit(&b);
	readfile(&b, src);
1246
	writefile(&b, dst, exec);
Russ Cox's avatar
Russ Cox committed
1247 1248 1249
	bfree(&b);
}

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
// dopack copies the package src to dst,
// appending the files listed in extra.
// The archive format is the traditional Unix ar format.
static void
dopack(char *dst, char *src, char **extra, int nextra)
{
	int i;
	char c, *p, *q;
	Buf b, bdst;
	
	binit(&b);
	binit(&bdst);

	readfile(&bdst, src);
	for(i=0; i<nextra; i++) {
		readfile(&b, extra[i]);
		// find last path element for archive member name
		p = xstrrchr(extra[i], '/');
		if(p)
			p++;
		q = xstrrchr(extra[i], '\\');
		if(q) {
			q++;
			if(p == nil || q > p)
				p = q;
		}
		if(p == nil)
			p = extra[i];
		bwritef(&bdst, "%-16.16s%-12d%-6d%-6d%-8o%-10d`\n", p, 0, 0, 0, 0644, b.len);
		bwriteb(&bdst, &b);
		if(b.len&1) {
			c = 0;
			bwrite(&bdst, &c, 1);
		}
	}

	writefile(&bdst, dst, 0);

	bfree(&b);
	bfree(&bdst);
}

Russ Cox's avatar
Russ Cox committed
1292 1293 1294 1295 1296
// buildorder records the order of builds for the 'go bootstrap' command.
static char *buildorder[] = {
	"lib9",
	"libbio",
	"libmach",
1297
	"liblink",
1298

1299
	"misc/pprof",
Russ Cox's avatar
Russ Cox committed
1300

1301 1302
	"cmd/addr2line",
	"cmd/objdump",
Russ Cox's avatar
Russ Cox committed
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	"cmd/pack",
	"cmd/prof",

	"cmd/cc",  // must be before c
	"cmd/gc",  // must be before g
	"cmd/%sl",  // must be before a, c, g
	"cmd/%sa",
	"cmd/%sc",
	"cmd/%sg",

	// The dependency order here was copied from a buildscript
	// back when there were build scripts.  Will have to
	// be maintained by hand, but shouldn't change very
	// often.
	"pkg/runtime",
	"pkg/errors",
	"pkg/sync/atomic",
	"pkg/sync",
	"pkg/io",
	"pkg/unicode",
	"pkg/unicode/utf8",
	"pkg/unicode/utf16",
	"pkg/bytes",
	"pkg/math",
	"pkg/strings",
	"pkg/strconv",
	"pkg/bufio",
	"pkg/sort",
	"pkg/container/heap",
	"pkg/encoding/base64",
	"pkg/syscall",
	"pkg/time",
	"pkg/os",
	"pkg/reflect",
	"pkg/fmt",
Russ Cox's avatar
Russ Cox committed
1338
	"pkg/encoding",
Russ Cox's avatar
Russ Cox committed
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
	"pkg/encoding/json",
	"pkg/flag",
	"pkg/path/filepath",
	"pkg/path",
	"pkg/io/ioutil",
	"pkg/log",
	"pkg/regexp/syntax",
	"pkg/regexp",
	"pkg/go/token",
	"pkg/go/scanner",
	"pkg/go/ast",
	"pkg/go/parser",
	"pkg/os/exec",
Alex Brainman's avatar
Alex Brainman committed
1352
	"pkg/os/signal",
Russ Cox's avatar
Russ Cox committed
1353 1354 1355
	"pkg/net/url",
	"pkg/text/template/parse",
	"pkg/text/template",
1356
	"pkg/go/doc",
1357
	"pkg/go/build",
Russ Cox's avatar
Russ Cox committed
1358 1359 1360
	"cmd/go",
};

1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
// cleantab records the directories to clean in 'go clean'.
// It is bigger than the buildorder because we clean all the
// compilers but build only the $GOARCH ones.
static char *cleantab[] = {
	"cmd/5a",
	"cmd/5c",
	"cmd/5g",
	"cmd/5l",
	"cmd/6a",
	"cmd/6c",
	"cmd/6g",
	"cmd/6l",
	"cmd/8a",
	"cmd/8c",
	"cmd/8g",
	"cmd/8l",
1377
	"cmd/addr2line",
1378 1379
	"cmd/cc",
	"cmd/gc",
Russ Cox's avatar
Russ Cox committed
1380
	"cmd/go",	
1381
	"cmd/objdump",
1382 1383 1384 1385 1386
	"cmd/pack",
	"cmd/prof",
	"lib9",
	"libbio",
	"libmach",
1387
	"liblink",
1388 1389 1390
	"pkg/bufio",
	"pkg/bytes",
	"pkg/container/heap",
Russ Cox's avatar
Russ Cox committed
1391
	"pkg/encoding",
1392 1393 1394 1395 1396 1397 1398
	"pkg/encoding/base64",
	"pkg/encoding/json",
	"pkg/errors",
	"pkg/flag",
	"pkg/fmt",
	"pkg/go/ast",
	"pkg/go/build",
1399
	"pkg/go/doc",
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
	"pkg/go/parser",
	"pkg/go/scanner",
	"pkg/go/token",
	"pkg/io",
	"pkg/io/ioutil",
	"pkg/log",
	"pkg/math",
	"pkg/net/url",
	"pkg/os",
	"pkg/os/exec",
	"pkg/path",
	"pkg/path/filepath",
	"pkg/reflect",
	"pkg/regexp",
	"pkg/regexp/syntax",
	"pkg/runtime",
	"pkg/sort",
	"pkg/strconv",
	"pkg/strings",
	"pkg/sync",
	"pkg/sync/atomic",
	"pkg/syscall",
	"pkg/text/template",
	"pkg/text/template/parse",
	"pkg/time",
	"pkg/unicode",
	"pkg/unicode/utf16",
	"pkg/unicode/utf8",
};

static void
clean(void)
{
	int i, j, k;
	Buf b, path;
	Vec dir;
1436

1437 1438 1439
	binit(&b);
	binit(&path);
	vinit(&dir);
1440

1441
	for(i=0; i<nelem(cleantab); i++) {
Rob Pike's avatar
Rob Pike committed
1442
		if((streq(cleantab[i], "cmd/prof")) && !isdir(cleantab[i]))
1443
			continue;
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
		bpathf(&path, "%s/src/%s", goroot, cleantab[i]);
		xreaddir(&dir, bstr(&path));
		// Remove generated files.
		for(j=0; j<dir.len; j++) {
			for(k=0; k<nelem(gentab); k++) {
				if(hasprefix(dir.p[j], gentab[k].nameprefix))
					xremove(bpathf(&b, "%s/%s", bstr(&path), dir.p[j]));
			}
		}
		// Remove generated binary named for directory.
		if(hasprefix(cleantab[i], "cmd/"))
			xremove(bpathf(&b, "%s/%s", bstr(&path), cleantab[i]+4));
	}

1458 1459 1460 1461 1462 1463 1464 1465 1466
	// remove src/pkg/runtime/z* unconditionally
	vreset(&dir);
	bpathf(&path, "%s/src/pkg/runtime", goroot);
	xreaddir(&dir, bstr(&path));
	for(j=0; j<dir.len; j++) {
		if(hasprefix(dir.p[j], "z"))
			xremove(bpathf(&b, "%s/%s", bstr(&path), dir.p[j]));
	}

Russ Cox's avatar
Russ Cox committed
1467 1468 1469
	if(rebuildall) {
		// Remove object tree.
		xremoveall(bpathf(&b, "%s/pkg/obj/%s_%s", goroot, gohostos, gohostarch));
1470

Russ Cox's avatar
Russ Cox committed
1471 1472 1473 1474 1475 1476 1477 1478
		// Remove installed packages and tools.
		xremoveall(bpathf(&b, "%s/pkg/%s_%s", goroot, gohostos, gohostarch));
		xremoveall(bpathf(&b, "%s/pkg/%s_%s", goroot, goos, goarch));
		xremoveall(tooldir);

		// Remove cached version info.
		xremove(bpathf(&b, "%s/VERSION.cache", goroot));
	}
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518

	bfree(&b);
	bfree(&path);
	vfree(&dir);
}

/*
 * command implementations
 */

void
usage(void)
{
	xprintf("usage: go tool dist [command]\n"
		"Commands are:\n"
		"\n"
		"banner         print installation banner\n"
		"bootstrap      rebuild everything\n"
		"clean          deletes all built files\n"
		"env [-p]       print environment (-p: include $PATH)\n"
		"install [dir]  install individual directory\n"
		"version        print Go version\n"
		"\n"
		"All commands take -v flags to emit extra information.\n"
	);
	xexit(2);
}

// The env command prints the default environment.
void
cmdenv(int argc, char **argv)
{
	bool pflag;
	char *sep;
	Buf b, b1;
	char *format;

	binit(&b);
	binit(&b1);

Russ Cox's avatar
Russ Cox committed
1519
	format = "%s=\"%s\"\n";
1520 1521
	pflag = 0;
	ARGBEGIN{
1522 1523 1524
	case '9':
		format = "%s='%s'\n";
		break;
1525 1526 1527 1528 1529 1530 1531
	case 'p':
		pflag = 1;
		break;
	case 'v':
		vflag++;
		break;
	case 'w':
1532
		format = "set %s=%s\r\n";
1533 1534 1535 1536 1537 1538 1539
		break;
	default:
		usage();
	}ARGEND

	if(argc > 0)
		usage();
1540

1541
	xprintf(format, "CC", defaultcc);
1542
	xprintf(format, "GOROOT", goroot);
1543
	xprintf(format, "GOBIN", gobin);
1544 1545
	xprintf(format, "GOARCH", goarch);
	xprintf(format, "GOOS", goos);
Russ Cox's avatar
Russ Cox committed
1546 1547 1548
	xprintf(format, "GOHOSTARCH", gohostarch);
	xprintf(format, "GOHOSTOS", gohostos);
	xprintf(format, "GOTOOLDIR", tooldir);
Russ Cox's avatar
Russ Cox committed
1549
	xprintf(format, "GOCHAR", gochar);
1550 1551
	if(streq(goarch, "arm"))
		xprintf(format, "GOARM", goarm);
1552 1553
	if(streq(goarch, "386"))
		xprintf(format, "GO386", go386);
Russ Cox's avatar
Russ Cox committed
1554

1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
	if(pflag) {
		sep = ":";
		if(streq(gohostos, "windows"))
			sep = ";";
		xgetenv(&b, "PATH");
		bprintf(&b1, "%s%s%s", gobin, sep, bstr(&b));
		xprintf(format, "PATH", bstr(&b1));
	}

	bfree(&b);
	bfree(&b1);
}

Russ Cox's avatar
Russ Cox committed
1568 1569 1570 1571 1572 1573 1574
// The bootstrap command runs a build from scratch,
// stopping at having installed the go_bootstrap command.
void
cmdbootstrap(int argc, char **argv)
{
	int i;
	Buf b;
Russ Cox's avatar
Russ Cox committed
1575 1576 1577
	char *oldgoos, *oldgoarch, *oldgochar;

	binit(&b);
Russ Cox's avatar
Russ Cox committed
1578

1579
	ARGBEGIN{
Russ Cox's avatar
Russ Cox committed
1580 1581 1582
	case 'a':
		rebuildall = 1;
		break;
1583 1584 1585
	case 's':
		sflag++;
		break;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
	case 'v':
		vflag++;
		break;
	default:
		usage();
	}ARGEND

	if(argc > 0)
		usage();

1596 1597
	if(rebuildall)
		clean();
1598
	goversion = findgoversion();
Russ Cox's avatar
Russ Cox committed
1599
	setup();
1600

1601 1602 1603
	xsetenv("GOROOT", goroot);
	xsetenv("GOROOT_FINAL", goroot_final);

Russ Cox's avatar
Russ Cox committed
1604 1605 1606 1607 1608 1609 1610 1611 1612
	// For the main bootstrap, building for host os/arch.
	oldgoos = goos;
	oldgoarch = goarch;
	oldgochar = gochar;
	goos = gohostos;
	goarch = gohostarch;
	gochar = gohostchar;
	xsetenv("GOARCH", goarch);
	xsetenv("GOOS", goos);
1613

Russ Cox's avatar
Russ Cox committed
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
	for(i=0; i<nelem(buildorder); i++) {
		install(bprintf(&b, buildorder[i], gohostchar));
		if(!streq(oldgochar, gohostchar) && xstrstr(buildorder[i], "%s"))
			install(bprintf(&b, buildorder[i], oldgochar));
	}

	goos = oldgoos;
	goarch = oldgoarch;
	gochar = oldgochar;
	xsetenv("GOARCH", goarch);
	xsetenv("GOOS", goos);

	// Build pkg/runtime for actual goos/goarch too.
	if(!streq(goos, gohostos) || !streq(goarch, gohostarch))
		install("pkg/runtime");

Russ Cox's avatar
Russ Cox committed
1630 1631 1632
	bfree(&b);
}

1633 1634 1635 1636
static char*
defaulttarg(void)
{
	char *p;
1637
	Buf pwd, src, real_src;
1638

1639 1640
	binit(&pwd);
	binit(&src);
1641
	binit(&real_src);
1642

1643 1644 1645 1646
	// xgetwd might return a path with symlinks fully resolved, and if
	// there happens to be symlinks in goroot, then the hasprefix test
	// will never succeed. Instead, we use xrealwd to get a canonical
	// goroot/src before the comparison to avoid this problem.
1647 1648 1649
	xgetwd(&pwd);
	p = btake(&pwd);
	bpathf(&src, "%s/src/", goroot);
1650 1651 1652 1653 1654 1655 1656
	xrealwd(&real_src, bstr(&src));
	if(!hasprefix(p, bstr(&real_src)))
		fatal("current directory %s is not under %s", p, bstr(&real_src));
	p += real_src.len;
	// guard againt xrealwd return the directory without the trailing /
	if(*p == slash[0])
		p++;
1657 1658 1659

	bfree(&pwd);
	bfree(&src);
1660
	bfree(&real_src);
1661

1662 1663 1664
	return p;
}

Russ Cox's avatar
Russ Cox committed
1665 1666 1667 1668 1669 1670
// Install installs the list of packages named on the command line.
void
cmdinstall(int argc, char **argv)
{
	int i;

1671
	ARGBEGIN{
1672 1673 1674
	case 's':
		sflag++;
		break;
1675 1676 1677 1678 1679 1680
	case 'v':
		vflag++;
		break;
	default:
		usage();
	}ARGEND
1681

1682 1683 1684 1685
	if(argc == 0)
		install(defaulttarg());

	for(i=0; i<argc; i++)
Russ Cox's avatar
Russ Cox committed
1686 1687
		install(argv[i]);
}
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711

// Clean deletes temporary objects.
// Clean -i deletes the installed objects too.
void
cmdclean(int argc, char **argv)
{
	ARGBEGIN{
	case 'v':
		vflag++;
		break;
	default:
		usage();
	}ARGEND

	if(argc > 0)
		usage();

	clean();
}

// Banner prints the 'now you've installed Go' banner.
void
cmdbanner(int argc, char **argv)
{
1712
	char *pathsep, *pid, *ns;
1713
	Buf b, b1, search, path;
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728

	ARGBEGIN{
	case 'v':
		vflag++;
		break;
	default:
		usage();
	}ARGEND

	if(argc > 0)
		usage();

	binit(&b);
	binit(&b1);
	binit(&search);
1729
	binit(&path);
1730

1731 1732 1733 1734 1735
	xprintf("\n");
	xprintf("---\n");
	xprintf("Installed Go for %s/%s in %s\n", goos, goarch, goroot);
	xprintf("Installed commands in %s\n", gobin);

1736 1737 1738 1739
	if(!xsamefile(goroot_final, goroot)) {
		// If the files are to be moved, don't check that gobin
		// is on PATH; assume they know what they are doing.
	} else if(streq(gohostos, "plan9")) {
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
		// Check that gobin is bound before /bin.
		readfile(&b, "#c/pid");
		bsubst(&b, " ", "");
		pid = btake(&b);
		bprintf(&b, "/proc/%s/ns", pid);
		ns = btake(&b);
		readfile(&b, ns);
		bprintf(&search, "bind -b %s /bin\n", gobin);
		if(xstrstr(bstr(&b), bstr(&search)) == nil)
			xprintf("*** You need to bind %s before /bin.\n", gobin);
	} else {
		// Check that gobin appears in $PATH.
		xgetenv(&b, "PATH");
		pathsep = ":";
		if(streq(gohostos, "windows"))
			pathsep = ";";
		bprintf(&b1, "%s%s%s", pathsep, bstr(&b), pathsep);
		bprintf(&search, "%s%s%s", pathsep, gobin, pathsep);
		if(xstrstr(bstr(&b1), bstr(&search)) == nil)
			xprintf("*** You need to add %s to your PATH.\n", gobin);
	}
1761 1762

	if(streq(gohostos, "darwin")) {
1763 1764 1765 1766
		if(isfile(bpathf(&path, "%s/cov", tooldir)))
			xprintf("\n"
				"On OS X the debuggers must be installed setgid procmod.\n"
				"Read and run ./sudo.bash to install the debuggers.\n");
1767
	}
1768

1769
	if(!xsamefile(goroot_final, goroot)) {
1770 1771 1772 1773 1774 1775 1776 1777
		xprintf("\n"
			"The binaries expect %s to be copied or moved to %s\n",
			goroot, goroot_final);
	}

	bfree(&b);
	bfree(&b1);
	bfree(&search);
1778
	bfree(&path);
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
}

// Version prints the Go version.
void
cmdversion(int argc, char **argv)
{
	ARGBEGIN{
	case 'v':
		vflag++;
		break;
	default:
		usage();
	}ARGEND

	if(argc > 0)
		usage();

1796
	xprintf("%s\n", goversion);
1797
}