@@ -19,6 +19,7 @@ import (
19
19
"encoding/json"
20
20
"fmt"
21
21
"os"
22
+ "sync"
22
23
23
24
"github.com/arduino/arduino-cli/executils"
24
25
"github.com/arduino/arduino-cli/i18n"
@@ -29,8 +30,9 @@ var tr = i18n.Tr
29
30
30
31
// Database keeps track of all the compile commands run by the builder
31
32
type Database struct {
32
- Contents []Command
33
- File * paths.Path
33
+ lock sync.Mutex
34
+ contents []Command
35
+ file * paths.Path
34
36
}
35
37
36
38
// Command keeps track of a single run of a compile command
@@ -44,8 +46,8 @@ type Command struct {
44
46
// NewDatabase creates an empty CompilationDatabase
45
47
func NewDatabase (filename * paths.Path ) * Database {
46
48
return & Database {
47
- File : filename ,
48
- Contents : []Command {},
49
+ file : filename ,
50
+ contents : []Command {},
49
51
}
50
52
}
51
53
@@ -56,16 +58,18 @@ func LoadDatabase(file *paths.Path) (*Database, error) {
56
58
return nil , err
57
59
}
58
60
res := NewDatabase (file )
59
- return res , json .Unmarshal (f , & res .Contents )
61
+ return res , json .Unmarshal (f , & res .contents )
60
62
}
61
63
62
64
// SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json,
63
65
// see https://clang.llvm.org/docs/JSONCompilationDatabase.html
64
66
func (db * Database ) SaveToFile () {
65
- if jsonContents , err := json .MarshalIndent (db .Contents , "" , " " ); err != nil {
67
+ db .lock .Lock ()
68
+ defer db .lock .Unlock ()
69
+ if jsonContents , err := json .MarshalIndent (db .contents , "" , " " ); err != nil {
66
70
fmt .Println (tr ("Error serializing compilation database: %s" , err ))
67
71
return
68
- } else if err := db .File .WriteFile (jsonContents ); err != nil {
72
+ } else if err := db .file .WriteFile (jsonContents ); err != nil {
69
73
fmt .Println (tr ("Error writing compilation database: %s" , err ))
70
74
}
71
75
}
@@ -89,5 +93,7 @@ func (db *Database) Add(target *paths.Path, command *executils.Process) {
89
93
File : target .String (),
90
94
}
91
95
92
- db .Contents = append (db .Contents , entry )
96
+ db .lock .Lock ()
97
+ db .contents = append (db .contents , entry )
98
+ db .lock .Unlock ()
93
99
}
0 commit comments