Skip to content

Commit 01f8947

Browse files
committed
Replaced compilation_database handler from arduino-cli
1 parent bb29960 commit 01f8947

File tree

2 files changed

+87
-25
lines changed

2 files changed

+87
-25
lines changed

Diff for: ls/builder.go

-25
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23-
"runtime"
2423
"strings"
2524
"sync"
2625
"time"
2726

28-
"github.com/arduino/arduino-cli/arduino/builder"
2927
"github.com/arduino/arduino-cli/arduino/libraries"
3028
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3129
"github.com/arduino/arduino-language-server/sourcemapper"
@@ -329,26 +327,3 @@ func (ls *INOLanguageServer) generateBuildEnvironment(ctx context.Context, fullB
329327

330328
return success, nil
331329
}
332-
333-
func canonicalizeCompileCommandsJSON(compileCommandsJSONPath *paths.Path) {
334-
compileCommands, err := builder.LoadCompilationDatabase(compileCommandsJSONPath)
335-
if err != nil {
336-
panic("could not find compile_commands.json")
337-
}
338-
for i, cmd := range compileCommands.Contents {
339-
if len(cmd.Arguments) == 0 {
340-
panic("invalid empty argument field in compile_commands.json")
341-
}
342-
343-
// clangd requires full path to compiler (including extension .exe on Windows!)
344-
compilerPath := paths.New(cmd.Arguments[0]).Canonical()
345-
compiler := compilerPath.String()
346-
if runtime.GOOS == "windows" && strings.ToLower(compilerPath.Ext()) != ".exe" {
347-
compiler += ".exe"
348-
}
349-
compileCommands.Contents[i].Arguments[0] = compiler
350-
}
351-
352-
// Save back compile_commands.json with OS native file separator and extension
353-
compileCommands.SaveToFile()
354-
}

Diff for: ls/compilation_database.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// This file is part of arduino-language-server.
2+
//
3+
// Copyright 2024 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU Affero General Public License version 3,
6+
// which covers the main part of arduino-language-server.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/agpl-3.0.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package ls
17+
18+
import (
19+
"runtime"
20+
"strings"
21+
22+
"github.com/arduino/go-paths-helper"
23+
"go.bug.st/json"
24+
)
25+
26+
// compilationDatabase represents a compile_commands.json content
27+
type compilationDatabase struct {
28+
Contents []compileCommand
29+
File *paths.Path
30+
}
31+
32+
// compileCommand keeps track of a single run of a compile command
33+
type compileCommand struct {
34+
Directory string `json:"directory"`
35+
Command string `json:"command,omitempty"`
36+
Arguments []string `json:"arguments,omitempty"`
37+
File string `json:"file"`
38+
}
39+
40+
// loadCompilationDatabase load a compile_commands.json file into a compilationDatabase structure
41+
func loadCompilationDatabase(file *paths.Path) (*compilationDatabase, error) {
42+
f, err := file.ReadFile()
43+
if err != nil {
44+
return nil, err
45+
}
46+
res := &compilationDatabase{
47+
File: file,
48+
Contents: []compileCommand{},
49+
}
50+
return res, json.Unmarshal(f, &res.Contents)
51+
}
52+
53+
// SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json,
54+
// see https://clang.llvm.org/docs/JSONCompilationDatabase.html
55+
func (db *compilationDatabase) save() error {
56+
if jsonContents, err := json.MarshalIndent(db.Contents, "", " "); err != nil {
57+
return err
58+
} else if err := db.File.WriteFile(jsonContents); err != nil {
59+
return err
60+
}
61+
return nil
62+
}
63+
64+
func canonicalizeCompileCommandsJSON(compileCommandsJSONPath *paths.Path) {
65+
// TODO: do canonicalization directly in `arduino-cli`
66+
67+
compileCommands, err := loadCompilationDatabase(compileCommandsJSONPath)
68+
if err != nil {
69+
panic("could not find compile_commands.json")
70+
}
71+
for i, cmd := range compileCommands.Contents {
72+
if len(cmd.Arguments) == 0 {
73+
panic("invalid empty argument field in compile_commands.json")
74+
}
75+
76+
// clangd requires full path to compiler (including extension .exe on Windows!)
77+
compilerPath := paths.New(cmd.Arguments[0]).Canonical()
78+
compiler := compilerPath.String()
79+
if runtime.GOOS == "windows" && strings.ToLower(compilerPath.Ext()) != ".exe" {
80+
compiler += ".exe"
81+
}
82+
compileCommands.Contents[i].Arguments[0] = compiler
83+
}
84+
85+
// Save back compile_commands.json with OS native file separator and extension
86+
compileCommands.save()
87+
}

0 commit comments

Comments
 (0)