Skip to content

Commit 970b696

Browse files
committed
Fix library prioritization
1 parent 23eec2c commit 970b696

File tree

8 files changed

+44
-12
lines changed

8 files changed

+44
-12
lines changed

arduino/libraries/libraries_location.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const (
3535
ReferencedPlatformBuiltIn
3636
// User are user installed libraries
3737
User
38+
// Unmanaged is for libraries set manually by the user in the CLI command or from the gRPC function.
39+
// Ideally it's used for `libraries` outside folders managed by the CLI.
40+
Unmanaged
3841
)
3942

4043
func (d *LibraryLocation) String() string {
@@ -47,6 +50,8 @@ func (d *LibraryLocation) String() string {
4750
return "ref-platform"
4851
case User:
4952
return "user"
53+
case Unmanaged:
54+
return "unmanaged"
5055
}
5156
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
5257
}
@@ -62,6 +67,8 @@ func (d *LibraryLocation) MarshalJSON() ([]byte, error) {
6267
return json.Marshal("ref-platform")
6368
case User:
6469
return json.Marshal("user")
70+
case Unmanaged:
71+
return json.Marshal("unmanaged")
6572
}
6673
return nil, fmt.Errorf("invalid library location value: %d", *d)
6774
}
@@ -81,6 +88,8 @@ func (d *LibraryLocation) UnmarshalJSON(b []byte) error {
8188
*d = ReferencedPlatformBuiltIn
8289
case "user":
8390
*d = User
91+
case "unmanaged":
92+
*d = Unmanaged
8493
}
8594
return fmt.Errorf("invalid library location: %s", s)
8695
}
@@ -96,6 +105,8 @@ func (d *LibraryLocation) ToRPCLibraryLocation() rpc.LibraryLocation {
96105
return rpc.LibraryLocation_LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN
97106
case User:
98107
return rpc.LibraryLocation_LIBRARY_LOCATION_USER
108+
case Unmanaged:
109+
return rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED
99110
}
100111
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
101112
}
@@ -111,6 +122,8 @@ func FromRPCLibraryLocation(l rpc.LibraryLocation) LibraryLocation {
111122
return ReferencedPlatformBuiltIn
112123
case rpc.LibraryLocation_LIBRARY_LOCATION_USER:
113124
return User
125+
case rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED:
126+
return Unmanaged
114127
}
115128
panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l))
116129
}

arduino/libraries/librariesresolver/cpp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ func computePriority(lib *libraries.Library, header, arch string) int {
158158
priority += 2
159159
case libraries.User:
160160
priority += 3
161+
case libraries.Unmanaged:
162+
priority += 4
161163
default:
162164
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
163165
}

cli/compile/compile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func NewCommand() *cobra.Command {
9898
command.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
9999
command.Flags().StringVar(&vidPid, "vid-pid", "", "When specified, VID/PID specific build properties are used, if board supports them.")
100100
command.Flags().StringSliceVar(&library, "library", []string{},
101-
"List of paths to libraries root folders. Can be used multiple times for different libraries.")
101+
"List of paths to libraries root folders. Libraries set this way have top priority in case of conflicts. Can be used multiple times for different libraries.")
102102
command.Flags().StringSliceVar(&libraries, "libraries", []string{},
103103
"List of custom libraries dir paths separated by commas. Or can be used multiple times for multiple libraries dir paths.")
104104
command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, "Optional, optimize compile output for debugging, rather than for release.")

docs/sketch-build-process.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ The "folder name priority" is determined as follows (in order of highest to lowe
102102

103103
The "location priority" is determined as follows (in order of highest to lowest priority):
104104

105+
1. The library is specified using the [`--library` option](commands/arduino-cli_compile.md#options) of
106+
`arduino-cli compile`
105107
1. The library is under a custom libraries path specified via the
106108
[`--libraries` option](commands/arduino-cli_compile.md#options) of `arduino-cli compile` (in decreasing order of
107109
priority when multiple custom paths are defined)

legacy/builder/libraries_loader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error {
6262
}
6363

6464
for _, dir := range ctx.LibrariesDirs {
65-
if err := lm.LoadLibraryFromDir(dir, libraries.User); err != nil {
65+
// Libraries specified this way have top priority
66+
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
6667
return err
6768
}
6869
}

rpc/cc/arduino/cli/commands/v1/lib.pb.go

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/commands/v1/lib.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ enum LibraryLocation {
297297
// this indicates the library is in the `libraries` subdirectory of a
298298
// platform referenced by the board's platform.
299299
LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN = 3;
300+
// Outside the `libraries` folders managed by the CLI.
301+
LIBRARY_LOCATION_UNMANAGED = 4;
300302
}
301303

302304
message ZipLibraryInstallRequest {

test/test_compile.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -928,24 +928,30 @@ def test_compile_with_library_priority(run_command, data_dir):
928928

929929
# Manually installs the same library and add a new custom header to it
930930
git_url = "https://github.com/arduino-libraries/WiFi101.git"
931-
lib_path = Path(data_dir, "my-libraries", "WiFi101")
932-
assert Repo.clone_from(git_url, lib_path, multi_options=["-b 0.16.1"])
933-
with open(lib_path / "src" / "WiFiSomething.h", "x") as f:
934-
f.writelines(["#ifndef WIFI_SOMETHING\n", "#define WIFI_SOMETHING\n", "#endif\n"])
931+
manually_install_lib_path = Path(data_dir, "my-libraries", "WiFi101")
932+
assert Repo.clone_from(git_url, manually_install_lib_path, multi_options=["-b 0.16.1"])
933+
# with open(manually_install_lib_path / "src" / "WiFiSomething.h", "x") as f:
934+
# f.writelines(["#ifndef WIFI_SOMETHING\n", "#define WIFI_SOMETHING\n", "#endif\n"])
935935

936936
# Install the same library we installed manually
937937
assert run_command("lib install WiFi101")
938938

939-
# Create new sketch and add custom header include
939+
# Create new sketch and add library include
940940
assert run_command(f"sketch new {sketch_path}")
941941
sketch_file = sketch_path / f"{sketch_name}.ino"
942942
lines = []
943943
with open(sketch_file, "r") as f:
944944
lines = f.readlines()
945-
lines = ["#include <WiFiSomething.h>"] + lines
945+
lines = ["#include <WiFi101.h>"] + lines
946946
with open(sketch_file, "w") as f:
947947
f.writelines(lines)
948948

949-
res = run_command(f"compile -b {fqbn} {sketch_path} --library {lib_path} -v")
949+
res = run_command(f"compile -b {fqbn} {sketch_path} --library {manually_install_lib_path} -v")
950950
assert res.ok
951-
assert "WiFi101" in res.stdout
951+
cli_installed_lib_path = Path(data_dir, "libraries", "WiFi101")
952+
expected_output = [
953+
'Multiple libraries were found for "WiFi101.h"',
954+
f" Used: {manually_install_lib_path}",
955+
f" Not used: {cli_installed_lib_path}",
956+
]
957+
assert "\n".join(expected_output) in res.stdout

0 commit comments

Comments
 (0)