2016-12-21 15:37:31 +02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2016-12-29 18:12:54 +02:00
|
|
|
"errors"
|
2016-12-30 13:27:35 +02:00
|
|
|
"log"
|
2016-12-21 18:42:23 +02:00
|
|
|
"os"
|
2016-12-21 15:37:31 +02:00
|
|
|
"os/exec"
|
2017-01-22 00:02:51 +02:00
|
|
|
"strings"
|
2016-12-21 15:37:31 +02:00
|
|
|
|
2017-01-15 00:01:32 +02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2016-12-29 18:12:54 +02:00
|
|
|
"golang.org/x/sync/errgroup"
|
2016-12-21 15:37:31 +02:00
|
|
|
)
|
|
|
|
|
2016-12-30 16:41:59 +02:00
|
|
|
// Pipe for build
|
2016-12-30 13:27:35 +02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-01-14 20:41:32 +02:00
|
|
|
// Description of the pipe
|
2017-01-14 19:14:35 +02:00
|
|
|
func (Pipe) Description() string {
|
2017-01-19 11:04:41 +02:00
|
|
|
return "Building binaries"
|
2016-12-30 13:27:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 16:41:59 +02:00
|
|
|
// Run the pipe
|
2017-01-14 18:06:57 +02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2016-12-29 18:12:54 +02:00
|
|
|
var g errgroup.Group
|
2017-01-14 23:47:15 +02:00
|
|
|
for _, goos := range ctx.Config.Build.Goos {
|
|
|
|
for _, goarch := range ctx.Config.Build.Goarch {
|
2017-01-14 19:08:10 +02:00
|
|
|
goos := goos
|
|
|
|
goarch := goarch
|
2017-03-19 01:40:26 +02:00
|
|
|
if !valid(goos, goarch) {
|
|
|
|
continue
|
|
|
|
}
|
2017-01-14 19:08:10 +02:00
|
|
|
name, err := nameFor(ctx, goos, goarch)
|
2017-01-14 16:34:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-14 19:08:10 +02:00
|
|
|
ctx.Archives[goos+goarch] = name
|
2016-12-29 18:12:54 +02:00
|
|
|
g.Go(func() error {
|
2017-01-14 19:08:10 +02:00
|
|
|
return build(name, goos, goarch, ctx)
|
2016-12-29 18:12:54 +02:00
|
|
|
})
|
2016-12-21 15:37:31 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 18:12:54 +02:00
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
2017-01-14 19:08:10 +02:00
|
|
|
func build(name, goos, goarch string, ctx *context.Context) error {
|
2017-01-30 12:06:48 +02:00
|
|
|
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Version
|
2017-03-23 02:06:37 +02:00
|
|
|
output := "dist/" + name + "/" + ctx.Config.Build.Binary + extFor(goos)
|
2017-01-19 11:04:41 +02:00
|
|
|
log.Println("Building", output)
|
2017-01-22 00:02:51 +02:00
|
|
|
if ctx.Config.Build.Hooks.Pre != "" {
|
2017-03-23 19:10:39 +02:00
|
|
|
log.Println("Running pre-build hook", ctx.Config.Build.Hooks.Pre)
|
2017-02-23 14:28:45 +02:00
|
|
|
cmd := strings.Fields(ctx.Config.Build.Hooks.Pre)
|
2017-01-22 00:02:51 +02:00
|
|
|
if err := run(goos, goarch, cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 14:25:43 +02:00
|
|
|
cmd := []string{"go", "build"}
|
|
|
|
if ctx.Config.Build.Flags != "" {
|
2017-02-23 14:27:54 +02:00
|
|
|
cmd = append(cmd, strings.Fields(ctx.Config.Build.Flags)...)
|
2017-02-22 14:25:43 +02:00
|
|
|
}
|
|
|
|
cmd = append(cmd, "-ldflags="+ldflags, "-o", output, ctx.Config.Build.Main)
|
2017-01-22 00:02:51 +02:00
|
|
|
if err := run(goos, goarch, cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ctx.Config.Build.Hooks.Post != "" {
|
2017-03-23 19:10:39 +02:00
|
|
|
log.Println("Running post-build hook", ctx.Config.Build.Hooks.Post)
|
2017-02-23 14:28:45 +02:00
|
|
|
cmd := strings.Fields(ctx.Config.Build.Hooks.Post)
|
2017-01-22 00:02:51 +02:00
|
|
|
if err := run(goos, goarch, cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(goos, goarch string, command []string) error {
|
|
|
|
cmd := exec.Command(command[0], command[1:]...)
|
2017-01-23 13:27:28 +02:00
|
|
|
cmd.Env = append(cmd.Env, os.Environ()...)
|
2017-01-23 13:41:51 +02:00
|
|
|
cmd.Env = append(cmd.Env, "GOOS="+goos, "GOARCH="+goarch)
|
2017-01-30 12:17:15 +02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
return errors.New(string(out))
|
2016-12-29 18:12:54 +02:00
|
|
|
}
|
2016-12-21 15:37:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-19 01:40:26 +02:00
|
|
|
|
|
|
|
// list from https://golang.org/doc/install/source#environment
|
|
|
|
var valids = []string{
|
|
|
|
"androidarm",
|
|
|
|
"darwin386",
|
|
|
|
"darwinamd64",
|
|
|
|
"darwinarm",
|
|
|
|
"darwinarm64",
|
|
|
|
"dragonflyamd64",
|
|
|
|
"freebsd386",
|
|
|
|
"freebsdamd64",
|
|
|
|
"freebsdarm",
|
|
|
|
"linux386",
|
|
|
|
"linuxamd64",
|
|
|
|
"linuxarm",
|
|
|
|
"linuxarm64",
|
|
|
|
"linuxppc64",
|
|
|
|
"linuxppc64le",
|
|
|
|
"linuxmips",
|
|
|
|
"linuxmipsle",
|
|
|
|
"linuxmips64",
|
|
|
|
"linuxmips64le",
|
|
|
|
"netbsd386",
|
|
|
|
"netbsdamd64",
|
|
|
|
"netbsdarm",
|
|
|
|
"openbsd386",
|
|
|
|
"openbsdamd64",
|
|
|
|
"openbsdarm",
|
|
|
|
"plan9386",
|
|
|
|
"plan9amd64",
|
|
|
|
"solarisamd64",
|
|
|
|
"windows386",
|
|
|
|
"windowsamd64",
|
|
|
|
}
|
|
|
|
|
|
|
|
func valid(goos, goarch string) bool {
|
|
|
|
var s = goos + goarch
|
|
|
|
for _, a := range valids {
|
|
|
|
if a == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|