Skip to content

Commit 2ded30c

Browse files
authoredNov 19, 2020
Add -ldflags flag (#322)
1 parent 50f568e commit 2ded30c

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed
 

‎mage/main.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type Invocation struct {
106106
CompileOut string // tells mage to compile a static binary to this path, but not execute
107107
GOOS string // sets the GOOS when producing a binary with -compileout
108108
GOARCH string // sets the GOARCH when producing a binary with -compileout
109+
Ldflags string // sets the ldflags when producing a binary with -compileout
109110
Stdout io.Writer // writer to write stdout messages to
110111
Stderr io.Writer // writer to write stderr messages to
111112
Stdin io.Reader // reader to read stdin from
@@ -182,6 +183,7 @@ func Parse(stderr, stdout io.Writer, args []string) (inv Invocation, cmd Command
182183
fs.StringVar(&inv.GoCmd, "gocmd", mg.GoCmd(), "use the given go binary to compile the output")
183184
fs.StringVar(&inv.GOOS, "goos", "", "set GOOS for binary produced with -compile")
184185
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")
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 {
395398
defer os.RemoveAll(main)
396399
}
397400
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 {
399402
errlog.Println("Error:", err)
400403
return 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 {
495498
debug.Println("compiling to", compileTo)
496499
debug.Println("compiling using gocmd:", goCmd)
497500
if isDebug {
@@ -506,7 +509,12 @@ func Compile(goos, goarch, magePath, goCmd, compileTo string, gofiles []string,
506509
for i := range gofiles {
507510
gofiles[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+
510518
debug.Printf("running %s %s", goCmd, strings.Join(args, " "))
511519
c := exec.Command(goCmd, args...)
512520
c.Env = environ

‎mage/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ func TestGoCmd(t *testing.T) {
13881388

13891389
buf := &bytes.Buffer{}
13901390
stderr := &bytes.Buffer{}
1391-
if err := Compile("", "", dir, os.Args[0], name, []string{}, false, stderr, buf); err != nil {
1391+
if err := Compile("", "", "", dir, os.Args[0], name, []string{}, false, stderr, buf); err != nil {
13921392
t.Log("stderr: ", stderr.String())
13931393
t.Fatal(err)
13941394
}

0 commit comments

Comments
 (0)