forked from skelterjohn/go-gb
/
pkg.go
1506 lines (1286 loc) · 32.3 KB
/
pkg.go
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 2011 John Asmuth
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
//"bufio"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strings"
)
type Package struct {
Dir, Base string
Cfg Config
Name, Target string
IsCmd bool
Active bool
ResultPath, InstallPath string
IsCGo bool
IsProtobuf bool
//these prevent multipath issues for tree following
built, cleaned, addedToBuild, gofmted, gofixed, scanned bool
NeedsBuild, NeedsInstall, NeedsGoInstall bool
GoSources []string
CGoSources []string
CSrcs []string
CHeaders []string
AsmSrcs []string
ProtoSrcs []string // for protobufs
Sources []string // the list of all .go, .c, .s source in the target
ProtoGoSrcs []string // the .go files that correspond to .proto files
DeadSources []string // all .go, .c, .s files that will not be included in the build
Objects []string
PkgSrc map[string][]string
TestSrc map[string][]string
PkgCGoSrc map[string][]string
SrcDeps map[string][]string
Deps []string
DepPkgs []*Package
TestSources []string
TestDeps []string
TestFuncs map[string][]string
TestDepPkgs []*Package
CGoCFlags map[string][]string
CGoLDFlags map[string][]string
HasMakefile bool
MustUseMakefile bool
IsInGOROOT bool
IsInGOPATH string
InTestData string
SourceTime, BinTime, InstTime, GOROOTPkgTime int64
FailedToBuild bool
//to make sure that only one thread works on a given package at a time
block chan bool
}
func NewPackage(base, dir string, inTestData string, cfg Config) (this *Package, err error) {
finfo, err := os.Stat(dir)
if err != nil || !finfo.IsDir() {
err = errors.New("not a directory")
return
}
this = new(Package)
this.Cfg = cfg
this.block = make(chan bool, 1)
this.Dir = path.Clean(dir)
this.InTestData = inTestData
this.PkgSrc = make(map[string][]string)
this.PkgCGoSrc = make(map[string][]string)
this.TestSrc = make(map[string][]string)
this.TestFuncs = make(map[string][]string)
this.CGoCFlags = make(map[string][]string)
this.CGoLDFlags = make(map[string][]string)
if rel := GetRelative(filepath.Join(GOROOT, "src"), dir, CWD); !strings.HasPrefix(rel, "..") && !filepath.IsAbs(rel) {
this.IsInGOROOT = true
if _, set := this.Cfg.Target(); set {
WarnLog.Printf("(in %s) Cannot override target inside GOROOT", this.Dir)
}
}
for _, gp := range GOPATHS {
if rel := GetRelative(filepath.Join(gp, "src"), dir, CWD); !strings.HasPrefix(rel, "..") && !filepath.IsAbs(rel) {
this.IsInGOPATH = gp //say which gopath we're in
if _, set := this.Cfg.Target(); set {
WarnLog.Printf("(in %s) Cannot override target inside GOPATH", this.Dir)
}
}
}
err = this.ScanForSource()
if err != nil {
return
}
err = this.GetSourceDeps()
if err != nil {
//return
}
if this.IsCmd {
this.Deps = append(this.Deps, "\"runtime\"")
}
this.FilterDeadSource()
this.Base = base
this.DepPkgs = make([]*Package, 0)
if strings.HasPrefix(this.Dir, "./") {
this.Dir = this.Dir[2:len(this.Dir)]
}
if _, err2 := os.Stat(path.Join(this.Dir, "/Makefile")); err2 == nil {
this.HasMakefile = true
}
if _, err2 := os.Stat(path.Join(this.Dir, "/makefile")); err2 == nil {
this.HasMakefile = true
}
if !this.HasMakefile && this.IsInGOROOT {
err = errors.New("GOROOT pkg without makefile - not meant to be built")
return
}
if this.IsInGOROOT && DoNotBuildGOROOT[GetRelative(GOROOT, this.Dir, CWD)] {
err = errors.New("can't build GOROOT core tools")
return
}
if len(this.Sources) == 0 {
err = errors.New("no source")
return
}
for _, src := range this.Sources {
var t int64
t, err = StatTime(path.Join(this.Dir, src))
if err != nil {
err = errors.New(fmt.Sprintf("'%s' just disappeared.\n", path.Join(this.Dir, src)))
return
}
if t > this.SourceTime {
this.SourceTime = t
}
}
if err != nil {
return
}
this.IsCmd = this.Name == "main"
this.Objects = append(this.Objects, path.Join(this.Dir, GetIBName()))
err = this.GetTarget()
if reqOS, ok := OSFiltersMust[this.Target]; ok && reqOS != GOOS {
err = errors.New(fmt.Sprintf("%s can only build with GOOS=%s", this.Target, reqOS))
}
if !FilterPkg(this.Target) {
err = errors.New("Filtered package based on GOOS/GOARCH")
return
}
if this.IsProtobuf && ProtocCMD == "" {
err = errors.New(fmt.Sprintf("(in %s) protoc not found for protobuf target", this.Dir))
ErrLog.Println(err)
return
}
if this.IsCGo {
if GCCCMD == "" {
err = errors.New(fmt.Sprintf("(in %s) gcc not found for cgo target", this.Dir))
ErrLog.Println(err)
return
}
if CGoCMD == "" {
err = errors.New(fmt.Sprintf("(in %s) cgo not found for cgo target", this.Dir))
ErrLog.Println(err)
return
}
}
if this.IsCGo && CGoCMD == "" {
err = errors.New(fmt.Sprintf("(in %s) cgo not found for cgo target", this.Dir))
ErrLog.Println(err)
return
}
if this.IsCmd && this.IsCGo {
err = errors.New(fmt.Sprintf("(in %s) cannot have a cgo cmd", this.Dir))
ErrLog.Println(err)
return
}
this.Active = (DoCmds && this.IsCmd) || (DoPkgs && !this.IsCmd)
return
}
func (this *Package) DetectCycles() (cycle []*Package) {
cycle = this.detectCycle(nil)
return
}
func (this *Package) detectCycle(visited []*Package) (cycle []*Package) {
for i, p := range visited {
if p == this {
_ = i
cycle = append(visited, this)
return
}
}
visited = append(visited, this)
for _, pkg := range this.DepPkgs {
cycle = pkg.detectCycle(visited)
if cycle != nil {
return
}
}
return
}
func (this *Package) ScanForSource() (err error) {
errch := make(chan error)
go func() {
wf := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
if !this.VisitDir(path, info) {
return filepath.SkipDir
}
} else {
this.VisitFile(path, info)
}
return nil
}
filepath.Walk(this.Dir, wf)
close(errch)
}()
for fperr := range errch {
ErrLog.Printf("Error while scanning: %s", fperr)
}
if len(this.AsmSrcs)+len(this.GoSources)+len(this.TestSources)+len(this.ProtoGoSrcs) == 0 { //allsources
err = errors.New("No source files in " + this.Dir)
}
/*
this.IsCGo = this.IsCGo || len(this.CSrcs) > 0
if this.IsCGo {
fmt.Println("CSrcs makes it CGo", this.CSrcs)
}
*/
return
}
func (this *Package) FilterDeadSource() {
deadset := make(map[string]bool)
for _, ds := range this.DeadSources {
deadset[ds] = true
}
for _, s := range this.PkgSrc[this.Name] {
deadset[s] = false
}
for _, s := range this.CGoSources {
deadset[s] = false
}
for _, s := range this.AsmSrcs {
deadset[s] = false
}
for _, s := range this.ProtoGoSrcs {
deadset[s] = false
}
this.DeadSources = []string{}
for s, ok := range deadset {
if ok {
this.DeadSources = append(this.DeadSources, s)
}
}
}
func (this *Package) VisitDir(dpath string, f os.FileInfo) bool {
return dpath == this.Dir // || strings.HasPrefix(dpath, path.Join(this.Dir, "src"))
}
func (this *Package) VisitFile(fpath string, f os.FileInfo) {
//ignore hidden and temporary files
if strings.HasPrefix(fpath, ".") {
return
}
if strings.HasPrefix(fpath, "#") {
return
}
//skip files generates by the cgo process
if strings.HasSuffix(fpath, ".cgo1.go") {
return
}
if strings.HasSuffix(fpath, ".cgo2.c") {
return
}
pb := path.Base(fpath)
if pb == "_cgo_gotypes.go" ||
pb == "_cgo_import.c" ||
pb == "__cgo_import.c" ||
pb == "_cgo_main.c" ||
pb == "_cgo_export.h" ||
pb == "_cgo_defun.c" {
return
}
//only get these from .proto files, if the .proto file exists
if strings.HasSuffix(fpath, ".pb.go") {
dotProto := fpath[:len(fpath)-len(".pb.go")] + ".proto"
absProto := filepath.Join(this.Dir, filepath.Base(dotProto))
if _, err := os.Stat(absProto); err == nil {
//if there is a .proto file, the .pb.go is an intermediate object
return
}
//otherwise it's a regular go file
}
//skip files flagged for different OS/ARCH
if !FilterFlag(fpath) {
return
}
//no longer necessary since this goes into the _test dir
if strings.HasSuffix(fpath, "_testmain.go") {
return
}
rootl := len(this.Dir) + 1
if this.Dir != "." {
fpath = fpath[rootl:len(fpath)]
}
if strings.HasSuffix(fpath, ".go") ||
strings.HasSuffix(fpath, ".c") ||
strings.HasSuffix(fpath, ".s") {
this.DeadSources = append(this.DeadSources, fpath)
}
if strings.HasSuffix(fpath, ".proto") {
this.ProtoSrcs = append(this.ProtoSrcs, fpath)
this.Sources = append(this.Sources, fpath)
generatedGo := GoForProto(fpath)
this.ProtoGoSrcs = append(this.ProtoGoSrcs, generatedGo)
this.IsProtobuf = true
}
if strings.HasSuffix(fpath, ".s") {
this.AsmSrcs = append(this.AsmSrcs, fpath)
this.Objects = append(this.Objects, fpath[:len(fpath)-2]+GetObjSuffix())
this.Sources = append(this.Sources, fpath)
}
if strings.HasSuffix(fpath, ".go") {
if strings.HasSuffix(fpath, "_test.go") {
this.TestSources = append(this.TestSources, fpath)
//} else if strings.HasPrefix(fpath, "cgo_") {
// this.CGoSources = append(this.CGoSources, fpath)
} else {
this.GoSources = append(this.GoSources, fpath)
}
this.Sources = append(this.Sources, fpath)
}
if strings.HasSuffix(fpath, ".h") {
this.CHeaders = append(this.CHeaders, fpath)
}
if strings.HasSuffix(fpath, ".c") {
this.CSrcs = append(this.CSrcs, fpath)
this.Sources = append(this.Sources, fpath)
}
}
func (this *Package) GetSourceDeps() (err error) {
this.SrcDeps = make(map[string][]string)
var nonCGoSrc []string
for _, src := range this.GoSources {
var fpkg, ftarget string
var fdeps []string
var cflags, ldflags []string
fpkg, ftarget, fdeps, _, cflags, ldflags, err = GetDeps(path.Join(this.Dir, src))
if err != nil {
BrokenMsg = append(BrokenMsg, fmt.Sprintf("(in %s) %s", this.Dir, err.Error()))
continue
}
this.SrcDeps[src] = fdeps
if ftarget != "" {
if this.IsInGOROOT {
WarnLog.Printf("(in %s) Cannot override target inside GOROOT", this.Dir)
} else if this.IsInGOROOT {
WarnLog.Printf("(in %s) Cannot override target inside GOPATH", this.Dir)
} else {
this.Target = ftarget
}
}
if fpkg != "documentation" {
if fpkg != "main" || this.Name == "" {
this.Name = fpkg
}
}
isCGoSrc := false
for _, dep := range fdeps {
if dep == "\"C\"" {
isCGoSrc = true
this.IsCGo = true
this.CGoCFlags[fpkg] = append(this.CGoCFlags[fpkg], cflags...)
this.CGoLDFlags[fpkg] = append(this.CGoLDFlags[fpkg], ldflags...)
}
}
if isCGoSrc && !(this.IsInGOROOT &&
(this.Name == "syscall" ||
this.Name == "runtime" ||
this.Name == "cgo")) {
this.SrcDeps[src] = append(this.SrcDeps[src], "\"runtime/cgo\"")
this.SrcDeps[src] = append(this.SrcDeps[src], "\"cgo\"-cmd")
this.CGoSources = append(this.CGoSources, src)
this.PkgCGoSrc[fpkg] = append(this.PkgCGoSrc[fpkg], src)
} else {
nonCGoSrc = append(nonCGoSrc, src)
this.PkgSrc[fpkg] = append(this.PkgSrc[fpkg], src)
}
}
for fpkg, flags := range this.CGoCFlags {
this.CGoCFlags[fpkg] = RemoveDups(flags)
}
for fpkg, flags := range this.CGoLDFlags {
this.CGoLDFlags[fpkg] = RemoveDups(flags)
}
this.GoSources = nonCGoSrc
for _, buildSrc := range this.PkgSrc[this.Name] {
this.Deps = append(this.Deps, this.SrcDeps[buildSrc]...)
}
for _, buildSrc := range this.PkgCGoSrc[this.Name] {
this.Deps = append(this.Deps, this.SrcDeps[buildSrc]...)
}
if this.IsProtobuf {
this.Deps = append(this.Deps, "\"math\"", "\"os\"", "\"goprotobuf.googlecode.com/hg/proto\"")
}
this.Deps = RemoveDups(this.Deps)
if Test {
for _, src := range this.TestSources {
var fpkg, ftarget string
var fdeps, ffuncs []string
fpkg, ftarget, fdeps, ffuncs, _, _, err = GetDeps(path.Join(this.Dir, src))
if this.Name != "\"runtime\"" {
fdeps = append(fdeps, "\"runtime\"")
}
for _, dep := range fdeps {
if dep == "\"C\"" {
ErrLog.Printf("Test src %s wants to use cgo... too much effort.\n", src)
continue
}
}
this.TestSrc[fpkg] = append(this.TestSrc[fpkg], src)
if err != nil {
BrokenMsg = append(BrokenMsg, fmt.Sprintf("(in %s) %s", this.Dir, err.Error()))
break
}
if ftarget != "" {
this.Target = ftarget
}
this.TestDeps = append(this.TestDeps, fdeps...)
this.TestFuncs[fpkg] = append(this.TestFuncs[fpkg], ffuncs...)
}
this.TestDeps = RemoveDups(this.TestDeps)
}
return
}
func (this *Package) GetTarget() (err error) {
if !this.IsCmd && this.IsInGOROOT && this.InTestData == "" {
//always the relative path
this.Target = GetRelative(path.Join(GOROOT, "src", "cmd"), this.Dir, CWD)
if !strings.HasPrefix(this.Target, "..") {
if this.IsCGo {
err = errors.New("gb can't compile the GOROOT c cmds")
return
}
this.IsCmd = true
this.MustUseMakefile = true
}
}
if !this.IsCmd && this.IsInGOROOT && this.InTestData == "" {
//always the relative path
this.Target = GetRelative(path.Join(GOROOT, "src", "pkg"), this.Dir, CWD)
if strings.HasPrefix(this.Target, "..") {
//if _, ok := this.PkgSrc["documentation"]; !ok && len(this.PkgSrc)==1 {
err = errors.New(fmt.Sprintf("(in %s) GOROOT pkg is not in $GOROOT/src/pkg", this.Dir))
ErrLog.Println(err)
//}
return
}
//fmt.Printf("found goroot relative path for %s = %s\n", this.Dir, this.Target)
} else if !this.IsCmd && this.IsInGOPATH != "" && this.InTestData == "" {
//this is a gopath target
this.Target = GetRelative(path.Join(this.IsInGOPATH, "src"), this.Dir, CWD)
if strings.HasPrefix(this.Target, "..") {
err = errors.New(fmt.Sprintf("(in %s) GOPATH pkg is not in $GOPATH/src/pkg for GOPATH=%s", this.Dir, this.IsInGOPATH))
ErrLog.Println(err)
return
}
} else {
if this.Target == "" {
this.Target = this.Base
if this.IsCmd {
this.Target = path.Base(this.Dir)
if this.Target == "." {
this.Target = filepath.Base(CWD)
}
} else {
if this.Target == "." {
this.Target = filepath.Base(CWD)
}
tryFixPrefix := func(prefix string) (fixed bool) {
if this.Base == this.Dir && HasPathPrefix(this.Dir, prefix) && this.Dir != prefix {
this.Target = GetRelative(prefix, this.Dir, CWD)
return true
}
return false
}
_ = tryFixPrefix(path.Join("src", "pkg")) || tryFixPrefix("pkg") || tryFixPrefix("src")
}
} else {
this.Base = this.Target
}
if cfgTarg, set := this.Cfg.Target(); set {
this.Target = cfgTarg
this.Base = this.Target
if this.Target == "-" || this.Target == "--" {
err = errors.New("directory opts-out")
return
}
}
if cfgMake, set := this.Cfg.AlwaysMakefile(); set {
this.MustUseMakefile = this.MustUseMakefile || cfgMake
}
}
this.Base = path.Clean(this.Base)
this.Target = path.Clean(this.Target)
err = nil
if this.IsCmd {
if GOOS == "windows" {
this.Target += ".exe"
}
}
if this.IsInGOROOT && this.InTestData == "" {
if this.IsCmd {
this.InstallPath = filepath.Join(GOBIN, this.Target)
} else {
this.InstallPath = filepath.Join(GOROOT, "pkg", GOOS+"_"+GOARCH, this.Target+".a")
}
this.ResultPath = this.InstallPath
} else if this.IsInGOPATH != "" && this.InTestData == "" {
if this.IsCmd {
this.InstallPath = filepath.Join(this.IsInGOPATH, "bin", this.Target)
} else {
this.InstallPath = filepath.Join(this.IsInGOPATH, "pkg", GOOS+"_"+GOARCH, this.Target+".a")
}
this.ResultPath = this.InstallPath
} else {
if this.IsCmd {
this.InstallPath = filepath.Join(GetInstallDirCmd(), this.Target)
if this.InTestData != "" {
buildDirTest := filepath.Join(this.InTestData, GetBuildDirCmd())
this.ResultPath = filepath.Join(buildDirTest, this.Target)
} else {
this.ResultPath = filepath.Join(GetBuildDirCmd(), this.Target)
}
} else {
this.InstallPath = filepath.Join(GetInstallDirPkg(), this.Target+".a")
if this.InTestData != "" {
buildDirTest := filepath.Join(this.InTestData, GetBuildDirPkg())
this.ResultPath = filepath.Join(buildDirTest, this.Target+".a")
} else {
this.ResultPath = filepath.Join(GetBuildDirPkg(), this.Target+".a")
}
}
}
if this.IsInGOROOT && ForceMakePkgs[this.Target] {
this.MustUseMakefile = true
}
this.Stat()
return
}
func (this *Package) PrintScan() {
if this.IsCmd && !DoCmds {
return
}
if !this.IsCmd && !DoPkgs {
return
}
if this.scanned {
return
}
this.scanned = true
for _, pkg := range this.DepPkgs {
pkg.PrintScan()
}
//build, install := this.Touched()
bis := ""
if !this.NeedsBuild {
bis = " (up to date)"
}
if !this.NeedsInstall {
bis = " (installed)"
}
var label string
if this.IsCmd {
label = "cmd"
} else {
label = "pkg"
}
if this.IsCGo && !this.IsCmd {
label = "cgo"
}
if this.InTestData != "" {
label = "testdata " + label
}
if this.IsInGOROOT {
label = "GOROOT " + label
} else if this.IsInGOPATH != "" {
label = "GOPATH=" + this.IsInGOPATH + " " + label
}
displayDir := this.Dir
if this.IsInGOROOT {
displayDir = strings.Replace(displayDir, GOROOT, "$GOROOT", 1)
}
var suffix string
if !this.IsInGOROOT && this.IsInGOPATH == "" && this.Dir != this.Target {
suffix = fmt.Sprintf(" in %s", displayDir)
}
fmt.Printf("%s \"%s\"%s%s\n", label, this.Target, suffix, bis)
if ScanList {
fmt.Printf(" %s Deps: %v\n", this.Name, this.Deps)
if Test {
fmt.Printf(" %s TestDeps: %v\n", this.Name, this.TestDeps)
}
}
if ScanListFiles {
this.ListSource()
}
}
func (this *Package) Stat() {
this.BinTime, _ = StatTime(this.ResultPath)
this.InstTime, _ = StatTime(this.InstallPath)
/*
resInfo, err := os.Stat(this.ResultPath)
if resInfo != nil && err == nil {
this.BinTime = resInfo.Mtime_ns
} else {
this.BinTime = 0
}
resInfo, err = os.Stat(this.InstallPath)
if resInfo != nil && err == nil {
this.InstTime = resInfo.Mtime_ns
} else {
this.InstTime = 0
}
*/
}
func (this *Package) CheckStatus() {
b, i := this.Touched()
this.NeedsBuild = b || this.NeedsBuild
this.NeedsInstall = i || this.NeedsInstall
}
func (this *Package) ResolveDeps() (err error) {
CheckDeps := func(deps []string, test bool) (err error) {
for _, dep := range deps {
if dep == "\"C\"" {
this.IsCGo = true
continue
}
if strings.HasPrefix(dep, "\"./") {
WarnLog.Printf("(in %s) gb does not support relative import %s", this.Dir, dep)
}
if pkg, ok := Packages[dep]; ok {
if test {
this.TestDepPkgs = append(this.TestDepPkgs, pkg)
} else {
this.DepPkgs = append(this.DepPkgs, pkg)
}
} else {
exists, when := PkgExistsInGOROOT(dep)
if exists {
if this.GOROOTPkgTime < when {
this.GOROOTPkgTime = when
}
}
if !IsGoInstallable(dep) {
if !exists {
//fmt.Printf("in %s: can't resolve pkg %s (maybe you aren't in the root?)\n", this.Dir, dep)
err = errors.New("unresolved packages")
}
} else {
if GoInstallUpdate {
this.NeedsBuild = true
}
if !exists {
if !GoInstall {
//fmt.Printf("in %s: can't resolve pkg %s (try using -g)\n", this.Dir, dep)
err = errors.New("unresolved packages")
} else {
this.NeedsGoInstall = true
this.NeedsBuild = true
}
}
}
}
}
return
}
err = CheckDeps(this.Deps, false)
if err != nil {
return
}
err = CheckDeps(this.TestDeps, true)
return
}
func (this *Package) Touched() (build, install bool) {
var inTime int64
build = this.NeedsBuild
install = this.NeedsInstall
for _, pkg := range this.DepPkgs {
db, di := pkg.Touched()
if db {
build = true
}
if di {
install = true
}
if pkg.BinTime > inTime {
inTime = pkg.BinTime
}
}
if this.GOROOTPkgTime > inTime {
inTime = this.GOROOTPkgTime
}
if this.SourceTime > inTime {
inTime = this.SourceTime
}
if inTime > this.BinTime {
build = true
}
if this.InstTime < this.BinTime || this.InstTime < inTime {
install = true
}
if build {
install = true
}
return
}
func (this *Package) Build() (err error) {
this.block <- true
defer func() {
<-this.block
}()
defer func() {
if err != nil {
this.FailedToBuild = true
this.CleanFiles()
}
}()
if this.FailedToBuild {
err = errors.New("Cannot build deps")
return
}
if !this.NeedsBuild {
return
}
if this.built {
return
}
this.built = true
if !TestCGO && (!this.HasMakefile && this.IsCGo) {
ErrLog.Printf("(in %s) this is a cgo project; please create a makefile\n", this.Dir)
return
}
if Exclusive && !ListedDirs[this.Dir] {
return
}
inTime := this.GOROOTPkgTime
if Concurrent {
for _, pkg := range this.DepPkgs {
go pkg.Build()
}
}
for _, pkg := range this.DepPkgs {
err = pkg.Build()
if err != nil {
return
}
if pkg.BinTime > inTime {
inTime = pkg.BinTime
}
}
if GoInstall {
for _, dep := range this.Deps {
if _, ok := Packages[dep]; !ok {
goinstTime := GoInstallPkg(dep)
if goinstTime > inTime {
inTime = goinstTime
}
}
}
}
if !this.Active {
return
}
if this.SourceTime > inTime {
inTime = this.SourceTime
}
if inTime > this.BinTime {
which := "cmd"
if this.Name != "main" {
which = "pkg"
}
labelDir := this.Dir
if HasPathPrefix(labelDir, GOROOT) {
labelDir = "$GOROOT" + labelDir[len(GOROOT):]
}
fmt.Printf("(in %s) building %s \"%s\"\n", labelDir, which, this.Target)
if this.IsProtobuf {
err = GenerateProtobufSource(this)
}
if err == nil {
if (Makefiles || this.MustUseMakefile) && this.HasMakefile {
err = MakeBuild(this)
} else if this.IsCGo {
err = BuildCgoPackage(this)
} else {
err = BuildPackage(this)
}
}
if err == nil {
PackagesBuilt++
} else {
BrokenPackages++
BrokenMsg = append(BrokenMsg, fmt.Sprintf("(in %s) could not build \"%s\"", this.Dir, this.Target))
}
}
if err != nil {
//this.CleanFiles()
}
if this.IsInGOROOT && this.HasMakefile {
err = this.Install()
}
this.NeedsBuild = false
this.Stat()
return
}
func (this *Package) Test() (err error) {
for _, pkg := range this.TestDepPkgs {
err = pkg.Build()
if err != nil {
return
}
}
if GoInstall {
for _, dep := range this.TestDeps {
if _, ok := Packages[dep]; !ok {
GoInstallPkg(dep)
}
}
}
if (Makefiles && this.HasMakefile) || this.IsCGo {
err = MakeTest(this)
return
}
testdir := path.Join(this.Dir, "_test")
if !MakeAMess {
defer func() {
if Verbose {
fmt.Printf(" Removing %s\n", testdir)
}
err = os.RemoveAll(testdir)
}()
}
fmt.Printf("(in %s) testing \"%s\"\n", this.Dir, this.Target)
var pkgtests, pkgbenchmarks map[string][]string
pkgtests = make(map[string][]string)
pkgbenchmarks = make(map[string][]string)
for name, funcs := range this.TestFuncs {
for _, f := range funcs {
if strings.HasPrefix(f, "Test") {
pkgtests[name] = append(pkgtests[name], f)