Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cca6936

Browse files
authored
Fix library priority selection on some convoluted cases (#565)
* Added test for new use cases * Moved PriorityForArchitecture in librariesresolver The method is used only in library resolver, there is not point in having it as method of Library. Removed also the unused SortByArchitecturePriority method. * Fixed library priority selection * Added another fix and test for lib priority
1 parent fc9b70b commit cca6936

File tree

4 files changed

+60
-44
lines changed

4 files changed

+60
-44
lines changed

‎arduino/libraries/libraries.go‎

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package libraries
1717

1818
import (
19-
"fmt"
20-
2119
"github.com/arduino/arduino-cli/arduino/cores"
2220
paths "github.com/arduino/go-paths-helper"
2321
properties "github.com/arduino/go-properties-orderedmap"
@@ -118,33 +116,6 @@ func (library *Library) IsArchitectureIndependent() bool {
118116
return library.IsOptimizedForArchitecture("*")
119117
}
120118

121-
// PriorityForArchitecture returns an integer that represents the
122-
// priority this lib has for the specified architecture based on
123-
// his location and the architectures directly supported (as exposed
124-
// on the `architecture` field of the `library.properties`)
125-
// This function returns an integer between 0 and 255, higher means
126-
// higher priority.
127-
func (library *Library) PriorityForArchitecture(arch string) uint8 {
128-
bonus := uint8(0)
129-
130-
// Bonus for core-optimized libraries
131-
if library.IsOptimizedForArchitecture(arch) {
132-
bonus = 0x10
133-
}
134-
135-
switch library.Location {
136-
case IDEBuiltIn:
137-
return bonus + 0x00
138-
case ReferencedPlatformBuiltIn:
139-
return bonus + 0x01
140-
case PlatformBuiltIn:
141-
return bonus + 0x02
142-
case User:
143-
return bonus + 0x03
144-
}
145-
panic(fmt.Sprintf("Invalid library location: %d", library.Location))
146-
}
147-
148119
// SourceDir represents a source dir of a library
149120
type SourceDir struct {
150121
Dir *paths.Path

‎arduino/libraries/librarieslist.go‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ func (list *List) FindByName(name string) *Library {
5050
return nil
5151
}
5252

53-
// SortByArchitecturePriority sorts the libraries in descending order using
54-
// the Arduino lib priority ordering (the first has the higher priority)
55-
func (list *List) SortByArchitecturePriority(arch string) {
56-
sort.Slice(*list, func(i, j int) bool {
57-
a, b := (*list)[i], (*list)[j]
58-
return a.PriorityForArchitecture(arch) > b.PriorityForArchitecture(arch)
59-
})
60-
}
61-
6253
// SortByName sorts the libraries by name
6354
func (list *List) SortByName() {
6455
sort.Slice(*list, func(i, j int) bool {

‎arduino/libraries/librariesresolver/cpp.go‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,36 @@ func computePriority(lib *libraries.Library, header, arch string) int {
124124
header = simplify(header)
125125
name := simplify(lib.Name)
126126

127-
priority := int(lib.PriorityForArchitecture(arch)) // between 0..255
127+
priority := 0
128+
129+
// Bonus for core-optimized libraries
130+
if lib.IsOptimizedForArchitecture(arch) || lib.IsArchitectureIndependent() {
131+
priority += 0x0100
132+
}
133+
134+
switch lib.Location {
135+
case libraries.IDEBuiltIn:
136+
priority += 0x0000
137+
case libraries.ReferencedPlatformBuiltIn:
138+
priority += 0x0001
139+
case libraries.PlatformBuiltIn:
140+
priority += 0x0002
141+
case libraries.User:
142+
priority += 0x0003
143+
default:
144+
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
145+
}
146+
128147
if name == header {
129-
priority += 0x500
148+
priority += 0x0050
130149
} else if name == header+"-master" {
131-
priority += 0x400
150+
priority += 0x0040
132151
} else if strings.HasPrefix(name, header) {
133-
priority += 0x300
152+
priority += 0x0030
134153
} else if strings.HasSuffix(name, header) {
135-
priority += 0x200
154+
priority += 0x0020
136155
} else if strings.Contains(name, header) {
137-
priority += 0x100
156+
priority += 0x0010
138157
}
139158
return priority
140159
}

‎arduino/libraries/librariesresolver/cpp_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,41 @@ var l4 = &libraries.Library{Name: "Another Calculus Lib", Location: libraries.Us
2929
var l5 = &libraries.Library{Name: "Yet Another Calculus Lib Improved", Location: libraries.User}
3030
var l6 = &libraries.Library{Name: "Calculus Unified Lib", Location: libraries.User}
3131
var l7 = &libraries.Library{Name: "AnotherLib", Location: libraries.User}
32+
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"}}
37+
38+
func runResolver(include string, arch string, libs ...*libraries.Library) *libraries.Library {
39+
libraryList := libraries.List{}
40+
libraryList.Add(libs...)
41+
resolver := NewCppResolver()
42+
resolver.headers[include] = libraryList
43+
return resolver.ResolveFor(include, arch)
44+
}
45+
46+
func TestArchitecturePriority(t *testing.T) {
47+
res := runResolver("Servo.h", "avr", bundleServo, userServo)
48+
require.NotNil(t, res)
49+
require.Equal(t, userServo, res, "selected library")
50+
51+
res = runResolver("Servo.h", "avr", bundleServo, userServoNonavr)
52+
require.NotNil(t, res)
53+
require.Equal(t, bundleServo, res, "selected library")
54+
55+
res = runResolver("Servo.h", "avr", bundleServo, userAnotherServo)
56+
require.NotNil(t, res)
57+
require.Equal(t, bundleServo, res, "selected library")
58+
59+
res = runResolver("Servo.h", "esp32", bundleServo, userAnotherServo)
60+
require.NotNil(t, res)
61+
require.Equal(t, userAnotherServo, res, "selected library")
62+
63+
res = runResolver("Servo.h", "esp32", userServoAllArch, userAnotherServo)
64+
require.NotNil(t, res)
65+
require.Equal(t, userServoAllArch, res, "selected library")
66+
}
3267

3368
func TestClosestMatchWithTotallyDifferentNames(t *testing.T) {
3469
libraryList := libraries.List{}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /