/
buildutil.go
750 lines (687 loc) · 19.8 KB
/
buildutil.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
// Copyright 2011 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.
// Package buildutil exposes some useful internal functions of the go/build.
package buildutil
import (
"bytes"
"errors"
"fmt"
"go/build"
"go/build/constraint"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// BuildTags adds and build tags found in name or content to allTags.
func BuildTags(name string, content []byte, allTags map[string]bool) {
ctxt := build.Default
goodOSArchFile(&ctxt, filepath.Base(name), allTags)
shouldBuild(&ctxt, content, allTags)
}
// GoodOSArchFile returns false if the name contains a $GOOS or $GOARCH
// suffix which does not match the build Context.
// The recognized name formats are:
//
// name_$(GOOS).*
// name_$(GOARCH).*
// name_$(GOOS)_$(GOARCH).*
// name_$(GOOS)_test.*
// name_$(GOARCH)_test.*
// name_$(GOOS)_$(GOARCH)_test.*
//
// An exception: if GOOS=android, then files with GOOS=linux are also matched.
func GoodOSArchFile(ctxt *build.Context, name string, allTags map[string]bool) bool {
return goodOSArchFile(ctxt, filepath.Base(name), allTags)
}
// ShouldBuild reports whether it is okay to use this file, and adds any build
// tags to allTags.
//
// Note: only +build tags are checked. Syntactically incorrect content may be
// marked as build-able if no +build tags are present.
func ShouldBuild(ctxt *build.Context, content []byte, allTags map[string]bool) bool {
return shouldBuildOnly(ctxt, content, allTags)
}
func Include(ctxt *build.Context, path string) bool {
if !goodOSArchFile(ctxt, filepath.Base(path), nil) {
return false
}
var f io.ReadCloser
var err error
if fn := ctxt.OpenFile; fn != nil {
f, err = fn(path)
} else {
f, err = os.Open(path)
}
if err != nil {
return false
}
data, err := readImportsFast(f)
f.Close()
if err != nil {
return false
}
return shouldBuildOnly(ctxt, data, nil)
}
func IncludeTags(ctxt *build.Context, path string, tags map[string]bool) (bool, error) {
if !goodOSArchFile(ctxt, filepath.Base(path), tags) {
return false, nil
}
var f io.ReadCloser
var err error
if fn := ctxt.OpenFile; fn != nil {
f, err = fn(path)
} else {
f, err = os.Open(path)
}
if err != nil {
return false, err
}
data, err := readImportsFast(f)
f.Close()
if err != nil {
return false, err
}
return shouldBuildOnly(ctxt, data, tags), nil
}
// TODO (CEV): rename
func ShortImport(ctxt *build.Context, path string) (string, bool) {
if !goodOSArchFile(ctxt, filepath.Base(path), nil) {
return "", false
}
var f io.ReadCloser
var err error
if fn := ctxt.OpenFile; fn != nil {
f, err = fn(path)
} else {
f, err = os.Open(path)
}
if err != nil {
return "", false
}
data, err := readImportsFast(f)
f.Close()
if err != nil {
return "", false
}
if !shouldBuildOnly(ctxt, data, nil) {
return "", false
}
name, err := readPackageName(data)
return name, err == nil
}
func ReadPackageName(path string, src interface{}) (string, error) {
rc, err := openReader(&build.Default, path, src)
if err != nil {
return "", err
}
data, err := readImportsFast(rc)
rc.Close()
if err != nil {
return "", err
}
return readPackageName(data)
}
// ReadPackageNameTags evaluates the Go source file at path and returns
// the package name, if it can be used with build.Context ctxt, populates
// any build tags (if tags is not nil), and any error that occured.
func ReadPackageNameTags(path string, src interface{}, tags map[string]bool) (string, bool, error) {
rc, err := openReader(&build.Default, path, src)
if err != nil {
return "", false, err
}
data, err := readImportsFast(rc)
rc.Close()
if err != nil {
return "", false, err
}
name, err := readPackageName(data)
if err != nil {
return "", false, err
}
return name, shouldBuildOnly(&build.Default, data, tags), nil
}
func ReadImports(path string, src interface{}) (pkgname string, imports []string, err error) {
rc, err := openReader(&build.Default, path, src)
if err != nil {
return
}
imports = make([]string, 0, 8)
data, err := readImports(rc, true, &imports)
rc.Close()
if err != nil {
return
}
pkgname, err = readPackageName(data)
return
}
// MatchFile reports whether the file with the given name matches the context
// and would be included in a Package created by ImportDir. It also returns
// the package name of the file.
//
// MatchFile considers the name of the file and may use ctxt.OpenFile to
// read some or all of the file's content. If src is not nil it will be
// used as the content of the file.
func MatchFile(ctxt *build.Context, dir, name string, src interface{}) (pkgName string, match bool, err error) {
rc, err := openReaderDirName(ctxt, dir, name, src)
if err != nil {
return
}
data, err := readImportsFast(rc)
rc.Close()
if err != nil {
return "", false, err
}
pkgName, err = readPackageName(data)
if err != nil {
return "", false, err
}
if !GoodOSArchFile(ctxt, name, nil) {
return pkgName, false, nil
}
match, _, err = shouldBuild(ctxt, data, nil)
return
}
var emptyConstraint Constraint
// A Constraint stores the build constraints of a Go source file and can be
// used to check if a file matches a build.Context.
//
// A nil Constraint is safe to use and matches any build.Context.
type Constraint struct {
expr constraint.Expr
}
// NewConstraint returns a new Constraint for the given constraint.Expr and
// build tags. This is a no-op if both expr and tags are nil.
func NewConstraint(expr constraint.Expr, tags map[string]bool) *Constraint {
if expr == nil && len(tags) == 0 {
return &emptyConstraint
}
return &Constraint{expr: expr}
}
// Expr returns the Constraint's constraint.Expr.
func (c *Constraint) Expr() constraint.Expr {
if c.Empty() {
return nil
}
return c.expr
}
// Empty returns true if c has no build constraints. An empty Constraint
// matches all files.
func (c *Constraint) Empty() bool { return c == nil || c.expr == nil }
// Eval reports whether build.Context ctxt matches the build constraint.
func (c *Constraint) Eval(ctxt *build.Context) bool {
return c.Empty() || eval(ctxt, c.expr, nil)
}
// ParseConstraint parses the build constraints of a Go source file, if any.
// The returned Constraint can be used to check if the file matches a
// build.Context.
func ParseConstraint(ctxt *build.Context, filename string, src interface{}) (*Constraint, error) {
rc, err := openReader(ctxt, filename, src)
if err != nil {
return nil, err
}
data, err := readImportsFast(rc)
rc.Close()
if err != nil {
return nil, err
}
expr, err := parseBuildConstraint(data)
if err != nil {
return nil, err
}
return &Constraint{expr: expr}, nil
}
func openReaderDirName(ctxt *build.Context, dir, name string, src interface{}) (io.ReadCloser, error) {
if src != nil {
switch s := src.(type) {
case string:
return ioutil.NopCloser(strings.NewReader(s)), nil
case []byte:
return ioutil.NopCloser(bytes.NewReader(s)), nil
case io.ReadCloser:
return s, nil
case io.Reader:
return ioutil.NopCloser(s), nil
default:
return nil, errors.New("invalid source")
}
}
// If dir is not empty it is joined with name
if dir != "" {
name = joinPath(ctxt, dir, name)
}
if ctxt.OpenFile != nil {
return ctxt.OpenFile(name)
}
return os.Open(name)
}
func openReader(ctxt *build.Context, filename string, src interface{}) (io.ReadCloser, error) {
return openReaderDirName(ctxt, "", filename, src)
}
var (
slashSlash = []byte("//")
slashStar = []byte("/*")
starSlash = []byte("*/")
)
var (
bSlashSlash = []byte(slashSlash)
bSlashStar = []byte(slashStar)
bPlusBuild = []byte("+build")
goBuildComment = []byte("//go:build")
errMultipleGoBuild = errors.New("multiple //go:build comments")
)
func isGoBuildComment(line []byte) bool {
if !bytes.HasPrefix(line, goBuildComment) {
return false
}
line = bytes.TrimSpace(line)
rest := line[len(goBuildComment):]
return len(rest) == 0 || len(bytes.TrimSpace(rest)) < len(rest)
}
// Special comment denoting a binary-only package.
// See https://golang.org/design/2775-binary-only-packages
// for more about the design of binary-only packages.
var binaryOnlyComment = []byte("//go:binary-only-package")
// shouldBuild reports whether it is okay to use this file,
// The rule is that in the file's leading run of // comments
// and blank lines, which must be followed by a blank line
// (to avoid including a Go package clause doc comment),
// lines beginning with '// +build' are taken as build directives.
//
// The file is accepted only if each such line lists something
// matching the file. For example:
//
// // +build windows linux
//
// marks the file as applicable only on Windows and Linux.
//
// For each build tag it consults, shouldBuild sets allTags[tag] = true.
//
// shouldBuild reports whether the file should be built
// and whether a //go:binary-only-package comment was found.
func shouldBuild(ctxt *build.Context, content []byte, allTags map[string]bool) (shouldBuild, binaryOnly bool, err error) {
// Identify leading run of // comments and blank lines,
// which must be followed by a blank line.
// Also identify any //go:build comments.
content, goBuild, sawBinaryOnly, err := parseFileHeader(content)
if err != nil {
return false, false, err
}
// If //go:build line is present, it controls.
// Otherwise fall back to +build processing.
switch {
case goBuild != nil:
x, err := constraint.Parse(string(goBuild))
if err != nil {
return false, false, fmt.Errorf("parsing //go:build line: %v", err)
}
shouldBuild = eval(ctxt, x, allTags)
default:
shouldBuild = true
p := content
for len(p) > 0 {
line := p
if i := bytes.IndexByte(line, '\n'); i >= 0 {
line, p = line[:i], p[i+1:]
} else {
p = p[len(p):]
}
line = bytes.TrimSpace(line)
if !bytes.HasPrefix(line, bSlashSlash) || !bytes.Contains(line, bPlusBuild) {
continue
}
text := string(line)
if !constraint.IsPlusBuild(text) {
continue
}
if x, err := constraint.Parse(text); err == nil {
if !eval(ctxt, x, allTags) {
shouldBuild = false
}
}
}
}
return shouldBuild, sawBinaryOnly, nil
}
// TODO: move to minimize diff with go/build.go
func parseBuildConstraint(content []byte) (constraint.Expr, error) {
// Identify leading run of // comments and blank lines,
// which must be followed by a blank line.
// Also identify any //go:build comments.
content, goBuild, _, err := parseFileHeader(content)
if err != nil {
return nil, err
}
// If //go:build line is present, it controls.
// Otherwise fall back to +build processing.
if goBuild != nil {
x, err := constraint.Parse(string(goBuild))
if err != nil {
return nil, fmt.Errorf("parsing //go:build line: %w", err)
}
return x, nil
}
// Synthesize //go:build expression from // +build lines.
var x constraint.Expr
p := content
for len(p) > 0 {
line := p
if i := bytes.IndexByte(line, '\n'); i >= 0 {
line, p = line[:i], p[i+1:]
} else {
p = p[len(p):]
}
line = bytes.TrimSpace(line)
if !bytes.HasPrefix(line, bSlashSlash) || !bytes.Contains(line, bPlusBuild) {
continue
}
text := string(line)
if !constraint.IsPlusBuild(text) {
continue
}
y, err := constraint.Parse(text)
if err != nil {
return nil, err
}
if x == nil {
x = y
} else {
x = &constraint.AndExpr{X: x, Y: y}
}
}
// WARN: x may be nil
return x, nil
}
// TODO: move to minimize diff with go/build.go
func shouldBuildOnly(ctxt *build.Context, content []byte, allTags map[string]bool) bool {
ok, _, _ := shouldBuild(ctxt, content, allTags)
return ok
}
func parseFileHeader(content []byte) (trimmed, goBuild []byte, sawBinaryOnly bool, err error) {
end := 0
p := content
ended := false // found non-blank, non-// line, so stopped accepting // +build lines
inSlashStar := false // in /* */ comment
Lines:
for len(p) > 0 {
line := p
if i := bytes.IndexByte(line, '\n'); i >= 0 {
line, p = line[:i], p[i+1:]
} else {
p = p[len(p):]
}
line = bytes.TrimSpace(line)
if len(line) == 0 && !ended { // Blank line
// Remember position of most recent blank line.
// When we find the first non-blank, non-// line,
// this "end" position marks the latest file position
// where a // +build line can appear.
// (It must appear _before_ a blank line before the non-blank, non-// line.
// Yes, that's confusing, which is part of why we moved to //go:build lines.)
// Note that ended==false here means that inSlashStar==false,
// since seeing a /* would have set ended==true.
end = len(content) - len(p)
continue Lines
}
if !bytes.HasPrefix(line, slashSlash) { // Not comment line
ended = true
}
if !inSlashStar && isGoBuildComment(line) {
if goBuild != nil {
return nil, nil, false, errMultipleGoBuild
}
goBuild = line
}
if !inSlashStar && bytes.Equal(line, binaryOnlyComment) {
sawBinaryOnly = true
}
Comments:
for len(line) > 0 {
if inSlashStar {
if i := bytes.Index(line, starSlash); i >= 0 {
inSlashStar = false
line = bytes.TrimSpace(line[i+len(starSlash):])
continue Comments
}
continue Lines
}
if bytes.HasPrefix(line, bSlashSlash) {
continue Lines
}
if bytes.HasPrefix(line, bSlashStar) {
inSlashStar = true
line = bytes.TrimSpace(line[len(bSlashStar):])
continue Comments
}
// Found non-comment text.
break Lines
}
}
return content[:end], goBuild, sawBinaryOnly, nil
}
func eval(ctxt *build.Context, x constraint.Expr, allTags map[string]bool) bool {
return x.Eval(func(tag string) bool { return matchTag(ctxt, tag, allTags) })
}
// Used by MatchContext
var compatibleOSes = map[string][]string{
"android": {"linux"},
"illumos": {"solaris"},
"ios": {"darwin"},
}
// matchTag reports whether the name is one of:
//
// cgo (if cgo is enabled)
// $GOOS
// $GOARCH
// ctxt.Compiler
// linux (if GOOS = android)
// solaris (if GOOS = illumos)
// tag (if tag is listed in ctxt.BuildTags or ctxt.ReleaseTags)
//
// It records all consulted tags in allTags.
func matchTag(ctxt *build.Context, name string, allTags map[string]bool) bool {
if allTags != nil {
allTags[name] = true
}
// special tags
if ctxt.CgoEnabled && name == "cgo" {
return true
}
if name == ctxt.GOOS || name == ctxt.GOARCH || name == ctxt.Compiler {
return true
}
if ctxt.GOOS == "android" && name == "linux" {
return true
}
if ctxt.GOOS == "illumos" && name == "solaris" {
return true
}
if ctxt.GOOS == "ios" && name == "darwin" {
return true
}
if matchUnixAndBoringCrypto {
if name == "unix" && unixOS[ctxt.GOOS] {
return true
}
if name == "boringcrypto" {
name = "goexperiment.boringcrypto" // boringcrypto is an old name for goexperiment.boringcrypto
}
}
// other tags
for _, tag := range ctxt.BuildTags {
if tag == name {
return true
}
}
for _, tag := range ctxt.ToolTags {
if tag == name {
return true
}
}
for _, tag := range ctxt.ReleaseTags {
if tag == name {
return true
}
}
return false
}
func inTestdata(sub string) bool {
return strings.Contains(sub, "/testdata/") || strings.HasSuffix(sub, "/testdata") ||
strings.HasPrefix(sub, "testdata/") || sub == "testdata"
}
// return ctxt.Import(".", dir, mode)
func ImportPath(ctxt *build.Context, dir string) (string, error) {
if dir == "" {
return "", errors.New("empty source dir")
}
if !isDir(ctxt, dir) {
return ".", errors.New("cannot find package \".\" in:\n\t" + filepath.FromSlash(dir))
}
importPath := "."
if !strings.HasPrefix(dir, ctxt.GOROOT) {
all := gopath(ctxt)
for i, root := range all {
rootsrc := joinPath(ctxt, root, "src")
if sub, ok := hasSubdirCtxt(ctxt, rootsrc, dir); ok && !inTestdata(sub) {
// We found a potential import path for dir,
// but check that using it wouldn't find something
// else first.
if ctxt.GOROOT != "" {
if dir := joinPath(ctxt, ctxt.GOROOT, "src", sub); isDir(ctxt, dir) {
// go/build records a conflict here
goto Found
}
}
for _, earlyRoot := range all[:i] {
if dir := joinPath(ctxt, earlyRoot, "src", sub); isDir(ctxt, dir) {
// go/build records a conflict here
goto Found
}
}
// sub would not name some other directory instead of this one.
// Record it.
importPath = sub
goto Found
}
}
}
if ctxt.GOROOT != "" {
root := joinPath(ctxt, ctxt.GOROOT, "src")
if sub, ok := hasSubdirCtxt(ctxt, root, dir); ok && !inTestdata(sub) {
importPath = sub
goto Found
}
}
Found:
return importPath, nil
}
// joinPath calls ctxt.JoinPath (if not nil) or else filepath.Join.
func joinPath(ctxt *build.Context, elem ...string) string {
if f := ctxt.JoinPath; f != nil {
return f(elem...)
}
return filepath.Join(elem...)
}
// splitPathList calls ctxt.SplitPathList (if not nil) or else filepath.SplitList.
func splitPathList(ctxt *build.Context, s string) []string {
if f := ctxt.SplitPathList; f != nil {
return f(s)
}
return filepath.SplitList(s)
}
// isDir calls ctxt.IsDir (if not nil) or else uses os.Stat.
func isDir(ctxt *build.Context, path string) bool {
if f := ctxt.IsDir; f != nil {
return f(path)
}
fi, err := os.Stat(path)
return err == nil && fi.IsDir()
}
// hasSubdirCtxt calls ctxt.HasSubdir (if not nil) or else uses
// the local file system to answer the question.
func hasSubdirCtxt(ctxt *build.Context, root, dir string) (rel string, ok bool) {
if f := ctxt.HasSubdir; f != nil {
return f(root, dir)
}
// Try using paths we received.
if rel, ok = hasSubdir(root, dir); ok {
return
}
// Try expanding symlinks and comparing
// expanded against unexpanded and
// expanded against expanded.
rootSym, _ := filepath.EvalSymlinks(root)
dirSym, _ := filepath.EvalSymlinks(dir)
if rel, ok = hasSubdir(rootSym, dir); ok {
return
}
if rel, ok = hasSubdir(root, dirSym); ok {
return
}
return hasSubdir(rootSym, dirSym)
}
// isSubdir reports if dir is within root by performing lexical analysis only.
func isSubdir(root, dir string) bool {
n := len(root)
return 0 < n && n < len(dir) && dir[0:n] == root && os.IsPathSeparator(dir[n])
}
// hasSubdir reports if dir is within root by performing lexical analysis only.
//
// NOTE: this is a faster alloc free version of: go/build.hasSubdir
func hasSubdir(root, dir string) (rel string, ok bool) {
if isSubdir(root, dir) {
return filepath.ToSlash(dir[len(root)+1:]), true
}
return "", false
}
// gopath returns the list of Go path directories.
func gopath(ctxt *build.Context) []string {
var all []string
for _, p := range splitPathList(ctxt, ctxt.GOPATH) {
if p == "" || p == ctxt.GOROOT {
// Empty paths are uninteresting.
// If the path is the GOROOT, ignore it.
// People sometimes set GOPATH=$GOROOT.
// Do not get confused by this common mistake.
continue
}
if strings.HasPrefix(p, "~") {
// Path segments starting with ~ on Unix are almost always
// users who have incorrectly quoted ~ while setting GOPATH,
// preventing it from expanding to $HOME.
// The situation is made more confusing by the fact that
// bash allows quoted ~ in $PATH (most shells do not).
// Do not get confused by this, and do not try to use the path.
// It does not exist, and printing errors about it confuses
// those users even more, because they think "sure ~ exists!".
// The go command diagnoses this situation and prints a
// useful error.
// On Windows, ~ is used in short names, such as c:\progra~1
// for c:\program files.
continue
}
all = append(all, p)
}
return all
}
// KnownOSList returns the known operating system values, sorted.
func KnownOSList() []string {
s := make([]string, len(knownOSList))
copy(s, knownOSList)
return s
}
// KnownArchList returns the known architecture values, sorted.
func KnownArchList() []string {
s := make([]string, len(knownArchList))
copy(s, knownArchList)
return s
}
var knownReleaseTag = func() map[string]bool {
m := make(map[string]bool, len(build.Default.ReleaseTags))
for _, v := range build.Default.ReleaseTags {
m[v] = true
}
return m
}()