Skip to content

Whitelist all .arduino15/packages dir for clang driver detection #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -515,7 +516,7 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
return errors.WithMessage(err, "reading generated cpp file from sketch")
}

compilers := examineCompileCommandsJSON(handler.buildPath)
canonicalizeCompileCommandsJSON(handler.buildPath)

if params == nil {
// If we are restarting re-synchronize clangd
Expand All @@ -538,7 +539,11 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
}
} else {
// Otherwise start clangd!
clangdStdout, clangdStdin, clangdStderr := startClangd(handler.buildPath, handler.buildSketchCpp, compilers)
dataFolder, err := extractDataFolderFromArduinoCLI()
if err != nil {
log.Printf(" error: %s", err)
}
clangdStdout, clangdStdin, clangdStderr := startClangd(handler.buildPath, handler.buildSketchCpp, dataFolder)
clangdStdio := streams.NewReadWriteCloser(clangdStdin, clangdStdout)
if enableLogging {
clangdStdio = streams.LogReadWriteCloserAs(clangdStdio, "inols-clangd.log")
Expand Down Expand Up @@ -572,6 +577,38 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
return nil
}

func extractDataFolderFromArduinoCLI() (*paths.Path, error) {
// XXX: do this from IDE or via gRPC
args := []string{globalCliPath,
"config",
"dump",
"--format", "json",
}
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)
log.Println("running: ", strings.Join(args, " "))
if err := cmd.Run(); err != nil {
return nil, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
}

type cmdRes struct {
Directories struct {
Data string `json:"data"`
} `json:"directories"`
}
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 Data Dir -> ", res.Directories.Data)
return paths.New(res.Directories.Data), nil
}

func (handler *InoHandler) refreshCppDocumentSymbols(prefix string) error {
// Query source code symbols
cppURI := lsp.NewDocumentURIFromPath(handler.buildSketchCpp)
Expand Down Expand Up @@ -656,7 +693,7 @@ func (handler *InoHandler) CheckCppIncludesChanges() {

includesCanary := ""
for _, line := range strings.Split(handler.sketchMapper.CppText.Text, "\n") {
if strings.Contains(line, "#include <") {
if strings.Contains(line, "#include ") {
includesCanary += line
}
}
Expand All @@ -668,7 +705,7 @@ func (handler *InoHandler) CheckCppIncludesChanges() {
}
}

func examineCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool {
func canonicalizeCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool {
// Open compile_commands.json and find the main cross-compiler executable
compileCommandsJSONPath := compileCommandsDir.Join("compile_commands.json")
compileCommands, err := builder.LoadCompilationDatabase(compileCommandsJSONPath)
Expand Down Expand Up @@ -698,15 +735,15 @@ func examineCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool
return compilers
}

func startClangd(compileCommandsDir, sketchCpp *paths.Path, compilers map[string]bool) (io.WriteCloser, io.ReadCloser, io.ReadCloser) {
func startClangd(compileCommandsDir, sketchCpp *paths.Path, dataFolder *paths.Path) (io.WriteCloser, io.ReadCloser, io.ReadCloser) {
// Start clangd
args := []string{
globalClangdPath,
"-log=verbose",
fmt.Sprintf(`--compile-commands-dir=%s`, compileCommandsDir),
}
for compiler := range compilers {
args = append(args, fmt.Sprintf("-query-driver=%s", compiler))
if dataFolder != nil {
args = append(args, fmt.Sprintf("-query-driver=%s", dataFolder.Join("packages", "**")))
}
if enableLogging {
log.Println(" Starting clangd:", strings.Join(args, " "))
Expand Down