@@ -24,26 +24,29 @@ import (
24
24
"github.com/arduino/go-paths-helper"
25
25
)
26
26
27
- type compilationCommand struct {
27
+ // CompilationDatabase keeps track of all the compile commands run by the builder
28
+ type CompilationDatabase struct {
29
+ contents []CompilationCommand
30
+ filename * paths.Path
31
+ }
32
+
33
+ // CompilationCommand keeps track of a single run of a compile command
34
+ type CompilationCommand struct {
28
35
Directory string `json:"directory"`
29
36
Arguments []string `json:"arguments"`
30
37
File string `json:"file"`
31
38
}
32
39
33
- type CompilationDatabase struct {
34
- contents []compilationCommand
35
- filename * paths.Path
36
- }
37
-
40
+ // NewCompilationDatabase creates an empty CompilationDatabase
38
41
func NewCompilationDatabase (filename * paths.Path ) * CompilationDatabase {
39
42
return & CompilationDatabase {
40
43
filename : filename ,
41
44
}
42
45
}
43
46
44
- func ( db * CompilationDatabase ) UpdateFile ( complete bool ) {
45
- // TODO: Read any existing file and use its contents for any
46
- // kept files, or any files not in db.contents if !complete.
47
+ // SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json,
48
+ // see https://clang.llvm.org/docs/JSONCompilationDatabase.html
49
+ func ( db * CompilationDatabase ) SaveToFile () {
47
50
if jsonContents , err := json .MarshalIndent (db .contents , "" , " " ); err != nil {
48
51
fmt .Printf ("Error serializing compilation database: %s" , err )
49
52
return
@@ -52,31 +55,27 @@ func (db *CompilationDatabase) UpdateFile(complete bool) {
52
55
}
53
56
}
54
57
55
- func ( db * CompilationDatabase ) dirForCommand (command * exec.Cmd ) string {
58
+ func dirForCommand (command * exec.Cmd ) string {
56
59
// This mimics what Cmd.Run also does: Use Dir if specified,
57
60
// current directory otherwise
58
61
if command .Dir != "" {
59
62
return command .Dir
60
- } else {
61
- dir , err := os .Getwd ()
62
- if err != nil {
63
- fmt .Printf ("Error getting current directory for compilation database: %s" , err )
64
- return ""
65
- }
66
- return dir
67
63
}
64
+ dir , err := os .Getwd ()
65
+ if err != nil {
66
+ fmt .Printf ("Error getting current directory for compilation database: %s" , err )
67
+ return ""
68
+ }
69
+ return dir
68
70
}
69
71
70
- func (db * CompilationDatabase ) ReplaceEntry (filename * paths.Path , command * exec.Cmd ) {
71
- entry := compilationCommand {
72
- Directory : db .dirForCommand (command ),
72
+ // Add adds a new CompilationDatabase entry
73
+ func (db * CompilationDatabase ) Add (target * paths.Path , command * exec.Cmd ) {
74
+ entry := CompilationCommand {
75
+ Directory : dirForCommand (command ),
73
76
Arguments : command .Args ,
74
- File : filename .String (),
77
+ File : target .String (),
75
78
}
76
79
77
80
db .contents = append (db .contents , entry )
78
81
}
79
-
80
- func (db * CompilationDatabase ) KeepEntry (filename * paths.Path ) {
81
- // TODO
82
- }
0 commit comments