2018-01-22 05:10:17 +02:00
|
|
|
// Package build provides a pipe that can build binaries for several
|
|
|
|
// languages.
|
2016-12-21 15:37:31 +02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2019-06-09 17:14:30 +02:00
|
|
|
"fmt"
|
2018-01-26 23:26:28 +02:00
|
|
|
"os"
|
|
|
|
"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"
|
2019-05-07 12:18:35 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/ids"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
|
|
|
builders "github.com/goreleaser/goreleaser/pkg/build"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2020-04-12 19:12:53 +02:00
|
|
|
"github.com/mattn/go-shellwords"
|
2019-04-14 20:16:01 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-01-21 18:31:08 +02:00
|
|
|
|
|
|
|
// langs to init
|
|
|
|
_ "github.com/goreleaser/goreleaser/internal/builders/golang"
|
2016-12-21 15:37:31 +02:00
|
|
|
)
|
|
|
|
|
2020-05-26 05:48:10 +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
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +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 {
|
2020-04-02 15:18:05 +02:00
|
|
|
if build.Skip {
|
|
|
|
log.WithField("id", build.ID).Info("skip is set")
|
|
|
|
continue
|
|
|
|
}
|
2017-06-28 00:36:36 +02:00
|
|
|
log.WithField("build", build).Debug("building")
|
2017-06-28 00:20:08 +02:00
|
|
|
if err := runPipeOnBuild(ctx, build); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-26 05:48:10 +02:00
|
|
|
// Default sets the pipe defaults.
|
2017-12-02 23:53:19 +02:00
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2019-07-27 15:55:56 +02:00
|
|
|
var ids = ids.New("builds")
|
2017-12-02 23:53:19 +02:00
|
|
|
for i, build := range ctx.Config.Builds {
|
|
|
|
ctx.Config.Builds[i] = buildWithDefaults(ctx, build)
|
2019-05-07 12:18:35 +02:00
|
|
|
ids.Inc(ctx.Config.Builds[i].ID)
|
2017-12-02 23:53:19 +02:00
|
|
|
}
|
|
|
|
if len(ctx.Config.Builds) == 0 {
|
|
|
|
ctx.Config.Builds = []config.Build{
|
|
|
|
buildWithDefaults(ctx, ctx.Config.SingleBuild),
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 12:18:35 +02:00
|
|
|
return ids.Validate()
|
2017-12-02 23:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildWithDefaults(ctx *context.Context, build config.Build) config.Build {
|
2018-01-21 18:31:08 +02:00
|
|
|
if build.Lang == "" {
|
|
|
|
build.Lang = "go"
|
|
|
|
}
|
2017-12-02 23:53:19 +02:00
|
|
|
if build.Binary == "" {
|
2018-08-16 19:25:02 +02:00
|
|
|
build.Binary = ctx.Config.ProjectName
|
2017-12-02 23:53:19 +02:00
|
|
|
}
|
2019-04-14 20:16:01 +02:00
|
|
|
if build.ID == "" {
|
2019-06-28 13:52:10 +02:00
|
|
|
build.ID = ctx.Config.ProjectName
|
2019-04-14 20:16:01 +02:00
|
|
|
}
|
2018-02-13 00:53:57 +02:00
|
|
|
for k, v := range build.Env {
|
|
|
|
build.Env[k] = os.ExpandEnv(v)
|
|
|
|
}
|
2018-01-26 23:35:12 +02:00
|
|
|
return builders.For(build.Lang).WithDefaults(build)
|
2017-12-02 23:53:19 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 00:20:08 +02:00
|
|
|
func runPipeOnBuild(ctx *context.Context, build config.Build) error {
|
2018-07-10 06:38:00 +02:00
|
|
|
var g = semerrgroup.New(ctx.Parallelism)
|
2018-01-22 02:44:06 +02:00
|
|
|
for _, target := range build.Targets {
|
2017-04-24 19:27:21 +02:00
|
|
|
target := target
|
2017-07-01 17:27:13 +02:00
|
|
|
build := build
|
2017-04-24 19:27:21 +02:00
|
|
|
g.Go(func() error {
|
2020-04-12 17:13:20 +02:00
|
|
|
opts, err := buildOptionsForTarget(ctx, build, target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runHook(ctx, *opts, build.Env, build.Hooks.Pre); err != nil {
|
|
|
|
return errors.Wrap(err, "pre hook failed")
|
|
|
|
}
|
|
|
|
if err := doBuild(ctx, build, *opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-15 16:19:20 +02:00
|
|
|
if !ctx.SkipPostBuildHooks {
|
|
|
|
if err := runHook(ctx, *opts, build.Env, build.Hooks.Post); err != nil {
|
|
|
|
return errors.Wrap(err, "post hook failed")
|
|
|
|
}
|
2020-04-12 17:13:20 +02:00
|
|
|
}
|
|
|
|
return nil
|
2017-04-24 19:27:21 +02:00
|
|
|
})
|
2016-12-21 15:37:31 +02:00
|
|
|
}
|
2020-04-12 17:13:20 +02:00
|
|
|
|
|
|
|
return g.Wait()
|
2017-04-09 15:43:23 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 17:13:20 +02:00
|
|
|
func runHook(ctx *context.Context, opts builders.Options, buildEnv []string, hooks config.BuildHooks) error {
|
|
|
|
if len(hooks) == 0 {
|
2017-04-09 15:43:23 +02:00
|
|
|
return nil
|
2017-03-23 19:20:24 +02:00
|
|
|
}
|
2020-04-12 17:13:20 +02:00
|
|
|
|
|
|
|
for _, hook := range hooks {
|
|
|
|
var env []string
|
|
|
|
|
|
|
|
env = append(env, ctx.Env.Strings()...)
|
|
|
|
env = append(env, buildEnv...)
|
|
|
|
|
|
|
|
for _, rawEnv := range hook.Env {
|
|
|
|
e, err := tmpl.New(ctx).WithBuildOptions(opts).Apply(rawEnv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
env = append(env, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := tmpl.New(ctx).WithBuildOptions(opts).Apply(hook.Dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sh, err := tmpl.New(ctx).WithBuildOptions(opts).
|
|
|
|
WithEnvS(env).
|
|
|
|
Apply(hook.Cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("hook", sh).Info("running hook")
|
2020-04-12 19:12:53 +02:00
|
|
|
cmd, err := shellwords.Parse(sh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-12 17:13:20 +02:00
|
|
|
|
|
|
|
if err := run(ctx, dir, cmd, env); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-14 20:16:20 +02:00
|
|
|
}
|
2020-04-12 17:13:20 +02:00
|
|
|
|
|
|
|
return nil
|
2016-12-29 18:12:54 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 17:13:20 +02:00
|
|
|
func doBuild(ctx *context.Context, build config.Build, opts builders.Options) error {
|
|
|
|
return builders.For(build.Lang).Build(ctx, build, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildOptionsForTarget(ctx *context.Context, build config.Build, target string) (*builders.Options, error) {
|
2019-11-15 15:43:40 +02:00
|
|
|
var ext = extFor(target, build.Flags)
|
2018-02-15 13:30:15 +02:00
|
|
|
|
2018-07-09 08:08:06 +02:00
|
|
|
binary, err := tmpl.New(ctx).Apply(build.Binary)
|
2018-02-15 13:30:15 +02:00
|
|
|
if err != nil {
|
2020-04-12 17:13:20 +02:00
|
|
|
return nil, err
|
2018-02-15 13:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
build.Binary = binary
|
2018-01-22 02:44:06 +02:00
|
|
|
var name = build.Binary + ext
|
2020-01-13 19:23:00 +02:00
|
|
|
path, err := filepath.Abs(
|
|
|
|
filepath.Join(
|
|
|
|
ctx.Config.Dist,
|
|
|
|
fmt.Sprintf("%s_%s", build.ID, target),
|
|
|
|
name,
|
|
|
|
),
|
2019-06-09 17:14:30 +02:00
|
|
|
)
|
2020-01-13 19:23:00 +02:00
|
|
|
if err != nil {
|
2020-04-12 17:13:20 +02:00
|
|
|
return nil, err
|
2020-01-13 19:23:00 +02:00
|
|
|
}
|
2020-07-23 20:46:43 +02:00
|
|
|
|
|
|
|
var goos string
|
|
|
|
var goarch string
|
|
|
|
|
|
|
|
if strings.Contains(target, "_") {
|
|
|
|
goos = strings.Split(target, "_")[0]
|
|
|
|
goarch = strings.Split(target, "_")[1]
|
|
|
|
}
|
|
|
|
|
2018-01-22 02:44:06 +02:00
|
|
|
log.WithField("binary", path).Info("building")
|
2020-04-12 17:13:20 +02:00
|
|
|
return &builders.Options{
|
2018-01-21 18:31:08 +02:00
|
|
|
Target: target,
|
2018-01-22 02:44:06 +02:00
|
|
|
Name: name,
|
|
|
|
Path: path,
|
2018-01-21 18:31:08 +02:00
|
|
|
Ext: ext,
|
2020-07-23 20:46:43 +02:00
|
|
|
Os: goos,
|
|
|
|
Arch: goarch,
|
2020-04-12 17:13:20 +02:00
|
|
|
}, nil
|
2016-12-21 15:37:31 +02:00
|
|
|
}
|
2018-01-22 02:44:06 +02:00
|
|
|
|
2019-11-15 15:43:40 +02:00
|
|
|
func extFor(target string, flags config.FlagArray) string {
|
2018-01-22 02:44:06 +02:00
|
|
|
if strings.Contains(target, "windows") {
|
2019-11-15 15:43:40 +02:00
|
|
|
for _, s := range flags {
|
|
|
|
if s == "-buildmode=c-shared" {
|
|
|
|
return ".dll"
|
|
|
|
}
|
|
|
|
if s == "-buildmode=c-archive" {
|
|
|
|
return ".lib"
|
|
|
|
}
|
|
|
|
}
|
2018-01-22 02:44:06 +02:00
|
|
|
return ".exe"
|
|
|
|
}
|
2019-10-01 21:00:44 +02:00
|
|
|
if target == "js_wasm" {
|
|
|
|
return ".wasm"
|
|
|
|
}
|
2018-01-22 02:44:06 +02:00
|
|
|
return ""
|
|
|
|
}
|
2018-01-26 23:26:28 +02:00
|
|
|
|
2020-04-12 17:13:20 +02:00
|
|
|
func run(ctx *context.Context, dir string, command, env []string) error {
|
2018-01-26 23:26:28 +02:00
|
|
|
/* #nosec */
|
|
|
|
var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
|
|
|
|
var log = log.WithField("env", env).WithField("cmd", command)
|
2019-04-09 14:15:05 +02:00
|
|
|
cmd.Env = env
|
2020-04-12 17:13:20 +02:00
|
|
|
if dir != "" {
|
|
|
|
cmd.Dir = dir
|
|
|
|
}
|
2019-04-09 14:15:05 +02:00
|
|
|
log.Debug("running")
|
2018-01-26 23:26:28 +02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
log.WithError(err).Debug("failed")
|
2020-04-12 15:44:12 +02:00
|
|
|
return errors.Wrapf(err, "%q", string(out))
|
2018-01-26 23:26:28 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|