-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuilder.go
362 lines (332 loc) · 10.4 KB
/
builder.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
package handler
import (
"bufio"
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
func generateCpp(inoCode []byte, sourcePath, fqbn string) (cppPath string, cppCode []byte, err error) {
// The CLI expects the `theSketchName.ino` file to be in `some/path/theSketchName` folder.
// Expected folder structure: `/path/to/temp/ino2cpp-${random}/theSketchName/theSketchName.ino`.
rawRootTempDir, err := ioutil.TempDir("", "ino2cpp-")
if err != nil {
err = errors.Wrap(err, "Error while creating temporary directory.")
return
}
rootTempDir, err := filepath.EvalSymlinks(rawRootTempDir)
if err != nil {
err = errors.Wrap(err, "Error while resolving symbolic links of temporary directory.")
return
}
sketchName := filepath.Base(sourcePath)
if strings.HasSuffix(sketchName, ".ino") {
sketchName = sketchName[:len(sketchName)-len(".ino")]
}
sketchTempPath := filepath.Join(rootTempDir, sketchName)
createDirIfNotExist(sketchTempPath)
// Write source file to temp dir
sketchFileName := sketchName + ".ino"
inoPath := filepath.Join(sketchTempPath, sketchFileName)
err = ioutil.WriteFile(inoPath, inoCode, 0600)
if err != nil {
err = errors.Wrap(err, "Error while writing source file to temporary directory.")
return
}
if enableLogging {
log.Println("Source file written to", inoPath)
}
// Copy all header files to temp dir
err = copyHeaderFiles(filepath.Dir(sourcePath), rootTempDir)
if err != nil {
return
}
// Generate compile_flags.txt
cppPath = filepath.Join(sketchTempPath, sketchFileName+".cpp")
flagsPath, err := generateCompileFlags(sketchTempPath, inoPath, sourcePath, fqbn)
if err != nil {
return
}
if enableLogging {
log.Println("Compile flags written to", flagsPath)
}
// Generate target file
cppCode, err = generateTargetFile(sketchTempPath, inoPath, cppPath, fqbn)
return
}
func createDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
panic(err)
}
}
}
func copyHeaderFiles(sourceDir string, destDir string) error {
fileInfos, err := ioutil.ReadDir(sourceDir)
if err != nil {
return err
}
for _, fileInfo := range fileInfos {
if !fileInfo.IsDir() && strings.HasSuffix(fileInfo.Name(), ".h") {
input, err := ioutil.ReadFile(filepath.Join(sourceDir, fileInfo.Name()))
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(destDir, fileInfo.Name()), input, 0644)
if err != nil {
return err
}
}
}
return nil
}
func updateCpp(inoCode []byte, sourcePath, fqbn string, fqbnChanged bool, cppPath string) (cppCode []byte, err error) {
tempDir := filepath.Dir(cppPath)
inoPath := strings.TrimSuffix(cppPath, ".cpp")
if inoCode != nil {
// Write source file to temp dir
err = ioutil.WriteFile(inoPath, inoCode, 0600)
if err != nil {
err = errors.Wrap(err, "Error while writing source file to temporary directory.")
return
}
if enableLogging {
log.Println("Source file written to", inoPath)
}
}
if fqbnChanged {
// Generate compile_flags.txt
var flagsPath string
flagsPath, err = generateCompileFlags(tempDir, inoPath, sourcePath, fqbn)
if err != nil {
return
}
if enableLogging {
log.Println("Compile flags written to", flagsPath)
}
}
// Generate target file
cppCode, err = generateTargetFile(tempDir, inoPath, cppPath, fqbn)
return
}
func generateCompileFlags(tempDir, inoPath, sourcePath, fqbn string) (string, error) {
var cliArgs []string
if len(fqbn) > 0 {
cliArgs = []string{"compile", "--fqbn", fqbn, "--show-properties", inoPath}
} else {
cliArgs = []string{"compile", "--show-properties", inoPath}
}
propertiesCmd := exec.Command(globalCliPath, cliArgs...)
output, err := propertiesCmd.Output()
if err != nil {
err = logCommandErr(globalCliPath, output, err, errMsgFilter(tempDir))
return "", err
}
properties, err := readProperties(bytes.NewReader(output))
if err != nil {
return "", errors.Wrap(err, "Error while reading build properties.")
}
flagsPath := filepath.Join(tempDir, "compile_flags.txt")
outFile, err := os.OpenFile(flagsPath, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return flagsPath, errors.Wrap(err, "Error while creating output file for compile flags.")
}
defer outFile.Close()
printer := Printer{Writer: bufio.NewWriter(outFile)}
printCompileFlags(properties, &printer, fqbn)
printLibraryPaths(sourcePath, &printer)
printer.Flush()
return flagsPath, printer.Err
}
func generateTargetFile(tempDir, inoPath, cppPath, fqbn string) (cppCode []byte, err error) {
var cliArgs []string
if len(fqbn) > 0 {
cliArgs = []string{"compile", "--fqbn", fqbn, "--preprocess", inoPath}
} else {
cliArgs = []string{"compile", "--preprocess", inoPath}
}
preprocessCmd := exec.Command(globalCliPath, cliArgs...)
cppCode, err = preprocessCmd.Output()
if err != nil {
err = logCommandErr(globalCliPath, cppCode, err, errMsgFilter(tempDir))
return
}
// Filter lines beginning with ERROR or WARNING
cppCode = []byte(filterErrorsAndWarnings(cppCode))
err = ioutil.WriteFile(cppPath, cppCode, 0600)
if err != nil {
err = errors.Wrap(err, "Error while writing target file to temporary directory.")
} else if enableLogging {
log.Println("Target file written to", cppPath)
}
return
}
func filterErrorsAndWarnings(cppCode []byte) string {
var sb strings.Builder
scanner := bufio.NewScanner(bytes.NewReader(cppCode))
for scanner.Scan() {
lineStr := scanner.Text()
if !(strings.HasPrefix(lineStr, "ERROR:") || strings.HasPrefix(lineStr, "WARNING:")) {
sb.WriteString(lineStr)
sb.WriteRune('\n')
}
}
return sb.String()
}
func copyIno2Cpp(inoCode string, cppPath string) (cppCode []byte, err error) {
inoPath := strings.TrimSuffix(cppPath, ".cpp")
filePrefix := "#include <Arduino.h>\n#line 1 \"" + inoPath + "\"\n"
cppCode = []byte(filePrefix + inoCode)
err = ioutil.WriteFile(cppPath, cppCode, 0600)
if err != nil {
err = errors.Wrap(err, "Error while writing target file to temporary directory.")
return
}
if enableLogging {
log.Println("Target file written to", cppPath)
}
return
}
func printCompileFlags(properties map[string]string, printer *Printer, fqbn string) {
if strings.Contains(fqbn, ":avr:") {
printer.Println("--target=avr")
} else if strings.Contains(fqbn, ":sam:") {
printer.Println("--target=arm-none-eabi")
}
cppFlags := expandProperty(properties, "compiler.cpp.flags")
printer.Println(splitFlags(cppFlags))
mcu := expandProperty(properties, "build.mcu")
if strings.Contains(fqbn, ":avr:") {
printer.Println("-mmcu=", mcu)
} else if strings.Contains(fqbn, ":sam:") {
printer.Println("-mcpu=", mcu)
}
fcpu := expandProperty(properties, "build.f_cpu")
printer.Println("-DF_CPU=", fcpu)
ideVersion := expandProperty(properties, "runtime.ide.version")
printer.Println("-DARDUINO=", ideVersion)
board := expandProperty(properties, "build.board")
printer.Println("-DARDUINO_", board)
arch := expandProperty(properties, "build.arch")
printer.Println("-DARDUINO_ARCH_", arch)
if strings.Contains(fqbn, ":sam:") {
libSamFlags := expandProperty(properties, "compiler.libsam.c.flags")
printer.Println(splitFlags(libSamFlags))
}
extraFlags := expandProperty(properties, "build.extra_flags")
printer.Println(splitFlags(extraFlags))
corePath := expandProperty(properties, "build.core.path")
printer.Println("-I", corePath)
variantPath := expandProperty(properties, "build.variant.path")
printer.Println("-I", variantPath)
if strings.Contains(fqbn, ":avr:") {
avrgccPath := expandProperty(properties, "runtime.tools.avr-gcc.path")
printer.Println("-I", filepath.Join(avrgccPath, "avr", "include"))
}
printLibraryPaths(corePath, printer)
}
func printLibraryPaths(basePath string, printer *Printer) {
parentDir := filepath.Dir(basePath)
if strings.HasSuffix(parentDir, string(filepath.Separator)) || strings.HasSuffix(parentDir, ".") {
return
}
libsDir := filepath.Join(parentDir, "libraries")
if libraries, err := ioutil.ReadDir(libsDir); err == nil {
for _, libInfo := range libraries {
if libInfo.IsDir() {
srcDir := filepath.Join(libsDir, libInfo.Name(), "src")
if srcInfo, err := os.Stat(srcDir); err == nil && srcInfo.IsDir() {
printer.Println("-I", srcDir)
} else {
printer.Println("-I", filepath.Join(libsDir, libInfo.Name()))
}
}
}
}
printLibraryPaths(parentDir, printer)
}
// Printer prints to a Writer and stores the first error.
type Printer struct {
Writer *bufio.Writer
Err error
}
// Println prints the given strings followed by a line break.
func (printer *Printer) Println(text ...string) {
totalLen := 0
for i := range text {
if len(text[i]) > 0 {
_, err := printer.Writer.WriteString(text[i])
if err != nil && printer.Err == nil {
printer.Err = err
}
totalLen += len(text[i])
}
}
if totalLen > 0 {
_, err := printer.Writer.WriteString("\n")
if err != nil && printer.Err == nil {
printer.Err = err
}
}
}
// Flush flushes the underlying writer.
func (printer *Printer) Flush() {
err := printer.Writer.Flush()
if err != nil && printer.Err == nil {
printer.Err = err
}
}
func splitFlags(flags string) string {
flagsBytes := []byte(flags)
result := make([]byte, len(flagsBytes))
inSingleQuotes := false
inDoubleQuotes := false
for i, b := range flagsBytes {
if b == '\'' && !inDoubleQuotes {
inSingleQuotes = !inSingleQuotes
}
if b == '"' && !inSingleQuotes {
inDoubleQuotes = !inDoubleQuotes
}
if b == ' ' && !inSingleQuotes && !inDoubleQuotes {
result[i] = '\n'
} else {
result[i] = b
}
}
return string(result)
}
func logCommandErr(command string, stdout []byte, err error, filter func(string) string) error {
message := ""
log.Println("Command error:", command, err)
if len(stdout) > 0 {
stdoutStr := string(stdout)
log.Println("------------------------------BEGIN STDOUT\n", stdoutStr, "------------------------------END STDOUT")
message += filter(stdoutStr)
}
if exitErr, ok := err.(*exec.ExitError); ok {
stderr := exitErr.Stderr
if len(stderr) > 0 {
stderrStr := string(stderr)
log.Println("------------------------------BEGIN STDERR\n", stderrStr, "------------------------------END STDERR")
message += filter(stderrStr)
}
}
if len(message) == 0 {
return err
}
return errors.New(message)
}
func errMsgFilter(tempDir string) func(string) string {
if !strings.HasSuffix(tempDir, string(filepath.Separator)) {
tempDir += string(filepath.Separator)
}
return func(s string) string {
return strings.ReplaceAll(s, tempDir, "")
}
}