Skip to content

Commit fe48668

Browse files
authored
Fix libraries priority selection (again) (#574)
* Reorganized tests * Fixed library.IsArchitectureIndependent method. Now it returns true for libraries that do not specify "architecture" fiels. * Fixed yet another even more convoluted case in bundle lib selection Fix #572 * Makes linter happy...
1 parent 947e792 commit fe48668

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

arduino/libraries/libraries.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ func (library *Library) String() string {
8383
// - the library is architecture independent
8484
// - the library doesn't specify any `architecture` field in library.properties
8585
func (library *Library) SupportsAnyArchitectureIn(archs ...string) bool {
86-
if len(library.Architectures) == 0 {
87-
return true
88-
}
8986
if library.IsArchitectureIndependent() {
9087
return true
9188
}
@@ -113,7 +110,7 @@ func (library *Library) IsOptimizedForArchitecture(arch string) bool {
113110
// compatible with all architectures (the `architecture` field in
114111
// library.properties contains the `*` item)
115112
func (library *Library) IsArchitectureIndependent() bool {
116-
return library.IsOptimizedForArchitecture("*")
113+
return library.IsOptimizedForArchitecture("*") || library.Architectures == nil || len(library.Architectures) == 0
117114
}
118115

119116
// SourceDir represents a source dir of a library

arduino/libraries/librariesresolver/cpp.go

+26-18
Original file line numberDiff line numberDiff line change
@@ -127,34 +127,42 @@ func computePriority(lib *libraries.Library, header, arch string) int {
127127
priority := 0
128128

129129
// Bonus for core-optimized libraries
130-
if lib.IsOptimizedForArchitecture(arch) || lib.IsArchitectureIndependent() {
131-
priority += 0x0100
130+
if lib.IsOptimizedForArchitecture(arch) {
131+
// give a slightly better bonus for libraries that have specific optimization
132+
// (it is more important than Location but less important than Name)
133+
priority += 1010
134+
} else if lib.IsArchitectureIndependent() {
135+
// standard bonus for architecture independent (vanilla) libraries
136+
priority += 1000
137+
} else {
138+
// the library is not architecture compatible
139+
priority += 0
140+
}
141+
142+
if name == header {
143+
priority += 500
144+
} else if name == header+"-master" {
145+
priority += 400
146+
} else if strings.HasPrefix(name, header) {
147+
priority += 300
148+
} else if strings.HasSuffix(name, header) {
149+
priority += 200
150+
} else if strings.Contains(name, header) {
151+
priority += 100
132152
}
133153

134154
switch lib.Location {
135155
case libraries.IDEBuiltIn:
136-
priority += 0x0000
156+
priority += 0
137157
case libraries.ReferencedPlatformBuiltIn:
138-
priority += 0x0001
158+
priority++
139159
case libraries.PlatformBuiltIn:
140-
priority += 0x0002
160+
priority += 2
141161
case libraries.User:
142-
priority += 0x0003
162+
priority += 3
143163
default:
144164
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
145165
}
146-
147-
if name == header {
148-
priority += 0x0050
149-
} else if name == header+"-master" {
150-
priority += 0x0040
151-
} else if strings.HasPrefix(name, header) {
152-
priority += 0x0030
153-
} else if strings.HasSuffix(name, header) {
154-
priority += 0x0020
155-
} else if strings.Contains(name, header) {
156-
priority += 0x0010
157-
}
158166
return priority
159167
}
160168

arduino/libraries/librariesresolver/cpp_test.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ var l5 = &libraries.Library{Name: "Yet Another Calculus Lib Improved", Location:
3030
var l6 = &libraries.Library{Name: "Calculus Unified Lib", Location: libraries.User}
3131
var l7 = &libraries.Library{Name: "AnotherLib", Location: libraries.User}
3232
var bundleServo = &libraries.Library{Name: "Servo", Location: libraries.IDEBuiltIn, Architectures: []string{"avr", "sam", "samd"}}
33-
var userServo = &libraries.Library{Name: "Servo", Location: libraries.User, Architectures: []string{"avr", "sam", "samd"}}
34-
var userServoAllArch = &libraries.Library{Name: "Servo", Location: libraries.User, Architectures: []string{"*"}}
35-
var userServoNonavr = &libraries.Library{Name: "Servo", Location: libraries.User, Architectures: []string{"sam", "samd"}}
36-
var userAnotherServo = &libraries.Library{Name: "AnotherServo", Location: libraries.User, Architectures: []string{"avr", "sam", "samd", "esp32"}}
3733

3834
func runResolver(include string, arch string, libs ...*libraries.Library) *libraries.Library {
3935
libraryList := libraries.List{}
@@ -44,6 +40,23 @@ func runResolver(include string, arch string, libs ...*libraries.Library) *libra
4440
}
4541

4642
func TestArchitecturePriority(t *testing.T) {
43+
userServo := &libraries.Library{
44+
Name: "Servo",
45+
Location: libraries.User,
46+
Architectures: []string{"avr", "sam", "samd"}}
47+
userServoAllArch := &libraries.Library{
48+
Name: "Servo",
49+
Location: libraries.User,
50+
Architectures: []string{"*"}}
51+
userServoNonavr := &libraries.Library{
52+
Name: "Servo",
53+
Location: libraries.User,
54+
Architectures: []string{"sam", "samd"}}
55+
userAnotherServo := &libraries.Library{
56+
Name: "AnotherServo",
57+
Location: libraries.User,
58+
Architectures: []string{"avr", "sam", "samd", "esp32"}}
59+
4760
res := runResolver("Servo.h", "avr", bundleServo, userServo)
4861
require.NotNil(t, res)
4962
require.Equal(t, userServo, res, "selected library")
@@ -63,6 +76,17 @@ func TestArchitecturePriority(t *testing.T) {
6376
res = runResolver("Servo.h", "esp32", userServoAllArch, userAnotherServo)
6477
require.NotNil(t, res)
6578
require.Equal(t, userServoAllArch, res, "selected library")
79+
80+
userSDAllArch := &libraries.Library{
81+
Name: "SD",
82+
Location: libraries.User,
83+
Architectures: []string{"*"}}
84+
builtinSDesp := &libraries.Library{
85+
Name: "SD",
86+
Location: libraries.PlatformBuiltIn,
87+
Architectures: []string{"esp8266"}}
88+
res = runResolver("SD.h", "esp8266", userSDAllArch, builtinSDesp)
89+
require.Equal(t, builtinSDesp, res, "selected library")
6690
}
6791

6892
func TestClosestMatchWithTotallyDifferentNames(t *testing.T) {

0 commit comments

Comments
 (0)