@@ -106,6 +106,7 @@ type Invocation struct {
106106CompileOut string // tells mage to compile a static binary to this path, but not execute
107107GOOS string // sets the GOOS when producing a binary with -compileout
108108GOARCH string // sets the GOARCH when producing a binary with -compileout
109+ Ldflags string // sets the ldflags when producing a binary with -compileout
109110Stdout io.Writer // writer to write stdout messages to
110111Stderr io.Writer // writer to write stderr messages to
111112Stdin io.Reader // reader to read stdin from
@@ -182,6 +183,7 @@ func Parse(stderr, stdout io.Writer, args []string) (inv Invocation, cmd Command
182183fs .StringVar (& inv .GoCmd , "gocmd" , mg .GoCmd (), "use the given go binary to compile the output" )
183184fs .StringVar (& inv .GOOS , "goos" , "" , "set GOOS for binary produced with -compile" )
184185fs .StringVar (& inv .GOARCH , "goarch" , "" , "set GOARCH for binary produced with -compile" )
186+ fs .StringVar (& inv .Ldflags , "ldflags" , "" , "set ldflags for binary produced with -compile" )
185187
186188// commands below
187189
@@ -219,6 +221,7 @@ Options:
219221 -gocmd <string>
220222 use the given go binary to compile the output (default: "go")
221223 -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: "")
222225 -h show description of a target
223226 -keep keep intermediate mage files around after running
224227 -t <string>
@@ -395,7 +398,7 @@ func Invoke(inv Invocation) int {
395398defer os .RemoveAll (main )
396399}
397400files = 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 {
399402errlog .Println ("Error:" , err )
400403return 1
401404}
@@ -491,7 +494,7 @@ func Magefiles(magePath, goos, goarch, goCmd string, stderr io.Writer, isDebug b
491494}
492495
493496// 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 {
495498debug .Println ("compiling to" , compileTo )
496499debug .Println ("compiling using gocmd:" , goCmd )
497500if isDebug {
@@ -506,7 +509,12 @@ func Compile(goos, goarch, magePath, goCmd, compileTo string, gofiles []string,
506509for i := range gofiles {
507510gofiles [i ] = filepath .Base (gofiles [i ])
508511}
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+
510518debug .Printf ("running %s %s" , goCmd , strings .Join (args , " " ))
511519c := exec .Command (goCmd , args ... )
512520c .Env = environ
0 commit comments