-
Notifications
You must be signed in to change notification settings - Fork 120
/
build.go
465 lines (378 loc) · 9.36 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
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strings"
"text/template"
"time"
)
func main() {
flag.Parse()
args, target := flag.Args(), "peg"
if len(args) > 0 {
target = args[0]
}
switch target {
case "buildinfo":
buildinfo()
case "peg":
peg()
case "clean":
clean()
case "test":
test()
case "bench":
bench()
case "help":
fmt.Println("go run build.go [target]")
fmt.Println(" peg - build peg from scratch")
fmt.Println(" clean - clean up")
fmt.Println(" test - run full test")
fmt.Println(" bench - run benchmark")
fmt.Println(" buildinfo - generate buildinfo.go")
}
}
const BuildinfoTemplate = `// Code Generated by "build.go buildinfo" DO NOT EDIT.
package main
const (
// VERSION is the version of peg
VERSION = "{{.Version}}"
// BUILDTIME is the build time of peg
BUILDTIME = "{{.Buildtime}}"
// COMMIT is the commit hash of peg
COMMIT = "{{.Commit}}"
// IS_TAGGED is there a version
IS_TAGGED = {{.IsTagged}}
)
`
func buildinfo() {
log.SetPrefix("buildinfo:")
type info struct {
Version string
Buildtime string
Commit string
IsTagged bool
}
infFile, err := os.Create("buildinfo.go")
defer infFile.Close()
if err != nil {
log.Println("open buildinfo.go: fatal:", err)
}
var inf info = info{
Version: "unknown", // show this if we can't get the version
}
vers, err := exec.Command("git", "tag", "--contains").Output()
if err != nil {
log.Println("error:", err)
} else if len(vers) > 1 { // ignore any single newlines that might exist
inf.IsTagged = true
inf.Version = strings.TrimSuffix(string(vers), "\n")
} else {
vers, err = exec.Command("git", "tag", "--merged", "--sort=v:refname").Output()
if err != nil {
log.Println("error:", err)
} else if len(vers) > 1 {
tags := strings.Split(string(vers), "\n")
inf.Version = tags[len(tags)-1]
}
}
cmit, err := exec.Command("git", "rev-parse", "HEAD").Output()
if err != nil {
log.Println("error:", err)
}
inf.Commit = strings.TrimSuffix(string(cmit), "\n")
// slice the constant to remove the timezone specifier
inf.Buildtime = time.Now().UTC().Format(time.RFC3339[0:19])
err = template.Must(template.New("buildinfo").Parse(BuildinfoTemplate)).Execute(infFile, inf)
if err != nil {
log.Println("error: template:", err)
}
log.SetPrefix("")
}
var processed = make(map[string]bool)
func done(file string, deps ...interface{}) bool {
fini := true
file = filepath.FromSlash(file)
info, err := os.Stat(file)
if err != nil {
fini = false
}
for _, dep := range deps {
switch dep := dep.(type) {
case string:
if info == nil {
fini = false
break
}
dep = filepath.FromSlash(dep)
fileInfo, err := os.Stat(dep)
if err != nil {
panic(err)
}
if fileInfo.ModTime().After(info.ModTime()) {
fini = false
}
case func() bool:
name := runtime.FuncForPC(reflect.ValueOf(dep).Pointer()).Name()
if result, ok := processed[name]; ok {
fini = fini && result
fmt.Printf("%s is done\n", name)
break
}
result := dep()
fini = fini && result
fmt.Printf("%s\n", name)
processed[name] = result
}
}
return fini
}
func chdir(dir string) string {
dir = filepath.FromSlash(dir)
working, err := os.Getwd()
if err != nil {
panic(err)
}
err = os.Chdir(dir)
if err != nil {
panic(err)
}
fmt.Printf("cd %s\n", dir)
return working
}
func command(name, inputFile, outputFile string, arg ...string) {
name = filepath.FromSlash(name)
inputFile = filepath.FromSlash(inputFile)
outputFile = filepath.FromSlash(outputFile)
fmt.Print(name)
for _, a := range arg {
fmt.Printf(" %s", a)
}
cmd := exec.Command(name, arg...)
if inputFile != "" {
fmt.Printf(" < %s", inputFile)
input, err := os.ReadFile(inputFile)
if err != nil {
panic(err)
}
writer, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
go func() {
defer writer.Close()
_, err := writer.Write([]byte(input))
if err != nil {
panic(err)
}
}()
}
if outputFile != "" {
fmt.Printf(" > %s\n", outputFile)
output, err := cmd.Output()
if err != nil {
panic(err)
}
err = os.WriteFile(outputFile, output, 0600)
if err != nil {
panic(err)
}
} else {
output, err := cmd.CombinedOutput()
fmt.Printf("\n%s", string(output))
if err != nil {
panic(err)
}
}
}
func delete(file string) {
file = filepath.FromSlash(file)
fmt.Printf("rm -f %s\n", file)
os.Remove(file)
}
func deleteFilesWithSuffix(suffix string) {
files, err := os.ReadDir(".")
if err != nil {
panic(err)
}
for _, file := range files {
if strings.HasSuffix(file.Name(), suffix) {
delete(file.Name())
}
}
}
func bootstrap() bool {
if done("bootstrap/bootstrap", "bootstrap/main.go", "tree/peg.go") {
return true
}
wd := chdir("bootstrap")
defer chdir(wd)
command("go", "", "", "build")
return false
}
func peg0() bool {
if done("cmd/peg-bootstrap/peg0", "cmd/peg-bootstrap/main.go", bootstrap) {
return true
}
wd := chdir("cmd/peg-bootstrap/")
defer chdir(wd)
deleteFilesWithSuffix(".peg.go")
command("../../bootstrap/bootstrap", "", "")
command("go", "", "", "build", "-tags", "bootstrap", "-o", "peg0")
return false
}
func peg1() bool {
if done("cmd/peg-bootstrap/peg1", peg0, "cmd/peg-bootstrap/bootstrap.peg") {
return true
}
wd := chdir("cmd/peg-bootstrap/")
defer chdir(wd)
deleteFilesWithSuffix(".peg.go")
command("./peg0", "bootstrap.peg", "peg1.peg.go")
command("go", "", "", "build", "-tags", "bootstrap", "-o", "peg1")
return false
}
func peg2() bool {
if done("cmd/peg-bootstrap/peg2", peg1, "cmd/peg-bootstrap/peg.bootstrap.peg") {
return true
}
wd := chdir("cmd/peg-bootstrap/")
defer chdir(wd)
deleteFilesWithSuffix(".peg.go")
command("./peg1", "peg.bootstrap.peg", "peg2.peg.go")
command("go", "", "", "build", "-tags", "bootstrap", "-o", "peg2")
return false
}
func peg3() bool {
if done("cmd/peg-bootstrap/peg3", peg2, "peg.peg") {
return true
}
wd := chdir("cmd/peg-bootstrap/")
defer chdir(wd)
deleteFilesWithSuffix(".peg.go")
command("./peg2", "../../peg.peg", "peg3.peg.go")
command("go", "", "", "build", "-tags", "bootstrap", "-o", "peg3")
return false
}
func peg_bootstrap() bool {
if done("cmd/peg-bootstrap/peg-bootstrap", peg3) {
return true
}
wd := chdir("cmd/peg-bootstrap/")
defer chdir(wd)
deleteFilesWithSuffix(".peg.go")
command("./peg3", "../../peg.peg", "peg-bootstrap.peg.go")
command("go", "", "", "build", "-tags", "bootstrap", "-o", "peg-bootstrap")
return false
}
func peg_peg_go() bool {
if done("peg.peg.go", peg_bootstrap) {
return true
}
command("cmd/peg-bootstrap/peg-bootstrap", "peg.peg", "peg.peg.go")
command("go", "", "", "build")
command("./peg", "", "", "-inline", "-switch", "peg.peg")
return false
}
func peg() bool {
if done("peg", peg_peg_go, "main.go") {
return true
}
command("go", "", "", "build")
return false
}
func clean() bool {
delete("bootstrap/bootstrap")
delete("grammars/c/c.peg.go")
delete("grammars/calculator/calculator.peg.go")
delete("grammars/fexl/fexl.peg.go")
delete("grammars/java/java_1_7.peg.go")
delete("grammars/long_test/long.peg.go")
wd := chdir("cmd/peg-bootstrap/")
defer chdir(wd)
deleteFilesWithSuffix(".peg.go")
delete("peg0")
delete("peg1")
delete("peg2")
delete("peg3")
delete("peg-bootstrap")
return false
}
func grammars_c() bool {
if done("grammars/c/c.peg.go", peg, "grammars/c/c.peg") {
return true
}
wd := chdir("grammars/c/")
defer chdir(wd)
command("../../peg", "", "", "-switch", "-inline", "c.peg")
return false
}
func grammars_calculator() bool {
if done("grammars/calculator/calculator.peg.go", peg, "grammars/calculator/calculator.peg") {
return true
}
wd := chdir("grammars/calculator/")
defer chdir(wd)
command("../../peg", "", "", "-switch", "-inline", "calculator.peg")
return false
}
func grammars_calculator_ast() bool {
if done("grammars/calculator_ast/calculator.peg.go", peg, "grammars/calculator_ast/calculator.peg") {
return true
}
wd := chdir("grammars/calculator_ast/")
defer chdir(wd)
command("../../peg", "", "", "-switch", "-inline", "calculator.peg")
return false
}
func grammars_fexl() bool {
if done("grammars/fexl/fexl.peg.go", peg, "grammars/fexl/fexl.peg") {
return true
}
wd := chdir("grammars/fexl/")
defer chdir(wd)
command("../../peg", "", "", "-switch", "-inline", "fexl.peg")
return false
}
func grammars_java() bool {
if done("grammars/java/java_1_7.peg.go", peg, "grammars/java/java_1_7.peg") {
return true
}
wd := chdir("grammars/java/")
defer chdir(wd)
command("../../peg", "", "", "-switch", "-inline", "java_1_7.peg")
return false
}
func grammars_long_test() bool {
if done("grammars/long_test/long.peg.go", peg, "grammars/long_test/long.peg") {
return true
}
wd := chdir("grammars/long_test/")
defer chdir(wd)
command("../../peg", "", "", "-switch", "-inline", "long.peg")
return false
}
func test() bool {
if done("", grammars_c, grammars_calculator, grammars_calculator_ast,
grammars_fexl, grammars_java, grammars_long_test) {
return true
}
command("go", "", "", "test", "-short", "-tags", "grammars", "./...")
return false
}
func bench() bool {
peg()
command("go", "", "", "test", "-benchmem", "-bench", ".")
return false
}