-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuilder.go
278 lines (258 loc) · 8.27 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
package handler
import (
"bufio"
"bytes"
"encoding/json"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/executils"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)
// func generateCpp(sourcePath, fqbn string) (*paths.Path, []byte, error) {
// // Generate target file
// if cppPath, err := generateBuildEnvironment(paths.New(sourcePath), fqbn); err != nil {
// return nil, nil, err
// } else if cppCode, err := cppPath.ReadFile(); err != nil {
// return nil, nil, err
// } else {
// return cppPath, cppCode, err
// }
// }
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 generateBuildEnvironment(sketchDir *paths.Path, fqbn string) (*paths.Path, error) {
// XXX: do this from IDE or via gRPC
args := []string{globalCliPath,
"compile",
"--fqbn", fqbn,
"--only-compilation-database",
"--clean",
"--format", "json",
sketchDir.String(),
}
cmd, err := executils.NewProcess(args...)
if err != nil {
return nil, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
}
cmdOutput := &bytes.Buffer{}
cmd.RedirectStdoutTo(cmdOutput)
cmd.SetDirFromPath(sketchDir)
log.Println("running: ", strings.Join(args, " "))
if err := cmd.Run(); err != nil {
return nil, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
}
type cmdBuilderRes struct {
BuildPath *paths.Path `json:"build_path"`
UsedLibraries []*libraries.Library
}
type cmdRes struct {
CompilerOut string `json:"compiler_out"`
CompilerErr string `json:"compiler_err"`
BuilderResult cmdBuilderRes `json:"builder_result"`
}
var res cmdRes
if err := json.Unmarshal(cmdOutput.Bytes(), &res); err != nil {
return nil, errors.Errorf("parsing arduino-cli output: %s", err)
}
// Return only the build path
log.Println("arduino-cli output:", cmdOutput)
return res.BuilderResult.BuildPath, nil
}
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(buildProps *properties.Map, 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 := buildProps.ExpandPropsInString(buildProps.Get("compiler.cpp.flags"))
printer.Println(splitFlags(cppFlags))
mcu := buildProps.ExpandPropsInString(buildProps.Get("build.mcu"))
if strings.Contains(fqbn, ":avr:") {
printer.Println("-mmcu=", mcu)
} else if strings.Contains(fqbn, ":sam:") {
printer.Println("-mcpu=", mcu)
}
fcpu := buildProps.ExpandPropsInString(buildProps.Get("build.f_cpu"))
printer.Println("-DF_CPU=", fcpu)
ideVersion := buildProps.ExpandPropsInString(buildProps.Get("runtime.ide.version"))
printer.Println("-DARDUINO=", ideVersion)
board := buildProps.ExpandPropsInString(buildProps.Get("build.board"))
printer.Println("-DARDUINO_", board)
arch := buildProps.ExpandPropsInString(buildProps.Get("build.arch"))
printer.Println("-DARDUINO_ARCH_", arch)
if strings.Contains(fqbn, ":sam:") {
libSamFlags := buildProps.ExpandPropsInString(buildProps.Get("compiler.libsam.c.flags"))
printer.Println(splitFlags(libSamFlags))
}
extraFlags := buildProps.ExpandPropsInString(buildProps.Get("build.extra_flags"))
printer.Println(splitFlags(extraFlags))
corePath := buildProps.ExpandPropsInString(buildProps.Get("build.core.path"))
printer.Println("-I", corePath)
variantPath := buildProps.ExpandPropsInString(buildProps.Get("build.variant.path"))
printer.Println("-I", variantPath)
if strings.Contains(fqbn, ":avr:") {
avrgccPath := buildProps.ExpandPropsInString(buildProps.Get("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 *exec.Cmd, stdout []byte, err error, filter func(string) string) error {
message := ""
log.Println("Command error:", command.Args, 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, "")
}
}