2018-01-21 14:31:08 -02:00
|
|
|
// Package build needs to be documented
|
2016-12-21 11:37:31 -02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2017-03-25 21:29:38 -03:00
|
|
|
"path/filepath"
|
2017-01-21 20:02:51 -02:00
|
|
|
"strings"
|
2016-12-21 11:37:31 -02:00
|
|
|
|
2017-06-22 00:09:14 -03:00
|
|
|
"github.com/apex/log"
|
2017-12-17 15:24:49 -02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2018-01-21 14:31:08 -02:00
|
|
|
builders "github.com/goreleaser/goreleaser/build"
|
|
|
|
"github.com/goreleaser/goreleaser/build/buildtarget"
|
|
|
|
"github.com/goreleaser/goreleaser/build/ext"
|
2017-06-27 19:20:08 -03:00
|
|
|
"github.com/goreleaser/goreleaser/config"
|
2017-01-14 20:01:32 -02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2018-01-21 14:31:08 -02:00
|
|
|
|
|
|
|
// langs to init
|
|
|
|
_ "github.com/goreleaser/goreleaser/internal/builders/golang"
|
2016-12-21 11:37:31 -02:00
|
|
|
)
|
|
|
|
|
2016-12-30 12:41:59 -02:00
|
|
|
// Pipe for build
|
2016-12-30 09:27:35 -02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 19:53:19 -02:00
|
|
|
func (Pipe) String() string {
|
|
|
|
return "building binaries"
|
2016-12-30 09:27:35 -02:00
|
|
|
}
|
|
|
|
|
2016-12-30 12:41:59 -02:00
|
|
|
// Run the pipe
|
2017-01-14 14:06:57 -02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2017-06-27 19:20:08 -03:00
|
|
|
for _, build := range ctx.Config.Builds {
|
2017-06-27 19:36:36 -03:00
|
|
|
log.WithField("build", build).Debug("building")
|
2017-06-27 19:20:08 -03:00
|
|
|
if err := runPipeOnBuild(ctx, build); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-02 19: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 {
|
2018-01-21 14:31:08 -02:00
|
|
|
if build.Lang == "" {
|
|
|
|
build.Lang = "go"
|
|
|
|
}
|
2017-12-02 19:53:19 -02:00
|
|
|
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-27 19:20:08 -03:00
|
|
|
func runPipeOnBuild(ctx *context.Context, build config.Build) error {
|
2017-12-29 17:07:06 -02:00
|
|
|
if err := runHook(ctx, build.Env, build.Hooks.Pre); err != nil {
|
2017-11-18 14:14:39 -02:00
|
|
|
return errors.Wrap(err, "pre hook failed")
|
2017-03-23 14:20:24 -03:00
|
|
|
}
|
2017-07-15 16:49:52 -03:00
|
|
|
sem := make(chan bool, ctx.Parallelism)
|
2016-12-29 14:12:54 -02:00
|
|
|
var g errgroup.Group
|
2017-07-09 13:14:35 -03:00
|
|
|
for _, target := range buildtarget.All(build) {
|
2017-04-24 14:27:21 -03:00
|
|
|
sem <- true
|
|
|
|
target := target
|
2017-07-01 12:27:13 -03:00
|
|
|
build := build
|
2017-04-24 14:27:21 -03:00
|
|
|
g.Go(func() error {
|
|
|
|
defer func() {
|
|
|
|
<-sem
|
|
|
|
}()
|
2017-07-01 12:27:13 -03:00
|
|
|
return doBuild(ctx, build, target)
|
2017-04-24 14:27:21 -03:00
|
|
|
})
|
2016-12-21 11:37:31 -02:00
|
|
|
}
|
2017-03-23 18:32:27 -03:00
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-29 17:07:06 -02:00
|
|
|
return errors.Wrap(runHook(ctx, build.Env, build.Hooks.Post), "post hook failed")
|
2017-04-09 10:43:23 -03:00
|
|
|
}
|
|
|
|
|
2017-12-29 17:07:06 -02:00
|
|
|
func runHook(ctx *context.Context, env []string, hook string) error {
|
2017-04-09 10:43:23 -03:00
|
|
|
if hook == "" {
|
|
|
|
return nil
|
2017-03-23 14:20:24 -03:00
|
|
|
}
|
2017-06-22 10:47:34 -03:00
|
|
|
log.WithField("hook", hook).Info("running hook")
|
2017-04-09 10:43:23 -03:00
|
|
|
cmd := strings.Fields(hook)
|
2018-01-21 18:54:33 -02:00
|
|
|
return builders.Run(ctx, cmd, env)
|
2016-12-29 14:12:54 -02:00
|
|
|
}
|
|
|
|
|
2017-07-06 19:49:21 -03:00
|
|
|
func doBuild(ctx *context.Context, build config.Build, target buildtarget.Target) error {
|
2017-12-17 22:28:24 -02:00
|
|
|
var ext = ext.For(target)
|
|
|
|
var binaryName = build.Binary + ext
|
2017-12-17 15:24:49 -02:00
|
|
|
var binary = filepath.Join(ctx.Config.Dist, target.String(), binaryName)
|
2017-12-18 21:04:25 -02:00
|
|
|
log.WithField("binary", binary).Info("building")
|
2018-01-21 14:31:08 -02:00
|
|
|
return builders.For(build.Lang).Build(ctx, build, builders.Options{
|
|
|
|
Target: target,
|
2017-12-17 15:24:49 -02:00
|
|
|
Name: binaryName,
|
2018-01-21 14:31:08 -02:00
|
|
|
Path: binary,
|
|
|
|
Ext: ext,
|
2017-12-17 15:24:49 -02:00
|
|
|
})
|
2016-12-21 11:37:31 -02:00
|
|
|
}
|