1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

249 lines
5.8 KiB
Go
Raw Normal View History

2018-01-22 01:10:17 -02:00
// Package build provides a pipe that can build binaries for several
// languages.
2016-12-21 11:37:31 -02:00
package build
import (
"fmt"
2018-01-26 19:26:28 -02:00
"os"
2017-03-25 21:29:38 -03:00
"path/filepath"
"reflect"
"strings"
2016-12-21 11:37:31 -02:00
"github.com/caarlos0/go-shellwords"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/deprecate"
2019-05-07 07:18:35 -03:00
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/shell"
"github.com/goreleaser/goreleaser/internal/skips"
"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"
2018-01-21 14:31:08 -02:00
// langs to init.
2018-01-21 14:31:08 -02:00
_ "github.com/goreleaser/goreleaser/internal/builders/golang"
2016-12-21 11:37:31 -02:00
)
// Pipe for build.
2016-12-30 09:27:35 -02:00
type Pipe struct{}
func (Pipe) String() string {
return "building binaries"
2016-12-30 09:27:35 -02:00
}
// Run the pipe.
2017-01-14 14:06:57 -02:00
func (Pipe) Run(ctx *context.Context) error {
g := semerrgroup.New(ctx.Parallelism)
2017-06-27 19:20:08 -03:00
for _, build := range ctx.Config.Builds {
if build.Skip {
log.WithField("id", build.ID).Info("skip is set")
continue
}
2017-06-27 19:36:36 -03:00
log.WithField("build", build).Debug("building")
runPipeOnBuild(ctx, g, build)
2017-06-27 19:20:08 -03:00
}
return g.Wait()
2017-06-27 19:20:08 -03:00
}
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
if !reflect.DeepEqual(ctx.Config.SingleBuild, config.Build{}) {
deprecate.Notice(ctx, "build")
}
ids := ids.New("builds")
for i, build := range ctx.Config.Builds {
build, err := buildWithDefaults(ctx, build)
if err != nil {
return err
}
ctx.Config.Builds[i] = build
2019-05-07 07:18:35 -03:00
ids.Inc(ctx.Config.Builds[i].ID)
}
if len(ctx.Config.Builds) == 0 {
build, err := buildWithDefaults(ctx, ctx.Config.SingleBuild)
if err != nil {
return err
}
ctx.Config.Builds = []config.Build{build}
}
2019-05-07 07:18:35 -03:00
return ids.Validate()
}
func buildWithDefaults(ctx *context.Context, build config.Build) (config.Build, error) {
if build.Builder == "" {
build.Builder = "go"
2018-01-21 14:31:08 -02:00
}
if build.Binary == "" {
2018-08-16 14:25:02 -03:00
build.Binary = ctx.Config.ProjectName
}
if build.ID == "" {
build.ID = ctx.Config.ProjectName
}
2018-02-12 20:53:57 -02:00
for k, v := range build.Env {
build.Env[k] = os.ExpandEnv(v)
}
return builders.For(build.Builder).WithDefaults(build)
}
func runPipeOnBuild(ctx *context.Context, g semerrgroup.Group, build config.Build) {
for _, target := range filter(ctx, build.Targets) {
2017-04-24 14:27:21 -03:00
target := target
2017-07-01 12:27:13 -03:00
build := build
2017-04-24 14:27:21 -03:00
g.Go(func() error {
opts, err := buildOptionsForTarget(ctx, build, target)
if err != nil {
return err
}
2023-09-17 16:59:34 +00:00
if !skips.Any(ctx, skips.PreBuildHooks) {
if err := runHook(ctx, *opts, build.Env, build.Hooks.Pre); err != nil {
return fmt.Errorf("pre hook failed: %w", err)
}
}
if err := doBuild(ctx, build, *opts); err != nil {
return err
}
if !skips.Any(ctx, skips.PostBuildHooks) {
if err := runHook(ctx, *opts, build.Env, build.Hooks.Post); err != nil {
return fmt.Errorf("post hook failed: %w", err)
}
}
return nil
2017-04-24 14:27:21 -03:00
})
2016-12-21 11:37:31 -02:00
}
2017-04-09 10:43:23 -03:00
}
func runHook(ctx *context.Context, opts builders.Options, buildEnv []string, hooks config.Hooks) error {
if len(hooks) == 0 {
2017-04-09 10:43:23 -03:00
return nil
2017-03-23 14:20:24 -03:00
}
for _, hook := range hooks {
var env []string
env = append(env, ctx.Env.Strings()...)
for _, rawEnv := range append(buildEnv, 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")
cmd, err := shellwords.Parse(sh)
if err != nil {
return err
}
if err := shell.Run(ctx, dir, cmd, env, hook.Output); err != nil {
return err
}
}
return nil
2016-12-29 14:12:54 -02:00
}
func doBuild(ctx *context.Context, build config.Build, opts builders.Options) error {
return builders.For(build.Builder).Build(ctx, build, opts)
}
func buildOptionsForTarget(ctx *context.Context, build config.Build, target string) (*builders.Options, error) {
ext := extFor(target, build.BuildDetails)
parts := strings.Split(target, "_")
if len(parts) < 2 {
return nil, fmt.Errorf("%s is not a valid build target", target)
}
goos := parts[0]
goarch := parts[1]
var gomips string
var goarm string
var goamd64 string
if strings.HasPrefix(goarch, "arm") && len(parts) > 2 {
goarm = parts[2]
}
if strings.HasPrefix(goarch, "mips") && len(parts) > 2 {
gomips = parts[2]
}
if strings.HasPrefix(goarch, "amd64") && len(parts) > 2 {
goamd64 = parts[2]
}
buildOpts := builders.Options{
Target: target,
Ext: ext,
Goos: goos,
Goarch: goarch,
Goarm: goarm,
Gomips: gomips,
Goamd64: goamd64,
}
bin, err := tmpl.New(ctx).WithBuildOptions(buildOpts).Apply(build.Binary)
if err != nil {
return nil, err
}
name := bin + ext
dir := fmt.Sprintf("%s_%s", build.ID, target)
if build.NoUniqueDistDir {
dir = ""
}
relpath := filepath.Join(ctx.Config.Dist, dir, name)
path, err := filepath.Abs(relpath)
if err != nil {
return nil, err
}
buildOpts.Path = path
feat: improve output and pipe skipping (#2480) * refactor: improve middleware Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: upload tests Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: twitter tests Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: source tests Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: snapshot tests Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * test: improved some tests Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: snapcraft skip Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip slack Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip sign Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip scoop Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip reddit Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip discord Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip publish Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip nfpm Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip milestone Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip custompublishers Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip checksums Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip changelog Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip brew Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip blob Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip before Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip artifactory Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip announce Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip defaults Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: cmds Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip docker Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * chore: todo Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: go.mod Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip release Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: remove old skip pipe errors Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip teams Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip brew Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix/test: skip smtp Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: lint issues Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip docker Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip brew and scoop Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip docker Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: skip http/artifactory Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * test: increase coverage Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * test: fix Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
2021-09-18 10:21:29 -03:00
buildOpts.Name = name
log.WithField("binary", relpath).Info("building")
return &buildOpts, nil
2016-12-21 11:37:31 -02:00
}
2018-01-21 22:44:06 -02:00
func extFor(target string, build config.BuildDetails) string {
// Configure the extensions for shared and static libraries - by default .so and .a respectively -
// with overrides for Windows (.dll for shared and .lib for static) and .dylib for macOS.
switch build.Buildmode {
case "c-shared":
if strings.Contains(target, "darwin") {
return ".dylib"
}
if strings.Contains(target, "windows") {
return ".dll"
}
return ".so"
case "c-archive":
if strings.Contains(target, "windows") {
return ".lib"
}
return ".a"
}
if target == "js_wasm" {
return ".wasm"
}
if strings.Contains(target, "windows") {
return ".exe"
}
2018-01-21 22:44:06 -02:00
return ""
}