Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -ldflags flag #322

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions mage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type Invocation struct {
CompileOut string // tells mage to compile a static binary to this path, but not execute
GOOS string // sets the GOOS when producing a binary with -compileout
GOARCH string // sets the GOARCH when producing a binary with -compileout
Ldflags string // sets the ldflags when producing a binary with -compileout
Stdout io.Writer // writer to write stdout messages to
Stderr io.Writer // writer to write stderr messages to
Stdin io.Reader // reader to read stdin from
Expand Down Expand Up @@ -182,6 +183,7 @@ func Parse(stderr, stdout io.Writer, args []string) (inv Invocation, cmd Command
fs.StringVar(&inv.GoCmd, "gocmd", mg.GoCmd(), "use the given go binary to compile the output")
fs.StringVar(&inv.GOOS, "goos", "", "set GOOS for binary produced with -compile")
fs.StringVar(&inv.GOARCH, "goarch", "", "set GOARCH for binary produced with -compile")
fs.StringVar(&inv.Ldflags, "ldflags", "", "set ldflags for binary produced with -compile")

// commands below

Expand Down Expand Up @@ -219,6 +221,7 @@ Options:
-gocmd <string>
use the given go binary to compile the output (default: "go")
-goos sets the GOOS for the binary created by -compile (default: current OS)
-ldflags sets the ldflags for the binary created by -compile (default: "")
-h show description of a target
-keep keep intermediate mage files around after running
-t <string>
Expand Down Expand Up @@ -395,7 +398,7 @@ func Invoke(inv Invocation) int {
defer os.RemoveAll(main)
}
files = append(files, main)
if err := Compile(inv.GOOS, inv.GOARCH, inv.Dir, inv.GoCmd, exePath, files, inv.Debug, inv.Stderr, inv.Stdout); err != nil {
if err := Compile(inv.GOOS, inv.GOARCH, inv.Ldflags, inv.Dir, inv.GoCmd, exePath, files, inv.Debug, inv.Stderr, inv.Stdout); err != nil {
errlog.Println("Error:", err)
return 1
}
Expand Down Expand Up @@ -491,7 +494,7 @@ func Magefiles(magePath, goos, goarch, goCmd string, stderr io.Writer, isDebug b
}

// Compile uses the go tool to compile the files into an executable at path.
func Compile(goos, goarch, magePath, goCmd, compileTo string, gofiles []string, isDebug bool, stderr, stdout io.Writer) error {
func Compile(goos, goarch, ldflags, magePath, goCmd, compileTo string, gofiles []string, isDebug bool, stderr, stdout io.Writer) error {
debug.Println("compiling to", compileTo)
debug.Println("compiling using gocmd:", goCmd)
if isDebug {
Expand All @@ -506,7 +509,12 @@ func Compile(goos, goarch, magePath, goCmd, compileTo string, gofiles []string,
for i := range gofiles {
gofiles[i] = filepath.Base(gofiles[i])
}
args := append([]string{"build", "-o", compileTo}, gofiles...)
buildArgs := []string{"build", "-o", compileTo}
if ldflags != "" {
buildArgs = append(buildArgs, "-ldflags", ldflags)
}
args := append(buildArgs, gofiles...)

debug.Printf("running %s %s", goCmd, strings.Join(args, " "))
c := exec.Command(goCmd, args...)
c.Env = environ
Expand Down
2 changes: 1 addition & 1 deletion mage/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ func TestGoCmd(t *testing.T) {

buf := &bytes.Buffer{}
stderr := &bytes.Buffer{}
if err := Compile("", "", dir, os.Args[0], name, []string{}, false, stderr, buf); err != nil {
if err := Compile("", "", "", dir, os.Args[0], name, []string{}, false, stderr, buf); err != nil {
t.Log("stderr: ", stderr.String())
t.Fatal(err)
}
Expand Down