...
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 if v[0] == '\'' || v[0] == '"' {
63 return fmt.Errorf("parameter may not start with quote character %c", v[0])
64 }
65 pattern := strings.TrimSpace(v[:i])
66 match = MatchPackage(pattern, cwd)
67 v = v[i+1:]
68 }
69 flags, err := quoted.Split(v)
70 if err != nil {
71 return err
72 }
73 if flags == nil {
74 flags = []string{}
75 }
76 f.values = append(f.values, ppfValue{match, flags})
77 return nil
78 }
79
80 func (f *PerPackageFlag) String() string { return f.raw }
81
82
83 func (f *PerPackageFlag) Present() bool {
84 return f.present
85 }
86
87
88 func (f *PerPackageFlag) For(p *Package) []string {
89 flags := []string{}
90 for _, v := range f.values {
91 if v.match(p) {
92 flags = v.flags
93 }
94 }
95 return flags
96 }
97
View as plain text