Skip to content

Commit

Permalink
Better defined sketch-vendored libraries priority
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Nov 12, 2024
1 parent dcc216f commit eaf3f0d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
11 changes: 11 additions & 0 deletions internal/arduino/libraries/libraries_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (
// Unmanaged is for libraries set manually by the user in the CLI command or from the gRPC function.
// Ideally it's used for `libraries` outside folders managed by the CLI.
Unmanaged
// Sketch is for libraries that are part of the sketch (inside the `libraries` subfolder of the sketch).
Sketch
)

func (d *LibraryLocation) String() string {
Expand All @@ -54,6 +56,8 @@ func (d *LibraryLocation) String() string {
return "user"
case Unmanaged:
return "unmanaged"
case Sketch:
return "sketch"
default:
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
}
Expand Down Expand Up @@ -86,6 +90,9 @@ func (d *LibraryLocation) UnmarshalJSON(b []byte) error {
case "unmanaged":
*d = Unmanaged
return nil
case "sketch":
*d = Sketch
return nil
default:
return errors.New(i18n.Tr("invalid library location: %s", s))
}
Expand All @@ -104,6 +111,8 @@ func (d *LibraryLocation) ToRPCLibraryLocation() rpc.LibraryLocation {
return rpc.LibraryLocation_LIBRARY_LOCATION_USER
case Unmanaged:
return rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED
case Sketch:
return rpc.LibraryLocation_LIBRARY_LOCATION_SKETCH
default:
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
}
Expand All @@ -122,6 +131,8 @@ func FromRPCLibraryLocation(l rpc.LibraryLocation) LibraryLocation {
return User
case rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED:
return Unmanaged
case rpc.LibraryLocation_LIBRARY_LOCATION_SKETCH:
return Sketch
default:
panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l))
}
Expand Down
1 change: 1 addition & 0 deletions internal/arduino/libraries/libraries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestLibLayoutAndLocationJSONUnMarshaler(t *testing.T) {
testLocation(ReferencedPlatformBuiltIn)
testLocation(User)
testLocation(Unmanaged)
testLocation(Sketch)
}

func TestLibrariesLoader(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion internal/arduino/libraries/librariesresolver/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,12 @@ func ComputePriority(lib *libraries.Library, header, arch string) int {
priority += 2
case libraries.User:
priority += 3
case libraries.Sketch:
// Bonus for sketch libraries, those libraries get a better priority than others
priority += 10000
case libraries.Unmanaged:
// Bonus for libraries specified via --libraries flags, those libraries gets the highest priority
priority += 10000
priority += 20000
default:
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/arduino/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func New(path *paths.Path) (*Sketch, error) {
}
libDirs.FilterDirs()
for _, libDir := range libDirs {
lib, err := libraries.Load(libDir, libraries.Unmanaged)
lib, err := libraries.Load(libDir, libraries.Sketch)
if err != nil {
return nil, fmt.Errorf("%s: %w", i18n.Tr("reading sketch libraries"), err)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/feedback/result/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const (
LibraryLocationPlatformBuiltin LibraryLocation = "platform"
LibraryLocationReferencedPlatformBuiltin LibraryLocation = "ref-platform"
LibraryLocationUnmanged LibraryLocation = "unmanaged"
LibraryLocationSketch LibraryLocation = "sketch"
)

func NewLibraryLocation(r rpc.LibraryLocation) LibraryLocation {
Expand All @@ -186,6 +187,8 @@ func NewLibraryLocation(r rpc.LibraryLocation) LibraryLocation {
return LibraryLocationUser
case rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED:
return LibraryLocationUnmanged
case rpc.LibraryLocation_LIBRARY_LOCATION_SKETCH:
return LibraryLocationSketch
}
return LibraryLocationIDEBuiltin
}
Expand Down
20 changes: 13 additions & 7 deletions rpc/cc/arduino/cli/commands/v1/lib.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rpc/cc/arduino/cli/commands/v1/lib.proto
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ enum LibraryLocation {
LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN = 3;
// Outside the `libraries` folders managed by the CLI.
LIBRARY_LOCATION_UNMANAGED = 4;
// Inside the `libraries` folder of the sketch.
LIBRARY_LOCATION_SKETCH = 5;
}

message ZipLibraryInstallRequest {
Expand Down

0 comments on commit eaf3f0d

Please sign in to comment.