2016-12-21 15:37:31 +02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
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-12-17 19:24:49 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
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-12-17 19:24:49 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2017-07-07 00:49:21 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/buildtarget"
|
2017-05-11 05:05:51 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/ext"
|
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-12-02 23:53:19 +02:00
|
|
|
func (Pipe) String() string {
|
|
|
|
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 {
|
2017-06-28 00:36:36 +02:00
|
|
|
log.WithField("build", build).Debug("building")
|
2017-11-18 18:14:39 +02:00
|
|
|
if err := checkMain(ctx, build); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-28 00:20:08 +02:00
|
|
|
if err := runPipeOnBuild(ctx, build); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
// Default sets the pipe defaults
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
|
|
for i, build := range ctx.Config.Builds {
|
|
|
|
ctx.Config.Builds[i] = buildWithDefaults(ctx, build)
|
|
|
|
}
|
|
|
|
if len(ctx.Config.Builds) == 0 {
|
|
|
|
ctx.Config.Builds = []config.Build{
|
|
|
|
buildWithDefaults(ctx, ctx.Config.SingleBuild),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildWithDefaults(ctx *context.Context, build config.Build) config.Build {
|
|
|
|
if build.Binary == "" {
|
|
|
|
build.Binary = ctx.Config.Release.GitHub.Name
|
|
|
|
}
|
|
|
|
if build.Main == "" {
|
|
|
|
build.Main = "."
|
|
|
|
}
|
|
|
|
if len(build.Goos) == 0 {
|
|
|
|
build.Goos = []string{"linux", "darwin"}
|
|
|
|
}
|
|
|
|
if len(build.Goarch) == 0 {
|
|
|
|
build.Goarch = []string{"amd64", "386"}
|
|
|
|
}
|
|
|
|
if len(build.Goarm) == 0 {
|
|
|
|
build.Goarm = []string{"6"}
|
|
|
|
}
|
|
|
|
if build.Ldflags == "" {
|
|
|
|
build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}"
|
|
|
|
}
|
|
|
|
return build
|
|
|
|
}
|
|
|
|
|
2017-06-28 00:20:08 +02:00
|
|
|
func runPipeOnBuild(ctx *context.Context, build config.Build) error {
|
2017-12-29 21:07:06 +02:00
|
|
|
if err := runHook(ctx, build.Env, build.Hooks.Pre); err != nil {
|
2017-11-18 18:14:39 +02:00
|
|
|
return errors.Wrap(err, "pre hook failed")
|
2017-03-23 19:20:24 +02:00
|
|
|
}
|
2017-07-15 21:49:52 +02:00
|
|
|
sem := make(chan bool, ctx.Parallelism)
|
2016-12-29 18:12:54 +02:00
|
|
|
var g errgroup.Group
|
2017-07-09 18:14:35 +02:00
|
|
|
for _, target := range buildtarget.All(build) {
|
2017-04-24 19:27:21 +02:00
|
|
|
sem <- true
|
|
|
|
target := target
|
2017-07-01 17:27:13 +02:00
|
|
|
build := build
|
2017-04-24 19:27:21 +02:00
|
|
|
g.Go(func() error {
|
|
|
|
defer func() {
|
|
|
|
<-sem
|
|
|
|
}()
|
2017-07-01 17:27:13 +02:00
|
|
|
return doBuild(ctx, build, 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-12-29 21:07:06 +02:00
|
|
|
return errors.Wrap(runHook(ctx, build.Env, build.Hooks.Post), "post hook failed")
|
2017-04-09 15:43:23 +02:00
|
|
|
}
|
|
|
|
|
2017-12-29 21:07:06 +02:00
|
|
|
func runHook(ctx *context.Context, 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-12-29 21:07:06 +02:00
|
|
|
return run(ctx, buildtarget.Runtime, cmd, env)
|
2016-12-29 18:12:54 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 00:49:21 +02:00
|
|
|
func doBuild(ctx *context.Context, build config.Build, target buildtarget.Target) error {
|
2017-12-18 02:28:24 +02:00
|
|
|
var ext = ext.For(target)
|
|
|
|
var binaryName = build.Binary + ext
|
2017-12-17 19:24:49 +02:00
|
|
|
var binary = filepath.Join(ctx.Config.Dist, target.String(), binaryName)
|
2017-12-19 01:04:25 +02:00
|
|
|
log.WithField("binary", binary).Info("building")
|
|
|
|
cmd := []string{"go", "build"}
|
|
|
|
if build.Flags != "" {
|
|
|
|
cmd = append(cmd, strings.Fields(build.Flags)...)
|
|
|
|
}
|
|
|
|
flags, err := ldflags(ctx, build)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = append(cmd, "-ldflags="+flags, "-o", binary, build.Main)
|
2017-12-29 21:07:06 +02:00
|
|
|
if err := run(ctx, target, cmd, build.Env); err != nil {
|
2017-12-19 01:04:25 +02:00
|
|
|
return errors.Wrapf(err, "failed to build for %s", target)
|
|
|
|
}
|
2017-12-17 19:24:49 +02:00
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Type: artifact.Binary,
|
|
|
|
Path: binary,
|
|
|
|
Name: binaryName,
|
|
|
|
Goos: target.OS,
|
|
|
|
Goarch: target.Arch,
|
|
|
|
Goarm: target.Arm,
|
2017-12-17 22:01:58 +02:00
|
|
|
Extra: map[string]string{
|
|
|
|
"Binary": build.Binary,
|
2017-12-18 02:28:24 +02:00
|
|
|
"Ext": ext,
|
2017-12-17 22:01:58 +02:00
|
|
|
},
|
2017-12-17 19:24:49 +02:00
|
|
|
})
|
2017-12-19 01:04:25 +02:00
|
|
|
return nil
|
2017-01-22 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-29 21:07:06 +02:00
|
|
|
func run(ctx *context.Context, target buildtarget.Target, command, env []string) error {
|
2017-11-26 16:09:12 +02:00
|
|
|
/* #nosec */
|
2017-12-29 21:07:06 +02:00
|
|
|
var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
|
2017-07-07 00:49:21 +02:00
|
|
|
env = append(env, target.Env()...)
|
2017-07-07 04:50:45 +02:00
|
|
|
var log = log.WithField("target", target.PrettyString()).
|
2017-06-25 16:37:40 +02:00
|
|
|
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")
|
2017-11-18 18:14:39 +02:00
|
|
|
return errors.New(string(out))
|
2016-12-29 18:12:54 +02:00
|
|
|
}
|
2016-12-21 15:37:31 +02:00
|
|
|
return nil
|
|
|
|
}
|