/
main.go
334 lines (284 loc) · 7.6 KB
/
main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
)
var opts = struct {
Verbose bool
SourceDir string
OutputDir string
Tags string
PlatformSubset string
Platform string
SkipCompress bool
Version string
}{}
func init() {
pflag.BoolVarP(&opts.Verbose, "verbose", "v", false, "be verbose")
pflag.StringVarP(&opts.SourceDir, "source", "s", "/restic", "path to the source code `directory`")
pflag.StringVarP(&opts.OutputDir, "output", "o", "/output", "path to the output `directory`")
pflag.StringVar(&opts.Tags, "tags", "", "additional build `tags`")
pflag.StringVar(&opts.PlatformSubset, "platform-subset", "", "specify `n/t` to only build this subset")
pflag.StringVarP(&opts.Platform, "platform", "p", "", "specify `os/arch` to only build this specific platform")
pflag.BoolVar(&opts.SkipCompress, "skip-compress", false, "skip binary compression step")
pflag.StringVar(&opts.Version, "version", "", "use `x.y.z` as the version for output files")
pflag.Parse()
}
func die(f string, args ...interface{}) {
if !strings.HasSuffix(f, "\n") {
f += "\n"
}
f = "\x1b[31m" + f + "\x1b[0m"
fmt.Fprintf(os.Stderr, f, args...)
os.Exit(1)
}
func msg(f string, args ...interface{}) {
if !strings.HasSuffix(f, "\n") {
f += "\n"
}
f = "\x1b[32m" + f + "\x1b[0m"
fmt.Printf(f, args...)
}
func verbose(f string, args ...interface{}) {
if !opts.Verbose {
return
}
if !strings.HasSuffix(f, "\n") {
f += "\n"
}
f = "\x1b[32m" + f + "\x1b[0m"
fmt.Printf(f, args...)
}
func rm(file string) {
err := os.Remove(file)
if os.IsNotExist(err) {
err = nil
}
if err != nil {
die("error removing %v: %v", file, err)
}
}
func mkdir(dir string) {
err := os.MkdirAll(dir, 0755)
if err != nil {
die("mkdir %v: %v", dir, err)
}
}
func abs(dir string) string {
absDir, err := filepath.Abs(dir)
if err != nil {
die("unable to find absolute path for %v: %v", dir, err)
}
return absDir
}
func build(sourceDir, outputDir, goos, goarch string) (filename string) {
filename = fmt.Sprintf("%v_%v_%v", "restic", goos, goarch)
if opts.Version != "" {
filename = fmt.Sprintf("%v_%v_%v_%v", "restic", opts.Version, goos, goarch)
}
if goos == "windows" {
filename += ".exe"
}
outputFile := filepath.Join(outputDir, filename)
tags := "selfupdate"
if opts.Tags != "" {
tags += "," + opts.Tags
}
c := exec.Command("go", "build",
"-o", outputFile,
"-ldflags", "-s -w",
"-tags", tags,
"./cmd/restic",
)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Dir = sourceDir
c.Env = append(os.Environ(),
"CGO_ENABLED=0",
"GOOS="+goos,
"GOARCH="+goarch,
)
if goarch == "arm" {
c.Env = append(c.Env, "GOARM=5")
}
verbose("run %v %v in %v", "go", c.Args, c.Dir)
err := c.Run()
if err != nil {
die("error building %v/%v: %v", goos, goarch, err)
}
return filename
}
func modTime(file string) time.Time {
fi, err := os.Lstat(file)
if err != nil {
die("unable to get modtime of %v: %v", file, err)
}
return fi.ModTime()
}
func touch(file string, t time.Time) {
err := os.Chtimes(file, t, t)
if err != nil {
die("unable to update timestamps for %v: %v", file, err)
}
}
func chmod(file string, mode os.FileMode) {
err := os.Chmod(file, mode)
if err != nil {
die("unable to chmod %v to %s: %v", file, mode, err)
}
}
func compress(goos, inputDir, filename string) (outputFile string) {
var c *exec.Cmd
switch goos {
case "windows":
outputFile = strings.TrimSuffix(filename, ".exe") + ".zip"
c = exec.Command("zip", "-q", "-X", outputFile, filename)
default:
outputFile = filename + ".bz2"
c = exec.Command("bzip2", filename)
}
rm(filepath.Join(inputDir, outputFile))
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Dir = inputDir
verbose("run %v %v in %v", "go", c.Args, c.Dir)
err := c.Run()
if err != nil {
die("error compressing: %v", err)
}
rm(filepath.Join(inputDir, filename))
return outputFile
}
func buildForTarget(sourceDir, outputDir, goos, goarch string) (filename string) {
mtime := modTime(filepath.Join(sourceDir, "VERSION"))
filename = build(sourceDir, outputDir, goos, goarch)
touch(filepath.Join(outputDir, filename), mtime)
chmod(filepath.Join(outputDir, filename), 0755)
if !opts.SkipCompress {
filename = compress(goos, outputDir, filename)
}
return filename
}
func buildTargets(sourceDir, outputDir string, targets map[string][]string) {
start := time.Now()
// the go compiler is already parallelized, thus reduce the concurrency a bit
workers := runtime.GOMAXPROCS(0) / 4
if workers < 1 {
workers = 1
}
msg("building with %d workers", workers)
type Job struct{ GOOS, GOARCH string }
var wg errgroup.Group
ch := make(chan Job)
for i := 0; i < workers; i++ {
wg.Go(func() error {
for job := range ch {
start := time.Now()
verbose("build %v/%v", job.GOOS, job.GOARCH)
buildForTarget(sourceDir, outputDir, job.GOOS, job.GOARCH)
msg("built %v/%v in %.3fs", job.GOOS, job.GOARCH, time.Since(start).Seconds())
}
return nil
})
}
wg.Go(func() error {
for goos, archs := range targets {
for _, goarch := range archs {
ch <- Job{goos, goarch}
}
}
close(ch)
return nil
})
_ = wg.Wait()
msg("build finished in %.3fs", time.Since(start).Seconds())
}
var defaultBuildTargets = map[string][]string{
"aix": {"ppc64"},
"darwin": {"amd64", "arm64"},
"freebsd": {"386", "amd64", "arm"},
"linux": {"386", "amd64", "arm", "arm64", "ppc64le", "mips", "mipsle", "mips64", "mips64le", "riscv64", "s390x"},
"netbsd": {"386", "amd64"},
"openbsd": {"386", "amd64"},
"windows": {"386", "amd64"},
"solaris": {"amd64"},
}
func downloadModules(sourceDir string) {
c := exec.Command("go", "mod", "download")
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Dir = sourceDir
err := c.Run()
if err != nil {
die("error downloading modules: %v", err)
}
}
func selectSubset(subset string, target map[string][]string) (map[string][]string, error) {
t, n, _ := strings.Cut(subset, "/")
part, err := strconv.ParseInt(t, 10, 8)
if err != nil {
return nil, fmt.Errorf("failed to parse platform subset %q", subset)
}
total, err := strconv.ParseInt(n, 10, 8)
if err != nil {
return nil, fmt.Errorf("failed to parse platform subset %q", subset)
}
if total < 0 || part < 0 {
return nil, errors.New("platform subset out of range")
}
if part >= total {
return nil, errors.New("t must be in 0 <= t < n")
}
// flatten platform list
platforms := []string{}
for os, archs := range target {
for _, arch := range archs {
platforms = append(platforms, os+"/"+arch)
}
}
sort.Strings(platforms)
// select subset
lower := len(platforms) * int(part) / int(total)
upper := len(platforms) * int(part+1) / int(total)
platforms = platforms[lower:upper]
return buildPlatformList(platforms), nil
}
func buildPlatformList(platforms []string) map[string][]string {
fmt.Printf("Building for %v\n", platforms)
targets := make(map[string][]string)
for _, platform := range platforms {
os, arch, _ := strings.Cut(platform, "/")
targets[os] = append(targets[os], arch)
}
return targets
}
func main() {
if len(pflag.Args()) != 0 {
die("USAGE: build-release-binaries [OPTIONS]")
}
targets := defaultBuildTargets
if opts.PlatformSubset != "" {
var err error
targets, err = selectSubset(opts.PlatformSubset, targets)
if err != nil {
die("%s", err)
}
} else if opts.Platform != "" {
targets = buildPlatformList([]string{opts.Platform})
}
sourceDir := abs(opts.SourceDir)
outputDir := abs(opts.OutputDir)
mkdir(outputDir)
downloadModules(sourceDir)
buildTargets(sourceDir, outputDir, targets)
}