1 package builder
2 3 import (
4 "fmt"
5 "runtime"
6 7 "moxie/compileopts"
8 "moxie/goenv"
9 )
10 11 // NewConfig builds a new Config object from a set of compiler options. It also
12 // loads some information from the environment while doing that. For example, it
13 // uses the currently active GOPATH (from the goenv package) to determine the Go
14 // version to use.
15 func NewConfig(options *compileopts.Options) (*compileopts.Config, error) {
16 spec, err := compileopts.LoadTarget(options)
17 if err != nil {
18 return nil, err
19 }
20 21 // Version range supported by Moxie.
22 const minorMin = 19
23 const minorMax = 25
24 25 // Check that we support this Go toolchain version.
26 gorootMajor, gorootMinor, err := goenv.GetGorootVersion()
27 if err != nil {
28 return nil, err
29 }
30 31 // Check that the Go toolchain version isn't too new, if we haven't been
32 // compiled with the latest Go version.
33 // This may be a bit too aggressive: if the newer version doesn't change the
34 // Go language we will most likely be able to compile it.
35 buildMajor, buildMinor, _, err := goenv.Parse(runtime.Version())
36 if err != nil {
37 return nil, err
38 }
39 if buildMajor != 1 || buildMinor < gorootMinor {
40 return nil, fmt.Errorf("cannot compile with Go toolchain version go%d.%d (Moxie was built using toolchain version %s)", gorootMajor, gorootMinor, runtime.Version())
41 }
42 43 return &compileopts.Config{
44 Options: options,
45 Target: spec,
46 GoMinorVersion: gorootMinor,
47 TestConfig: options.TestConfig,
48 }, nil
49 }
50