2017-04-14 15:39:32 -03:00
|
|
|
// Package build implements Pipe and can build Go projects for
|
|
|
|
// several platforms, with pre and post hook support.
|
2016-12-21 11:37:31 -02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2017-05-10 10:20:07 -03:00
|
|
|
"fmt"
|
2016-12-30 09:27:35 -02:00
|
|
|
"log"
|
2016-12-21 14:42:23 -02:00
|
|
|
"os"
|
2016-12-21 11:37:31 -02:00
|
|
|
"os/exec"
|
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-01-14 20:01:32 -02:00
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2016-12-29 14:12:54 -02:00
|
|
|
"golang.org/x/sync/errgroup"
|
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-01-14 19:41:32 +01:00
|
|
|
// Description of the pipe
|
2017-01-14 15:14:35 -02:00
|
|
|
func (Pipe) Description() string {
|
2017-01-19 10:04:41 +01:00
|
|
|
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-04-09 10:43:23 -03:00
|
|
|
if err := runHook(ctx.Config.Build.Hooks.Pre); err != nil {
|
|
|
|
return err
|
2017-03-23 14:20:24 -03:00
|
|
|
}
|
2017-04-21 14:40:25 +02:00
|
|
|
sem := make(chan bool, 4)
|
2016-12-29 14:12:54 -02:00
|
|
|
var g errgroup.Group
|
2017-04-26 20:08:25 -03:00
|
|
|
for _, target := range buildTargets(ctx) {
|
2017-04-24 14:27:21 -03:00
|
|
|
name, err := nameFor(ctx, target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-12-21 11:37:31 -02:00
|
|
|
}
|
2017-04-24 14:27:21 -03:00
|
|
|
ctx.Archives[target.String()] = name
|
|
|
|
|
|
|
|
sem <- true
|
|
|
|
target := target
|
|
|
|
g.Go(func() error {
|
|
|
|
defer func() {
|
|
|
|
<-sem
|
|
|
|
}()
|
|
|
|
return build(ctx, name, target)
|
|
|
|
})
|
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-04-09 10:43:23 -03:00
|
|
|
return runHook(ctx.Config.Build.Hooks.Post)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runHook(hook string) error {
|
|
|
|
if hook == "" {
|
|
|
|
return nil
|
2017-03-23 14:20:24 -03:00
|
|
|
}
|
2017-04-09 10:44:28 -03:00
|
|
|
log.Println("Running hook", hook)
|
2017-04-09 10:43:23 -03:00
|
|
|
cmd := strings.Fields(hook)
|
2017-04-24 14:27:21 -03:00
|
|
|
return run(runtimeTarget, cmd)
|
2016-12-29 14:12:54 -02:00
|
|
|
}
|
|
|
|
|
2017-04-24 14:27:21 -03:00
|
|
|
func build(ctx *context.Context, name string, target buildTarget) error {
|
2017-03-25 21:29:38 -03:00
|
|
|
output := filepath.Join(
|
2017-03-25 21:31:16 -03:00
|
|
|
ctx.Config.Dist,
|
2017-03-25 21:29:38 -03:00
|
|
|
name,
|
2017-04-24 14:27:21 -03:00
|
|
|
ctx.Config.Build.Binary+extFor(target.goos),
|
2017-03-25 21:29:38 -03:00
|
|
|
)
|
2017-01-19 10:04:41 +01:00
|
|
|
log.Println("Building", output)
|
2017-02-22 09:25:43 -03:00
|
|
|
cmd := []string{"go", "build"}
|
|
|
|
if ctx.Config.Build.Flags != "" {
|
2017-02-23 09:27:54 -03:00
|
|
|
cmd = append(cmd, strings.Fields(ctx.Config.Build.Flags)...)
|
2017-02-22 09:25:43 -03:00
|
|
|
}
|
2017-03-25 20:24:38 -03:00
|
|
|
flags, err := ldflags(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = append(cmd, "-ldflags="+flags, "-o", output, ctx.Config.Build.Main)
|
2017-04-24 14:27:21 -03:00
|
|
|
return run(target, cmd)
|
2017-01-21 20:02:51 -02:00
|
|
|
}
|
|
|
|
|
2017-04-24 14:27:21 -03:00
|
|
|
func run(target buildTarget, command []string) error {
|
2017-01-21 20:02:51 -02:00
|
|
|
cmd := exec.Command(command[0], command[1:]...)
|
2017-01-23 09:27:28 -02:00
|
|
|
cmd.Env = append(cmd.Env, os.Environ()...)
|
2017-04-24 14:27:21 -03:00
|
|
|
cmd.Env = append(
|
|
|
|
cmd.Env,
|
|
|
|
"GOOS="+target.goos,
|
|
|
|
"GOARCH="+target.goarch,
|
|
|
|
"GOARM="+target.goarm,
|
|
|
|
)
|
2017-01-30 08:17:15 -02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
2017-05-10 10:20:07 -03:00
|
|
|
return fmt.Errorf("build failed: %v\n%v", target, string(out))
|
2016-12-29 14:12:54 -02:00
|
|
|
}
|
2016-12-21 11:37:31 -02:00
|
|
|
return nil
|
|
|
|
}
|