-
-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for compile_command.json output (#1081)
* Draft: Support generating a compile_commands.json file This is still very rough and unfinished. * Added compile flag to produce only the compilation database * Save compilation database inside build path * Removed 'Writing compilation database...' message Also slightly refactore code * Added missing (c) header * Renamed some functions, did some small cleanups * compilation database: made some fields public * compilation database: added LoadCompilationDatabase method * Added unit tests for compilation_database Co-authored-by: Matthijs Kooijman <matthijs@stdin.nl>
- Loading branch information
1 parent
bf7a319
commit 441f8eb
Showing
16 changed files
with
303 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.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 builder | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/arduino/go-paths-helper" | ||
) | ||
|
||
// CompilationDatabase keeps track of all the compile commands run by the builder | ||
type CompilationDatabase struct { | ||
Contents []CompilationCommand | ||
File *paths.Path | ||
} | ||
|
||
// CompilationCommand keeps track of a single run of a compile command | ||
type CompilationCommand struct { | ||
Directory string `json:"directory"` | ||
Command string `json:"command,omitempty"` | ||
Arguments []string `json:"arguments,omitempty"` | ||
File string `json:"file"` | ||
} | ||
|
||
// NewCompilationDatabase creates an empty CompilationDatabase | ||
func NewCompilationDatabase(filename *paths.Path) *CompilationDatabase { | ||
return &CompilationDatabase{ | ||
File: filename, | ||
} | ||
} | ||
|
||
// LoadCompilationDatabase reads a compilation database from a file | ||
func LoadCompilationDatabase(file *paths.Path) (*CompilationDatabase, error) { | ||
f, err := file.ReadFile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
res := &CompilationDatabase{ | ||
File: file, | ||
Contents: []CompilationCommand{}, | ||
} | ||
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) SaveToFile() { | ||
if jsonContents, err := json.MarshalIndent(db.Contents, "", " "); err != nil { | ||
fmt.Printf("Error serializing compilation database: %s", err) | ||
return | ||
} else if err := db.File.WriteFile(jsonContents); err != nil { | ||
fmt.Printf("Error writing compilation database: %s", err) | ||
} | ||
} | ||
|
||
func dirForCommand(command *exec.Cmd) string { | ||
// This mimics what Cmd.Run also does: Use Dir if specified, | ||
// current directory otherwise | ||
if command.Dir != "" { | ||
return command.Dir | ||
} | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
fmt.Printf("Error getting current directory for compilation database: %s", err) | ||
return "" | ||
} | ||
return dir | ||
} | ||
|
||
// Add adds a new CompilationDatabase entry | ||
func (db *CompilationDatabase) Add(target *paths.Path, command *exec.Cmd) { | ||
entry := CompilationCommand{ | ||
Directory: dirForCommand(command), | ||
Arguments: command.Args, | ||
File: target.String(), | ||
} | ||
|
||
db.Contents = append(db.Contents, entry) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.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 builder | ||
|
||
import ( | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/arduino/go-paths-helper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCompilationDatabase(t *testing.T) { | ||
tmpfile, err := paths.WriteToTempFile([]byte{}, nil, "") | ||
require.NoError(t, err) | ||
defer tmpfile.Remove() | ||
|
||
cmd := exec.Command("gcc", "arg1", "arg2") | ||
db := NewCompilationDatabase(tmpfile) | ||
db.Add(paths.New("test"), cmd) | ||
db.SaveToFile() | ||
|
||
db2, err := LoadCompilationDatabase(tmpfile) | ||
require.NoError(t, err) | ||
require.Equal(t, db, db2) | ||
require.Len(t, db2.Contents, 1) | ||
require.Equal(t, db2.Contents[0].File, "test") | ||
require.Equal(t, db2.Contents[0].Command, "") | ||
require.Equal(t, db2.Contents[0].Arguments, []string{"gcc", "arg1", "arg2"}) | ||
cwd, err := paths.Getwd() | ||
require.NoError(t, err) | ||
require.Equal(t, db2.Contents[0].Directory, cwd.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.