2016-12-21 11:37:31 -02:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-12-29 14:12:54 -02:00
|
|
|
"errors"
|
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-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 {
|
2016-12-29 14:12:54 -02:00
|
|
|
var g errgroup.Group
|
2017-01-14 19:47:15 -02:00
|
|
|
for _, goos := range ctx.Config.Build.Goos {
|
|
|
|
for _, goarch := range ctx.Config.Build.Goarch {
|
2017-01-14 15:08:10 -02:00
|
|
|
goos := goos
|
|
|
|
goarch := goarch
|
|
|
|
name, err := nameFor(ctx, goos, goarch)
|
2017-01-14 12:34:22 -02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-14 15:08:10 -02:00
|
|
|
ctx.Archives[goos+goarch] = name
|
2016-12-29 14:12:54 -02:00
|
|
|
g.Go(func() error {
|
2017-01-14 15:08:10 -02:00
|
|
|
return build(name, goos, goarch, ctx)
|
2016-12-29 14:12:54 -02:00
|
|
|
})
|
2016-12-21 11:37:31 -02:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 14:12:54 -02:00
|
|
|
return g.Wait()
|
|
|
|
}
|
|
|
|
|
2017-01-14 15:08:10 -02:00
|
|
|
func build(name, goos, goarch string, ctx *context.Context) error {
|
2017-01-14 14:06:57 -02:00
|
|
|
ldflags := ctx.Config.Build.Ldflags + " -X main.version=" + ctx.Git.CurrentTag
|
2017-01-14 19:47:15 -02:00
|
|
|
output := "dist/" + name + "/" + ctx.Config.Build.BinaryName + extFor(goos)
|
2017-01-19 10:04:41 +01:00
|
|
|
log.Println("Building", output)
|
2016-12-29 14:12:54 -02:00
|
|
|
cmd := exec.Command(
|
|
|
|
"go",
|
|
|
|
"build",
|
2017-01-11 20:25:52 -02:00
|
|
|
"-ldflags="+ldflags,
|
|
|
|
"-o", output,
|
2017-01-14 14:06:57 -02:00
|
|
|
ctx.Config.Build.Main,
|
2016-12-29 14:12:54 -02:00
|
|
|
)
|
|
|
|
cmd.Env = append(
|
|
|
|
cmd.Env,
|
2017-01-14 15:08:10 -02:00
|
|
|
"GOOS="+goos,
|
|
|
|
"GOARCH="+goarch,
|
2016-12-29 14:12:54 -02:00
|
|
|
"GOROOT="+os.Getenv("GOROOT"),
|
|
|
|
"GOPATH="+os.Getenv("GOPATH"),
|
|
|
|
)
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stdout
|
2017-01-04 10:13:49 -02:00
|
|
|
if err := cmd.Run(); err != nil {
|
2016-12-29 14:12:54 -02:00
|
|
|
return errors.New(stdout.String())
|
|
|
|
}
|
2016-12-21 11:37:31 -02:00
|
|
|
return nil
|
|
|
|
}
|