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