-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmagefile.go
More file actions
272 lines (226 loc) · 6.2 KB
/
magefile.go
File metadata and controls
272 lines (226 loc) · 6.2 KB
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
// +build mage
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Config holds the setup variables required for a build
type Config struct {
OS string // GOOS
Arch string // GOOS
EnableDebug bool
Env map[string]string
EnableCGo bool
}
// BeforeBuildCallback hooks into the build process
type BeforeBuildCallback func(cfg Config) (Config, error)
// Callbacks give you a way to run custom behavior when things happen
var beforeBuild = func(cfg Config) (Config, error) {
return cfg, nil
}
// SetBeforeBuildCallback configures a custom callback
func SetBeforeBuildCallback(cb BeforeBuildCallback) error {
beforeBuild = cb
return nil
}
var exname string
func getExecutableName(os string, arch string) (string, error) {
if exname == "" {
exename, err := getExecutable()
if err != nil {
return "", err
}
exname = exename
}
exeName := fmt.Sprintf("%s_%s_%s", exname, os, arch)
if os == "windows" {
exeName = fmt.Sprintf("%s.exe", exeName)
}
return exeName, nil
}
func getExecutable() (string, error) {
return "go-irisnative", nil
}
func buildBackend(cfg Config) (exeName string, err error) {
cfg, err = beforeBuild(cfg)
if err != nil {
return
}
exeName, err = getExecutableName(cfg.OS, cfg.Arch)
if err != nil {
return
}
ldFlags := ""
if !cfg.EnableCGo {
// Link statically
ldFlags = `-extldflags "-static"`
}
if !cfg.EnableDebug {
// Add linker flags to drop debug information
prefix := ""
if ldFlags != "" {
prefix = " "
}
ldFlags = fmt.Sprintf("-w -s%s%s", prefix, ldFlags)
}
args := []string{
"build", "-o", filepath.Join("dist", exeName),
}
if ldFlags != "" {
args = append(args, "-ldflags", ldFlags)
}
if cfg.EnableDebug {
args = append(args, "-gcflags=all=-N -l")
}
args = append(args, "./src")
cfg.Env["GOARCH"] = cfg.Arch
cfg.Env["GOOS"] = cfg.OS
if !cfg.EnableCGo {
cfg.Env["CGO_ENABLED"] = "0"
}
// TODO: Change to sh.RunWithV once available.
err = sh.RunWith(cfg.Env, "go", args...)
return
}
func newBuildConfig(os string, arch string) Config {
return Config{
OS: os,
Arch: arch,
EnableDebug: false,
Env: map[string]string{},
}
}
// Build is a namespace.
type Build mg.Namespace
// Linux builds the back-end plugin for Linux.
func (Build) Linux() (string, error) {
return buildBackend(newBuildConfig("linux", "amd64"))
}
// LinuxARM builds the back-end plugin for Linux on ARM.
func (Build) LinuxARM() (string, error) {
return buildBackend(newBuildConfig("linux", "arm"))
}
// LinuxARM64 builds the back-end plugin for Linux on ARM64.
func (Build) LinuxARM64() (string, error) {
return buildBackend(newBuildConfig("linux", "arm64"))
}
// Windows builds the back-end plugin for Windows.
func (Build) Windows() (string, error) {
return buildBackend(newBuildConfig("windows", "amd64"))
}
// Darwin builds the back-end plugin for OSX.
func (Build) Darwin() (string, error) {
return buildBackend(newBuildConfig("darwin", "amd64"))
}
// DarwinARM64 builds the back-end plugin for OSX on ARM (M1).
func (Build) DarwinARM64() (string, error) {
return buildBackend(newBuildConfig("darwin", "arm64"))
}
// Debug builds the debug version for the current platform
func (Build) Debug() (string, error) {
cfg := newBuildConfig(runtime.GOOS, runtime.GOARCH)
cfg.EnableDebug = true
return buildBackend(cfg)
}
// Backend build a production build for the current platform
func (Build) Backend() (string, error) {
cfg := newBuildConfig(runtime.GOOS, runtime.GOARCH)
return buildBackend(cfg)
}
// BuildAll builds production executables for all supported platforms.
func BuildAll() { //revive:disable-line
b := Build{}
mg.Deps(b.Linux, b.Windows, b.Darwin, b.DarwinARM64, b.LinuxARM64, b.LinuxARM)
}
// Test runs backend tests.
func Test() error {
if err := sh.RunV("go", "test", "./src/...", "-v"); err != nil {
return err
}
return nil
}
// Coverage runs backend tests and makes a coverage report.
func Coverage() error {
// Create a coverage file if it does not already exist
if err := os.MkdirAll(filepath.Join(".", "coverage"), os.ModePerm); err != nil {
return err
}
if err := sh.RunV("go", "test", "./src/...", "-v", "-cover", "-coverprofile=coverage/backend.out"); err != nil {
return err
}
if err := sh.RunV("go", "tool", "cover", "-html=coverage/backend.out", "-o", "coverage/backend.html"); err != nil {
return err
}
return nil
}
// Lint audits the source style
func Lint() error {
return sh.RunV("golangci-lint", "run", "./...")
}
// Format formats the sources.
func Format() error {
if err := sh.RunV("gofmt", "-w", "."); err != nil {
return err
}
return nil
}
// Clean cleans build artifacts, by deleting the dist directory.
func Clean() error {
err := os.RemoveAll("dist")
if err != nil {
return err
}
err = os.RemoveAll("coverage")
if err != nil {
return err
}
err = os.RemoveAll("ci")
if err != nil {
return err
}
return nil
}
// checkLinuxPtraceScope verifies that ptrace is configured as required.
func checkLinuxPtraceScope() error {
ptracePath := "/proc/sys/kernel/yama/ptrace_scope"
byteValue, err := ioutil.ReadFile(ptracePath)
if err != nil {
return fmt.Errorf("unable to read ptrace_scope: %w", err)
}
val := strings.TrimSpace(string(byteValue))
if val != "0" {
log.Printf("WARNING:")
fmt.Printf("ptrace_scope set to value other than 0 (currently: %s), this might prevent debugger from connecting\n", val)
fmt.Printf("try writing \"0\" to %s\n", ptracePath)
fmt.Printf("Set ptrace_scope to 0? y/N (default N)\n")
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
if scanner.Text() == "y" || scanner.Text() == "Y" {
// if err := sh.RunV("echo", "0", "|", "sudo", "tee", ptracePath); err != nil {
// return // Error?
// }
log.Printf("TODO, run: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope")
} else {
fmt.Printf("Did not write\n")
}
}
}
return nil
}
// Run runs executable
func Run(what string) error {
mainPath := filepath.Join("example", what, "main.go")
if _, err := os.Stat(mainPath); os.IsNotExist(err) {
fmt.Printf("Example `%s` not found\n", what)
return nil
}
return sh.RunV("go", "run", mainPath)
}