-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.go
549 lines (484 loc) · 12.7 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//go:build mage
// This file is used by mage to build the project.
package main
import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
mgtarget "github.com/magefile/mage/target"
)
const (
GO = "GO"
CC = "CC"
CXX = "CXX"
CLANG_FORMAT = "CLANG_FORMAT"
EH_FRAME_BIN = "EH_FRAME_BIN"
)
var (
Default = All
// toolBinaries is a map of tool ENV vars to their default binary.
// If the ENV var is not set, the default binary will be used.
// If the default is NOT a path, it will be looked up in the PATH.
toolBinaries = map[string]string{
GO: mg.GoCmd(),
CC: "zig cc",
CXX: "zig c++",
CLANG_FORMAT: "clang-format",
}
// goToolBinaries is a map of go tool ENV vars to their default binary.
// It will be run with `go run`.
// e.g go run github.com/parca-dev/parca-agent/tree/main/cmd/eh-frame@latest
goToolBinaries = map[string]string{
EH_FRAME_BIN: "github.com/parca-dev/parca-agent/cmd/eh-frame@latest",
}
host = targetFromGoArch(runtime.GOARCH)
)
type Target struct {
goarch string
goos string
cTarget string
}
var targets = []Target{
{
goarch: "amd64",
goos: "linux",
cTarget: "x86_64-linux-gnu",
},
{
goarch: "arm64",
goos: "linux",
cTarget: "aarch64-linux-gnu",
},
}
func targetFromGoArch(goArch string) Target {
for _, t := range targets {
if t.goarch == goArch {
return t
}
}
panic(fmt.Errorf("unknown go arch %s", goArch))
}
func (t Target) String() string {
return fmt.Sprintf("%s (%s) on (%s)", t.goarch, t.cTarget, host)
}
func (t Target) CrossCCCmd() string {
return fmt.Sprintf("%s -target %s", bin(CC), t.cTarget)
}
func (t Target) CrossCXXCmd() string {
return fmt.Sprintf("%s -target %s", bin(CXX), t.cTarget)
}
func (t Target) GoCmd() string {
return fmt.Sprintf("%s -goarch %s -goos %s", bin(GO), t.goarch, t.goos)
}
const (
out = "out"
vendored = "vendored"
tables = "tables"
defaultTables = "default"
finalTables = "final"
compactTables = "compact"
)
// fullOutPath returns the full path to the output binary.
func fullOutPath(arch, name string) string {
return path.Join(out, arch, name)
}
// fullTablesPath returns the full path to the output tables.
func fullTablesPath(arch, name string) string {
return path.Join(tables, arch, name)
}
var Directories = []string{
out,
tables,
}
// All runs all the targets.
func All() error {
var err error
var format Format
err = errors.Join(err, format.All())
var build Build
err = errors.Join(err, build.All())
var generate Generate
err = errors.Join(err, generate.All())
return err
}
const (
goGlob = "./src/*.go"
cppGlob = "./src/*.cpp"
)
// Format runs all the formatters.
type Format mg.Namespace
// Go runs gofmt on all the Go code.
func (Format) Go() error {
matches, err := filepath.Glob(goGlob)
if err != nil {
return err
}
return sh.Run(bin(GO), "fmt", strings.Join(matches, " "))
}
// Cpp runs clang-format on all the C code.
func (Format) Cpp() error {
matches, err := filepath.Glob(cppGlob)
if err != nil {
return err
}
args := []string{"-i"}
args = append(args, matches...)
return sh.Run(bin(CLANG_FORMAT), args...)
}
// All runs all the linters.
// clang-format for C code.
// gofmt for Go code.
func (Format) All() error {
mg.Deps(Format.Go, Format.Cpp)
return nil
}
// Build runs all the builders.
type Build mg.Namespace
// Build builds all the binaries.
func (Build) All() error {
mg.Deps(Build.Go, Build.Cpp)
return nil
}
// Variant is a build variant.
type Variant struct {
name string
src string
flags []string
}
var goBuildVariants = []Variant{
{
name: "basic-go",
src: "./src/main.go",
flags: nil,
},
}
// Go builds all the Go binaries.
func (Build) Go() error {
mg.Deps(dirs)
var errs error
for _, vr := range goBuildVariants {
if err := buildGo(host, vr.src, vr.name, vr.flags...); err != nil {
errs = errors.Join(errs, err)
}
}
for _, vr := range cppBuildVariantsArm64 {
if err := buildCpp(host, vr.src, vr.name, vr.flags...); err != nil {
errs = errors.Join(errs, err)
}
}
for _, vr := range cppBuildVariantsX86 {
if err := buildCpp(host, vr.src, vr.name, vr.flags...); err != nil {
errs = errors.Join(errs, err)
}
}
return errs
}
var cppBuildVariantsCommon = []Variant{
{
name: "basic-cpp",
src: "./src/basic-cpp.cpp",
flags: nil,
},
{
name: "basic-cpp-with-debuginfo",
src: "./src/basic-cpp.cpp",
flags: []string{"-g"},
},
{
name: "basic-cpp-no-fp",
src: "./src/basic-cpp.cpp",
flags: []string{"-fno-omit-frame-pointer"},
},
{
name: "basic-cpp-no-fp-with-debuginfo",
src: "./src/basic-cpp.cpp",
flags: []string{"-fno-omit-frame-pointer", "-g"},
},
{
name: "basic-cpp-no-pie-with-compressed-debuginfo",
src: "./src/basic-cpp.cpp",
flags: []string{"-fno-pie", "-fno-PIE", "-g", "-gz=zlib"},
},
{
name: "basic-cpp-no-pie-and-no-fp",
src: "./src/basic-cpp.cpp",
flags: []string{"-fno-pie", "-fno-PIE", "-fno-omit-frame-pointer"},
},
{
name: "basic-cpp-no-pie-and-no-fp-with-compressed-debuginfo",
src: "./src/basic-cpp.cpp",
flags: []string{"-fno-pie", "-fno-PIE", "-fno-omit-frame-pointer", "-g", "-gz=zlib"},
},
{
name: "basic-cpp-plt",
src: "./src/basic-cpp-plt.cpp",
flags: nil,
},
{
name: "basic-cpp-plt-with-debuginfo",
src: "./src/basic-cpp-plt.cpp",
flags: []string{"-g"},
},
{
name: "basic-cpp-plt-pie",
src: "./src/basic-cpp-plt.cpp",
flags: []string{"-fPIE", "-pie"},
},
{
name: "basic-cpp-plt-hardened",
src: "./src/basic-cpp-plt.cpp",
flags: []string{"-fPIE", "-pie", "-fstack-protector-all", "-D_FORTIFY_SOURCE=2", "-Wl,-z,now", "-Wl,-z,relro", "-O2"},
},
{
name: "basic-cpp-plt-no-pie",
src: "./src/basic-cpp-plt.cpp",
flags: []string{"-fno-pie", "-fno-PIE"},
},
{
name: "basic-cpp-multithreaded-no-fp",
src: "./src/basic-cpp-multithreaded.cpp",
flags: []string{"-fno-omit-frame-pointer"},
},
}
// TODO(sylfrena): Add Build() for this
var cppBuildVariantsX86 = []Variant{
// -c -g -gz=zlib
// The toy JIT binary is written for x86 and uses assembly, so is architecture specific
// Because this uses assembly, cross compiling will not work here.
{
name: "basic-cpp-jit",
src: "./src/basic-cpp-jit.cpp",
flags: []string{"-g"},
},
{
name: "basic-cpp-jit-no-fp",
src: "./src/basic-cpp-jit.cpp",
flags: []string{"-fno-omit-frame-pointer"},
},
}
// TODO(sylfrena): Add Build() for this
var cppBuildVariantsArm64 = []Variant{
// Variants with Pointer Authentication (only available for Arm64)
// This may not work with cross-compilation.
{
name: "basic-cpp-pac-leaf",
src: "./src/basic-cpp.cpp",
flags: []string{"-g", "-fno-PIE", "-no-pie", "-mbranch-protection=pac-ret+leaf"},
},
{
name: "basic-cpp-pac-bti",
src: "./src/basic-cpp.cpp",
flags: []string{"-g", "-fno-PIE", "-no-pie", "-mbranch-protection=pac-ret+bti"},
},
}
// Cpp builds all the C binaries.
func (Build) Cpp() error {
mg.Deps(dirs)
var errs error
for _, vr := range cppBuildVariantsCommon {
if err := buildCpp(host, vr.src, vr.name, vr.flags...); err != nil {
errs = errors.Join(errs, err)
}
}
for _, vr := range cppBuildVariantsArm64 {
if err := buildCpp(host, vr.src, vr.name, vr.flags...); err != nil {
errs = errors.Join(errs, err)
}
}
for _, vr := range cppBuildVariantsX86 {
if err := buildCpp(host, vr.src, vr.name, vr.flags...); err != nil {
errs = errors.Join(errs, err)
}
}
return errs
}
// Cross builds all the binaries for all the targets.
func (Build) Cross() error {
mg.Deps(dirs)
var errs error
for _, target := range targets {
log.Printf("Building for %s\n", target)
for _, vr := range goBuildVariants {
if err := buildGo(target, vr.src, vr.name, vr.flags...); err != nil {
errs = errors.Join(errs, err)
}
}
for _, vr := range cppBuildVariantsCommon {
if err := buildCpp(target, vr.src, vr.name, vr.flags...); err != nil {
errs = errors.Join(errs, err)
}
}
}
return errs
}
// buildCpp builds a C binary for the given target if it has changed.
func buildCpp(target Target, src, out string, flags ...string) error {
out = fullOutPath(target.goarch, out)
changed, err := mgtarget.Path(out, src)
if err != nil {
return err
}
if !changed {
return nil
}
return run(target.CrossCXXCmd(), append([]string{src, "-o", out}, flags...)...)
}
// buildGo builds a Go binary for the given target if it has changed.
func buildGo(target Target, src, out string, flags ...string) error {
out = fullOutPath(target.goarch, out)
changed, err := mgtarget.Path(out, src)
if err != nil {
return err
}
if !changed {
return nil
}
return sh.RunWith(map[string]string{
"GOARCH": target.goarch,
"GOOS": target.goos,
}, bin(GO), append([]string{"build", "-o", out, src}, flags...)...)
}
// Generate runs all the generators.
type Generate mg.Namespace
// All generates all the tables.
func (Generate) All() error {
mg.SerialDeps(Generate.Default, Generate.Final, Generate.Compact)
return nil
}
// generateTables generates all the tables for the given flags.
func generateTables(src, dst string, flags ...string) error {
for _, target := range targets {
err := filepath.Walk(path.Join(src, target.goarch), func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
outputFilePath := fullTablesPath(target.goarch, path.Join(dst, fmt.Sprintf("ours_%s.txt", info.Name())))
changed, err := mgtarget.Path(outputFilePath, p)
if err != nil {
return err
}
if !changed {
return nil
}
output, err := runOutGoTool(EH_FRAME_BIN, append([]string{"--executable", p}, flags...)...)
if err != nil {
return err
}
if err := os.MkdirAll(path.Dir(outputFilePath), 0755); err != nil {
return err
}
return os.WriteFile(outputFilePath, []byte(output), 0644)
})
if err != nil {
return err
}
}
return nil
}
// Default generates all the default tables.
func (Generate) Default() error {
var errs error
errs = errors.Join(errs, generateTables(out, defaultTables))
errs = errors.Join(errs, generateTables(vendored, defaultTables))
return errs
}
// Final generates all the final tables.
func (Generate) Final() error {
var errs error
errs = errors.Join(errs, generateTables(out, finalTables, "--final"))
errs = errors.Join(errs, generateTables(vendored, finalTables, "--final"))
return errs
}
// Compact generates all the compact tables.
func (Generate) Compact() error {
var errs error
errs = errors.Join(errs, generateTables(out, compactTables, "--compact"))
errs = errors.Join(errs, generateTables(vendored, compactTables, "--compact"))
return errs
}
// Clean cleans all the generated files and binaries.
func Clean() error {
for _, dir := range Directories {
if err := os.RemoveAll(dir); err != nil {
return err
}
}
return nil
}
// Env prints the environment variables.
func Env() error {
for _, vr := range os.Environ() {
fmt.Println(vr)
}
return nil
}
// Helpers:
// dirs creates all the directories.
func dirs() error {
for _, target := range targets {
for _, dir := range Directories {
if err := os.MkdirAll(path.Join(dir, target.goarch), 0755); err != nil {
return err
}
}
}
return nil
}
// getOrDefault returns the ENV var or the default binary.
func getOrDefault(key string) string {
if cmd := os.Getenv(key); cmd != "" {
return cmd
}
if val, ok := toolBinaries[key]; ok {
return val
}
panic(fmt.Errorf("no default binary for %s", key))
}
// bin returns the full path to the binary.
func bin(cmd string) string {
exe := getOrDefault(cmd)
if strings.HasPrefix(exe, "./") || strings.HasPrefix(exe, "../") || strings.HasPrefix(exe, "/") {
if _, err := os.Stat(exe); os.IsNotExist(err) {
panic(fmt.Sprintf("binary %s does not exist: %s", cmd, exe))
}
}
parts := strings.Split(exe, " ")
if len(parts) > 1 {
exe = parts[0]
}
if _, err := exec.LookPath(exe); err != nil {
panic(fmt.Sprintf("binary %s does not exist in PATH: %s", cmd, exe))
}
return strings.Join(parts, " ")
}
// run runs the command with the given args.
func run(cmd string, args ...string) error {
parts := strings.Split(cmd, " ")
if len(parts) > 1 {
return sh.Run(parts[0], append(parts[1:], args...)...)
}
return sh.Run(cmd, args...)
}
var (
goRun = sh.RunCmd(bin(GO), "run")
goRunOut = sh.OutCmd(bin(GO), "run")
)
// runGoTool runs the go tool with the given args.
func runGoTool(cmd string, args ...string) error {
return goRun(append([]string{goToolBinaries[cmd]}, args...)...)
}
// runOutGoTool runs the go tool with the given args and returns the output.
func runOutGoTool(cmd string, args ...string) (string, error) {
return goRunOut(append([]string{goToolBinaries[cmd]}, args...)...)
}