-
Notifications
You must be signed in to change notification settings - Fork 17.3k
/
build.c
1692 lines (1506 loc) · 37.6 KB
/
build.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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"
#include "arg.h"
/*
* Initialization for any invocation.
*/
// The usual variables.
char *goarch;
char *gobin;
char *gohostarch;
char *gohostchar;
char *gohostos;
char *goos;
char *goarm;
char *go386;
char *goroot = GOROOT_FINAL;
char *goroot_final = GOROOT_FINAL;
char *goextlinkenabled = "";
char *workdir;
char *tooldir;
char *gochar;
char *goversion;
char *slash; // / for unix, \ for windows
bool rebuildall = 0;
static bool shouldbuild(char*, char*);
static void copy(char*, char*, int);
static char *findgoversion(void);
// 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",
"linux",
"freebsd",
"netbsd",
"openbsd",
"plan9",
"windows",
};
static void rmworkdir(void);
// find reports the first index of p in l[0:n], or else -1.
int
find(char *p, char **l, int n)
{
int i;
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;
binit(&b);
xgetenv(&b, "GOROOT");
if(b.len > 0) {
// if not "/", then strip trailing path separator
if(b.len >= 2 && b.p[b.len - 1] == slash[0])
b.len--;
goroot = btake(&b);
}
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);
xgetenv(&b, "GOARM");
if(b.len == 0)
bwritestr(&b, xgetgoarm());
goarm = btake(&b);
xgetenv(&b, "GO386");
if(b.len == 0) {
if(cansse2())
bwritestr(&b, "sse2");
else
bwritestr(&b, "387");
}
go386 = btake(&b);
p = bpathf(&b, "%s/include/u.h", goroot);
if(!isfile(p)) {
fatal("$GOROOT is not set correctly or not exported\n"
"\tGOROOT=%s\n"
"\t%s does not exist", goroot, p);
}
xgetenv(&b, "GOHOSTARCH");
if(b.len > 0)
gohostarch = btake(&b);
i = find(gohostarch, okgoarch, nelem(okgoarch));
if(i < 0)
fatal("unknown $GOHOSTARCH %s", gohostarch);
bprintf(&b, "%c", gochars[i]);
gohostchar = btake(&b);
xgetenv(&b, "GOARCH");
if(b.len == 0)
bwritestr(&b, gohostarch);
goarch = btake(&b);
i = find(goarch, okgoarch, nelem(okgoarch));
if(i < 0)
fatal("unknown $GOARCH %s", goarch);
bprintf(&b, "%c", gochars[i]);
gochar = btake(&b);
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);
}
xsetenv("GOROOT", goroot);
xsetenv("GOARCH", goarch);
xsetenv("GOOS", goos);
xsetenv("GOARM", goarm);
xsetenv("GO386", go386);
// Make the environment more predictable.
xsetenv("LANG", "C");
xsetenv("LANGUAGE", "en_US.UTF8");
goversion = findgoversion();
workdir = xworkdir();
xatexit(rmworkdir);
bpathf(&b, "%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch);
tooldir = btake(&b);
bfree(&b);
}
// rmworkdir deletes the work directory.
static void
rmworkdir(void)
{
if(vflag > 1)
errprintf("rm -rf %s\n", workdir);
xremoveall(workdir);
}
// 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;
binit(&b);
binit(&path);
binit(&bmore);
binit(&branch);
vinit(&tags);
// 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);
// 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;
}
// 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?
tag = "devel";
rev = ".";
run(&b, goroot, CheckExit, "hg", "log", "-b", bstr(&branch), "-r", ".:0", "--template", "{tags} + ", nil);
splitfields(&tags, bstr(&b));
nrev = 0;
for(i=0; i<tags.len; i++) {
p = tags.p[i];
if(streq(p, "+"))
nrev++;
// NOTE: Can reenable the /* */ code when we want to
// start reporting versions named 'weekly' again.
if(/*hasprefix(p, "weekly.") ||*/ hasprefix(p, "go")) {
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);
}
if(rev[0]) {
// Tag is before the revision we're building.
// Add extra information.
run(&bmore, goroot, CheckExit, "hg", "log", "--template", " +{node|short} {date|date}", "-r", rev, nil);
chomp(&bmore);
}
bprintf(&b, "%s", tag);
if(bmore.len > 0)
bwriteb(&b, &bmore);
// Cache version.
writefile(&b, bstr(&path), 0);
done:
p = btake(&b);
bfree(&b);
bfree(&path);
bfree(&bmore);
bfree(&branch);
vfree(&tags);
return p;
}
/*
* 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",
"6prof",
"cgo",
"ebnflint",
"goapi",
"gofix",
"goinstall",
"gomake",
"gopack",
"gopprof",
"gotest",
"gotype",
"govet",
"goyacc",
"quietgcc",
};
// Unreleased directories (relative to $GOROOT) that should
// not be in release branches.
static char *unreleased[] = {
"src/cmd/prof",
"src/pkg/old",
};
// setup sets up the tree for the initial build.
static void
setup(void)
{
int i;
Buf b;
char *p;
binit(&b);
// Create bin directory.
p = bpathf(&b, "%s/bin", goroot);
if(!isdir(p))
xmkdir(p);
// Create package directory.
p = bpathf(&b, "%s/pkg", goroot);
if(!isdir(p))
xmkdir(p);
p = bpathf(&b, "%s/pkg/%s_%s", goroot, gohostos, gohostarch);
if(rebuildall)
xremoveall(p);
xmkdirall(p);
if(!streq(goos, gohostos) || !streq(goarch, gohostarch)) {
p = bpathf(&b, "%s/pkg/%s_%s", goroot, goos, goarch);
if(rebuildall)
xremoveall(p);
xmkdirall(p);
}
// Create object directory.
// We keep it in pkg/ so that all the generated binaries
// 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.
if(rebuildall)
xremoveall(tooldir);
xmkdirall(tooldir);
// Remove tool binaries from before the tool/gohostos_gohostarch
xremoveall(bpathf(&b, "%s/bin/tool", goroot));
// Remove old pre-tool binaries.
for(i=0; i<nelem(oldtool); i++)
xremove(bpathf(&b, "%s/bin/%s", goroot, oldtool[i]));
// 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;
}
}
// For release, make sure excluded things are excluded.
if(hasprefix(goversion, "release.") || hasprefix(goversion, "go")) {
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));
}
bfree(&b);
}
/*
* C library and tool building
*/
// gccargs is the gcc command line to use for compiling a single C file.
static char *proto_gccargs[] = {
"-Wall",
// native Plan 9 compilers don't like non-standard prototypes
// so let gcc catch them.
"-Wstrict-prototypes",
"-Wextra",
"-Wunused",
"-Wuninitialized",
"-Wno-sign-compare",
"-Wno-missing-braces",
"-Wno-parentheses",
"-Wno-unknown-pragmas",
"-Wno-switch",
"-Wno-comment",
"-Wno-missing-field-initializers",
"-Werror",
"-fno-common",
"-ggdb",
"-pipe",
#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
"-O2",
#endif
};
static Vec gccargs;
// deptab lists changes to the default dependencies for a given prefix.
// deps ending in /* read the whole directory; deps beginning with -
// 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",
}},
{"cmd/cc", {
"-pgen.c",
"-pswt.c",
}},
{"cmd/gc", {
"-cplx.c",
"-pgen.c",
"-y1.tab.c", // makefile dreg
"opnames.h",
}},
{"cmd/5c", {
"../cc/pgen.c",
"../cc/pswt.c",
"../5l/enam.c",
"$GOROOT/pkg/obj/$GOOS_$GOARCH/libcc.a",
}},
{"cmd/6c", {
"../cc/pgen.c",
"../cc/pswt.c",
"../6l/enam.c",
"$GOROOT/pkg/obj/$GOOS_$GOARCH/libcc.a",
}},
{"cmd/8c", {
"../cc/pgen.c",
"../cc/pswt.c",
"../8l/enam.c",
"$GOROOT/pkg/obj/$GOOS_$GOARCH/libcc.a",
}},
{"cmd/5g", {
"../gc/cplx.c",
"../gc/pgen.c",
"../5l/enam.c",
"$GOROOT/pkg/obj/$GOOS_$GOARCH/libgc.a",
}},
{"cmd/6g", {
"../gc/cplx.c",
"../gc/pgen.c",
"../6l/enam.c",
"$GOROOT/pkg/obj/$GOOS_$GOARCH/libgc.a",
}},
{"cmd/8g", {
"../gc/cplx.c",
"../gc/pgen.c",
"../8l/enam.c",
"$GOROOT/pkg/obj/$GOOS_$GOARCH/libgc.a",
}},
{"cmd/5l", {
"../ld/*",
"enam.c",
}},
{"cmd/6l", {
"../ld/*",
"enam.c",
}},
{"cmd/8l", {
"../ld/*",
"enam.c",
}},
{"cmd/", {
"$GOROOT/pkg/obj/$GOOS_$GOARCH/libmach.a",
"$GOROOT/pkg/obj/$GOOS_$GOARCH/libbio.a",
"$GOROOT/pkg/obj/$GOOS_$GOARCH/lib9.a",
}},
{"pkg/runtime", {
"zasm_$GOOS_$GOARCH.h",
"zsys_$GOOS_$GOARCH.s",
"zgoarch_$GOARCH.go",
"zgoos_$GOOS.go",
"zruntime_defs_$GOOS_$GOARCH.go",
"zversion.go",
}},
};
// depsuffix records the allowed suffixes for source files.
char *depsuffix[] = {
".c",
".h",
".s",
".go",
".goc",
};
// gentab records how to generate some trivial files.
static struct {
char *nameprefix;
void (*gen)(char*, char*);
} gentab[] = {
{"opnames.h", gcopnames},
{"enam.c", mkenam},
{"zasm_", mkzasm},
{"zsys_", mkzsys},
{"zgoarch_", mkzgoarch},
{"zgoos_", mkzgoos},
{"zruntime_defs_", mkzruntimedefs},
{"zversion.go", mkzversion},
};
// install installs the library, package, or binary associated with dir,
// which is relative to $GOROOT/src.
static void
install(char *dir)
{
char *name, *p, *elem, *prefix, *exe;
bool islib, ispkg, isgo, stale, clang;
Buf b, b1, path;
Vec compile, files, link, go, missing, clean, lib, extra;
Time ttarg, t;
int i, j, k, n, doclean, targ, usecpp;
if(vflag) {
if(!streq(goos, gohostos) || !streq(goarch, gohostarch))
errprintf("%s (%s/%s)\n", dir, goos, goarch);
else
errprintf("%s\n", dir);
}
binit(&b);
binit(&b1);
binit(&path);
vinit(&compile);
vinit(&files);
vinit(&link);
vinit(&go);
vinit(&missing);
vinit(&clean);
vinit(&lib);
vinit(&extra);
// 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),
bpathf(&b1, "%s/misc/%s", goroot, name), 1);
goto out;
}
// For release, cmd/prof is not included.
if((streq(dir, "cmd/prof")) && !isdir(bstr(&path))) {
if(vflag > 1)
errprintf("skipping %s - does not exist\n", dir);
goto out;
}
// set up gcc command line on first run.
if(gccargs.len == 0) {
xgetenv(&b, "CC");
if(b.len == 0)
bprintf(&b, "gcc");
clang = contains(bstr(&b), "clang");
splitfields(&gccargs, bstr(&b));
for(i=0; i<nelem(proto_gccargs); i++)
vadd(&gccargs, proto_gccargs[i]);
if(clang) {
// disable ASCII art in clang errors, if possible
vadd(&gccargs, "-fno-caret-diagnostics");
// clang is too smart about unused command-line arguments
vadd(&gccargs, "-Qunused-arguments");
}
if(streq(gohostos, "darwin")) {
// golang.org/issue/5261
vadd(&gccargs, "-mmacosx-version-min=10.6");
}
}
islib = hasprefix(dir, "lib") || streq(dir, "cmd/cc") || streq(dir, "cmd/gc");
ispkg = hasprefix(dir, "pkg");
isgo = ispkg || streq(dir, "cmd/go") || streq(dir, "cmd/cgo");
exe = "";
if(streq(gohostos, "windows"))
exe = ".exe";
// Start final link command line.
// Note: code below knows that link.p[targ] is the target.
if(islib) {
// C library.
vadd(&link, "ar");
if(streq(gohostos, "plan9"))
vadd(&link, "rc");
else
vadd(&link, "rsc");
prefix = "";
if(!hasprefix(name, "lib"))
prefix = "lib";
targ = link.len;
vadd(&link, bpathf(&b, "%s/pkg/obj/%s_%s/%s%s.a", goroot, gohostos, gohostarch, prefix, name));
} else if(ispkg) {
// Go library (package).
vadd(&link, bpathf(&b, "%s/pack", tooldir));
vadd(&link, "grc");
p = bprintf(&b, "%s/pkg/%s_%s/%s", goroot, goos, goarch, dir+4);
*xstrrchr(p, '/') = '\0';
xmkdirall(p);
targ = link.len;
vadd(&link, bpathf(&b, "%s/pkg/%s_%s/%s.a", goroot, goos, goarch, dir+4));
} else if(streq(dir, "cmd/go") || streq(dir, "cmd/cgo")) {
// Go command.
vadd(&link, bpathf(&b, "%s/%sl", tooldir, gochar));
vadd(&link, "-o");
elem = name;
if(streq(elem, "go"))
elem = "go_bootstrap";
targ = link.len;
vadd(&link, bpathf(&b, "%s/%s%s", tooldir, elem, exe));
} else {
// C command. Use gccargs.
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);
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");
}
}
ttarg = mtime(link.p[targ]);
// Gather files that are sources for this target.
// Everything in that directory, and any target-specific
// additions.
xreaddir(&files, bstr(&path));
// 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;
for(i=0; i<nelem(deptab); i++) {
if(hasprefix(dir, deptab[i].prefix)) {
for(j=0; (p=deptab[i].dep[j])!=nil; j++) {
breset(&b1);
bwritestr(&b1, p);
bsubst(&b1, "$GOROOT", goroot);
bsubst(&b1, "$GOOS", goos);
bsubst(&b1, "$GOARCH", goarch);
p = bstr(&b1);
if(hassuffix(p, ".a")) {
if(streq(gohostos, "plan9") && hassuffix(p, "libbio.a"))
continue;
vadd(&lib, bpathf(&b, "%s", p));
continue;
}
if(hassuffix(p, "/*")) {
bpathf(&b, "%s/%s", bstr(&path), p);
b.len -= 2;
xreaddir(&extra, bstr(&b));
bprintf(&b, "%s", p);
b.len -= 2;
for(k=0; k<extra.len; k++)
vadd(&files, bpathf(&b1, "%s/%s", bstr(&b), extra.p[k]));
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;
}
vadd(&files, p);
}
}
}
vuniq(&files);
// Convert to absolute paths.
for(i=0; i<files.len; i++) {
if(!isabs(files.p[i])) {
bpathf(&b, "%s/%s", bstr(&path), files.p[i]);
xfree(files.p[i]);
files.p[i] = btake(&b);
}
}
// Is the target up-to-date?
stale = rebuildall;
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);
if(t != 0 && !hassuffix(p, ".a") && !shouldbuild(p, dir)) {
xfree(files.p[i]);
continue;
}
if(hassuffix(p, ".go"))
vadd(&go, p);
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;
// If there are no files to compile, we're done.
if(files.len == 0)
goto out;
for(i=0; i<lib.len && !stale; i++)
if(mtime(lib.p[i]) > ttarg)
stale = 1;
if(!stale)
goto out;
// For package runtime, copy some files into the work space.
if(streq(dir, "pkg/runtime")) {
copy(bpathf(&b, "%s/arch_GOARCH.h", workdir),
bpathf(&b1, "%s/arch_%s.h", bstr(&path), goarch), 0);
copy(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))
copy(bpathf(&b, "%s/signal_GOOS_GOARCH.h", workdir), p, 0);
copy(bpathf(&b, "%s/os_GOOS.h", workdir),
bpathf(&b1, "%s/os_%s.h", bstr(&path), goos), 0);
copy(bpathf(&b, "%s/signals_GOOS.h", workdir),
bpathf(&b1, "%s/signals_%s.h", bstr(&path), goos), 0);
}
// Generate any missing files; regenerate existing ones.
for(i=0; i<files.len; i++) {
p = files.p[i];
elem = lastelem(p);
for(j=0; j<nelem(gentab); j++) {
if(hasprefix(elem, gentab[j].nameprefix)) {
if(vflag > 1)
errprintf("generate %s\n", p);
gentab[j].gen(bstr(&path), p);
// 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.
goto built;
}
}
// Did not rebuild p.
if(find(p, missing.p, missing.len) >= 0)
fatal("missing file %s", p);
built:;
}
// 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),
bpathf(&b1, "%s/zasm_%s_%s.h", bstr(&path), goos, goarch), 0);
}
// Generate .c files from .goc files.
if(streq(dir, "pkg/runtime")) {
for(i=0; i<files.len; i++) {
p = files.p[i];
if(!hassuffix(p, ".goc"))
continue;
// b = path/zp but with _goos_goarch.c instead of .goc
bprintf(&b, "%s%sz%s", bstr(&path), slash, lastelem(p));
b.len -= 4;
bwritef(&b, "_%s_%s.c", goos, goarch);
goc2c(p, bstr(&b));
vadd(&files, bstr(&b));
}
vuniq(&files);
}
if((!streq(goos, gohostos) || !streq(goarch, gohostarch)) && isgo) {
// We've generated the right files; the go command can do the build.
if(vflag > 1)
errprintf("skip build for cross-compile %s\n", dir);
goto nobuild;
}
// The files generated by GNU Bison use macros that aren't
// supported by the Plan 9 compilers so we have to use the
// external preprocessor when compiling.
usecpp = 0;
if(streq(gohostos, "plan9")) {
for(i=0; i<files.len; i++) {
p = files.p[i];
if(hassuffix(p, "y.tab.c") || hassuffix(p, "y.tab.h")){
usecpp = 1;
break;
}
}
}
// 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.
if(streq(gohostos, "plan9")) {
vadd(&compile, bprintf(&b, "%sc", gohostchar));
vadd(&compile, "-FTVw");
if(usecpp)
vadd(&compile, "-Bp+");
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));
}
vadd(&compile, "-I");
vadd(&compile, bstr(&path));
// lib9/goos.c gets the default constants hard-coded.
if(streq(name, "goos.c")) {
vadd(&compile, "-D");
vadd(&compile, bprintf(&b, "GOOS=\"%s\"", goos));
vadd(&compile, "-D");
vadd(&compile, bprintf(&b, "GOARCH=\"%s\"", goarch));
bprintf(&b1, "%s", goroot_final);
bsubst(&b1, "\\", "\\\\"); // turn into C string
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));
vadd(&compile, "-D");
vadd(&compile, bprintf(&b, "GO_EXTLINK_ENABLED=\"%s\"", goextlinkenabled));
}
// gc/lex.c records the GOEXPERIMENT setting used during the build.
if(streq(name, "lex.c")) {
xgetenv(&b, "GOEXPERIMENT");
vadd(&compile, "-D");
vadd(&compile, bprintf(&b1, "GOEXPERIMENT=\"%s\"", bstr(&b)));
}
} else {
// Supporting files for a Go package.
if(hassuffix(files.p[i], ".s"))
vadd(&compile, bpathf(&b, "%s/%sa", tooldir, gochar));
else {
vadd(&compile, bpathf(&b, "%s/%sc", tooldir, gochar));
vadd(&compile, "-F");
vadd(&compile, "-V");
vadd(&compile, "-w");
}
vadd(&compile, "-I");
vadd(&compile, workdir);
vadd(&compile, "-I");
vadd(&compile, bprintf(&b, "%s/pkg/%s_%s", goroot, goos, goarch));
vadd(&compile, "-D");
vadd(&compile, bprintf(&b, "GOOS_%s", goos));
vadd(&compile, "-D");
vadd(&compile, bprintf(&b, "GOARCH_%s", goarch));
}
bpathf(&b, "%s/%s", workdir, lastelem(files.p[i]));
doclean = 1;
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]));
doclean = 0;
}
// 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';
vadd(&compile, "-o");
vadd(&compile, bstr(&b));
vadd(&compile, files.p[i]);
bgrunv(bstr(&path), CheckExit, &compile);
vadd(&link, bstr(&b));
if(doclean)
vadd(&clean, bstr(&b));
}