forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpp.go
183 lines (163 loc) · 5.34 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
// 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/libraries"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/schollz/closestmatch"
"github.com/sirupsen/logrus"
)
// Cpp finds libraries made for the C++ language
type Cpp struct {
headers map[string]libraries.List
}
// 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
}
// ScanLibrary reads a library to find and cache C++ headers for later retrieval
func (resolver *Cpp) ScanLibrary(lib *libraries.Library) error {
cppHeaders, err := lib.SourceDir.ReadDir()
if err != nil {
return fmt.Errorf("reading lib src dir: %s", err)
}
cppHeaders.FilterSuffix(".h", ".hpp", ".hh")
for _, cppHeaderPath := range cppHeaders {
cppHeader := cppHeaderPath.Base()
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)
priority := 0
// Bonus for core-optimized libraries
if lib.IsOptimizedForArchitecture(arch) || lib.IsArchitectureIndependent() {
priority += 0x0100
}
switch lib.Location {
case libraries.IDEBuiltIn:
priority += 0x0000
case libraries.ReferencedPlatformBuiltIn:
priority += 0x0001
case libraries.PlatformBuiltIn:
priority += 0x0002
case libraries.User:
priority += 0x0003
default:
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
}
if name == header {
priority += 0x0050
} else if name == header+"-master" {
priority += 0x0040
} else if strings.HasPrefix(name, header) {
priority += 0x0030
} else if strings.HasSuffix(name, header) {
priority += 0x0020
} else if strings.Contains(name, header) {
priority += 0x0010
}
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
}