forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompilation_database.go
90 lines (79 loc) · 2.86 KB
/
compilation_database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 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 [email protected].
package builder
import (
"encoding/json"
"fmt"
"os"
"github.com/arduino/arduino-cli/executils"
"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,
Contents: []CompilationCommand{},
}
}
// 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 := NewCompilationDatabase(file)
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.Println(tr("Error serializing compilation database: %s", err))
return
} else if err := db.File.WriteFile(jsonContents); err != nil {
fmt.Println(tr("Error writing compilation database: %s", err))
}
}
// Add adds a new CompilationDatabase entry
func (db *CompilationDatabase) Add(target *paths.Path, command *executils.Process) {
commandDir := command.GetDir()
if commandDir == "" {
// This mimics what Cmd.Run also does: Use Dir if specified,
// current directory otherwise
dir, err := os.Getwd()
if err != nil {
fmt.Println(tr("Error getting current directory for compilation database: %s", err))
}
commandDir = dir
}
entry := CompilationCommand{
Directory: commandDir,
Arguments: command.GetArgs(),
File: target.String(),
}
db.Contents = append(db.Contents, entry)
}