diff --git a/go.go b/go.go index 37edb36..2d25837 100644 --- a/go.go +++ b/go.go @@ -28,6 +28,7 @@ type CompileOpts struct { Gcflags string Asmflags string Tags string + ModMode string Cgo bool Rebuild bool GoCmd string @@ -107,6 +108,9 @@ func GoCrossCompile(opts *CompileOpts) error { if opts.Rebuild { args = append(args, "-a") } + if opts.ModMode != "" { + args = append(args, "-mod", opts.ModMode) + } args = append(args, "-gcflags", opts.Gcflags, "-ldflags", opts.Ldflags, diff --git a/main.go b/main.go index d8098c9..9eca6a4 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,10 @@ import ( "os" "os/exec" "runtime" + "strings" "sync" + + version "github.com/hashicorp/go-version" ) func main() { @@ -26,6 +29,7 @@ func realMain() int { var flagGcflags, flagAsmflags string var flagCgo, flagRebuild, flagListOSArch bool var flagGoCmd string + var modMode string flags := flag.NewFlagSet("gox", flag.ExitOnError) flags.Usage = func() { printUsage() } flags.Var(platformFlag.ArchFlagValue(), "arch", "arch to build for or skip") @@ -43,6 +47,7 @@ func realMain() int { flags.StringVar(&flagGcflags, "gcflags", "", "") flags.StringVar(&flagAsmflags, "asmflags", "", "") flags.StringVar(&flagGoCmd, "gocmd", "go", "") + flags.StringVar(&modMode, "mod", "", "") if err := flags.Parse(os.Args[1:]); err != nil { flags.Usage() return 1 @@ -77,14 +82,14 @@ func realMain() int { return 1 } - version, err := GoVersion() + versionStr, err := GoVersion() if err != nil { fmt.Fprintf(os.Stderr, "error reading Go version: %s", err) return 1 } if flagListOSArch { - return mainListOSArch(version) + return mainListOSArch(versionStr) } // Determine the packages that we want to compile. Default to the @@ -102,7 +107,7 @@ func realMain() int { } // Determine the platforms we're building for - platforms := platformFlag.Platforms(SupportedPlatforms(version)) + platforms := platformFlag.Platforms(SupportedPlatforms(versionStr)) if len(platforms) == 0 { fmt.Println("No valid platforms to build for. If you specified a value") fmt.Println("for the 'os', 'arch', or 'osarch' flags, make sure you're") @@ -110,6 +115,26 @@ func realMain() int { return 1 } + // Assume -mod is supported when no version prefix is found + if modMode != "" && strings.HasPrefix(versionStr, "go") { + // go-version only cares about version numbers + current, err := version.NewVersion(versionStr[2:]) + if err != nil { + fmt.Fprintf(os.Stderr, "Unable to parse current go version: %s\n%s", versionStr, err.Error()) + return 1 + } + + constraint, err := version.NewConstraint(">= 1.11") + if err != nil { + panic(err) + } + + if !constraint.Check(current) { + fmt.Printf("Go compiler version %s does not support the -mod flag\n", versionStr) + modMode = "" + } + } + // Build in parallel! fmt.Printf("Number of parallel builds: %d\n\n", parallel) var errorLock sync.Mutex @@ -133,6 +158,7 @@ func realMain() int { Gcflags: flagGcflags, Asmflags: flagAsmflags, Tags: tags, + ModMode: modMode, Cgo: flagCgo, Rebuild: flagRebuild, GoCmd: flagGoCmd, @@ -187,6 +213,7 @@ Options: -ldflags="" Additional '-ldflags' value to pass to go build -asmflags="" Additional '-asmflags' value to pass to go build -tags="" Additional '-tags' value to pass to go build + -mod="" Additional '-mod' value to pass to go build -os="" Space-separated list of operating systems to build for -osarch="" Space-separated list of os/arch pairs to build for -osarch-list List supported os/arch pairs for your Go version