forked from dcjones/mk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mk.go
431 lines (377 loc) · 10.4 KB
/
mk.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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/mattn/go-isatty"
)
var (
// True if messages should be printed with fancy colors.
// By default, if the output stream is not the terminal, colors are disabled.
color bool
// Default shell to use if none specified via $shell.
defaultShell string
// Do not drop shell arguments when calling with no further arguments
// This works around `sh -c commands...` being a thing, but allows the `rc -v commands...` argument-less flags
dontDropArgs bool
// True if we are ignoring timestamps and rebuilding everything.
rebuildall bool = false
// Set of targets for which we are forcing rebuild
rebuildtargets map[string]bool = make(map[string]bool)
// Lock on standard out, messages don't get interleaved too much.
mkMsgMutex sync.Mutex
// Limit the number of recipes executed simultaneously.
subprocsAllowed int
// Current subprocesses being executed
subprocsRunning int
// Wakeup on a free subprocess slot.
subprocsRunningCond *sync.Cond = sync.NewCond(&sync.Mutex{})
// Prevent more than one recipe at a time from trying to take over
exclusiveSubproc = sync.Mutex{}
// The maximum number of times an rule may be applied.
// This limits recursion of both meta- and non-meta-rules!
// Maybe, this shouldn't affect meta-rules?!
maxRuleCnt int = 1
)
// Wait until there is an available subprocess slot.
func reserveSubproc() {
subprocsRunningCond.L.Lock()
for subprocsRunning >= subprocsAllowed {
subprocsRunningCond.Wait()
}
subprocsRunning++
subprocsRunningCond.L.Unlock()
}
// Free up another subprocess to run.
func finishSubproc() {
subprocsRunningCond.L.Lock()
subprocsRunning--
subprocsRunningCond.Signal()
subprocsRunningCond.L.Unlock()
}
// Make everyone wait while we
func reserveExclusiveSubproc() {
exclusiveSubproc.Lock()
// Wait until everything is done running
stolenSubprocs := 0
subprocsRunningCond.L.Lock()
stolenSubprocs = subprocsAllowed - subprocsRunning
subprocsRunning = subprocsAllowed
for stolenSubprocs < subprocsAllowed {
subprocsRunningCond.Wait()
stolenSubprocs += subprocsAllowed - subprocsRunning
subprocsRunning = subprocsAllowed
}
}
func finishExclusiveSubproc() {
subprocsRunning = 0
subprocsRunningCond.Broadcast()
subprocsRunningCond.L.Unlock()
exclusiveSubproc.Unlock()
}
// Ansi color codes.
const (
ansiTermDefault = "\033[0m"
ansiTermBlack = "\033[30m"
ansiTermRed = "\033[31m"
ansiTermGreen = "\033[32m"
ansiTermYellow = "\033[33m"
ansiTermBlue = "\033[34m"
ansiTermMagenta = "\033[35m"
ansiTermBright = "\033[1m"
ansiTermUnderline = "\033[4m"
)
// Build a node's prereqs. Block until completed.
//
func mkNodePrereqs(g *graph, u *node, e *edge, prereqs []*node, dryrun bool,
required bool) nodeStatus {
prereqstat := make(chan nodeStatus)
pending := 0
// build prereqs that need building
for i := range prereqs {
prereqs[i].mutex.Lock()
switch prereqs[i].status {
case nodeStatusReady, nodeStatusNop:
go mkNode(g, prereqs[i], dryrun, required)
fallthrough
case nodeStatusStarted:
prereqs[i].listeners = append(prereqs[i].listeners, prereqstat)
pending++
}
prereqs[i].mutex.Unlock()
}
// wait until all the prereqs are built
status := nodeStatusDone
for pending > 0 {
s := <-prereqstat
pending--
if s == nodeStatusFailed {
status = nodeStatusFailed
}
}
return status
}
// Build a target in the graph.
//
// This selects an appropriate rule (edge) and builds all prerequisites
// concurrently.
//
// Args:
// g: Graph in which the node lives.
// u: Node to (possibly) build.
// dryrun: Don't actually build anything, just pretend.
// required: Avoid building this node, unless its prereqs are out of date.
//
func mkNode(g *graph, u *node, dryrun bool, required bool) {
// try to claim on this node
u.mutex.Lock()
if u.status != nodeStatusReady && u.status != nodeStatusNop {
u.mutex.Unlock()
return
} else {
u.status = nodeStatusStarted
}
u.mutex.Unlock()
// when finished, notify the listeners
finalstatus := nodeStatusDone
defer func() {
u.mutex.Lock()
u.status = finalstatus
for i := range u.listeners {
u.listeners[i] <- u.status
}
u.listeners = u.listeners[0:0]
u.mutex.Unlock()
}()
// there's no rules.
if len(u.prereqs) == 0 {
if !(u.r != nil && u.r.attributes.virtual) && !u.exists {
wd, _ := os.Getwd()
mkError(fmt.Sprintf("don't know how to make %s in %s\n", u.name, wd))
}
finalstatus = nodeStatusNop
return
}
// there should otherwise be exactly one edge with an associated rule
prereqs := make([]*node, 0)
var e *edge = nil
for i := range u.prereqs {
if u.prereqs[i].r != nil {
e = u.prereqs[i]
}
if u.prereqs[i].v != nil {
prereqs = append(prereqs, u.prereqs[i].v)
}
}
// this should have been caught during graph building
if e == nil {
wd, _ := os.Getwd()
mkError(fmt.Sprintf("don't know how to make %s in %s", u.name, wd))
}
prereqs_required := required && (e.r.attributes.virtual || !u.exists)
mkNodePrereqs(g, u, e, prereqs, dryrun, prereqs_required)
uptodate := true
if !e.r.attributes.virtual {
u.updateTimestamp()
if !u.exists && required {
uptodate = false
} else if u.exists || required {
for i := range prereqs {
if u.t.Before(prereqs[i].t) || prereqs[i].status == nodeStatusDone {
uptodate = false
}
}
} else if required {
uptodate = false
}
} else {
uptodate = false
}
_, isrebuildtarget := rebuildtargets[u.name]
if isrebuildtarget || rebuildall {
uptodate = false
}
// make another pass on the prereqs, since we know we need them now
if !uptodate {
mkNodePrereqs(g, u, e, prereqs, dryrun, true)
}
// execute the recipe, unless the prereqs failed
if !uptodate && finalstatus != nodeStatusFailed && len(e.r.recipe) > 0 {
if e.r.attributes.exclusive {
reserveExclusiveSubproc()
} else {
reserveSubproc()
}
if !dorecipe(u.name, u, e, dryrun) {
finalstatus = nodeStatusFailed
}
u.updateTimestamp()
if e.r.attributes.exclusive {
finishExclusiveSubproc()
} else {
finishSubproc()
}
} else if finalstatus != nodeStatusFailed {
finalstatus = nodeStatusNop
}
}
func mkError(msg string) {
mkPrintError(msg)
os.Exit(1)
}
func mkPrintError(msg string) {
if color {
os.Stderr.WriteString(ansiTermRed)
}
fmt.Fprintf(os.Stderr, "error: %s\n", msg)
if color {
os.Stderr.WriteString(ansiTermDefault)
}
}
func mkPrintSuccess(msg string) {
if !color {
fmt.Println(msg)
} else {
fmt.Printf("%s%s%s\n", ansiTermGreen, msg, ansiTermDefault)
}
}
func mkPrintMessage(msg string) {
mkMsgMutex.Lock()
if !color {
fmt.Println(msg)
} else {
fmt.Printf("%s%s%s\n", ansiTermBlue, msg, ansiTermDefault)
}
mkMsgMutex.Unlock()
}
func mkPrintRecipe(target string, recipe string, quiet bool) {
mkMsgMutex.Lock()
if !color {
fmt.Printf("%s: ", target)
} else {
fmt.Printf("%s%s%s → %s",
ansiTermBlue+ansiTermBright+ansiTermUnderline, target,
ansiTermDefault, ansiTermBlue)
}
if quiet {
if !color {
fmt.Println("...")
} else {
fmt.Println("…")
}
} else {
printIndented(os.Stdout, recipe, len(target)+3)
if len(recipe) == 0 {
os.Stdout.WriteString("\n")
}
}
if color {
os.Stdout.WriteString(ansiTermDefault)
}
mkMsgMutex.Unlock()
}
func main() {
var directory string
var mkfilepath string
var interactive bool
var dryrun bool
var shallowrebuild bool
var quiet bool
flag.StringVar(&directory, "C", "", "directory to change in to")
flag.StringVar(&mkfilepath, "f", "mkfile", "use the given file as mkfile")
flag.BoolVar(&dryrun, "n", false, "print commands without actually executing")
flag.BoolVar(&shallowrebuild, "r", false, "force building of just targets")
flag.BoolVar(&rebuildall, "a", false, "force building of all dependencies")
flag.IntVar(&subprocsAllowed, "p", runtime.NumCPU(), "maximum number of jobs to execute in parallel")
flag.IntVar(&maxRuleCnt, "l", 1, "maximum number of times a specific rule can be applied (recursion)")
flag.BoolVar(&interactive, "i", false, "prompt before executing rules")
flag.BoolVar(&quiet, "q", false, "don't print recipes before executing them")
flag.BoolVar(&color, "color", isatty.IsTerminal(os.Stdout.Fd()), "turn color on/off")
flag.StringVar(&defaultShell, "shell", "sh -c", "default shell to use if none are specified via $shell")
flag.BoolVar(&dontDropArgs, "F", false, "don't drop shell arguments when no further arguments are specified")
// TODO(rjk): P9P mk command line compatability.
flag.Parse()
if directory != "" {
err := os.Chdir(directory)
if err != nil {
mkError(fmt.Sprintf("changing directory to `%s' failed", directory))
}
}
mkfile, err := os.Open(mkfilepath)
if err != nil {
mkError("no mkfile found")
}
input, _ := ioutil.ReadAll(mkfile)
mkfile.Close()
abspath, err := filepath.Abs(mkfilepath)
if err != nil {
mkError("unable to find mkfile's absolute path")
}
env := make(map[string][]string)
for _, elem := range os.Environ() {
vals := strings.SplitN(elem, "=", 2)
env[vals[0]] = append(env[vals[0]], vals[1])
}
rs := parse(string(input), mkfilepath, abspath, env)
if quiet {
for i := range rs.rules {
rs.rules[i].attributes.quiet = true
}
}
targets := flag.Args()
// build the first non-meta rule in the makefile, if none are given explicitly
if len(targets) == 0 {
for i := range rs.rules {
if !rs.rules[i].ismeta {
for j := range rs.rules[i].targets {
targets = append(targets, rs.rules[i].targets[j].spat)
}
break
}
}
}
if len(targets) == 0 {
fmt.Println("mk: nothing to mk")
return
}
if shallowrebuild {
for i := range targets {
rebuildtargets[targets[i]] = true
}
}
// Create a dummy virtual rule that depends on every target
root := rule{}
root.targets = []pattern{{false, "", nil}}
root.attributes = attribSet{false, false, false, false, false, false, false, true, false}
root.prereqs = targets
rs.add(root)
// Keep a global reference to the total state of mk variables.
GlobalMkState = rs.vars
if interactive {
g := buildgraph(rs, "")
mkNode(g, g.root, true, true)
fmt.Print("Proceed? ")
in := bufio.NewReader(os.Stdin)
for {
c, _, err := in.ReadRune()
if err != nil {
return
} else if strings.IndexRune(" \n\t\r", c) >= 0 {
continue
} else if c == 'y' {
break
} else {
return
}
}
}
g := buildgraph(rs, "")
mkNode(g, g.root, dryrun, true)
}
var GlobalMkState map[string][]string