Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #128 from mkeeler/go-mod-vendor
Browse files Browse the repository at this point in the history
Allow -mod passthrough to the go compiler.
  • Loading branch information
mitchellh authored Apr 10, 2019
2 parents 9cc4875 + afae30c commit d8caaff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions go.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type CompileOpts struct {
Gcflags string
Asmflags string
Tags string
ModMode string
Cgo bool
Rebuild bool
GoCmd string
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 30 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"os"
"os/exec"
"runtime"
"strings"
"sync"

version "github.com/hashicorp/go-version"
)

func main() {
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -102,14 +107,34 @@ 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")
fmt.Println("using a valid value.")
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
Expand All @@ -133,6 +158,7 @@ func realMain() int {
Gcflags: flagGcflags,
Asmflags: flagAsmflags,
Tags: tags,
ModMode: modMode,
Cgo: flagCgo,
Rebuild: flagRebuild,
GoCmd: flagGoCmd,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d8caaff

Please sign in to comment.