-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
219 lines (197 loc) · 4.51 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
//go:build !mgo_launcher
package main
import (
"bytes"
"context"
"embed"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"unicode"
"golang.org/x/mod/semver"
"golang.org/x/sync/errgroup"
)
func main() {
goos := os.Getenv("GOOS")
if goos == "" {
goos = runtime.GOOS
}
if goos != "linux" {
fmt.Printf("GOOS=%q is not supported\n", goos)
os.Exit(1)
}
goarch := os.Getenv("GOARCH")
if goarch == "" {
goarch = runtime.GOARCH
}
if goarch != "amd64" {
fmt.Printf("GOARCH=%q is not supported\n", goarch)
os.Exit(1)
}
if goamd64 := os.Getenv("GOAMD64"); goamd64 != "" {
fmt.Printf("GOAMD64 must not be set (currently %q)\n", goamd64)
os.Exit(1)
}
cmd := exec.Command("go")
cmd.Args = []string{"go", "version"}
buf, err := cmd.Output()
if err != nil {
fmt.Printf("fetching go version: %v\n", err)
os.Exit(2)
}
m := regexp.MustCompile(`^go version go([0-9]+\.[0-9]+(\.[0-9]+)?)`).FindSubmatch(buf)
if m == nil {
fmt.Printf("parsing go version: malformed: %q\n", string(buf))
os.Exit(2)
}
if semver.Compare("v"+string(m[1]), "v1.20") < 0 {
fmt.Printf("installed go version too old: %q\n", string(m[1]))
os.Exit(2)
}
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("getting cwd: %v\n", err)
os.Exit(2)
}
o := filepath.Base(cwd)
var args []string
for i := 1; i < len(os.Args); i++ {
if strings.TrimSpace(os.Args[i]) == "-o" && len(os.Args) > i+1 {
o = os.Args[i+1]
i++
} else if prefix, arg := "-o=", strings.TrimLeftFunc(os.Args[i], unicode.IsSpace); strings.HasPrefix(arg, prefix) {
o = arg[len(prefix):]
} else {
args = append(args, os.Args[i])
}
}
if !filepath.IsAbs(o) {
o = filepath.Join(cwd, o)
}
tmpdir, err := os.MkdirTemp("", "mgo")
if err != nil {
fmt.Printf("creating temp dir: %v\n", err)
os.Exit(2)
}
defer os.RemoveAll(tmpdir)
err = fs.WalkDir(launcherSource, ".", func(path string, d fs.DirEntry, err error) error {
dpath := filepath.Join(tmpdir, path)
switch {
case err != nil:
return err
case d.Type().IsRegular():
buf, err := fs.ReadFile(launcherSource, path)
if err != nil {
return fmt.Errorf("read file %q: %w", path, err)
}
err = os.WriteFile(dpath, buf, 0600)
if err != nil {
return fmt.Errorf("write file %q: %w", dpath, err)
}
case d.Type().IsDir():
err := os.MkdirAll(dpath, 0700)
if err != nil {
return fmt.Errorf("create dir %q: %w", dpath, err)
}
default:
return fmt.Errorf("unknown dir entry %q, type %q", path, d.Type())
}
return nil
})
if err != nil {
fmt.Printf("copying vendored dependencies: %v\n", err)
os.Exit(2)
}
stdout := &lwriter{Writer: os.Stdout}
stderr := &lwriter{Writer: os.Stderr}
np := runtime.NumCPU()
if pbi, _ := strconv.Atoi(os.Getenv("MGO_PARALLEL_BUILD")); pbi > 0 {
np = pbi
}
sema := make(chan struct{}, np)
eg, ctx := errgroup.WithContext(context.Background())
for _, v := range []string{"v1", "v2", "v3", "v4"} {
v := v
eg.Go(func() error {
sema <- struct{}{}
defer func() { <-sema }()
cmd := exec.CommandContext(ctx, "go")
cmd.Args = append([]string{"go", "build", "-o", filepath.Join(tmpdir, "mgo."+v)}, args...)
cmd.Env = append(os.Environ(), "GOAMD64="+v)
cmd.Stdout = &writer{prefix: []byte(v + ": "), w: stdout}
cmd.Stderr = &writer{prefix: []byte(v + ": "), w: stderr}
err := cmd.Run()
if err != nil {
return fmt.Errorf("building variant %q: %w", v, err)
}
return nil
})
}
err = eg.Wait()
if err != nil {
fmt.Println(err)
os.Exit(2)
}
cmd = exec.Command("go")
cmd.Args = []string{"go", "build", "-C", tmpdir, "-o", o, "-trimpath", "-tags", "mgo_launcher"}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Printf("building launcher: %v\n", err)
os.Exit(2)
}
}
//go:embed go.mod go.sum launcher.go
var launcherSource embed.FS
type writer struct {
prefix []byte
w *lwriter
buf bytes.Buffer
}
func (w *writer) Write(buf []byte) (int, error) {
w.w.Lock()
defer w.w.Unlock()
r, _ := w.buf.Write(buf)
for {
line, err := w.buf.ReadBytes('\n')
if err != nil {
if err == io.EOF {
return r, nil
}
return r, err
}
_, err = w.write(w.prefix)
if err != nil {
return r, err
}
_, err = w.write(line)
if err != nil {
return r, err
}
}
}
func (w *writer) write(buf []byte) (int, error) {
var r int
for len(buf) > 0 {
n, err := w.w.Write(buf)
r += n
if err != nil {
return r, err
}
buf = buf[n:]
}
return r, nil
}
type lwriter struct {
sync.Mutex
io.Writer
}