options.go raw
1 package compileopts
2
3 import (
4 "fmt"
5 "regexp"
6 "strings"
7 "time"
8 )
9
10 var (
11 validBuildModeOptions = []string{"default"}
12 validGCOptions = []string{"none", "leaking", "conservative", "boehm"}
13 validSchedulerOptions = []string{"none", "tasks"}
14 validPanicStrategyOptions = []string{"print", "trap"}
15 validOptOptions = []string{"none", "0", "1", "2", "s", "z"}
16 )
17
18 // Options contains extra options to give to the compiler.
19 type Options struct {
20 GOOS string
21 GOARCH string
22 Directory string // working dir
23 Target string
24 BuildMode string
25 Opt string
26 GC string
27 PanicStrategy string
28 Scheduler string
29 StackSize uint64
30 Work bool
31 InterpTimeout time.Duration
32 PrintIR bool
33 DumpSSA bool
34 VerifyIR bool
35 SkipDWARF bool
36 PrintCommands func(cmd string, args ...string) `json:"-"`
37 Semaphore chan struct{} `json:"-"`
38 Debug bool
39 Nobounds bool
40 PrintSizes string
41 PrintAllocs *regexp.Regexp
42 PrintStacks bool
43 Tags []string
44 GlobalValues map[string]map[string]string
45 TestConfig TestConfig
46 LLVMFeatures string
47 ExtLDFlags []string
48 }
49
50 // Verify performs validation on the given options.
51 func (o *Options) Verify() error {
52 if o.BuildMode != "" && !isInArray(validBuildModeOptions, o.BuildMode) {
53 return fmt.Errorf(`invalid buildmode option '%s': valid values are %s`,
54 o.BuildMode, strings.Join(validBuildModeOptions, ", "))
55 }
56 if o.GC != "" && !isInArray(validGCOptions, o.GC) {
57 return fmt.Errorf(`invalid gc option '%s': valid values are %s`,
58 o.GC, strings.Join(validGCOptions, ", "))
59 }
60 if o.Scheduler != "" && !isInArray(validSchedulerOptions, o.Scheduler) {
61 return fmt.Errorf(`invalid scheduler option '%s': valid values are %s`,
62 o.Scheduler, strings.Join(validSchedulerOptions, ", "))
63 }
64 if o.PanicStrategy != "" && !isInArray(validPanicStrategyOptions, o.PanicStrategy) {
65 return fmt.Errorf(`invalid panic option '%s': valid values are %s`,
66 o.PanicStrategy, strings.Join(validPanicStrategyOptions, ", "))
67 }
68 if o.Opt != "" && !isInArray(validOptOptions, o.Opt) {
69 return fmt.Errorf("invalid -opt=%s: valid values are %s",
70 o.Opt, strings.Join(validOptOptions, ", "))
71 }
72 return nil
73 }
74
75 func isInArray(arr []string, item string) bool {
76 for _, i := range arr {
77 if i == item {
78 return true
79 }
80 }
81 return false
82 }
83