Skip to content

Commit 076de1e

Browse files
matthijskooijmancmaglie
authored andcommitted
Draft: Support generating a compile_commands.json file
This is still very rough and unfinished.
1 parent 5edef82 commit 076de1e

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package builder
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/arduino/go-paths-helper"
7+
"io/ioutil"
8+
"os"
9+
"os/exec"
10+
)
11+
12+
type compilationCommand struct {
13+
Directory string `json:"directory"`
14+
Arguments []string `json:"arguments"`
15+
File string `json:"file"`
16+
}
17+
18+
type CompilationDatabase struct {
19+
contents []compilationCommand
20+
filename *paths.Path
21+
}
22+
23+
func NewCompilationDatabase(filename *paths.Path) *CompilationDatabase {
24+
return &CompilationDatabase{
25+
filename: filename,
26+
}
27+
}
28+
29+
func (db *CompilationDatabase) UpdateFile(complete bool) {
30+
// TODO: Read any existing file and use its contents for any
31+
// kept files, or any files not in db.contents if !complete.
32+
fmt.Printf("Writing compilation database to \"%s\"...\n", db.filename.String())
33+
34+
contents := db.contents
35+
jsonContents, err := json.MarshalIndent(contents, "", " ")
36+
if err != nil {
37+
fmt.Printf("Error serializing compilation database: %s", err)
38+
return
39+
}
40+
err = ioutil.WriteFile(db.filename.String(), jsonContents, 0644)
41+
if err != nil {
42+
fmt.Printf("Error writing compilation database: %s", err)
43+
}
44+
}
45+
46+
func (db *CompilationDatabase) dirForCommand(command *exec.Cmd) string {
47+
// This mimics what Cmd.Run also does: Use Dir if specified,
48+
// current directory otherwise
49+
if command.Dir != "" {
50+
return command.Dir
51+
} else {
52+
dir, err := os.Getwd()
53+
if err != nil {
54+
fmt.Printf("Error getting current directory for compilation database: %s", err)
55+
return ""
56+
}
57+
return dir
58+
}
59+
}
60+
61+
func (db *CompilationDatabase) ReplaceEntry(filename *paths.Path, command *exec.Cmd) {
62+
entry := compilationCommand{
63+
Directory: db.dirForCommand(command),
64+
Arguments: command.Args,
65+
File: filename.String(),
66+
}
67+
68+
db.contents = append(db.contents, entry)
69+
}
70+
71+
func (db *CompilationDatabase) KeepEntry(filename *paths.Path) {
72+
// TODO
73+
}

commands/compile/compile.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
116116
builderCtx.PackageManager = pm
117117
builderCtx.FQBN = fqbn
118118
builderCtx.SketchLocation = sketch.FullPath
119+
builderCtx.CompilationDatabase = bldr.NewCompilationDatabase(
120+
sketch.FullPath.Join("compile_commands.json"),
121+
)
119122

120123
// FIXME: This will be redundant when arduino-builder will be part of the cli
121124
builderCtx.HardwareDirs = configuration.HardwareDirectories(configuration.Settings)

legacy/builder/builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ func (s *Builder) Run(ctx *types.Context) error {
9696

9797
mainErr := runCommands(ctx, commands)
9898

99+
// TODO: Make proper step?
100+
if ctx.CompilationDatabase != nil {
101+
ctx.CompilationDatabase.UpdateFile(mainErr != nil)
102+
}
103+
99104
commands = []types.Command{
100105
&PrintUsedAndNotUsedLibraries{SketchError: mainErr != nil},
101106

legacy/builder/builder_utils/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
258258
return nil, errors.WithStack(err)
259259
}
260260
} else if ctx.Verbose {
261+
if ctx.CompilationDatabase != nil {
262+
ctx.CompilationDatabase.KeepEntry(source)
263+
}
261264
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_PREVIOUS_COMPILED_FILE, objectFile)
262265
}
263266

legacy/builder/types/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"io"
2020
"strings"
2121

22+
"github.com/arduino/arduino-cli/arduino/builder"
2223
"github.com/arduino/arduino-cli/arduino/cores"
2324
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2425
"github.com/arduino/arduino-cli/arduino/libraries"
@@ -163,6 +164,9 @@ type Context struct {
163164

164165
// Sizer results
165166
ExecutableSectionsSize ExecutablesFileSections
167+
168+
// Compilation Database to build/update
169+
CompilationDatabase *builder.CompilationDatabase
166170
}
167171

168172
// ExecutableSectionSize represents a section of the executable output file

0 commit comments

Comments
 (0)