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 (
|
2019-06-09 12:14:30 -03:00
|
|
|
"fmt"
|
2018-01-26 19:26:28 -02:00
|
|
|
"os"
|
2017-03-25 21:29:38 -03:00
|
|
|
"path/filepath"
|
2023-02-09 08:56:37 -03:00
|
|
|
"reflect"
|
2017-01-21 20:02:51 -02:00
|
|
|
"strings"
|
2016-12-21 11:37:31 -02:00
|
|
|
|
2021-03-22 08:45:18 -03:00
|
|
|
"github.com/caarlos0/go-shellwords"
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2023-02-09 08:56:37 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/deprecate"
|
2019-05-07 07:18:35 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/ids"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/semerrgroup"
|
2021-11-23 15:53:12 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/shell"
|
2023-09-16 17:01:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/skips"
|
2018-08-14 23:50:20 -03:00
|
|
|
"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
|
|
|
|
2020-08-04 03:21:26 +00: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
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Pipe for build.
|
2016-12-30 09:27:35 -02:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 19:53:19 -02:00
|
|
|
func (Pipe) String() string {
|
|
|
|
return "building binaries"
|
2016-12-30 09:27:35 -02:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Run the pipe.
|
2017-01-14 14:06:57 -02:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
2022-06-29 00:31:33 -03:00
|
|
|
g := semerrgroup.New(ctx.Parallelism)
|
2017-06-27 19:20:08 -03:00
|
|
|
for _, build := range ctx.Config.Builds {
|
2020-04-02 15:18:05 +02:00
|
|
|
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")
|
2022-06-29 00:31:33 -03:00
|
|
|
runPipeOnBuild(ctx, g, build)
|
2017-06-27 19:20:08 -03:00
|
|
|
}
|
2022-06-29 00:31:33 -03:00
|
|
|
return g.Wait()
|
2017-06-27 19:20:08 -03:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Default sets the pipe defaults.
|
2017-12-02 19:53:19 -02:00
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2023-02-09 08:56:37 -03:00
|
|
|
if !reflect.DeepEqual(ctx.Config.SingleBuild, config.Build{}) {
|
|
|
|
deprecate.Notice(ctx, "build")
|
|
|
|
}
|
|
|
|
|
2021-03-22 08:45:18 -03:00
|
|
|
ids := ids.New("builds")
|
2017-12-02 19:53:19 -02:00
|
|
|
for i, build := range ctx.Config.Builds {
|
2020-11-05 04:20:14 -03:00
|
|
|
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)
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
|
|
|
if len(ctx.Config.Builds) == 0 {
|
2020-11-05 04:20:14 -03:00
|
|
|
build, err := buildWithDefaults(ctx, ctx.Config.SingleBuild)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
2020-11-05 04:20:14 -03:00
|
|
|
ctx.Config.Builds = []config.Build{build}
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
2019-05-07 07:18:35 -03:00
|
|
|
return ids.Validate()
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
|
|
|
|
2020-11-05 04:20:14 -03:00
|
|
|
func buildWithDefaults(ctx *context.Context, build config.Build) (config.Build, error) {
|
2021-09-11 13:01:57 -03:00
|
|
|
if build.Builder == "" {
|
|
|
|
build.Builder = "go"
|
2018-01-21 14:31:08 -02:00
|
|
|
}
|
2017-12-02 19:53:19 -02:00
|
|
|
if build.Binary == "" {
|
2018-08-16 14:25:02 -03:00
|
|
|
build.Binary = ctx.Config.ProjectName
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
2019-04-14 15:16:01 -03:00
|
|
|
if build.ID == "" {
|
2022-05-11 11:41:33 -03:00
|
|
|
build.ID = ctx.Config.ProjectName
|
2019-04-14 15:16:01 -03:00
|
|
|
}
|
2018-02-12 20:53:57 -02:00
|
|
|
for k, v := range build.Env {
|
|
|
|
build.Env[k] = os.ExpandEnv(v)
|
|
|
|
}
|
2021-09-11 13:01:57 -03:00
|
|
|
return builders.For(build.Builder).WithDefaults(build)
|
2017-12-02 19:53:19 -02:00
|
|
|
}
|
|
|
|
|
2022-06-29 00:31:33 -03:00
|
|
|
func runPipeOnBuild(ctx *context.Context, g semerrgroup.Group, build config.Build) {
|
2023-11-27 18:29:50 -03:00
|
|
|
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 {
|
2020-04-12 16:13:20 +01:00
|
|
|
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)
|
|
|
|
}
|
2020-04-12 16:13:20 +01:00
|
|
|
}
|
|
|
|
if err := doBuild(ctx, build, *opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-16 17:01:20 -03:00
|
|
|
if !skips.Any(ctx, skips.PostBuildHooks) {
|
2020-05-15 15:19:20 +01:00
|
|
|
if err := runHook(ctx, *opts, build.Env, build.Hooks.Post); err != nil {
|
2020-09-21 14:47:51 -03:00
|
|
|
return fmt.Errorf("post hook failed: %w", err)
|
2020-05-15 15:19:20 +01:00
|
|
|
}
|
2020-04-12 16:13:20 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-01 09:31:43 -03:00
|
|
|
func runHook(ctx *context.Context, opts builders.Options, buildEnv []string, hooks config.Hooks) error {
|
2020-04-12 16:13:20 +01:00
|
|
|
if len(hooks) == 0 {
|
2017-04-09 10:43:23 -03:00
|
|
|
return nil
|
2017-03-23 14:20:24 -03:00
|
|
|
}
|
2020-04-12 16:13:20 +01:00
|
|
|
|
|
|
|
for _, hook := range hooks {
|
|
|
|
var env []string
|
|
|
|
|
|
|
|
env = append(env, ctx.Env.Strings()...)
|
2022-11-25 15:26:29 -03:00
|
|
|
for _, rawEnv := range append(buildEnv, hook.Env...) {
|
2020-04-12 16:13:20 +01:00
|
|
|
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")
|
2020-04-12 14:12:53 -03:00
|
|
|
cmd, err := shellwords.Parse(sh)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-12 16:13:20 +01:00
|
|
|
|
2022-02-05 16:00:49 -03:00
|
|
|
if err := shell.Run(ctx, dir, cmd, env, hook.Output); err != nil {
|
2020-04-12 16:13:20 +01:00
|
|
|
return err
|
|
|
|
}
|
2019-04-14 15:16:20 -03:00
|
|
|
}
|
2020-04-12 16:13:20 +01:00
|
|
|
|
|
|
|
return nil
|
2016-12-29 14:12:54 -02:00
|
|
|
}
|
|
|
|
|
2020-04-12 16:13:20 +01:00
|
|
|
func doBuild(ctx *context.Context, build config.Build, opts builders.Options) error {
|
2021-09-11 13:01:57 -03:00
|
|
|
return builders.For(build.Builder).Build(ctx, build, opts)
|
2020-04-12 16:13:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildOptionsForTarget(ctx *context.Context, build config.Build, target string) (*builders.Options, error) {
|
2022-11-12 03:35:51 +01:00
|
|
|
ext := extFor(target, build.BuildDetails)
|
2021-09-11 13:01:57 -03:00
|
|
|
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]
|
2021-01-12 21:27:46 +01:00
|
|
|
|
2021-09-11 13:01:57 -03:00
|
|
|
var gomips string
|
|
|
|
var goarm string
|
2022-04-11 22:43:22 -03:00
|
|
|
var goamd64 string
|
2021-09-11 13:01:57 -03:00
|
|
|
if strings.HasPrefix(goarch, "arm") && len(parts) > 2 {
|
|
|
|
goarm = parts[2]
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(goarch, "mips") && len(parts) > 2 {
|
|
|
|
gomips = parts[2]
|
2021-01-12 21:27:46 +01:00
|
|
|
}
|
2022-04-11 22:43:22 -03:00
|
|
|
if strings.HasPrefix(goarch, "amd64") && len(parts) > 2 {
|
|
|
|
goamd64 = parts[2]
|
|
|
|
}
|
2021-01-12 21:27:46 +01:00
|
|
|
|
|
|
|
buildOpts := builders.Options{
|
2022-04-11 22:43:22 -03:00
|
|
|
Target: target,
|
|
|
|
Ext: ext,
|
|
|
|
Goos: goos,
|
|
|
|
Goarch: goarch,
|
|
|
|
Goarm: goarm,
|
|
|
|
Gomips: gomips,
|
|
|
|
Goamd64: goamd64,
|
2021-01-12 21:27:46 +01:00
|
|
|
}
|
2018-02-15 11:30:15 +00:00
|
|
|
|
2024-03-16 11:49:56 -03:00
|
|
|
bin, err := tmpl.New(ctx).WithBuildOptions(buildOpts).Apply(build.Binary)
|
|
|
|
if err != nil {
|
2020-04-12 16:13:20 +01:00
|
|
|
return nil, err
|
2018-02-15 11:30:15 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 11:49:56 -03:00
|
|
|
name := bin + ext
|
2021-06-06 19:44:24 -03:00
|
|
|
dir := fmt.Sprintf("%s_%s", build.ID, target)
|
|
|
|
if build.NoUniqueDistDir {
|
|
|
|
dir = ""
|
|
|
|
}
|
2022-02-05 17:44:07 -03:00
|
|
|
relpath := filepath.Join(ctx.Config.Dist, dir, name)
|
|
|
|
path, err := filepath.Abs(relpath)
|
2020-01-13 18:23:00 +01:00
|
|
|
if err != nil {
|
2020-04-12 16:13:20 +01:00
|
|
|
return nil, err
|
2020-01-13 18:23:00 +01:00
|
|
|
}
|
2021-09-11 13:01:57 -03:00
|
|
|
buildOpts.Path = path
|
2021-09-18 10:21:29 -03:00
|
|
|
buildOpts.Name = name
|
2020-07-23 20:46:43 +02:00
|
|
|
|
2022-02-05 17:44:07 -03:00
|
|
|
log.WithField("binary", relpath).Info("building")
|
2021-01-12 21:27:46 +01:00
|
|
|
return &buildOpts, nil
|
2016-12-21 11:37:31 -02:00
|
|
|
}
|
2018-01-21 22:44:06 -02:00
|
|
|
|
2022-11-12 03:35:51 +01: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.
|
2022-11-11 23:42:45 -03:00
|
|
|
switch build.Buildmode {
|
|
|
|
case "c-shared":
|
2022-11-12 03:35:51 +01:00
|
|
|
if strings.Contains(target, "darwin") {
|
|
|
|
return ".dylib"
|
|
|
|
}
|
|
|
|
if strings.Contains(target, "windows") {
|
|
|
|
return ".dll"
|
|
|
|
}
|
|
|
|
return ".so"
|
2022-11-11 23:42:45 -03:00
|
|
|
case "c-archive":
|
2022-11-12 03:35:51 +01:00
|
|
|
if strings.Contains(target, "windows") {
|
|
|
|
return ".lib"
|
|
|
|
}
|
|
|
|
return ".a"
|
|
|
|
}
|
|
|
|
|
2022-11-11 23:42:45 -03:00
|
|
|
if target == "js_wasm" {
|
|
|
|
return ".wasm"
|
|
|
|
}
|
|
|
|
|
2022-11-12 03:35:51 +01:00
|
|
|
if strings.Contains(target, "windows") {
|
|
|
|
return ".exe"
|
|
|
|
}
|
|
|
|
|
2018-01-21 22:44:06 -02:00
|
|
|
return ""
|
|
|
|
}
|