Skip to content

Fix libraries priority selection (again) #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions arduino/libraries/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ func (library *Library) String() string {
// - the library is architecture independent
// - the library doesn't specify any `architecture` field in library.properties
func (library *Library) SupportsAnyArchitectureIn(archs ...string) bool {
if len(library.Architectures) == 0 {
return true
}
if library.IsArchitectureIndependent() {
return true
}
Expand Down Expand Up @@ -113,7 +110,7 @@ func (library *Library) IsOptimizedForArchitecture(arch string) bool {
// compatible with all architectures (the `architecture` field in
// library.properties contains the `*` item)
func (library *Library) IsArchitectureIndependent() bool {
return library.IsOptimizedForArchitecture("*")
return library.IsOptimizedForArchitecture("*") || library.Architectures == nil || len(library.Architectures) == 0
}

// SourceDir represents a source dir of a library
Expand Down
44 changes: 26 additions & 18 deletions arduino/libraries/librariesresolver/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,42 @@ func computePriority(lib *libraries.Library, header, arch string) int {
priority := 0

// Bonus for core-optimized libraries
if lib.IsOptimizedForArchitecture(arch) || lib.IsArchitectureIndependent() {
priority += 0x0100
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 name == header {
priority += 500
} else if name == header+"-master" {
priority += 400
} else if strings.HasPrefix(name, header) {
priority += 300
} else if strings.HasSuffix(name, header) {
priority += 200
} else if strings.Contains(name, header) {
priority += 100
}

switch lib.Location {
case libraries.IDEBuiltIn:
priority += 0x0000
priority += 0
case libraries.ReferencedPlatformBuiltIn:
priority += 0x0001
priority++
case libraries.PlatformBuiltIn:
priority += 0x0002
priority += 2
case libraries.User:
priority += 0x0003
priority += 3
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
}

Expand Down
32 changes: 28 additions & 4 deletions arduino/libraries/librariesresolver/cpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ var l5 = &libraries.Library{Name: "Yet Another Calculus Lib Improved", Location:
var l6 = &libraries.Library{Name: "Calculus Unified Lib", Location: libraries.User}
var l7 = &libraries.Library{Name: "AnotherLib", Location: libraries.User}
var bundleServo = &libraries.Library{Name: "Servo", Location: libraries.IDEBuiltIn, Architectures: []string{"avr", "sam", "samd"}}
var userServo = &libraries.Library{Name: "Servo", Location: libraries.User, Architectures: []string{"avr", "sam", "samd"}}
var userServoAllArch = &libraries.Library{Name: "Servo", Location: libraries.User, Architectures: []string{"*"}}
var userServoNonavr = &libraries.Library{Name: "Servo", Location: libraries.User, Architectures: []string{"sam", "samd"}}
var userAnotherServo = &libraries.Library{Name: "AnotherServo", Location: libraries.User, Architectures: []string{"avr", "sam", "samd", "esp32"}}

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

func TestArchitecturePriority(t *testing.T) {
userServo := &libraries.Library{
Name: "Servo",
Location: libraries.User,
Architectures: []string{"avr", "sam", "samd"}}
userServoAllArch := &libraries.Library{
Name: "Servo",
Location: libraries.User,
Architectures: []string{"*"}}
userServoNonavr := &libraries.Library{
Name: "Servo",
Location: libraries.User,
Architectures: []string{"sam", "samd"}}
userAnotherServo := &libraries.Library{
Name: "AnotherServo",
Location: libraries.User,
Architectures: []string{"avr", "sam", "samd", "esp32"}}

res := runResolver("Servo.h", "avr", bundleServo, userServo)
require.NotNil(t, res)
require.Equal(t, userServo, res, "selected library")
Expand All @@ -63,6 +76,17 @@ func TestArchitecturePriority(t *testing.T) {
res = runResolver("Servo.h", "esp32", userServoAllArch, userAnotherServo)
require.NotNil(t, res)
require.Equal(t, userServoAllArch, res, "selected library")

userSDAllArch := &libraries.Library{
Name: "SD",
Location: libraries.User,
Architectures: []string{"*"}}
builtinSDesp := &libraries.Library{
Name: "SD",
Location: libraries.PlatformBuiltIn,
Architectures: []string{"esp8266"}}
res = runResolver("SD.h", "esp8266", userSDAllArch, builtinSDesp)
require.Equal(t, builtinSDesp, res, "selected library")
}

func TestClosestMatchWithTotallyDifferentNames(t *testing.T) {
Expand Down