forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpp.go
241 lines (217 loc) · 7.46 KB
/
cpp.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// 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 librariesresolver
import (
"fmt"
"path/filepath"
"strings"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/i18n"
"github.com/schollz/closestmatch"
"github.com/sirupsen/logrus"
)
// Cpp finds libraries made for the C++ language
type Cpp struct {
headers map[string]libraries.List
}
var tr = i18n.Tr
// NewCppResolver creates a new Cpp resolver
func NewCppResolver() *Cpp {
return &Cpp{
headers: map[string]libraries.List{},
}
}
// ScanFromLibrariesManager reads all librariers loaded in the LibrariesManager to find
// and cache all C++ headers for later retrieval
func (resolver *Cpp) ScanFromLibrariesManager(lm *librariesmanager.LibrariesManager) error {
for _, libAlternatives := range lm.Libraries {
for _, lib := range libAlternatives.Alternatives {
resolver.ScanLibrary(lib)
}
}
return nil
}
// ScanIDEBuiltinLibraries reads ide-builtin librariers loaded in the LibrariesManager to find
// and cache all C++ headers for later retrieval.
func (resolver *Cpp) ScanIDEBuiltinLibraries(lm *librariesmanager.LibrariesManager) error {
for _, libAlternatives := range lm.Libraries {
for _, lib := range libAlternatives.Alternatives {
if lib.Location == libraries.IDEBuiltIn {
resolver.ScanLibrary(lib)
}
}
}
return nil
}
// ScanUserAndUnmanagedLibraries reads user/unmanaged librariers loaded in the LibrariesManager to find
// and cache all C++ headers for later retrieval.
func (resolver *Cpp) ScanUserAndUnmanagedLibraries(lm *librariesmanager.LibrariesManager) error {
for _, libAlternatives := range lm.Libraries {
for _, lib := range libAlternatives.Alternatives {
if lib.Location == libraries.User || lib.Location == libraries.Unmanaged {
resolver.ScanLibrary(lib)
}
}
}
return nil
}
// ScanPlatformLibraries reads platform-bundled libraries for a specific platform loaded in the LibrariesManager
// to find and cache all C++ headers for later retrieval.
func (resolver *Cpp) ScanPlatformLibraries(lm *librariesmanager.LibrariesManager, platform *cores.PlatformRelease) error {
for _, libAlternatives := range lm.Libraries {
for _, lib := range libAlternatives.Alternatives {
if lib.Location != libraries.PlatformBuiltIn && lib.Location != libraries.ReferencedPlatformBuiltIn {
continue
}
if lib.ContainerPlatform != platform {
continue
}
resolver.ScanLibrary(lib)
}
}
return nil
}
// ScanLibrary reads a library to find and cache C++ headers for later retrieval
func (resolver *Cpp) ScanLibrary(lib *libraries.Library) error {
cppHeaders, err := lib.SourceHeaders()
if err != nil {
return fmt.Errorf(tr("reading lib headers: %s"), err)
}
for _, cppHeader := range cppHeaders {
l := resolver.headers[cppHeader]
l.Add(lib)
resolver.headers[cppHeader] = l
}
return nil
}
// AlternativesFor returns all the libraries that provides the specified header
func (resolver *Cpp) AlternativesFor(header string) libraries.List {
return resolver.headers[header]
}
// ResolveFor finds the most suitable library for the specified combination of
// header and architecture. If no libraries provides the requested header, nil is returned
func (resolver *Cpp) ResolveFor(header, architecture string) *libraries.Library {
logrus.Infof("Resolving include %s for arch %s", header, architecture)
var found libraries.List
var foundPriority int
for _, lib := range resolver.headers[header] {
libPriority := computePriority(lib, header, architecture)
msg := " discarded"
if found == nil || foundPriority < libPriority {
found = libraries.List{}
found.Add(lib)
foundPriority = libPriority
msg = " found better lib"
} else if foundPriority == libPriority {
found.Add(lib)
msg = " found another lib with same priority"
}
logrus.
WithField("lib", lib.Name).
WithField("prio", fmt.Sprintf("%03X", libPriority)).
Infof(msg)
}
if found == nil {
return nil
}
if len(found) == 1 {
return found[0]
}
// If more than one library qualifies use the "closestmatch" algorithm to
// find the best matching one (instead of choosing it randomly)
if best := findLibraryWithNameBestDistance(header, found); best != nil {
logrus.WithField("lib", best.Name).Info(" library with the best matching name")
return best
}
found.SortByName()
logrus.WithField("lib", found[0].Name).Info(" first library in alphabetic order")
return found[0]
}
func simplify(name string) string {
name = utils.SanitizeName(name)
name = strings.ToLower(name)
return name
}
func computePriority(lib *libraries.Library, header, arch string) int {
header = strings.TrimSuffix(header, filepath.Ext(header))
header = simplify(header)
name := simplify(lib.Name)
realName := simplify(lib.RealName)
priority := 0
// Bonus for core-optimized libraries
if lib.IsOptimizedForArchitecture(arch) {
// give a slightly better bonus for libraries that have specific optimization
// (it is more important than Location but less important than Name)
priority += 1010
} else if lib.IsArchitectureIndependent() {
// standard bonus for architecture independent (vanilla) libraries
priority += 1000
} else {
// the library is not architecture compatible
priority += 0
}
if realName == header && name == header {
priority += 600
} else if realName == header || name == header {
priority += 500
} else if realName == header+"-master" || name == header+"-master" {
priority += 400
} else if strings.HasPrefix(realName, header) || strings.HasPrefix(name, header) {
priority += 300
} else if strings.HasSuffix(realName, header) || strings.HasSuffix(name, header) {
priority += 200
} else if strings.Contains(realName, header) || strings.Contains(name, header) {
priority += 100
}
switch lib.Location {
case libraries.IDEBuiltIn:
priority += 0
case libraries.ReferencedPlatformBuiltIn:
priority++
case libraries.PlatformBuiltIn:
priority += 2
case libraries.User:
priority += 3
case libraries.Unmanaged:
priority += 4
default:
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
}
return priority
}
func findLibraryWithNameBestDistance(name string, libs libraries.List) *libraries.Library {
// Create closestmatch DB
wordsToTest := []string{}
for _, lib := range libs {
wordsToTest = append(wordsToTest, simplify(lib.Name))
}
// Choose a set of bag sizes, more is more accurate but slower
bagSizes := []int{2}
// Create a closestmatch object and find the best matching name
cm := closestmatch.New(wordsToTest, bagSizes)
closestName := cm.Closest(name)
// Return the closest-matching lib
var winner *libraries.Library
for _, lib := range libs {
if closestName == simplify(lib.Name) {
winner = lib
break
}
}
return winner
}