1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/pipeline/build/build.go

121 lines
2.9 KiB
Go
Raw Normal View History

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"
"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"
2017-07-01 17:27:13 +02:00
"github.com/goreleaser/goreleaser/internal/name"
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 {
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
}
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
}
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
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-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-07-01 17:27:13 +02:00
func doBuild(ctx *context.Context, build config.Build, target buildTarget) error {
folder, err := name.For(ctx, target.goos, target.goarch, target.goarm)
if err != nil {
return err
}
2017-07-01 17:46:08 +02:00
ctx.AddFolder(target.String(), folder)
2017-07-01 17:27:13 +02:00
var binary = filepath.Join(
2017-03-26 02:31:16 +02:00
ctx.Config.Dist,
2017-07-01 17:27:13 +02:00
folder,
build.Binary+ext.For(target.goos),
2017-03-26 02:29:38 +02:00
)
if ctx.Config.Archive.Format == "binary" {
2017-07-03 05:57:39 +02:00
bin, err := name.ForBuild(ctx, build, target.goos, target.goarch, target.goarm)
if err != nil {
return err
}
binary = filepath.Join(
ctx.Config.Dist,
folder,
bin+ext.For(target.goos),
)
}
2017-07-01 17:27:13 +02:00
log.WithField("binary", binary).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-06-28 00:36:36 +02:00
flags, err := ldflags(ctx, build)
2017-03-26 01:24:38 +02:00
if err != nil {
return err
}
2017-07-01 17:27:13 +02:00
cmd = append(cmd, "-ldflags="+flags, "-o", binary, build.Main)
2017-06-28 00:20:08 +02:00
return run(target, cmd, build.Env)
}
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
}