-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcompilation_database.go
87 lines (76 loc) · 2.83 KB
/
compilation_database.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
// This file is part of arduino-language-server.
//
// Copyright 2024 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU Affero General Public License version 3,
// which covers the main part of arduino-language-server.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/agpl-3.0.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package ls
import (
"runtime"
"strings"
"github.com/arduino/go-paths-helper"
"go.bug.st/json"
)
// compilationDatabase represents a compile_commands.json content
type compilationDatabase struct {
Contents []compileCommand
File *paths.Path
}
// compileCommand keeps track of a single run of a compile command
type compileCommand struct {
Directory string `json:"directory"`
Command string `json:"command,omitempty"`
Arguments []string `json:"arguments,omitempty"`
File string `json:"file"`
}
// loadCompilationDatabase load a compile_commands.json file into a compilationDatabase structure
func loadCompilationDatabase(file *paths.Path) (*compilationDatabase, error) {
f, err := file.ReadFile()
if err != nil {
return nil, err
}
res := &compilationDatabase{
File: file,
Contents: []compileCommand{},
}
return res, json.Unmarshal(f, &res.Contents)
}
// SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json,
// see https://clang.llvm.org/docs/JSONCompilationDatabase.html
func (db *compilationDatabase) save() error {
if jsonContents, err := json.MarshalIndent(db.Contents, "", " "); err != nil {
return err
} else if err := db.File.WriteFile(jsonContents); err != nil {
return err
}
return nil
}
func canonicalizeCompileCommandsJSON(compileCommandsJSONPath *paths.Path) {
// TODO: do canonicalization directly in `arduino-cli`
compileCommands, err := 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.save()
}