-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
207 lines (170 loc) · 3.7 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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
)
type buildrule struct {
// user input
output string
inputs []string
command string
// calculated attributes
built bool
lock *sync.Mutex
}
var parents *Set
var sources *Set
var jobs map[string]buildrule
var semaphoreChan chan bool
func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
func parseLine(line string) buildrule {
// the format is:
//
// <output>\t[<input-1>\t...]--<command>
parts := strings.SplitN(line, "\t--\t", 2)
filenames := strings.Split(parts[0], "\t")
if len(parts) <= 1 && len(strings.SplitN(line, " -- ", 2)) > 0 {
fmt.Println("ERROR: couldn't parse line:", line)
fmt.Println("Are you using spaces instead of tabs?")
os.Exit(1)
}
return buildrule{
output: filepath.Clean(filenames[0]),
inputs: Map(filenames[1:], filepath.Clean),
command: parts[1],
built: false,
lock: &sync.Mutex{},
}
}
func parseStdin() buildrule {
// TODO: ensure input is not ""
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
rule := parseLine(scanner.Text())
parents.Add(rule.output)
for _, input := range rule.inputs {
parents.Remove(input)
sources.Add(input)
}
sources.Remove(rule.output)
jobs[rule.output] = rule
}
parents.CleanUp()
sources.CleanUp()
jobs[""] = buildrule{
output: "",
inputs: parents.SetToSlice(),
command: "echo \"Build successful!\"",
built: false,
lock: &sync.Mutex{},
}
return jobs[""]
}
var cancel chan bool
var cancelled bool
func performBuild(rule buildrule) {
var err error
var wg sync.WaitGroup
var done = make(chan bool)
defer close(done)
// open a lock on building this item
rule.lock.Lock()
defer rule.lock.Unlock()
if !rule.built && !cancelled {
// Build all the children in a pool of parallel goroutines
wg.Add(len(rule.inputs))
go func() {
wg.Wait()
done <- true
}()
for _, job := range rule.inputs {
j, present := jobs[job]
go func(job string, j buildrule, present bool) {
if present {
performBuild(j)
} else {
if !sources.Contains(job) {
fmt.Println("WAS NOT PRESENT", j, job)
}
}
wg.Done()
}(job, j, present)
}
// wait for it to finish
select {
case <-done:
break
case <-cancel:
fmt.Println("CANCELLING")
return
}
// Determine if we should run the command
var output_stat, input_stat os.FileInfo
var build = false
output_stat, err = os.Stat(rule.output)
if err != nil {
build = true
} else {
for _, input := range rule.inputs {
input_stat, err = os.Stat(input)
// TODO check error
if err != nil {
fmt.Println("COULD NOT DO", input, "MISSING")
cancelled = true
for {
cancel <- true
}
}
if input_stat.ModTime().After(output_stat.ModTime()) {
build = true
break
}
}
}
// Run the command
if build {
cmd := exec.Command("sh", "-c", rule.command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// version that prints the inputs, for debugging
//fmt.Println(rule.inputs, "\n", "#", rule.command)
fmt.Println("#", rule.command)
semaphoreChan <- true
err := cmd.Run()
<-semaphoreChan
if err != nil {
fmt.Println("COULD NOT DO", rule.command, err)
cancelled = true
for {
cancel <- true
}
}
}
}
}
func main() {
jobs = make(map[string]buildrule)
parents = NewSet()
sources = NewSet()
cancel = make(chan bool)
cancelled = false
top := parseStdin()
//fmt.Println("sources", sources.SetToSlice())
semaphoreChan = make(chan bool, 8)
performBuild(top)
close(semaphoreChan)
if cancelled {
os.Exit(1)
}
}