Skip to content

Commit e252b1d

Browse files
committed
Fixed yet another even more convoluted case in bundle lib selection
Fix arduino#572
1 parent f79d94b commit e252b1d

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

arduino/libraries/libraries.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (library *Library) IsOptimizedForArchitecture(arch string) bool {
113113
// compatible with all architectures (the `architecture` field in
114114
// library.properties contains the `*` item)
115115
func (library *Library) IsArchitectureIndependent() bool {
116-
return library.IsOptimizedForArchitecture("*")
116+
return library.IsOptimizedForArchitecture("*") || library.Architectures == nil || len(library.Architectures) == 0
117117
}
118118

119119
// SourceDir represents a source dir of a library

arduino/libraries/librariesresolver/cpp.go

+25-18
Original file line numberDiff line numberDiff line change
@@ -127,34 +127,41 @@ 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 wins over Location but not on Name)
133+
priority += 1010
134+
} else if lib.IsArchitectureIndependent() {
135+
priority += 1000
136+
} else {
137+
// otherwise is optimized for another architecture (so it's not compatible)
138+
priority += 0
139+
}
140+
141+
if name == header {
142+
priority += 500
143+
} else if name == header+"-master" {
144+
priority += 400
145+
} else if strings.HasPrefix(name, header) {
146+
priority += 300
147+
} else if strings.HasSuffix(name, header) {
148+
priority += 200
149+
} else if strings.Contains(name, header) {
150+
priority += 100
132151
}
133152

134153
switch lib.Location {
135154
case libraries.IDEBuiltIn:
136-
priority += 0x0000
155+
priority += 0
137156
case libraries.ReferencedPlatformBuiltIn:
138-
priority += 0x0001
157+
priority += 1
139158
case libraries.PlatformBuiltIn:
140-
priority += 0x0002
159+
priority += 2
141160
case libraries.User:
142-
priority += 0x0003
161+
priority += 3
143162
default:
144163
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
145164
}
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-
}
158165
return priority
159166
}
160167

arduino/libraries/librariesresolver/cpp_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ func TestArchitecturePriority(t *testing.T) {
7676
res = runResolver("Servo.h", "esp32", userServoAllArch, userAnotherServo)
7777
require.NotNil(t, res)
7878
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")
7990
}
8091

8192
func TestClosestMatchWithTotallyDifferentNames(t *testing.T) {

0 commit comments

Comments
 (0)