|
| 1 | +// This file is part of arduino-language-server. |
| 2 | +// |
| 3 | +// Copyright 2024 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU Affero General Public License version 3, |
| 6 | +// which covers the main part of arduino-language-server. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/agpl-3.0.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package ls |
| 17 | + |
| 18 | +import ( |
| 19 | + "runtime" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/arduino/go-paths-helper" |
| 23 | + "go.bug.st/json" |
| 24 | +) |
| 25 | + |
| 26 | +// compilationDatabase represents a compile_commands.json content |
| 27 | +type compilationDatabase struct { |
| 28 | + Contents []compileCommand |
| 29 | + File *paths.Path |
| 30 | +} |
| 31 | + |
| 32 | +// compileCommand keeps track of a single run of a compile command |
| 33 | +type compileCommand struct { |
| 34 | + Directory string `json:"directory"` |
| 35 | + Command string `json:"command,omitempty"` |
| 36 | + Arguments []string `json:"arguments,omitempty"` |
| 37 | + File string `json:"file"` |
| 38 | +} |
| 39 | + |
| 40 | +// loadCompilationDatabase load a compile_commands.json file into a compilationDatabase structure |
| 41 | +func loadCompilationDatabase(file *paths.Path) (*compilationDatabase, error) { |
| 42 | + f, err := file.ReadFile() |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + res := &compilationDatabase{ |
| 47 | + File: file, |
| 48 | + Contents: []compileCommand{}, |
| 49 | + } |
| 50 | + return res, json.Unmarshal(f, &res.Contents) |
| 51 | +} |
| 52 | + |
| 53 | +// SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json, |
| 54 | +// see https://clang.llvm.org/docs/JSONCompilationDatabase.html |
| 55 | +func (db *compilationDatabase) save() error { |
| 56 | + if jsonContents, err := json.MarshalIndent(db.Contents, "", " "); err != nil { |
| 57 | + return err |
| 58 | + } else if err := db.File.WriteFile(jsonContents); err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func canonicalizeCompileCommandsJSON(compileCommandsJSONPath *paths.Path) { |
| 65 | + // TODO: do canonicalization directly in `arduino-cli` |
| 66 | + |
| 67 | + compileCommands, err := loadCompilationDatabase(compileCommandsJSONPath) |
| 68 | + if err != nil { |
| 69 | + panic("could not find compile_commands.json") |
| 70 | + } |
| 71 | + for i, cmd := range compileCommands.Contents { |
| 72 | + if len(cmd.Arguments) == 0 { |
| 73 | + panic("invalid empty argument field in compile_commands.json") |
| 74 | + } |
| 75 | + |
| 76 | + // clangd requires full path to compiler (including extension .exe on Windows!) |
| 77 | + compilerPath := paths.New(cmd.Arguments[0]).Canonical() |
| 78 | + compiler := compilerPath.String() |
| 79 | + if runtime.GOOS == "windows" && strings.ToLower(compilerPath.Ext()) != ".exe" { |
| 80 | + compiler += ".exe" |
| 81 | + } |
| 82 | + compileCommands.Contents[i].Arguments[0] = compiler |
| 83 | + } |
| 84 | + |
| 85 | + // Save back compile_commands.json with OS native file separator and extension |
| 86 | + compileCommands.save() |
| 87 | +} |
0 commit comments