Skip to content

Commit 4d4c550

Browse files
committed
Renamed some functions, did some small cleanups
1 parent 4ae7a83 commit 4d4c550

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

arduino/builder/compilation_database.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,29 @@ import (
2424
"github.com/arduino/go-paths-helper"
2525
)
2626

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 {
2835
Directory string `json:"directory"`
2936
Arguments []string `json:"arguments"`
3037
File string `json:"file"`
3138
}
3239

33-
type CompilationDatabase struct {
34-
contents []compilationCommand
35-
filename *paths.Path
36-
}
37-
40+
// NewCompilationDatabase creates an empty CompilationDatabase
3841
func NewCompilationDatabase(filename *paths.Path) *CompilationDatabase {
3942
return &CompilationDatabase{
4043
filename: filename,
4144
}
4245
}
4346

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() {
4750
if jsonContents, err := json.MarshalIndent(db.contents, "", " "); err != nil {
4851
fmt.Printf("Error serializing compilation database: %s", err)
4952
return
@@ -52,31 +55,27 @@ func (db *CompilationDatabase) UpdateFile(complete bool) {
5255
}
5356
}
5457

55-
func (db *CompilationDatabase) dirForCommand(command *exec.Cmd) string {
58+
func dirForCommand(command *exec.Cmd) string {
5659
// This mimics what Cmd.Run also does: Use Dir if specified,
5760
// current directory otherwise
5861
if command.Dir != "" {
5962
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
6763
}
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
6870
}
6971

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),
7376
Arguments: command.Args,
74-
File: filename.String(),
77+
File: target.String(),
7578
}
7679

7780
db.contents = append(db.contents, entry)
7881
}
79-
80-
func (db *CompilationDatabase) KeepEntry(filename *paths.Path) {
81-
// TODO
82-
}

legacy/builder/builder.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ func (s *Builder) Run(ctx *types.Context) error {
9696

9797
mainErr := runCommands(ctx, commands)
9898

99-
// TODO: Make proper step?
10099
if ctx.CompilationDatabase != nil {
101-
ctx.CompilationDatabase.UpdateFile(mainErr != nil)
100+
ctx.CompilationDatabase.SaveToFile()
102101
}
103102

104103
commands = []types.Command{

legacy/builder/builder_utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
252252
return nil, errors.WithStack(err)
253253
}
254254
if ctx.CompilationDatabase != nil {
255-
ctx.CompilationDatabase.ReplaceEntry(source, command)
255+
ctx.CompilationDatabase.Add(source, command)
256256
}
257257
if !objIsUpToDate && !ctx.OnlyUpdateCompilationDatabase {
258258
_, _, err = utils.ExecCommand(ctx, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */)

0 commit comments

Comments
 (0)