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

Commit d8caaff

Browse files
authored
Merge pull request #128 from mkeeler/go-mod-vendor
Allow -mod passthrough to the go compiler.
2 parents 9cc4875 + afae30c commit d8caaff

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

go.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type CompileOpts struct {
2828
Gcflags string
2929
Asmflags string
3030
Tags string
31+
ModMode string
3132
Cgo bool
3233
Rebuild bool
3334
GoCmd string
@@ -107,6 +108,9 @@ func GoCrossCompile(opts *CompileOpts) error {
107108
if opts.Rebuild {
108109
args = append(args, "-a")
109110
}
111+
if opts.ModMode != "" {
112+
args = append(args, "-mod", opts.ModMode)
113+
}
110114
args = append(args,
111115
"-gcflags", opts.Gcflags,
112116
"-ldflags", opts.Ldflags,

main.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
"os"
77
"os/exec"
88
"runtime"
9+
"strings"
910
"sync"
11+
12+
version "github.com/hashicorp/go-version"
1013
)
1114

1215
func main() {
@@ -26,6 +29,7 @@ func realMain() int {
2629
var flagGcflags, flagAsmflags string
2730
var flagCgo, flagRebuild, flagListOSArch bool
2831
var flagGoCmd string
32+
var modMode string
2933
flags := flag.NewFlagSet("gox", flag.ExitOnError)
3034
flags.Usage = func() { printUsage() }
3135
flags.Var(platformFlag.ArchFlagValue(), "arch", "arch to build for or skip")
@@ -43,6 +47,7 @@ func realMain() int {
4347
flags.StringVar(&flagGcflags, "gcflags", "", "")
4448
flags.StringVar(&flagAsmflags, "asmflags", "", "")
4549
flags.StringVar(&flagGoCmd, "gocmd", "go", "")
50+
flags.StringVar(&modMode, "mod", "", "")
4651
if err := flags.Parse(os.Args[1:]); err != nil {
4752
flags.Usage()
4853
return 1
@@ -77,14 +82,14 @@ func realMain() int {
7782
return 1
7883
}
7984

80-
version, err := GoVersion()
85+
versionStr, err := GoVersion()
8186
if err != nil {
8287
fmt.Fprintf(os.Stderr, "error reading Go version: %s", err)
8388
return 1
8489
}
8590

8691
if flagListOSArch {
87-
return mainListOSArch(version)
92+
return mainListOSArch(versionStr)
8893
}
8994

9095
// Determine the packages that we want to compile. Default to the
@@ -102,14 +107,34 @@ func realMain() int {
102107
}
103108

104109
// Determine the platforms we're building for
105-
platforms := platformFlag.Platforms(SupportedPlatforms(version))
110+
platforms := platformFlag.Platforms(SupportedPlatforms(versionStr))
106111
if len(platforms) == 0 {
107112
fmt.Println("No valid platforms to build for. If you specified a value")
108113
fmt.Println("for the 'os', 'arch', or 'osarch' flags, make sure you're")
109114
fmt.Println("using a valid value.")
110115
return 1
111116
}
112117

118+
// Assume -mod is supported when no version prefix is found
119+
if modMode != "" && strings.HasPrefix(versionStr, "go") {
120+
// go-version only cares about version numbers
121+
current, err := version.NewVersion(versionStr[2:])
122+
if err != nil {
123+
fmt.Fprintf(os.Stderr, "Unable to parse current go version: %s\n%s", versionStr, err.Error())
124+
return 1
125+
}
126+
127+
constraint, err := version.NewConstraint(">= 1.11")
128+
if err != nil {
129+
panic(err)
130+
}
131+
132+
if !constraint.Check(current) {
133+
fmt.Printf("Go compiler version %s does not support the -mod flag\n", versionStr)
134+
modMode = ""
135+
}
136+
}
137+
113138
// Build in parallel!
114139
fmt.Printf("Number of parallel builds: %d\n\n", parallel)
115140
var errorLock sync.Mutex
@@ -133,6 +158,7 @@ func realMain() int {
133158
Gcflags: flagGcflags,
134159
Asmflags: flagAsmflags,
135160
Tags: tags,
161+
ModMode: modMode,
136162
Cgo: flagCgo,
137163
Rebuild: flagRebuild,
138164
GoCmd: flagGoCmd,
@@ -187,6 +213,7 @@ Options:
187213
-ldflags="" Additional '-ldflags' value to pass to go build
188214
-asmflags="" Additional '-asmflags' value to pass to go build
189215
-tags="" Additional '-tags' value to pass to go build
216+
-mod="" Additional '-mod' value to pass to go build
190217
-os="" Space-separated list of operating systems to build for
191218
-osarch="" Space-separated list of os/arch pairs to build for
192219
-osarch-list List supported os/arch pairs for your Go version

0 commit comments

Comments
 (0)