From 68a266505d9730cb3aa358f64810770720fb7992 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 23 Jan 2020 13:16:08 +0100 Subject: [PATCH 1/4] Added test for new use cases --- .../libraries/librariesresolver/cpp_test.go | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/arduino/libraries/librariesresolver/cpp_test.go b/arduino/libraries/librariesresolver/cpp_test.go index 74184f29ea3..3c6598920e4 100644 --- a/arduino/libraries/librariesresolver/cpp_test.go +++ b/arduino/libraries/librariesresolver/cpp_test.go @@ -29,6 +29,36 @@ var l4 = &libraries.Library{Name: "Another Calculus Lib", Location: libraries.Us var l5 = &libraries.Library{Name: "Yet Another Calculus Lib Improved", Location: libraries.User} 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 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{} + libraryList.Add(libs...) + resolver := NewCppResolver() + resolver.headers[include] = libraryList + return resolver.ResolveFor(include, arch) +} + +func TestArchitecturePriority(t *testing.T) { + res := runResolver("Servo.h", "avr", bundleServo, userServo) + require.NotNil(t, res) + require.Equal(t, userServo, res, "selected library") + + res = runResolver("Servo.h", "avr", bundleServo, userServoNonavr) + require.NotNil(t, res) + require.Equal(t, bundleServo, res, "selected library") + + res = runResolver("Servo.h", "avr", bundleServo, userAnotherServo) + require.NotNil(t, res) + require.Equal(t, bundleServo, res, "selected library") + + res = runResolver("Servo.h", "esp32", bundleServo, userAnotherServo) + require.NotNil(t, res) + require.Equal(t, userAnotherServo, res, "selected library") +} func TestClosestMatchWithTotallyDifferentNames(t *testing.T) { libraryList := libraries.List{} From 66a897d05b6fdb59aa3bd292c3b7d26a9f780398 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 23 Jan 2020 13:17:38 +0100 Subject: [PATCH 2/4] 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. --- arduino/libraries/libraries.go | 29 ---------------------- arduino/libraries/librarieslist.go | 9 ------- arduino/libraries/librariesresolver/cpp.go | 21 +++++++++++++++- 3 files changed, 20 insertions(+), 39 deletions(-) diff --git a/arduino/libraries/libraries.go b/arduino/libraries/libraries.go index 701e8085cde..eb063387435 100644 --- a/arduino/libraries/libraries.go +++ b/arduino/libraries/libraries.go @@ -16,8 +16,6 @@ package libraries import ( - "fmt" - "github.com/arduino/arduino-cli/arduino/cores" paths "github.com/arduino/go-paths-helper" properties "github.com/arduino/go-properties-orderedmap" @@ -118,33 +116,6 @@ func (library *Library) IsArchitectureIndependent() bool { return library.IsOptimizedForArchitecture("*") } -// PriorityForArchitecture returns an integer that represents the -// priority this lib has for the specified architecture based on -// his location and the architectures directly supported (as exposed -// on the `architecture` field of the `library.properties`) -// This function returns an integer between 0 and 255, higher means -// higher priority. -func (library *Library) PriorityForArchitecture(arch string) uint8 { - bonus := uint8(0) - - // Bonus for core-optimized libraries - if library.IsOptimizedForArchitecture(arch) { - bonus = 0x10 - } - - switch library.Location { - case IDEBuiltIn: - return bonus + 0x00 - case ReferencedPlatformBuiltIn: - return bonus + 0x01 - case PlatformBuiltIn: - return bonus + 0x02 - case User: - return bonus + 0x03 - } - panic(fmt.Sprintf("Invalid library location: %d", library.Location)) -} - // SourceDir represents a source dir of a library type SourceDir struct { Dir *paths.Path diff --git a/arduino/libraries/librarieslist.go b/arduino/libraries/librarieslist.go index 008e3f1c9cc..f1644b84a29 100644 --- a/arduino/libraries/librarieslist.go +++ b/arduino/libraries/librarieslist.go @@ -50,15 +50,6 @@ func (list *List) FindByName(name string) *Library { return nil } -// SortByArchitecturePriority sorts the libraries in descending order using -// the Arduino lib priority ordering (the first has the higher priority) -func (list *List) SortByArchitecturePriority(arch string) { - sort.Slice(*list, func(i, j int) bool { - a, b := (*list)[i], (*list)[j] - return a.PriorityForArchitecture(arch) > b.PriorityForArchitecture(arch) - }) -} - // SortByName sorts the libraries by name func (list *List) SortByName() { sort.Slice(*list, func(i, j int) bool { diff --git a/arduino/libraries/librariesresolver/cpp.go b/arduino/libraries/librariesresolver/cpp.go index 4b7d10572c7..b30d687760c 100644 --- a/arduino/libraries/librariesresolver/cpp.go +++ b/arduino/libraries/librariesresolver/cpp.go @@ -124,7 +124,26 @@ func computePriority(lib *libraries.Library, header, arch string) int { header = simplify(header) name := simplify(lib.Name) - priority := int(lib.PriorityForArchitecture(arch)) // between 0..255 + priority := 0 + + // Bonus for core-optimized libraries + if lib.IsOptimizedForArchitecture(arch) { + priority += 0x10 + } + + switch lib.Location { + case libraries.IDEBuiltIn: + priority += 0x00 + case libraries.ReferencedPlatformBuiltIn: + priority += 0x01 + case libraries.PlatformBuiltIn: + priority += 0x02 + case libraries.User: + priority += 0x03 + default: + panic(fmt.Sprintf("Invalid library location: %d", lib.Location)) + } + if name == header { priority += 0x500 } else if name == header+"-master" { From 712612b911223467a00a26ed27b10fdea63a3244 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 23 Jan 2020 13:20:09 +0100 Subject: [PATCH 3/4] Fixed library priority selection --- arduino/libraries/librariesresolver/cpp.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/arduino/libraries/librariesresolver/cpp.go b/arduino/libraries/librariesresolver/cpp.go index b30d687760c..0010a59a0b8 100644 --- a/arduino/libraries/librariesresolver/cpp.go +++ b/arduino/libraries/librariesresolver/cpp.go @@ -128,32 +128,32 @@ func computePriority(lib *libraries.Library, header, arch string) int { // Bonus for core-optimized libraries if lib.IsOptimizedForArchitecture(arch) { - priority += 0x10 + priority += 0x0100 } switch lib.Location { case libraries.IDEBuiltIn: - priority += 0x00 + priority += 0x0000 case libraries.ReferencedPlatformBuiltIn: - priority += 0x01 + priority += 0x0001 case libraries.PlatformBuiltIn: - priority += 0x02 + priority += 0x0002 case libraries.User: - priority += 0x03 + priority += 0x0003 default: panic(fmt.Sprintf("Invalid library location: %d", lib.Location)) } if name == header { - priority += 0x500 + priority += 0x0050 } else if name == header+"-master" { - priority += 0x400 + priority += 0x0040 } else if strings.HasPrefix(name, header) { - priority += 0x300 + priority += 0x0030 } else if strings.HasSuffix(name, header) { - priority += 0x200 + priority += 0x0020 } else if strings.Contains(name, header) { - priority += 0x100 + priority += 0x0010 } return priority } From f38d64afda367f081edeed55bb3742969cbe6903 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 23 Jan 2020 15:50:33 +0100 Subject: [PATCH 4/4] Added another fix and test for lib priority --- arduino/libraries/librariesresolver/cpp.go | 2 +- arduino/libraries/librariesresolver/cpp_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/arduino/libraries/librariesresolver/cpp.go b/arduino/libraries/librariesresolver/cpp.go index 0010a59a0b8..47e240722cf 100644 --- a/arduino/libraries/librariesresolver/cpp.go +++ b/arduino/libraries/librariesresolver/cpp.go @@ -127,7 +127,7 @@ func computePriority(lib *libraries.Library, header, arch string) int { priority := 0 // Bonus for core-optimized libraries - if lib.IsOptimizedForArchitecture(arch) { + if lib.IsOptimizedForArchitecture(arch) || lib.IsArchitectureIndependent() { priority += 0x0100 } diff --git a/arduino/libraries/librariesresolver/cpp_test.go b/arduino/libraries/librariesresolver/cpp_test.go index 3c6598920e4..2a9af1cff6d 100644 --- a/arduino/libraries/librariesresolver/cpp_test.go +++ b/arduino/libraries/librariesresolver/cpp_test.go @@ -31,6 +31,7 @@ var l6 = &libraries.Library{Name: "Calculus Unified Lib", Location: libraries.Us 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"}} @@ -58,6 +59,10 @@ func TestArchitecturePriority(t *testing.T) { res = runResolver("Servo.h", "esp32", bundleServo, userAnotherServo) require.NotNil(t, res) require.Equal(t, userAnotherServo, res, "selected library") + + res = runResolver("Servo.h", "esp32", userServoAllArch, userAnotherServo) + require.NotNil(t, res) + require.Equal(t, userServoAllArch, res, "selected library") } func TestClosestMatchWithTotallyDifferentNames(t *testing.T) {