| 1 | package build |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "io" |
| 6 | "log" |
| 7 | "os" |
| 8 | "runtime/pprof" |
| 9 | |
| 10 | "github.com/mmcloughlin/avo/pass" |
| 11 | "github.com/mmcloughlin/avo/printer" |
| 12 | ) |
| 13 | |
| 14 | // Config contains options for an avo main function. |
| 15 | type Config struct { |
| 16 | ErrOut io.Writer |
| 17 | MaxErrors int // max errors to report; 0 means unlimited |
| 18 | CPUProfile io.WriteCloser |
| 19 | Passes []pass.Interface |
| 20 | } |
| 21 | |
| 22 | // Main is the standard main function for an avo program. This extracts the |
| 23 | // result from the build Context (logging and exiting on error), and performs |
| 24 | // configured passes. |
| 25 | func Main(cfg *Config, context *Context) int { |
| 26 | diag := log.New(cfg.ErrOut, "", 0) |
| 27 | |
| 28 | if cfg.CPUProfile != nil { |
| 29 | defer cfg.CPUProfile.Close() |
| 30 | if err := pprof.StartCPUProfile(cfg.CPUProfile); err != nil { |
| 31 | diag.Println("could not start CPU profile: ", err) |
| 32 | return 1 |
| 33 | } |
| 34 | defer pprof.StopCPUProfile() |
| 35 | } |
| 36 | |
| 37 | f, err := context.Result() |
| 38 | if err != nil { |
| 39 | LogError(diag, err, cfg.MaxErrors) |
| 40 | return 1 |
| 41 | } |
| 42 | |
| 43 | p := pass.Concat(cfg.Passes...) |
| 44 | if err := p.Execute(f); err != nil { |
| 45 | diag.Println(err) |
| 46 | return 1 |
| 47 | } |
| 48 | |
| 49 | return 0 |
| 50 | } |
| 51 | |
| 52 | // Flags represents CLI flags for an avo program. |
| 53 | type Flags struct { |
| 54 | errout *outputValue |
| 55 | allerrors bool |
| 56 | cpuprof *outputValue |
| 57 | pkg string |
| 58 | printers []*printerValue |
| 59 | } |
| 60 | |
| 61 | // NewFlags initializes avo flags for the given FlagSet. |
| 62 | func NewFlags(fs *flag.FlagSet) *Flags { |
| 63 | f := &Flags{} |
| 64 | |
| 65 | f.errout = newOutputValue(os.Stderr) |
| 66 | fs.Var(f.errout, "log", "diagnostics output") |
| 67 | |
| 68 | fs.BoolVar(&f.allerrors, "e", false, "no limit on number of errors reported") |
| 69 | |
| 70 | f.cpuprof = newOutputValue(nil) |
| 71 | fs.Var(f.cpuprof, "cpuprofile", "write cpu profile to `file`") |
| 72 | |
| 73 | fs.StringVar(&f.pkg, "pkg", "", "package name (defaults to current directory name)") |
| 74 | |
| 75 | goasm := newPrinterValue(printer.NewGoAsm, os.Stdout) |
| 76 | fs.Var(goasm, "out", "assembly output") |
| 77 | f.printers = append(f.printers, goasm) |
| 78 | |
| 79 | stubs := newPrinterValue(printer.NewStubs, nil) |
| 80 | fs.Var(stubs, "stubs", "go stub file") |
| 81 | f.printers = append(f.printers, stubs) |
| 82 | |
| 83 | return f |
| 84 | } |
| 85 | |
| 86 | // Config builds a configuration object based on flag values. |
| 87 | func (f *Flags) Config() *Config { |
| 88 | pc := printer.NewGoRunConfig() |
| 89 | if f.pkg != "" { |
| 90 | pc.Pkg = f.pkg |
| 91 | } |
| 92 | passes := []pass.Interface{pass.Compile} |
| 93 | for _, pv := range f.printers { |
| 94 | p := pv.Build(pc) |
| 95 | if p != nil { |
| 96 | passes = append(passes, p) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | cfg := &Config{ |
| 101 | ErrOut: f.errout.w, |
| 102 | MaxErrors: 10, |
| 103 | CPUProfile: f.cpuprof.w, |
| 104 | Passes: passes, |
| 105 | } |
| 106 | |
| 107 | if f.allerrors { |
| 108 | cfg.MaxErrors = 0 |
| 109 | } |
| 110 | |
| 111 | return cfg |
| 112 | } |
| 113 | |
| 114 | type outputValue struct { |
| 115 | w io.WriteCloser |
| 116 | filename string |
| 117 | } |
| 118 | |
| 119 | func newOutputValue(dflt io.WriteCloser) *outputValue { |
| 120 | return &outputValue{w: dflt} |
| 121 | } |
| 122 | |
| 123 | func (o *outputValue) String() string { |
| 124 | if o == nil { |
| 125 | return "" |
| 126 | } |
| 127 | return o.filename |
| 128 | } |
| 129 | |
| 130 | func (o *outputValue) Set(s string) error { |
| 131 | o.filename = s |
| 132 | if s == "-" { |
| 133 | o.w = nopwritecloser{os.Stdout} |
| 134 | return nil |
| 135 | } |
| 136 | f, err := os.Create(s) |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | o.w = f |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | type printerValue struct { |
| 145 | *outputValue |
| 146 | Builder printer.Builder |
| 147 | } |
| 148 | |
| 149 | func newPrinterValue(b printer.Builder, dflt io.WriteCloser) *printerValue { |
| 150 | return &printerValue{ |
| 151 | outputValue: newOutputValue(dflt), |
| 152 | Builder: b, |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func (p *printerValue) Build(cfg printer.Config) pass.Interface { |
| 157 | if p.outputValue.w == nil { |
| 158 | return nil |
| 159 | } |
| 160 | return &pass.Output{ |
| 161 | Writer: p.outputValue.w, |
| 162 | Printer: p.Builder(cfg), |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // nopwritecloser wraps a Writer and provides a null implementation of Close(). |
| 167 | type nopwritecloser struct { |
| 168 | io.Writer |
| 169 | } |
| 170 | |
| 171 | func (nopwritecloser) Close() error { return nil } |