2017-04-14 20:39:32 +02:00
|
|
|
// Package build implements Pipe and can build Go projects for
|
|
|
|
// several platforms, with pre and post hook support.
|
2016-12-21 15:37:31 +02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2017-05-10 15:20:07 +02:00
|
|
|
"fmt"
|
2016-12-21 18:42:23 +02:00
|
|
|
"os"
|
2016-12-21 15:37:31 +02:00
|
|
|
"os/exec"
|
2017-03-26 02:29:38 +02:00
|
|
|
"path/filepath"
|
2017-01-22 00:02:51 +02:00
|
|
|
"strings"
|
2016-12-21 15:37:31 +02:00
|
|
|
|
2017-06-22 05:09:14 +02:00
|
|
|
"github.com/apex/log"
|
2017-06-28 00:20:08 +02:00
|
|
|
"github.com/goreleaser/goreleaser/config"
|
2017-01-15 00:01:32 +02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2017-05-11 05:05:51 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/ext"
|
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 {
|
2017-06-28 00:20:08 +02:00
|
|
|
for _, build := range ctx.Config.Builds {
|
|
|
|
if err := runPipeOnBuild(ctx, build); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runPipeOnBuild(ctx *context.Context, build config.Build) error {
|
|
|
|
if err := runHook(build.Env, build.Hooks.Pre); err != nil {
|
2017-04-09 15:43:23 +02:00
|
|
|
return err
|
2017-03-23 19:20:24 +02:00
|
|
|
}
|
2017-04-21 14:40:25 +02:00
|
|
|
sem := make(chan bool, 4)
|
2016-12-29 18:12:54 +02:00
|
|
|
var g errgroup.Group
|
2017-06-28 00:20:08 +02:00
|
|
|
for _, target := range buildTargets(build) {
|
2017-04-24 19:27:21 +02:00
|
|
|
name, err := nameFor(ctx, target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-12-21 15:37:31 +02:00
|
|
|
}
|
2017-04-24 19:27:21 +02:00
|
|
|
ctx.Archives[target.String()] = name
|
|
|
|
|
|
|
|
sem <- true
|
|
|
|
target := target
|
|
|
|
g.Go(func() error {
|
|
|
|
defer func() {
|
|
|
|
<-sem
|
|
|
|
}()
|
2017-06-28 00:20:08 +02:00
|
|
|
return doBuild(ctx, build, name, target)
|
2017-04-24 19:27:21 +02:00
|
|
|
})
|
2016-12-21 15:37:31 +02:00
|
|
|
}
|
2017-03-23 23:32:27 +02:00
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-28 00:20:08 +02:00
|
|
|
return runHook(build.Env, build.Hooks.Post)
|
2017-04-09 15:43:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 15:43:25 +02:00
|
|
|
func runHook(env []string, hook string) error {
|
2017-04-09 15:43:23 +02:00
|
|
|
if hook == "" {
|
|
|
|
return nil
|
2017-03-23 19:20:24 +02:00
|
|
|
}
|
2017-06-22 15:47:34 +02:00
|
|
|
log.WithField("hook", hook).Info("running hook")
|
2017-04-09 15:43:23 +02:00
|
|
|
cmd := strings.Fields(hook)
|
2017-05-11 15:47:03 +02:00
|
|
|
return run(runtimeTarget, cmd, env)
|
2016-12-29 18:12:54 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 00:20:08 +02:00
|
|
|
func doBuild(ctx *context.Context, build config.Build, name string, target buildTarget) error {
|
|
|
|
output := filepath.Join(ctx.Config.Dist, name, build.Binary+ext.For(target.goos))
|
2017-06-22 15:47:34 +02:00
|
|
|
log.WithField("binary", output).Info("building")
|
2017-02-22 14:25:43 +02:00
|
|
|
cmd := []string{"go", "build"}
|
2017-06-28 00:20:08 +02:00
|
|
|
if build.Flags != "" {
|
|
|
|
cmd = append(cmd, strings.Fields(build.Flags)...)
|
2017-02-22 14:25:43 +02:00
|
|
|
}
|
2017-03-26 01:24:38 +02:00
|
|
|
flags, err := ldflags(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-28 00:20:08 +02:00
|
|
|
cmd = append(cmd, "-ldflags="+flags, "-o", output, build.Main)
|
|
|
|
return run(target, cmd, build.Env)
|
2017-01-22 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 15:47:03 +02:00
|
|
|
func run(target buildTarget, command, env []string) error {
|
2017-06-22 05:25:52 +02:00
|
|
|
var cmd = exec.Command(command[0], command[1:]...)
|
|
|
|
env = append(env, "GOOS="+target.goos, "GOARCH="+target.goarch, "GOARM="+target.goarm)
|
2017-06-25 16:37:40 +02:00
|
|
|
var log = log.WithField("target", target.PrettyString()).
|
|
|
|
WithField("env", env).
|
|
|
|
WithField("cmd", command)
|
2017-01-23 13:27:28 +02:00
|
|
|
cmd.Env = append(cmd.Env, os.Environ()...)
|
2017-05-11 15:43:25 +02:00
|
|
|
cmd.Env = append(cmd.Env, env...)
|
2017-06-25 16:37:40 +02:00
|
|
|
log.Debug("running")
|
2017-01-30 12:17:15 +02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
2017-06-25 16:37:40 +02:00
|
|
|
log.WithError(err).Debug("failed")
|
|
|
|
return fmt.Errorf("build failed for %s:\n%v", target.PrettyString(), string(out))
|
2016-12-29 18:12:54 +02:00
|
|
|
}
|
2016-12-21 15:37:31 +02:00
|
|
|
return nil
|
|
|
|
}
|