Unverified Commit fe48668a authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

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...
parent 947e792b
......@@ -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
}
......@@ -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
......
......@@ -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
}
......
......@@ -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{}
......@@ -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")
......@@ -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) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment