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-30 13:27:35 +02:00
|
|
|
"log"
|
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-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"
|
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-05-11 15:43:25 +02:00
|
|
|
if err := runHook(ctx.Config.Build.Env, ctx.Config.Build.Hooks.Pre); err != nil {
|
2017-04-09 15:43:23 +02:00
|
|
|
return err
|
2017-03-23 19:20:24 +02:00
|
|
|
}
|
2017-04-21 14:40:25 +02:00
|
|
|
sem := make(chan bool, 4)
|
2016-12-29 18:12:54 +02:00
|
|
|
var g errgroup.Group
|
2017-04-27 01:08:25 +02:00
|
|
|
for _, target := range buildTargets(ctx) {
|
2017-04-24 19:27:21 +02:00
|
|
|
name, err := nameFor(ctx, target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-12-21 15:37:31 +02:00
|
|
|
}
|
2017-04-24 19:27:21 +02: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 15:37:31 +02:00
|
|
|
}
|
2017-03-23 23:32:27 +02:00
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-11 15:43:25 +02:00
|
|
|
return runHook(ctx.Config.Build.Env, ctx.Config.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-04-09 15:44:28 +02:00
|
|
|
log.Println("Running hook", 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-04-24 19:27:21 +02:00
|
|
|
func build(ctx *context.Context, name string, target buildTarget) error {
|
2017-03-26 02:29:38 +02:00
|
|
|
output := filepath.Join(
|
2017-03-26 02:31:16 +02:00
|
|
|
ctx.Config.Dist,
|
2017-03-26 02:29:38 +02:00
|
|
|
name,
|
2017-05-11 05:05:51 +02:00
|
|
|
ctx.Config.Build.Binary+ext.For(target.goos),
|
2017-03-26 02:29:38 +02:00
|
|
|
)
|
2017-01-19 11:04:41 +02:00
|
|
|
log.Println("Building", output)
|
2017-02-22 14:25:43 +02:00
|
|
|
cmd := []string{"go", "build"}
|
|
|
|
if ctx.Config.Build.Flags != "" {
|
2017-02-23 14:27:54 +02:00
|
|
|
cmd = append(cmd, strings.Fields(ctx.Config.Build.Flags)...)
|
2017-02-22 14:25:43 +02:00
|
|
|
}
|
2017-03-26 01:24:38 +02:00
|
|
|
flags, err := ldflags(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd = append(cmd, "-ldflags="+flags, "-o", output, ctx.Config.Build.Main)
|
2017-05-11 15:47:03 +02:00
|
|
|
return run(target, cmd, ctx.Config.Build.Env)
|
2017-01-22 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 15:47:03 +02:00
|
|
|
func run(target buildTarget, command, env []string) error {
|
2017-01-22 00:02:51 +02:00
|
|
|
cmd := exec.Command(command[0], command[1:]...)
|
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-04-24 19:27:21 +02:00
|
|
|
cmd.Env = append(
|
|
|
|
cmd.Env,
|
|
|
|
"GOOS="+target.goos,
|
|
|
|
"GOARCH="+target.goarch,
|
|
|
|
"GOARM="+target.goarm,
|
|
|
|
)
|
2017-01-30 12:17:15 +02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
2017-06-01 07:31:38 +02:00
|
|
|
return fmt.Errorf("build failed: %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
|
|
|
|
}
|