@@ -106,6 +106,7 @@ type Invocation struct {
106
106
CompileOut string // tells mage to compile a static binary to this path, but not execute
107
107
GOOS string // sets the GOOS when producing a binary with -compileout
108
108
GOARCH string // sets the GOARCH when producing a binary with -compileout
109
+ Ldflags string // sets the ldflags when producing a binary with -compileout
109
110
Stdout io.Writer // writer to write stdout messages to
110
111
Stderr io.Writer // writer to write stderr messages to
111
112
Stdin io.Reader // reader to read stdin from
@@ -182,6 +183,7 @@ func Parse(stderr, stdout io.Writer, args []string) (inv Invocation, cmd Command
182
183
fs .StringVar (& inv .GoCmd , "gocmd" , mg .GoCmd (), "use the given go binary to compile the output" )
183
184
fs .StringVar (& inv .GOOS , "goos" , "" , "set GOOS for binary produced with -compile" )
184
185
fs .StringVar (& inv .GOARCH , "goarch" , "" , "set GOARCH for binary produced with -compile" )
186
+ fs .StringVar (& inv .Ldflags , "ldflags" , "" , "set ldflags for binary produced with -compile" )
185
187
186
188
// commands below
187
189
@@ -219,6 +221,7 @@ Options:
219
221
-gocmd <string>
220
222
use the given go binary to compile the output (default: "go")
221
223
-goos sets the GOOS for the binary created by -compile (default: current OS)
224
+ -ldflags sets the ldflags for the binary created by -compile (default: "")
222
225
-h show description of a target
223
226
-keep keep intermediate mage files around after running
224
227
-t <string>
@@ -395,7 +398,7 @@ func Invoke(inv Invocation) int {
395
398
defer os .RemoveAll (main )
396
399
}
397
400
files = append (files , main )
398
- if err := Compile (inv .GOOS , inv .GOARCH , inv .Dir , inv .GoCmd , exePath , files , inv .Debug , inv .Stderr , inv .Stdout ); err != nil {
401
+ if err := Compile (inv .GOOS , inv .GOARCH , inv .Ldflags , inv . Dir , inv .GoCmd , exePath , files , inv .Debug , inv .Stderr , inv .Stdout ); err != nil {
399
402
errlog .Println ("Error:" , err )
400
403
return 1
401
404
}
@@ -491,7 +494,7 @@ func Magefiles(magePath, goos, goarch, goCmd string, stderr io.Writer, isDebug b
491
494
}
492
495
493
496
// Compile uses the go tool to compile the files into an executable at path.
494
- func Compile (goos , goarch , magePath , goCmd , compileTo string , gofiles []string , isDebug bool , stderr , stdout io.Writer ) error {
497
+ func Compile (goos , goarch , ldflags , magePath , goCmd , compileTo string , gofiles []string , isDebug bool , stderr , stdout io.Writer ) error {
495
498
debug .Println ("compiling to" , compileTo )
496
499
debug .Println ("compiling using gocmd:" , goCmd )
497
500
if isDebug {
@@ -506,7 +509,12 @@ func Compile(goos, goarch, magePath, goCmd, compileTo string, gofiles []string,
506
509
for i := range gofiles {
507
510
gofiles [i ] = filepath .Base (gofiles [i ])
508
511
}
509
- args := append ([]string {"build" , "-o" , compileTo }, gofiles ... )
512
+ buildArgs := []string {"build" , "-o" , compileTo }
513
+ if ldflags != "" {
514
+ buildArgs = append (buildArgs , "-ldflags" , ldflags )
515
+ }
516
+ args := append (buildArgs , gofiles ... )
517
+
510
518
debug .Printf ("running %s %s" , goCmd , strings .Join (args , " " ))
511
519
c := exec .Command (goCmd , args ... )
512
520
c .Env = environ
0 commit comments