/
build.go
240 lines (211 loc) · 5.33 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Package build provides a pipe that can build binaries for several
// languages.
package build
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/logext"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"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"
"github.com/mattn/go-shellwords"
// langs to init.
_ "github.com/goreleaser/goreleaser/internal/builders/golang"
)
// Pipe for build.
type Pipe struct{}
func (Pipe) String() string {
return "building binaries"
}
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
for _, build := range ctx.Config.Builds {
if build.Skip {
log.WithField("id", build.ID).Info("skip is set")
continue
}
log.WithField("build", build).Debug("building")
if err := runPipeOnBuild(ctx, build); err != nil {
return err
}
}
return nil
}
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
var 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
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}
}
return ids.Validate()
}
func buildWithDefaults(ctx *context.Context, build config.Build) (config.Build, error) {
if build.Lang == "" {
build.Lang = "go"
}
if build.Binary == "" {
build.Binary = ctx.Config.ProjectName
}
if build.ID == "" {
build.ID = ctx.Config.ProjectName
}
for k, v := range build.Env {
build.Env[k] = os.ExpandEnv(v)
}
return builders.For(build.Lang).WithDefaults(build)
}
func runPipeOnBuild(ctx *context.Context, build config.Build) error {
var g = semerrgroup.New(ctx.Parallelism)
for _, target := range build.Targets {
target := target
build := build
g.Go(func() error {
opts, err := buildOptionsForTarget(ctx, build, target)
if err != nil {
return err
}
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 !ctx.SkipPostBuildHooks {
if err := runHook(ctx, *opts, build.Env, build.Hooks.Post); err != nil {
return fmt.Errorf("post hook failed: %w", err)
}
}
return nil
})
}
return g.Wait()
}
func runHook(ctx *context.Context, opts builders.Options, buildEnv []string, hooks config.BuildHooks) error {
if len(hooks) == 0 {
return nil
}
for _, hook := range hooks {
var env []string
env = append(env, ctx.Env.Strings()...)
env = append(env, buildEnv...)
for _, rawEnv := range 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 := run(ctx, dir, cmd, env); err != nil {
return err
}
}
return nil
}
func doBuild(ctx *context.Context, build config.Build, opts builders.Options) error {
return builders.For(build.Lang).Build(ctx, build, opts)
}
func buildOptionsForTarget(ctx *context.Context, build config.Build, target string) (*builders.Options, error) {
var ext = extFor(target, build.Flags)
binary, err := tmpl.New(ctx).Apply(build.Binary)
if err != nil {
return nil, err
}
build.Binary = binary
var name = build.Binary + ext
path, err := filepath.Abs(
filepath.Join(
ctx.Config.Dist,
fmt.Sprintf("%s_%s", build.ID, target),
name,
),
)
if err != nil {
return nil, err
}
var goos string
var goarch string
if strings.Contains(target, "_") {
goos = strings.Split(target, "_")[0]
goarch = strings.Split(target, "_")[1]
}
log.WithField("binary", path).Info("building")
return &builders.Options{
Target: target,
Name: name,
Path: path,
Ext: ext,
Os: goos,
Arch: goarch,
}, nil
}
func extFor(target string, flags config.FlagArray) string {
if strings.Contains(target, "windows") {
for _, s := range flags {
if s == "-buildmode=c-shared" {
return ".dll"
}
if s == "-buildmode=c-archive" {
return ".lib"
}
}
return ".exe"
}
if target == "js_wasm" {
return ".wasm"
}
return ""
}
func run(ctx *context.Context, dir string, command, env []string) error {
/* #nosec */
var cmd = exec.CommandContext(ctx, command[0], command[1:]...)
var entry = log.WithField("cmd", command)
cmd.Env = env
var b bytes.Buffer
cmd.Stderr = io.MultiWriter(logext.NewErrWriter(entry), &b)
cmd.Stdout = io.MultiWriter(logext.NewWriter(entry), &b)
if dir != "" {
cmd.Dir = dir
}
entry.WithField("env", env).Debug("running")
if err := cmd.Run(); err != nil {
entry.WithError(err).Debug("failed")
return fmt.Errorf("%q: %w", b.String(), err)
}
return nil
}