github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/build/bin.go (about)

     1  package build
     2  
     3  import (
     4  	"os/exec"
     5  	"runtime"
     6  	"strings"
     7  )
     8  
     9  func buildCmd(opts *Options) (*exec.Cmd, error) {
    10  	if len(opts.GoCommand) == 0 {
    11  		opts.GoCommand = "build"
    12  	}
    13  	buildArgs := []string{opts.GoCommand}
    14  
    15  	if len(opts.Mod) != 0 {
    16  		buildArgs = append(buildArgs, "-mod", opts.Mod)
    17  	}
    18  
    19  	buildArgs = append(buildArgs, opts.BuildFlags...)
    20  
    21  	tf := opts.App.BuildTags(opts.Environment, opts.Tags...)
    22  	if len(tf) > 0 {
    23  		buildArgs = append(buildArgs, "-tags", tf.String())
    24  	}
    25  
    26  	if opts.GoCommand == "build" {
    27  		bin := opts.App.Bin
    28  		if runtime.GOOS == "windows" {
    29  			if !strings.HasSuffix(bin, ".exe") {
    30  				bin += ".exe"
    31  			}
    32  			bin = strings.Replace(bin, "/", "\\", -1)
    33  		} else {
    34  			bin = strings.TrimSuffix(bin, ".exe")
    35  		}
    36  		buildArgs = append(buildArgs, "-o", bin)
    37  	}
    38  
    39  	flags := []string{}
    40  
    41  	if opts.Static {
    42  		flags = append(flags, "-linkmode external", "-extldflags \"-static\"")
    43  	}
    44  
    45  	// Add any additional ldflags passed in to the build args
    46  	if len(opts.LDFlags) > 0 {
    47  		flags = append(flags, opts.LDFlags)
    48  	}
    49  	if len(flags) > 0 {
    50  		buildArgs = append(buildArgs, "-ldflags", strings.Join(flags, " "))
    51  	}
    52  
    53  	return exec.Command("go", buildArgs...), nil
    54  }