-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemtpostproc.go
253 lines (217 loc) · 6.26 KB
/
emtpostproc.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"github.com/jessevdk/go-flags"
)
/*
* Sketch names must start with a letter or number, followed by letters,
* numbers, dashes, dots and underscores. Maximum length is 63 characters.
*/
var line1r = regexp.MustCompile(`^(#line\s*1).*\/([a-zA-Z0-9_-]*)\.(ino|pde)`)
var loopre = regexp.MustCompile(`\s*void\s+(loop[a-zA-Z0-9_]*)\(\s*(void)?\s*\)\s*(;|\s*)`)
var setupre = regexp.MustCompile(`\s*void\s+(setup[a-zA-Z0-9_]*)\(\s*(void)?\s*\)\s*(;|\s*)`)
var insert = regexp.MustCompile(`769d20fcd7a0eedaf64270f591438b01`)
/*
* Command line arguments
*/
var opts struct {
OutputDir string `short:"o" description:"Output directory of post processed sketch cpp file"`
BuildDir string `short:"b" description:"Sketch build directory" required:"true"`
SketchName string `short:"s" description:"Sketch name" required:"true"`
Core string `short:"c" description:"Core to build for" required:"true"`
Variant string `short:"v" description:"Variant to build for" required:"true"`
}
/*
* Structure to hold Sketch variables
*/
type sketch struct {
name string
sketchStartLine int
setupName string
setupLine int
loopName string
loopLine int
mangled bool
}
/* Sketches array */
var sketches []sketch
func printLines(lines []string) {
for _, line := range lines {
fmt.Println(line)
}
}
// writeLines writes the lines to the given file.
func writeLines(lines []string, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}
return w.Flush()
}
// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func main() {
numSketches := 0
var validSketches []sketch
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(2) // panic(err)
}
lines, err := readLines(opts.BuildDir + opts.SketchName + ".cpp")
if err != nil {
log.Fatalf("readLines: %s", err)
}
/* Get sketch names */
for i, line := range lines {
match := line1r.FindStringSubmatch(line)
if len(match) > 0 {
var s = sketch{match[2], i, "", 0, "", 0, false}
numSketches++
sketches = append(sketches, s)
}
}
/* Collect setup line number and name */
numSketches = -1
for i, line := range lines {
if line1r.MatchString(line) {
numSketches++
}
match := setupre.FindStringSubmatch(line)
if len(match) > 0 {
if match[3] == ";" {
} else {
sketches[numSketches].setupLine = i
sketches[numSketches].setupName = match[1]
}
}
}
/* Collect loop line number and name */
numSketches = -1
for i, line := range lines {
if line1r.MatchString(line) {
numSketches++
}
match := loopre.FindStringSubmatch(line)
if len(match) > 0 {
if match[3] == ";" {
} else {
sketches[numSketches].loopLine = i
sketches[numSketches].loopName = match[1]
}
}
}
/*
* Only keep the Sketches that have a matching setup/loop tuple
*/
for _, s := range sketches {
if s.loopName != "" && s.setupName != "" {
validSketches = append(validSketches, s)
}
}
/*
* in EMT, all setup and loop tuples in a single Sketch become a loo.
* Each setup and loop tuple therefor need a unique name. This loop will change the loop and setup
* into setup<Sketch name> and loop<Sketch name> and mark it as mangeled for later use when generationg
* main.cpp
*/
for i, s := range validSketches {
/* Valid sketch. Figure out if we need to get setup and loop a different name */
if s.loopName == "loop" && s.setupName == "setup" {
validSketches[i].mangled = true
validSketches[i].loopName = s.loopName + s.name
validSketches[i].setupName = s.setupName + s.name
}
}
insertedLines := 0
for _, s := range validSketches {
if s.mangled {
lines = append(lines, make([]string, 4)...)
copy(lines[s.sketchStartLine+insertedLines+4:], lines[s.sketchStartLine+insertedLines:])
lines[s.sketchStartLine+insertedLines] = "#undef loop"
lines[s.sketchStartLine+insertedLines+1] = "#undef setup"
lines[s.sketchStartLine+insertedLines+2] = "#define setup " + s.setupName
lines[s.sketchStartLine+insertedLines+3] = "#define loop " + s.loopName
insertedLines += 4
}
}
/* Write out sketch cpp code */
if err := writeLines(lines, opts.BuildDir+"/"+opts.SketchName+".cpp"); err != nil {
log.Fatalf("writeLines: %s", err)
}
insertLine := 0
/* On to the template to generate main.cpp */
dirSelf, _ := filepath.Abs(filepath.Dir(os.Args[0]))
lines, err = readLines(dirSelf + "/templates/" + opts.Core + "/" + opts.Variant + ".main.template")
if err != nil {
log.Fatalf("Read template: %s", err)
}
for i, line := range lines {
if !insert.MatchString(line) {
continue
}
insertLine = i + 1
break
}
insertLen := (len(validSketches) * 3) + 6
lines = append(lines, make([]string, insertLen)...)
copy(lines[insertLine+insertLen:], lines[insertLine:])
for _, s := range validSketches {
lines[insertLine] = "extern void " + s.setupName + "();"
lines[insertLine+1] = "extern void " + s.loopName + "();"
insertLine += 2
}
lines[insertLine] = ""
insertLine++
lines[insertLine] = "#define NUM_SKETCHES " + strconv.Itoa(len(validSketches))
insertLine++
lines[insertLine] = "void (*func_ptr[NUM_SKETCHES][" + strconv.Itoa(len(validSketches)*2) + "])(void) = {"
insertLine++
for i, s := range validSketches {
lines[insertLine] = "\t{" + s.setupName + " ," + s.loopName + "}"
if i+1 != len(validSketches) {
lines[insertLine] += ","
}
insertLine++
}
lines[insertLine] = "};"
insertLine++
lines[insertLine] = "const char *taskNames[] = {"
insertLine++
lines[insertLine] = "\t"
for i, s := range validSketches {
lines[insertLine] += "\"" + s.loopName + "\""
if i+1 != len(validSketches) {
lines[insertLine] += ", "
}
// insertLine++
}
lines[insertLine] += "};"
/* Write out sketch cpp code */
if err := writeLines(lines, opts.BuildDir+"/main.cpp"); err != nil {
log.Fatalf("writeLines: %s", err)
}
}