1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
9 * Initialization for any invocation.
12 // The usual variables.
21 char *goroot = GOROOT_FINAL;
22 char *goroot_final = GOROOT_FINAL;
23 char *goextlinkenabled = "";
28 char *slash; // / for unix, \ for windows
32 char *defaultcxxtarget;
33 char *defaultcctarget;
37 static bool shouldbuild(char*, char*);
38 static void dopack(char*, char*, char**, int);
39 static char *findgoversion(void);
41 // The known architecture letters.
42 static char *gochars = "566899";
44 // The known architectures.
45 static char *okgoarch[] = {
46 // same order as gochars
55 // The known operating systems.
56 static char *okgoos[] = {
70 static void rmworkdir(void);
72 // find reports the first index of p in l[0:n], or else -1.
74 find(char *p, char **l, int n)
84 // init handles initialization of the various global state, like goroot and goarch.
94 xgetenv(&b, "GOROOT");
96 // if not "/", then strip trailing path separator
97 if(b.len >= 2 && b.p[b.len - 1] == slash[0])
102 xgetenv(&b, "GOBIN");
104 bprintf(&b, "%s%sbin", goroot, slash);
109 bwritestr(&b, gohostos);
111 if(find(goos, okgoos, nelem(okgoos)) < 0)
112 fatal("unknown $GOOS %s", goos);
114 xgetenv(&b, "GOARM");
116 bwritestr(&b, xgetgoarm());
119 xgetenv(&b, "GO386");
122 bwritestr(&b, "sse2");
124 bwritestr(&b, "387");
128 p = bpathf(&b, "%s/include/u.h", goroot);
130 fatal("$GOROOT is not set correctly or not exported\n"
132 "\t%s does not exist", goroot, p);
135 xgetenv(&b, "GOHOSTARCH");
137 gohostarch = btake(&b);
139 i = find(gohostarch, okgoarch, nelem(okgoarch));
141 fatal("unknown $GOHOSTARCH %s", gohostarch);
142 bprintf(&b, "%c", gochars[i]);
143 gohostchar = btake(&b);
145 xgetenv(&b, "GOARCH");
147 bwritestr(&b, gohostarch);
149 i = find(goarch, okgoarch, nelem(okgoarch));
151 fatal("unknown $GOARCH %s", goarch);
152 bprintf(&b, "%c", gochars[i]);
155 xgetenv(&b, "GO_EXTLINK_ENABLED");
157 goextlinkenabled = btake(&b);
158 if(!streq(goextlinkenabled, "0") && !streq(goextlinkenabled, "1"))
159 fatal("unknown $GO_EXTLINK_ENABLED %s", goextlinkenabled);
164 // Use clang on OS X, because gcc is deprecated there.
165 // Xcode for OS X 10.9 Mavericks will ship a fake "gcc" binary that
166 // actually runs clang. We prepare different command
167 // lines for the two binaries, so it matters what we call it.
168 // See golang.org/issue/5822.
170 bprintf(&b, "clang");
174 defaultcc = btake(&b);
176 xgetenv(&b, "CFLAGS");
177 defaultcflags = btake(&b);
179 xgetenv(&b, "LDFLAGS");
180 defaultldflags = btake(&b);
182 xgetenv(&b, "CC_FOR_TARGET");
184 bprintf(&b, defaultcc);
186 defaultcctarget = btake(&b);
188 xgetenv(&b, "CXX_FOR_TARGET");
193 bprintf(&b, "clang++");
198 defaultcxxtarget = btake(&b);
200 xsetenv("GOROOT", goroot);
201 xsetenv("GOARCH", goarch);
202 xsetenv("GOOS", goos);
203 xsetenv("GOARM", goarm);
204 xsetenv("GO386", go386);
206 // Make the environment more predictable.
207 xsetenv("LANG", "C");
208 xsetenv("LANGUAGE", "en_US.UTF8");
210 goversion = findgoversion();
212 workdir = xworkdir();
215 bpathf(&b, "%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch);
221 // rmworkdir deletes the work directory.
226 errprintf("rm -rf %s\n", workdir);
230 // Remove trailing spaces.
236 while(b->len > 0 && ((c=b->p[b->len-1]) == ' ' || c == '\t' || c == '\r' || c == '\n'))
241 // findgoversion determines the Go version to use in the version string.
247 Buf b, path, bmore, branch;
256 // The $GOROOT/VERSION file takes priority, for distributions
257 // without the Mercurial repo.
258 bpathf(&path, "%s/VERSION", goroot);
259 if(isfile(bstr(&path))) {
260 readfile(&b, bstr(&path));
262 // Commands such as "dist version > VERSION" will cause
263 // the shell to create an empty VERSION file and set dist's
264 // stdout to its fd. dist in turn looks at VERSION and uses
265 // its content if available, which is empty at this point.
270 // The $GOROOT/VERSION.cache file is a cache to avoid invoking
271 // hg every time we run this command. Unlike VERSION, it gets
272 // deleted by the clean command.
273 bpathf(&path, "%s/VERSION.cache", goroot);
274 if(isfile(bstr(&path))) {
275 readfile(&b, bstr(&path));
280 // Otherwise, use Mercurial.
281 // What is the current branch?
282 run(&branch, goroot, CheckExit, "hg", "identify", "-b", nil);
285 // What are the tags along the current branch?
288 run(&b, goroot, CheckExit, "hg", "log", "-b", bstr(&branch), "-r", ".:0", "--template", "{tags} + ", nil);
289 splitfields(&tags, bstr(&b));
291 for(i=0; i<tags.len; i++) {
295 // Only show the beta tag for the exact revision.
296 if(hasprefix(p, "go") && (!contains(p, "beta") || nrev == 0)) {
298 // If this tag matches the current checkout
299 // exactly (no "+" yet), don't show extra
300 // revision information.
308 // Did not find a tag; use branch name.
309 bprintf(&b, "branch.%s", bstr(&branch));
314 // Tag is before the revision we're building.
315 // Add extra information.
316 run(&bmore, goroot, CheckExit, "hg", "log", "--template", " +{node|short} {date|date}", "-r", rev, nil);
320 bprintf(&b, "%s", tag);
325 writefile(&b, bstr(&path), 0);
341 * Initial tree setup.
344 // The old tools that no longer live in $GOBIN or $GOROOT/bin.
345 static char *oldtool[] = {
346 "5a", "5c", "5g", "5l",
347 "6a", "6c", "6g", "6l",
348 "8a", "8c", "8g", "8l",
349 "9a", "9c", "9g", "9l",
368 // Unreleased directories (relative to $GOROOT) that should
369 // not be in release branches.
370 static char *unreleased[] = {
376 // setup sets up the tree for the initial build.
386 // Create bin directory.
387 p = bpathf(&b, "%s/bin", goroot);
391 // Create package directory.
392 p = bpathf(&b, "%s/pkg", goroot);
395 p = bpathf(&b, "%s/pkg/%s_%s", goroot, gohostos, gohostarch);
399 if(!streq(goos, gohostos) || !streq(goarch, gohostarch)) {
400 p = bpathf(&b, "%s/pkg/%s_%s", goroot, goos, goarch);
406 // Create object directory.
407 // We keep it in pkg/ so that all the generated binaries
408 // are in one tree. If pkg/obj/libgc.a exists, it is a dreg from
409 // before we used subdirectories of obj. Delete all of obj
411 bpathf(&b, "%s/pkg/obj/libgc.a", goroot);
413 xremoveall(bpathf(&b, "%s/pkg/obj", goroot));
414 p = bpathf(&b, "%s/pkg/obj/%s_%s", goroot, gohostos, gohostarch);
419 // Create tool directory.
420 // We keep it in pkg/, just like the object directory above.
425 // Remove tool binaries from before the tool/gohostos_gohostarch
426 xremoveall(bpathf(&b, "%s/bin/tool", goroot));
428 // Remove old pre-tool binaries.
429 for(i=0; i<nelem(oldtool); i++)
430 xremove(bpathf(&b, "%s/bin/%s", goroot, oldtool[i]));
432 // If $GOBIN is set and has a Go compiler, it must be cleaned.
433 for(i=0; gochars[i]; i++) {
434 if(isfile(bprintf(&b, "%s%s%c%s", gobin, slash, gochars[i], "g"))) {
435 for(i=0; i<nelem(oldtool); i++)
436 xremove(bprintf(&b, "%s%s%s", gobin, slash, oldtool[i]));
441 // For release, make sure excluded things are excluded.
442 if(hasprefix(goversion, "release.") || (hasprefix(goversion, "go") && !contains(goversion, "beta"))) {
443 for(i=0; i<nelem(unreleased); i++)
444 if(isdir(bpathf(&b, "%s/%s", goroot, unreleased[i])))
445 fatal("%s should not exist in release build", bstr(&b));
452 * C library and tool building
455 // gccargs is the gcc command line to use for compiling a single C file.
456 static char *proto_gccargs[] = {
458 // native Plan 9 compilers don't like non-standard prototypes
459 // so let gcc catch them.
460 "-Wstrict-prototypes",
464 "-Wno-missing-braces",
466 "-Wno-unknown-pragmas",
469 "-Wno-missing-field-initializers",
476 // gccargs2 is the second part of gccargs.
477 // it is used if the environment isn't defining CFLAGS.
478 static char *proto_gccargs2[] = {
479 // on older versions of GCC, -Wuninitialized is not supported
480 // without -O, so put it here together with -O settings in case
481 // the user's $CFLAGS doesn't include -O.
483 #if defined(__NetBSD__) && defined(__arm__)
484 // GCC 4.5.4 (NetBSD nb1 20120916) on ARM is known to mis-optimize gc/mparith3.c
485 // Fix available at http://patchwork.ozlabs.org/patch/64562/.
492 static Vec gccargs, ldargs;
494 // deptab lists changes to the default dependencies for a given prefix.
495 // deps ending in /* read the whole directory; deps beginning with -
496 // exclude files with that prefix.
498 char *prefix; // prefix of target
499 char *dep[20]; // dependency tweaks for targets with that prefix
502 "$GOROOT/include/u.h",
503 "$GOROOT/include/utf.h",
504 "$GOROOT/include/fmt.h",
505 "$GOROOT/include/libc.h",
510 "$GOROOT/include/u.h",
511 "$GOROOT/include/utf.h",
512 "$GOROOT/include/fmt.h",
513 "$GOROOT/include/libc.h",
514 "$GOROOT/include/bio.h",
517 "$GOROOT/include/u.h",
518 "$GOROOT/include/utf.h",
519 "$GOROOT/include/fmt.h",
520 "$GOROOT/include/libc.h",
521 "$GOROOT/include/bio.h",
522 "$GOROOT/include/ar.h",
523 "$GOROOT/include/link.h",
538 "-y1.tab.c", // makefile dreg
544 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libcc.a",
549 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libcc.a",
554 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libcc.a",
559 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libcc.a",
567 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libgc.a",
575 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libgc.a",
583 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libgc.a",
591 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libgc.a",
609 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/liblink.a",
610 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/libbio.a",
611 "$GOROOT/pkg/obj/$GOHOSTOS_$GOHOSTARCH/lib9.a",
614 "zaexperiment.h", // must sort above zasm
615 "zasm_$GOOS_$GOARCH.h",
616 "zsys_$GOOS_$GOARCH.s",
617 "zgoarch_$GOARCH.go",
619 "zruntime_defs_$GOOS_$GOARCH.go",
624 // depsuffix records the allowed suffixes for source files.
625 char *depsuffix[] = {
632 // gentab records how to generate some trivial files.
635 void (*gen)(char*, char*);
637 {"opnames.h", gcopnames},
638 {"anames5.c", mkanames},
639 {"anames6.c", mkanames},
640 {"anames8.c", mkanames},
641 {"anames9.c", mkanames},
643 {"zdefaultcc.go", mkzdefaultcc},
645 {"zgoarch_", mkzgoarch},
647 {"zruntime_defs_", mkzruntimedefs},
648 {"zversion.go", mkzversion},
649 {"zaexperiment.h", mkzexperiment},
651 // not generated anymore, but delete the file if we see it
655 // install installs the library, package, or binary associated with dir,
656 // which is relative to $GOROOT/src.
660 char *name, *p, *elem, *prefix, *exe;
661 bool islib, ispkg, isgo, stale, ispackcmd;
662 Buf b, b1, path, final_path, final_name;
663 Vec compile, files, link, go, missing, clean, lib, extra;
665 int i, j, k, n, doclean, targ;
668 if(!streq(goos, gohostos) || !streq(goarch, gohostarch))
669 errprintf("%s (%s/%s)\n", dir, goos, goarch);
671 errprintf("%s\n", dir);
689 // path = full path to dir.
690 bpathf(&path, "%s/src/%s", goroot, dir);
691 bpathf(&final_path, "%s/src/%s", goroot_final, dir);
692 name = lastelem(dir);
694 // set up gcc command line on first run.
695 if(gccargs.len == 0) {
696 bprintf(&b, "%s %s", defaultcc, defaultcflags);
697 splitfields(&gccargs, bstr(&b));
698 for(i=0; i<nelem(proto_gccargs); i++)
699 vadd(&gccargs, proto_gccargs[i]);
700 if(defaultcflags[0] == '\0') {
701 for(i=0; i<nelem(proto_gccargs2); i++)
702 vadd(&gccargs, proto_gccargs2[i]);
704 if(contains(gccargs.p[0], "clang")) {
705 // disable ASCII art in clang errors, if possible
706 vadd(&gccargs, "-fno-caret-diagnostics");
707 // clang is too smart about unused command-line arguments
708 vadd(&gccargs, "-Qunused-arguments");
710 // disable word wrapping in error messages
711 vadd(&gccargs, "-fmessage-length=0");
712 if(streq(gohostos, "darwin")) {
713 // golang.org/issue/5261
714 vadd(&gccargs, "-mmacosx-version-min=10.6");
717 if(ldargs.len == 0 && defaultldflags[0] != '\0') {
718 bprintf(&b, "%s", defaultldflags);
719 splitfields(&ldargs, bstr(&b));
722 islib = hasprefix(dir, "lib") || streq(dir, "cmd/cc") || streq(dir, "cmd/gc");
723 ispkg = !islib && !hasprefix(dir, "cmd/");
724 isgo = ispkg || streq(dir, "cmd/go") || streq(dir, "cmd/cgo");
727 if(streq(gohostos, "windows"))
730 // Start final link command line.
731 // Note: code below knows that link.p[targ] is the target.
736 if(streq(gohostos, "plan9"))
741 if(!hasprefix(name, "lib"))
744 vadd(&link, bpathf(&b, "%s/pkg/obj/%s_%s/%s%s.a", goroot, gohostos, gohostarch, prefix, name));
746 // Go library (package).
748 vadd(&link, "pack"); // program name - unused here, but all the other cases record one
749 p = bprintf(&b, "%s/pkg/%s_%s/%s", goroot, goos, goarch, dir);
750 *xstrrchr(p, '/') = '\0';
753 vadd(&link, bpathf(&b, "%s/pkg/%s_%s/%s.a", goroot, goos, goarch, dir));
754 } else if(streq(dir, "cmd/go") || streq(dir, "cmd/cgo")) {
756 vadd(&link, bpathf(&b, "%s/%sl", tooldir, gochar));
759 if(streq(elem, "go"))
760 elem = "go_bootstrap";
762 vadd(&link, bpathf(&b, "%s/%s%s", tooldir, elem, exe));
764 // C command. Use gccargs and ldargs.
765 if(streq(gohostos, "plan9")) {
766 vadd(&link, bprintf(&b, "%sl", gohostchar));
769 vadd(&link, bpathf(&b, "%s/%s", tooldir, name));
771 vcopy(&link, gccargs.p, gccargs.len);
772 vcopy(&link, ldargs.p, ldargs.len);
774 vadd(&link, "-static");
777 vadd(&link, bpathf(&b, "%s/%s%s", tooldir, name, exe));
778 if(streq(gohostarch, "amd64"))
780 else if(streq(gohostarch, "386"))
784 ttarg = mtime(link.p[targ]);
786 // Gather files that are sources for this target.
787 // Everything in that directory, and any target-specific
789 xreaddir(&files, bstr(&path));
791 // Remove files beginning with . or _,
792 // which are likely to be editor temporary files.
793 // This is the same heuristic build.ScanDir uses.
794 // There do exist real C files beginning with _,
795 // so limit that check to just Go files.
797 for(i=0; i<files.len; i++) {
799 if(hasprefix(p, ".") || (hasprefix(p, "_") && hassuffix(p, ".go")))
806 for(i=0; i<nelem(deptab); i++) {
807 if(streq(dir, deptab[i].prefix) ||
808 (hassuffix(deptab[i].prefix, "/") && hasprefix(dir, deptab[i].prefix))) {
809 for(j=0; (p=deptab[i].dep[j])!=nil; j++) {
812 bsubst(&b1, "$GOROOT", goroot);
813 bsubst(&b1, "$GOOS", goos);
814 bsubst(&b1, "$GOARCH", goarch);
815 bsubst(&b1, "$GOHOSTOS", gohostos);
816 bsubst(&b1, "$GOHOSTARCH", gohostarch);
818 if(hassuffix(p, ".a")) {
819 vadd(&lib, bpathf(&b, "%s", p));
822 if(hassuffix(p, "/*")) {
823 bpathf(&b, "%s/%s", bstr(&path), p);
825 xreaddir(&extra, bstr(&b));
826 bprintf(&b, "%s", p);
828 for(k=0; k<extra.len; k++)
829 vadd(&files, bpathf(&b1, "%s/%s", bstr(&b), extra.p[k]));
832 if(hasprefix(p, "-")) {
835 for(k=0; k<files.len; k++) {
836 if(hasprefix(files.p[k], p))
839 files.p[n++] = files.p[k];
850 // Convert to absolute paths.
851 for(i=0; i<files.len; i++) {
852 if(!isabs(files.p[i])) {
853 bpathf(&b, "%s/%s", bstr(&path), files.p[i]);
855 files.p[i] = btake(&b);
859 // Is the target up-to-date?
862 for(i=0; i<files.len; i++) {
864 for(j=0; j<nelem(depsuffix); j++)
865 if(hassuffix(p, depsuffix[j]))
871 if(t != 0 && !hassuffix(p, ".a") && !shouldbuild(p, dir)) {
875 if(hassuffix(p, ".go"))
881 files.p[n++] = files.p[i];
884 files.p[n++] = files.p[i];
888 // If there are no files to compile, we're done.
892 for(i=0; i<lib.len && !stale; i++)
893 if(mtime(lib.p[i]) > ttarg)
899 // For package runtime, copy some files into the work space.
900 if(streq(dir, "runtime")) {
901 copyfile(bpathf(&b, "%s/arch_GOARCH.h", workdir),
902 bpathf(&b1, "%s/arch_%s.h", bstr(&path), goarch), 0);
903 copyfile(bpathf(&b, "%s/defs_GOOS_GOARCH.h", workdir),
904 bpathf(&b1, "%s/defs_%s_%s.h", bstr(&path), goos, goarch), 0);
905 p = bpathf(&b1, "%s/signal_%s_%s.h", bstr(&path), goos, goarch);
907 copyfile(bpathf(&b, "%s/signal_GOOS_GOARCH.h", workdir), p, 0);
908 copyfile(bpathf(&b, "%s/os_GOOS.h", workdir),
909 bpathf(&b1, "%s/os_%s.h", bstr(&path), goos), 0);
910 copyfile(bpathf(&b, "%s/signals_GOOS.h", workdir),
911 bpathf(&b1, "%s/signals_%s.h", bstr(&path), goos), 0);
912 copyfile(bpathf(&b, "%s/pkg/%s_%s/textflag.h", goroot, goos, goarch),
913 bpathf(&b1, "%s/src/cmd/ld/textflag.h", goroot), 0);
914 copyfile(bpathf(&b, "%s/pkg/%s_%s/funcdata.h", goroot, goos, goarch),
915 bpathf(&b1, "%s/src/runtime/funcdata.h", goroot), 0);
918 // Generate any missing files; regenerate existing ones.
919 for(i=0; i<files.len; i++) {
922 for(j=0; j<nelem(gentab); j++) {
923 if(gentab[j].gen == nil)
925 if(hasprefix(elem, gentab[j].nameprefix)) {
927 errprintf("generate %s\n", p);
928 gentab[j].gen(bstr(&path), p);
929 // Do not add generated file to clean list.
930 // In runtime, we want to be able to
931 // build the package with the go tool,
932 // and it assumes these generated files already
933 // exist (it does not know how to build them).
934 // The 'clean' command can remove
935 // the generated files.
939 // Did not rebuild p.
940 if(find(p, missing.p, missing.len) >= 0)
941 fatal("missing file %s", p);
945 // One more copy for package runtime.
946 // The last batch was required for the generators.
947 // This one is generated.
948 if(streq(dir, "runtime")) {
949 copyfile(bpathf(&b, "%s/zasm_GOOS_GOARCH.h", workdir),
950 bpathf(&b1, "%s/zasm_%s_%s.h", bstr(&path), goos, goarch), 0);
953 if((!streq(goos, gohostos) || !streq(goarch, gohostarch)) && isgo) {
954 // We've generated the right files; the go command can do the build.
956 errprintf("skip build for cross-compile %s\n", dir);
960 // Compile the files.
961 for(i=0; i<files.len; i++) {
962 if(!hassuffix(files.p[i], ".c") && !hassuffix(files.p[i], ".s"))
964 name = lastelem(files.p[i]);
968 // C library or tool.
969 if(streq(gohostos, "plan9")) {
970 vadd(&compile, bprintf(&b, "%sc", gohostchar));
971 vadd(&compile, "-FTVwp");
972 vadd(&compile, "-DPLAN9");
973 vadd(&compile, "-D__STDC__=1");
974 vadd(&compile, "-D__SIZE_TYPE__=ulong"); // for GNU Bison
975 vadd(&compile, bpathf(&b, "-I%s/include/plan9", goroot));
976 vadd(&compile, bpathf(&b, "-I%s/include/plan9/%s", goroot, gohostarch));
978 vcopy(&compile, gccargs.p, gccargs.len);
979 vadd(&compile, "-c");
980 if(streq(gohostarch, "amd64"))
981 vadd(&compile, "-m64");
982 else if(streq(gohostarch, "386"))
983 vadd(&compile, "-m32");
985 vadd(&compile, "-I");
986 vadd(&compile, bpathf(&b, "%s/include", goroot));
989 if(streq(dir, "lib9"))
990 vadd(&compile, "-DPLAN9PORT");
993 vadd(&compile, "-I");
994 vadd(&compile, bstr(&path));
996 // lib9/goos.c gets the default constants hard-coded.
997 if(streq(name, "goos.c")) {
998 vadd(&compile, "-D");
999 vadd(&compile, bprintf(&b, "GOOS=\"%s\"", goos));
1000 vadd(&compile, "-D");
1001 vadd(&compile, bprintf(&b, "GOARCH=\"%s\"", goarch));
1002 bprintf(&b1, "%s", goroot_final);
1003 bsubst(&b1, "\\", "\\\\"); // turn into C string
1004 vadd(&compile, "-D");
1005 vadd(&compile, bprintf(&b, "GOROOT=\"%s\"", bstr(&b1)));
1006 vadd(&compile, "-D");
1007 vadd(&compile, bprintf(&b, "GOVERSION=\"%s\"", goversion));
1008 vadd(&compile, "-D");
1009 vadd(&compile, bprintf(&b, "GOARM=\"%s\"", goarm));
1010 vadd(&compile, "-D");
1011 vadd(&compile, bprintf(&b, "GO386=\"%s\"", go386));
1012 vadd(&compile, "-D");
1013 vadd(&compile, bprintf(&b, "GO_EXTLINK_ENABLED=\"%s\"", goextlinkenabled));
1016 // gc/lex.c records the GOEXPERIMENT setting used during the build.
1017 if(streq(name, "lex.c")) {
1018 xgetenv(&b, "GOEXPERIMENT");
1019 vadd(&compile, "-D");
1020 vadd(&compile, bprintf(&b1, "GOEXPERIMENT=\"%s\"", bstr(&b)));
1023 // Supporting files for a Go package.
1024 if(hassuffix(files.p[i], ".s"))
1025 vadd(&compile, bpathf(&b, "%s/%sa", tooldir, gochar));
1027 vadd(&compile, bpathf(&b, "%s/%sc", tooldir, gochar));
1028 vadd(&compile, "-F");
1029 vadd(&compile, "-V");
1030 vadd(&compile, "-w");
1032 vadd(&compile, "-I");
1033 vadd(&compile, workdir);
1034 vadd(&compile, "-I");
1035 vadd(&compile, bprintf(&b, "%s/pkg/%s_%s", goroot, goos, goarch));
1036 vadd(&compile, "-D");
1037 vadd(&compile, bprintf(&b, "GOOS_%s", goos));
1038 vadd(&compile, "-D");
1039 vadd(&compile, bprintf(&b, "GOARCH_%s", goarch));
1040 vadd(&compile, "-D");
1041 vadd(&compile, bprintf(&b, "GOOS_GOARCH_%s_%s", goos, goarch));
1044 bpathf(&b, "%s/%s", workdir, lastelem(files.p[i]));
1046 if(!isgo && streq(gohostos, "darwin")) {
1047 // To debug C programs on OS X, it is not enough to say -ggdb
1048 // on the command line. You have to leave the object files
1049 // lying around too. Leave them in pkg/obj/, which does not
1050 // get removed when this tool exits.
1051 bpathf(&b1, "%s/pkg/obj/%s", goroot, dir);
1052 xmkdirall(bstr(&b1));
1053 bpathf(&b, "%s/%s", bstr(&b1), lastelem(files.p[i]));
1057 // Change the last character of the output file (which was c or s).
1058 if(streq(gohostos, "plan9"))
1059 b.p[b.len-1] = gohostchar[0];
1062 vadd(&compile, "-o");
1063 vadd(&compile, bstr(&b));
1064 vadd(&compile, files.p[i]);
1065 bgrunv(bstr(&path), CheckExit, &compile);
1067 vadd(&link, bstr(&b));
1069 vadd(&clean, bstr(&b));
1074 // The last loop was compiling individual files.
1075 // Hand the Go files to the compiler en masse.
1077 vadd(&compile, bpathf(&b, "%s/%sg", tooldir, gochar));
1079 bpathf(&b, "%s/_go_.a", workdir);
1080 vadd(&compile, "-pack");
1081 vadd(&compile, "-o");
1082 vadd(&compile, bstr(&b));
1083 vadd(&clean, bstr(&b));
1085 vadd(&link, bstr(&b));
1087 vadd(&compile, "-p");
1088 if(hasprefix(dir, "pkg/"))
1089 vadd(&compile, dir+4);
1091 vadd(&compile, "main");
1093 if(streq(dir, "runtime"))
1094 vadd(&compile, "-+");
1096 vcopy(&compile, go.p, go.len);
1098 runv(nil, bstr(&path), CheckExit, &compile);
1101 xremove(link.p[targ]);
1102 dopack(link.p[targ], bstr(&b), &link.p[targ+1], link.len - (targ+1));
1107 if(!islib && !isgo) {
1108 // C binaries need the libraries explicitly, and -lm.
1109 vcopy(&link, lib.p, lib.len);
1110 if(!streq(gohostos, "plan9"))
1114 // Remove target before writing it.
1115 xremove(link.p[targ]);
1117 runv(nil, nil, CheckExit, &link);
1120 // In package runtime, we install runtime.h and cgocall.h too,
1121 // for use by cgo compilation.
1122 if(streq(dir, "runtime")) {
1123 copyfile(bpathf(&b, "%s/pkg/%s_%s/cgocall.h", goroot, goos, goarch),
1124 bpathf(&b1, "%s/src/runtime/cgocall.h", goroot), 0);
1125 copyfile(bpathf(&b, "%s/pkg/%s_%s/runtime.h", goroot, goos, goarch),
1126 bpathf(&b1, "%s/src/runtime/runtime.h", goroot), 0);
1131 for(i=0; i<clean.len; i++)
1132 xremove(clean.p[i]);
1147 // matchfield reports whether the field matches this build.
1154 p = xstrrchr(f, ',');
1156 return streq(f, goos) || streq(f, goarch) || streq(f, "cmd_go_bootstrap") || streq(f, "go1.1") || (streq(goos, "android") && streq(f, "linux"));
1158 res = matchfield(f) && matchfield(p+1);
1163 // shouldbuild reports whether we should build this file.
1164 // It applies the same rules that are used with context tags
1165 // in package go/build, except that the GOOS and GOARCH
1166 // can appear anywhere in the file name, not just after _.
1167 // In particular, they can be the entire file name (like windows.c).
1168 // We also allow the special tag cmd_go_bootstrap.
1169 // See ../go/bootstrap.go and package go/build.
1171 shouldbuild(char *file, char *dir)
1178 // Check file name for GOOS or GOARCH.
1179 name = lastelem(file);
1180 for(i=0; i<nelem(okgoos); i++) {
1181 if(streq(okgoos[i], goos))
1183 p = xstrstr(name, okgoos[i]);
1186 p += xstrlen(okgoos[i]);
1187 if(*p == '.' || *p == '_' || *p == '\0')
1190 for(i=0; i<nelem(okgoarch); i++) {
1191 if(streq(okgoarch[i], goarch))
1193 p = xstrstr(name, okgoarch[i]);
1196 p += xstrlen(okgoarch[i]);
1197 if(*p == '.' || *p == '_' || *p == '\0')
1202 if(contains(name, "_test"))
1205 // cmd/go/doc.go has a giant /* */ comment before
1206 // it gets to the important detail that it is not part of
1207 // package main. We don't parse those comments,
1208 // so special case that file.
1209 if(hassuffix(file, "cmd/go/doc.go") || hassuffix(file, "cmd\\go\\doc.go"))
1211 if(hassuffix(file, "cmd/cgo/doc.go") || hassuffix(file, "cmd\\cgo\\doc.go"))
1214 // Check file contents for // +build lines.
1221 splitlines(&lines, bstr(&b));
1222 for(i=0; i<lines.len; i++) {
1224 while(*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
1228 if(contains(p, "package documentation")) {
1232 if(contains(p, "package main") && !streq(dir, "cmd/go") && !streq(dir, "cmd/cgo")) {
1236 if(!hasprefix(p, "//"))
1238 if(!contains(p, "+build"))
1240 splitfields(&fields, lines.p[i]);
1241 if(fields.len < 2 || !streq(fields.p[1], "+build"))
1243 for(j=2; j<fields.len; j++) {
1245 if((*p == '!' && !matchfield(p+1)) || matchfield(p))
1261 // copy copies the file src to dst, via memory (so only good for small files).
1263 copyfile(char *dst, char *src, int exec)
1268 errprintf("cp %s %s\n", src, dst);
1272 writefile(&b, dst, exec);
1276 // dopack copies the package src to dst,
1277 // appending the files listed in extra.
1278 // The archive format is the traditional Unix ar format.
1280 dopack(char *dst, char *src, char **extra, int nextra)
1289 readfile(&bdst, src);
1290 for(i=0; i<nextra; i++) {
1291 readfile(&b, extra[i]);
1292 // find last path element for archive member name
1293 p = xstrrchr(extra[i], '/');
1296 q = xstrrchr(extra[i], '\\');
1299 if(p == nil || q > p)
1304 bwritef(&bdst, "%-16.16s%-12d%-6d%-6d%-8o%-10d`\n", p, 0, 0, 0, 0644, b.len);
1308 bwrite(&bdst, &c, 1);
1312 writefile(&bdst, dst, 0);
1318 // buildorder records the order of builds for the 'go bootstrap' command.
1319 static char *buildorder[] = {
1324 "cmd/cc", // must be before c
1325 "cmd/gc", // must be before g
1326 "cmd/%sl", // must be before a, c, g
1331 // The dependency order here was copied from a buildscript
1332 // back when there were build scripts. Will have to
1333 // be maintained by hand, but shouldn't change very
1372 "text/template/parse",
1379 // cleantab records the directories to clean in 'go clean'.
1380 // It is bigger than the buildorder because we clean all the
1381 // compilers but build only the $GOARCH ones.
1382 static char *cleantab[] = {
1383 // Commands and C libraries.
1443 "text/template/parse",
1461 for(i=0; i<nelem(cleantab); i++) {
1462 bpathf(&path, "%s/src/%s", goroot, cleantab[i]);
1463 xreaddir(&dir, bstr(&path));
1464 // Remove generated files.
1465 for(j=0; j<dir.len; j++) {
1466 for(k=0; k<nelem(gentab); k++) {
1467 if(hasprefix(dir.p[j], gentab[k].nameprefix))
1468 xremove(bpathf(&b, "%s/%s", bstr(&path), dir.p[j]));
1471 // Remove generated binary named for directory.
1472 if(hasprefix(cleantab[i], "cmd/"))
1473 xremove(bpathf(&b, "%s/%s", bstr(&path), cleantab[i]+4));
1476 // remove src/runtime/z* unconditionally
1478 bpathf(&path, "%s/src/runtime", goroot);
1479 xreaddir(&dir, bstr(&path));
1480 for(j=0; j<dir.len; j++) {
1481 if(hasprefix(dir.p[j], "z"))
1482 xremove(bpathf(&b, "%s/%s", bstr(&path), dir.p[j]));
1486 // Remove object tree.
1487 xremoveall(bpathf(&b, "%s/pkg/obj/%s_%s", goroot, gohostos, gohostarch));
1489 // Remove installed packages and tools.
1490 xremoveall(bpathf(&b, "%s/pkg/%s_%s", goroot, gohostos, gohostarch));
1491 xremoveall(bpathf(&b, "%s/pkg/%s_%s", goroot, goos, goarch));
1492 xremoveall(tooldir);
1494 // Remove cached version info.
1495 xremove(bpathf(&b, "%s/VERSION.cache", goroot));
1504 * command implementations
1510 xprintf("usage: go tool dist [command]\n"
1513 "banner print installation banner\n"
1514 "bootstrap rebuild everything\n"
1515 "clean deletes all built files\n"
1516 "env [-p] print environment (-p: include $PATH)\n"
1517 "install [dir] install individual directory\n"
1518 "version print Go version\n"
1520 "All commands take -v flags to emit extra information.\n"
1525 // The env command prints the default environment.
1527 cmdenv(int argc, char **argv)
1537 format = "%s=\"%s\"\n";
1541 format = "%s='%s'\n";
1550 format = "set %s=%s\r\n";
1559 xprintf(format, "CC", defaultcc);
1560 xprintf(format, "CC_FOR_TARGET", defaultcctarget);
1561 xprintf(format, "GOROOT", goroot);
1562 xprintf(format, "GOBIN", gobin);
1563 xprintf(format, "GOARCH", goarch);
1564 xprintf(format, "GOOS", goos);
1565 xprintf(format, "GOHOSTARCH", gohostarch);
1566 xprintf(format, "GOHOSTOS", gohostos);
1567 xprintf(format, "GOTOOLDIR", tooldir);
1568 xprintf(format, "GOCHAR", gochar);
1569 if(streq(goarch, "arm"))
1570 xprintf(format, "GOARM", goarm);
1571 if(streq(goarch, "386"))
1572 xprintf(format, "GO386", go386);
1576 if(streq(gohostos, "windows"))
1578 xgetenv(&b, "PATH");
1579 bprintf(&b1, "%s%s%s", gobin, sep, bstr(&b));
1580 xprintf(format, "PATH", bstr(&b1));
1587 // The bootstrap command runs a build from scratch,
1588 // stopping at having installed the go_bootstrap command.
1590 cmdbootstrap(int argc, char **argv)
1594 char *oldgoos, *oldgoarch, *oldgochar;
1615 if(isdir(bpathf(&b, "%s/src/pkg", goroot))) {
1617 "The Go package sources have moved to $GOROOT/src.\n"
1618 "*** %s still exists. ***\n"
1619 "It probably contains stale files that may confuse the build.\n"
1620 "Please (check what's there and) remove it and try again.\n"
1621 "See http://golang.org/s/go14nopkg\n", bpathf(&b, "%s/src/pkg", goroot));
1626 goversion = findgoversion();
1629 xsetenv("GOROOT", goroot);
1630 xsetenv("GOROOT_FINAL", goroot_final);
1632 // For the main bootstrap, building for host os/arch.
1637 goarch = gohostarch;
1638 gochar = gohostchar;
1639 xsetenv("GOARCH", goarch);
1640 xsetenv("GOOS", goos);
1642 for(i=0; i<nelem(buildorder); i++) {
1643 install(bprintf(&b, buildorder[i], gohostchar));
1644 if(!streq(oldgochar, gohostchar) && xstrstr(buildorder[i], "%s"))
1645 install(bprintf(&b, buildorder[i], oldgochar));
1651 xsetenv("GOARCH", goarch);
1652 xsetenv("GOOS", goos);
1654 // Build runtime for actual goos/goarch too.
1655 if(!streq(goos, gohostos) || !streq(goarch, gohostarch))
1665 Buf pwd, src, real_src;
1671 // xgetwd might return a path with symlinks fully resolved, and if
1672 // there happens to be symlinks in goroot, then the hasprefix test
1673 // will never succeed. Instead, we use xrealwd to get a canonical
1674 // goroot/src before the comparison to avoid this problem.
1677 bpathf(&src, "%s/src/", goroot);
1678 xrealwd(&real_src, bstr(&src));
1679 if(!hasprefix(p, bstr(&real_src)))
1680 fatal("current directory %s is not under %s", p, bstr(&real_src));
1682 // guard againt xrealwd return the directory without the trailing /
1693 // Install installs the list of packages named on the command line.
1695 cmdinstall(int argc, char **argv)
1711 install(defaulttarg());
1713 for(i=0; i<argc; i++)
1717 // Clean deletes temporary objects.
1718 // Clean -i deletes the installed objects too.
1720 cmdclean(int argc, char **argv)
1736 // Banner prints the 'now you've installed Go' banner.
1738 cmdbanner(int argc, char **argv)
1740 char *pathsep, *pid, *ns;
1741 Buf b, b1, search, path;
1761 xprintf("Installed Go for %s/%s in %s\n", goos, goarch, goroot);
1762 xprintf("Installed commands in %s\n", gobin);
1764 if(!xsamefile(goroot_final, goroot)) {
1765 // If the files are to be moved, don't check that gobin
1766 // is on PATH; assume they know what they are doing.
1767 } else if(streq(gohostos, "plan9")) {
1768 // Check that gobin is bound before /bin.
1769 readfile(&b, "#c/pid");
1770 bsubst(&b, " ", "");
1772 bprintf(&b, "/proc/%s/ns", pid);
1775 bprintf(&search, "bind -b %s /bin\n", gobin);
1776 if(xstrstr(bstr(&b), bstr(&search)) == nil)
1777 xprintf("*** You need to bind %s before /bin.\n", gobin);
1779 // Check that gobin appears in $PATH.
1780 xgetenv(&b, "PATH");
1782 if(streq(gohostos, "windows"))
1784 bprintf(&b1, "%s%s%s", pathsep, bstr(&b), pathsep);
1785 bprintf(&search, "%s%s%s", pathsep, gobin, pathsep);
1786 if(xstrstr(bstr(&b1), bstr(&search)) == nil)
1787 xprintf("*** You need to add %s to your PATH.\n", gobin);
1790 if(streq(gohostos, "darwin")) {
1791 if(isfile(bpathf(&path, "%s/cov", tooldir)))
1793 "On OS X the debuggers must be installed setgid procmod.\n"
1794 "Read and run ./sudo.bash to install the debuggers.\n");
1797 if(!xsamefile(goroot_final, goroot)) {
1799 "The binaries expect %s to be copied or moved to %s\n",
1800 goroot, goroot_final);
1809 // Version prints the Go version.
1811 cmdversion(int argc, char **argv)
1824 xprintf("%s\n", goversion);