8000 Add `--library` flag to `compile` command by silvanocerza · Pull Request #1258 · arduino/arduino-cli · GitHub
[go: up one dir, main page]

Skip to content

Add --library flag to compile command #1258

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 5 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix library prioritization
  • Loading branch information
silvanocerza committed Apr 8, 2021
commit 970b696d151d3051b4c34fe2d8fc9e9da0430573
13 changes: 13 additions & 0 deletions arduino/libraries/libraries_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const (
ReferencedPlatformBuiltIn
// User are user installed libraries
User
// 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
)

func (d *LibraryLocation) String() string {
Expand All @@ -47,6 +50,8 @@ func (d *LibraryLocation) String() string {
return "ref-platform"
case User:
return "user"
case Unmanaged:
return "unmanaged"
}
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
}
Expand All @@ -62,6 +67,8 @@ func (d *LibraryLocation) MarshalJSON() ([]byte, error) {
return json.Marshal("ref-platform")
case User:
return json.Marshal("user")
case Unmanaged:
return json.Marshal("unmanaged")
}
return nil, fmt.Errorf("invalid library location value: %d", *d)
}
Expand All @@ -81,6 +88,8 @@ func (d *LibraryLocation) UnmarshalJSON(b []byte) error {
*d = ReferencedPlatformBuiltIn
case "user":
*d = User
case "unmanaged":
*d = Unmanaged
}
return fmt.Errorf("invalid library location: %s", s)
}
Expand All @@ -96,6 +105,8 @@ func (d *LibraryLocation) ToRPCLibraryLocation() rpc.LibraryLocation {
return rpc.LibraryLocation_LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN
case User:
return rpc.LibraryLocation_LIBRARY_LOCATION_USER
case Unmanaged:
return rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED
}
panic(fmt.Sprintf("invalid LibraryLocation value %d", *d))
}
Expand All @@ -111,6 +122,8 @@ func FromRPCLibraryLocation(l rpc.LibraryLocation) LibraryLocation {
return ReferencedPlatformBuiltIn
case rpc.LibraryLocation_LIBRARY_LOCATION_USER:
return User
case rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED:
return Unmanaged
}
panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l))
}
2 changes: 2 additions & 0 deletions arduino/libraries/librariesresolver/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func computePriority(lib *libraries.Library, header, arch string) int {
priority += 2
case libraries.User:
priority += 3
case libraries.Unmanaged:
priority += 4
default:
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
}
Expand Down
2 changes: 1 addition & 1 deletion cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewCommand() *cobra.Command {
command.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
command.Flags().StringVar(&vidPid, "vid-pid", "", "When specified, VID/PID specific build properties are used, if board supports them.")
command.Flags().StringSliceVar(&library, "library", []string{},
"List of paths to libraries root folders. Can be used multiple times for different libraries.")
"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.")
command.Flags().StringSliceVar(&libraries, "libraries", []string{},
"List of custom libraries dir paths separated by commas. Or can be used multiple times for multiple libraries dir paths.")
command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, "Optional, optimize compile output for debugging, rather than for release.")
Expand Down
2 changes: 2 additions & 0 deletions docs/sketch-build-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ The "folder name priority" is determined as follows (in order of highest to lowe

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

1. The library is specified using the [`--library` option](commands/arduino-cli_compile.md#options) of
`arduino-cli compile`
1. The library is under a custom libraries path specified via the
[`--libraries` option](commands/arduino-cli_compile.md#options) of `arduino-cli compile` (in decreasing order of
priority when multiple custom paths are defined)
Expand Down
3 changes: 2 additions & 1 deletion legacy/builder/libraries_loader.go
< 8000 /div>
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error {
}

for _, dir := range ctx.LibrariesDirs {
if err := lm.LoadLibraryFromDir(dir, libraries.User); err != nil {
// Libraries specified this way have top priority
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
return err
}
}
Expand Down
10 changes: 8 additions & 2 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 @@ -297,6 +297,8 @@ enum LibraryLocation {
// this indicates the library is in the `libraries` subdirectory of a
// platform referenced by the board's platform.
LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN = 3;
// Outside the `libraries` folders managed by the CLI.
LIBRARY_LOCATION_UNMANAGED = 4;
}

message ZipLibraryInstallRequest {
Expand Down
22 changes: 14 additions & 8 deletions test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,24 +928,30 @@ def test_compile_with_library_priority(run_command, data_dir):

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

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

# Create new sketch and add custom header include
# Create new sketch and add library include
assert run_command(f"sketch new {sketch_path}")
sketch_file = sketch_path / f"{sketch_name}.ino"
lines = []
with open(sketch_file, "r") as f:
lines = f.readlines()
lines = ["#include <WiFiSomething.h>"] + lines
lines = ["#include <WiFi101.h>"] + lines
with open(sketch_file, "w") as f:
f.writelines(lines)

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