1
2
3
4
5 package load
6
7 import (
8 "cmd/go/internal/base"
9 "cmd/internal/quoted"
10 "fmt"
11 "strings"
12 )
13
14 var (
15 BuildAsmflags PerPackageFlag
16 BuildGcflags PerPackageFlag
17 BuildLdflags PerPackageFlag
18 BuildGccgoflags PerPackageFlag
19 )
20
21
22
23
24 type PerPackageFlag struct {
25 raw string
26 present bool
27 values []ppfValue
28 }
29
30
31 type ppfValue struct {
32 match func(*Package) bool
33 flags []string
34 }
35
36
37 func (f *PerPackageFlag) Set(v string) error {
38 return f.set(v, base.Cwd())
39 }
40
41
42 func (f *PerPackageFlag) set(v, cwd string) error {
43 f.raw = v
44 f.present = true
45 match := func(p *Package) bool { return p.Internal.CmdlinePkg || p.Internal.CmdlineFiles }
46
47 v = strings.TrimSpace(v)
48 if v == "" {
49
50
51 f.values = append(f.values, ppfValue{match, []string{}})
52 return nil
53 }
54 if !strings.HasPrefix(v, "-") {
55 i := strings.Index(v, "=")
56 if i < 0 {
57 return fmt.Errorf("missing =<value> in <pattern>=<value>")
58 }
59 if i == 0 {
60 return fmt.Errorf("missing <pattern> in <pattern>=<value>")
61 }
62 pattern := strings.TrimSpace(v[:i])
63 match = MatchPackage(pattern, cwd)
64 v = v[i+1:]
65 }
66 flags, err := quoted.Split(v)
67 if err != nil {
68 return err
69 }
70 if flags == nil {
71 flags = []string{}
72 }
73 f.values = append(f.values, ppfValue{match, flags})
74 return nil
75 }
76
77 func (f *PerPackageFlag) String() string { return f.raw }
78
79
80 func (f *PerPackageFlag) Present() bool {
81 return f.present
82 }
83
84
85 func (f *PerPackageFlag) For(p *Package) []string {
86 flags := []string{}
87 for _, v := range f.values {
88 if v.match(p) {
89 flags = v.flags
90 }
91 }
92 return flags
93 }
94
View as plain text