-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuilder.go
340 lines (301 loc) · 10 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
package ls
import (
"bytes"
"context"
"fmt"
"io"
"runtime"
"strings"
"sync"
"time"
"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/executils"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-language-server/sourcemapper"
"github.com/arduino/arduino-language-server/streams"
"github.com/arduino/go-paths-helper"
"github.com/fatih/color"
"github.com/pkg/errors"
"go.bug.st/json"
"go.bug.st/lsp"
"go.bug.st/lsp/jsonrpc"
"google.golang.org/grpc"
)
type sketchRebuilder struct {
ls *INOLanguageServer
trigger chan chan<- bool
cancel func()
mutex sync.Mutex
}
// newSketchBuilder makes a new SketchRebuilder and returns its pointer
func newSketchBuilder(ls *INOLanguageServer) *sketchRebuilder {
res := &sketchRebuilder{
trigger: make(chan chan<- bool, 1),
cancel: func() {},
ls: ls,
}
go func() {
defer streams.CatchAndLogPanic()
res.rebuilderLoop()
}()
return res
}
func (ls *INOLanguageServer) triggerRebuildAndWait(logger jsonrpc.FunctionLogger) {
completed := make(chan bool)
ls.sketchRebuilder.TriggerRebuild(completed)
ls.writeUnlock(logger)
<-completed
ls.writeLock(logger, true)
}
func (ls *INOLanguageServer) triggerRebuild() {
ls.sketchRebuilder.TriggerRebuild(nil)
}
// TriggerRebuild schedule a sketch rebuild (it will be executed asynchronously)
func (r *sketchRebuilder) TriggerRebuild(completed chan<- bool) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.cancel() // Stop possibly already running builds
select {
case r.trigger <- completed:
default:
}
}
func (r *sketchRebuilder) rebuilderLoop() {
logger := NewLSPFunctionLogger(color.HiMagentaString, "SKETCH REBUILD: ")
for {
completed := <-r.trigger
for {
// Concede a 200ms delay to accumulate bursts of changes
select {
case <-r.trigger:
continue
case <-time.After(time.Second):
}
break
}
r.ls.progressHandler.Create("arduinoLanguageServerRebuild")
r.ls.progressHandler.Begin("arduinoLanguageServerRebuild", &lsp.WorkDoneProgressBegin{Title: "Building sketch"})
ctx, cancel := context.WithCancel(context.Background())
r.mutex.Lock()
logger.Logf("Sketch rebuild started")
r.cancel = cancel
r.mutex.Unlock()
if err := r.doRebuildArduinoPreprocessedSketch(ctx, logger); err != nil {
logger.Logf("Error: %s", err)
}
cancel()
r.ls.progressHandler.End("arduinoLanguageServerRebuild", &lsp.WorkDoneProgressEnd{Message: "done"})
if completed != nil {
close(completed)
}
}
}
func (r *sketchRebuilder) doRebuildArduinoPreprocessedSketch(ctx context.Context, logger jsonrpc.FunctionLogger) error {
ls := r.ls
if success, err := ls.generateBuildEnvironment(ctx, !r.ls.config.SkipLibrariesDiscoveryOnRebuild, logger); err != nil {
return err
} else if !success {
return fmt.Errorf("build failed")
}
ls.writeLock(logger, true)
defer ls.writeUnlock(logger)
// Check one last time if the process has been canceled
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if cppContent, err := ls.buildSketchCpp.ReadFile(); err == nil {
oldVersion := ls.sketchMapper.CppText.Version
ls.sketchMapper = sourcemapper.CreateInoMapper(cppContent)
ls.sketchMapper.CppText.Version = oldVersion + 1
ls.sketchMapper.DebugLogAll()
} else {
return errors.WithMessage(err, "reading generated cpp file from sketch")
}
// Send didSave to notify clang that the source cpp is changed
logger.Logf("Sending 'didSave' notification to Clangd")
cppURI := lsp.NewDocumentURIFromPath(ls.buildSketchCpp)
didSaveParams := &lsp.DidSaveTextDocumentParams{
TextDocument: lsp.TextDocumentIdentifier{URI: cppURI},
}
if err := ls.Clangd.conn.TextDocumentDidSave(didSaveParams); err != nil {
logger.Logf("error reinitializing clangd:", err)
return err
}
// Send the full text to clang
logger.Logf("Sending full-text 'didChange' notification to Clangd")
didChangeParams := &lsp.DidChangeTextDocumentParams{
TextDocument: lsp.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: lsp.TextDocumentIdentifier{URI: cppURI},
Version: ls.sketchMapper.CppText.Version,
},
ContentChanges: []lsp.TextDocumentContentChangeEvent{
{Text: ls.sketchMapper.CppText.Text},
},
}
if err := ls.Clangd.conn.TextDocumentDidChange(didChangeParams); err != nil {
logger.Logf("error reinitializing clangd:", err)
return err
}
return nil
}
func (ls *INOLanguageServer) generateBuildEnvironment(ctx context.Context, fullBuild bool, logger jsonrpc.FunctionLogger) (bool, error) {
var buildPath *paths.Path
if fullBuild {
buildPath = ls.fullBuildPath
} else {
buildPath = ls.buildPath
}
// Extract all build information from language server status
ls.readLock(logger, false)
sketchRoot := ls.sketchRoot
config := ls.config
type overridesFile struct {
Overrides map[string]string `json:"overrides"`
}
data := overridesFile{Overrides: map[string]string{}}
for uri, trackedFile := range ls.trackedIdeDocs {
rel, err := paths.New(uri).RelFrom(sketchRoot)
if err != nil {
ls.readUnlock(logger)
return false, errors.WithMessage(err, "dumping tracked files")
}
data.Overrides[rel.String()] = trackedFile.Text
}
ls.readUnlock(logger)
var success bool
if config.CliPath == nil {
// Establish a connection with the arduino-cli gRPC server
conn, err := grpc.Dial(config.CliDaemonAddress, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return false, fmt.Errorf("error connecting to arduino-cli rpc server: %w", err)
}
defer conn.Close()
client := rpc.NewArduinoCoreServiceClient(conn)
compileReq := &rpc.CompileRequest{
Instance: &rpc.Instance{Id: int32(config.CliInstanceNumber)},
Fqbn: config.Fqbn,
SketchPath: sketchRoot.String(),
SourceOverride: data.Overrides,
BuildPath: buildPath.String(),
CreateCompilationDatabaseOnly: true,
Verbose: true,
SkipLibrariesDiscovery: !fullBuild,
}
compileReqJSON, _ := json.MarshalIndent(compileReq, "", " ")
logger.Logf("Running build with: %s", string(compileReqJSON))
compRespStream, err := client.Compile(context.Background(), compileReq)
if err != nil {
return false, fmt.Errorf("error running compile: %w", err)
}
// Loop and consume the server stream until all the operations are done.
stdout := ""
stderr := ""
for {
compResp, err := compRespStream.Recv()
if err == io.EOF {
success = true
logger.Logf("Compile successful!")
break
}
if err != nil {
logger.Logf("build stdout:")
logger.Logf(stdout)
logger.Logf("build stderr:")
logger.Logf(stderr)
return false, fmt.Errorf("error running compile: %w", err)
}
if resp := compResp.GetOutStream(); resp != nil {
stdout += string(resp)
}
if resperr := compResp.GetErrStream(); resperr != nil {
stderr += string(resperr)
}
}
} else {
// Dump overrides into a temporary json file
for filename, override := range data.Overrides {
logger.Logf("Dumping %s override:\n%s", filename, override)
}
var overridesJSON *paths.Path
if jsonBytes, err := json.MarshalIndent(data, "", " "); err != nil {
return false, errors.WithMessage(err, "dumping tracked files")
} else if tmp, err := paths.WriteToTempFile(jsonBytes, nil, ""); err != nil {
return false, errors.WithMessage(err, "dumping tracked files")
} else {
overridesJSON = tmp
defer tmp.Remove()
}
// Run arduino-cli to perform the build
args := []string{config.CliPath.String(),
"--config-file", config.CliConfigPath.String(),
"compile",
"--fqbn", config.Fqbn,
"--only-compilation-database",
"--source-override", overridesJSON.String(),
"--build-path", buildPath.String(),
"--format", "json",
}
if !fullBuild {
args = append(args, "--skip-libraries-discovery")
}
args = append(args, sketchRoot.String())
cmd, err := executils.NewProcess(nil, args...)
if err != nil {
return false, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
}
cmdOutput := &bytes.Buffer{}
cmd.RedirectStdoutTo(cmdOutput)
cmd.SetDirFromPath(sketchRoot)
logger.Logf("running: %s", strings.Join(args, " "))
if err := cmd.RunWithinContext(ctx); err != nil {
return false, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
}
// Currently those values are not used, keeping here for future improvements
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"`
Success bool `json:"success"`
}
var res cmdRes
if err := json.Unmarshal(cmdOutput.Bytes(), &res); err != nil {
return false, errors.Errorf("parsing arduino-cli output: %s", err)
}
logger.Logf("arduino-cli output: %s", cmdOutput)
success = res.Success
}
if fullBuild {
ls.CopyFullBuildResults(logger, buildPath)
return ls.generateBuildEnvironment(ctx, false, logger)
}
// TODO: do canonicalization directly in `arduino-cli`
canonicalizeCompileCommandsJSON(buildPath.Join("compile_commands.json"))
return success, nil
}
func canonicalizeCompileCommandsJSON(compileCommandsJSONPath *paths.Path) {
compileCommands, err := builder.LoadCompilationDatabase(compileCommandsJSONPath)
if err != nil {
panic("could not find compile_commands.json")
}
for i, cmd := range compileCommands.Contents {
if len(cmd.Arguments) == 0 {
panic("invalid empty argument field in compile_commands.json")
}
// clangd requires full path to compiler (including extension .exe on Windows!)
compilerPath := paths.New(cmd.Arguments[0]).Canonical()
compiler := compilerPath.String()
if runtime.GOOS == "windows" && strings.ToLower(compilerPath.Ext()) != ".exe" {
compiler += ".exe"
}
compileCommands.Contents[i].Arguments[0] = compiler
}
// Save back compile_commands.json with OS native file separator and extension
compileCommands.SaveToFile()
}