Skip to content

Commit 23eec2c

Browse files
committed
Add --library flag to compile command
1 parent a4ee670 commit 23eec2c

File tree

17 files changed

+171
-57
lines changed

17 files changed

+171
-57
lines changed

arduino/libraries/librariesmanager/librariesmanager.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,29 @@ func (sc *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) err
204204
return nil
205205
}
206206

207+
// LoadLibraryFromDir loads one single library from the libRootDir.
208+
// libRootDir must point to the root of a valid library.
209+
// An error is returned if the path doesn't exist or loading of the library fails.
210+
func (lm *LibrariesManager) LoadLibraryFromDir(libRootDir *paths.Path, location libraries.LibraryLocation) error {
211+
if libRootDir.NotExist() {
212+
return fmt.Errorf("library path does not exist: %s", libRootDir)
213+
}
214+
215+
library, err := libraries.Load(libRootDir, location)
216+
if err != nil {
217+
return fmt.Errorf("loading library from %s: %s", libRootDir, err)
218+
}
219+
220+
alternatives, ok := lm.Libraries[library.Name]
221+
if !ok {
222+
alternatives = &LibraryAlternatives{}
223+
lm.Libraries[library.Name] = alternatives
224+
}
225+
alternatives.Add(library)
226+
227+
return nil
228+
}
229+
207230
// FindByReference return the installed library matching the Reference
208231
// name and version or, if the version is nil, the library installed
209232
// in the User folder.

cli/compile/compile.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ var (
5151
port string // Upload port, e.g.: COM10 or /dev/ttyACM0.
5252
verify bool // Upload, verify uploaded binary after the upload.
5353
exportDir string // The compiled binary is written to this file
54-
libraries []string // List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.
5554
optimizeForDebug bool // Optimize compile output for debug, not for release
5655
programmer string // Use the specified programmer to upload
5756
clean bool // Cleanup the build folder and do not use any cached build
5857
compilationDatabaseOnly bool // Only create compilation database without actually compiling
5958
sourceOverrides string // Path to a .json file that contains a set of replacements of the sketch source code.
59+
// library and libraries sound similar but they're actually different.
60+
// library expects a path to the root folder of one single library.
61+
// libraries expects a path to a directory containing multiple libraries, similarly to the <directories.user>/libraries path.
62+
library []string // List of paths to libraries root folders. Can be used multiple times for different libraries
63+
libraries []string // List of custom libraries dir paths separated by commas. Or can be used multiple times for multiple libraries paths.
6064
)
6165

6266
// NewCommand created a new `compile` command
@@ -93,8 +97,10 @@ func NewCommand() *cobra.Command {
9397
command.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0")
9498
command.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
9599
command.Flags().StringVar(&vidPid, "vid-pid", "", "When specified, VID/PID specific build properties are used, if board supports them.")
100+
command.Flags().StringSliceVar(&library, "library", []string{},
101+
"List of paths to libraries root folders. Can be used multiple times for different libraries.")
96102
command.Flags().StringSliceVar(&libraries, "libraries", []string{},
97-
"List of custom libraries paths separated by commas. Or can be used multiple times for multiple libraries paths.")
103+
"List of custom libraries dir paths separated by commas. Or can be used multiple times for multiple libraries dir paths.")
98104
command.Flags().BoolVar(&optimizeForDebug, "optimize-for-debug", false, "Optional, optimize compile output for debugging, rather than for release.")
99105
command.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload.")
100106
command.Flags().BoolVar(&compilationDatabaseOnly, "only-compilation-database", false, "Just produce the compilation database, without actually compiling.")
@@ -171,6 +177,7 @@ func run(cmd *cobra.Command, args []string) {
171177
Clean: clean,
172178
CreateCompilationDatabaseOnly: compilationDatabaseOnly,
173179
SourceOverride: overrides,
180+
Library: library,
174181
}
175182
compileOut := new(bytes.Buffer)
176183
compileErr := new(bytes.Buffer)

commands/compile/compile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
137137
builderCtx.OtherLibrariesDirs = paths.NewPathList(req.GetLibraries()...)
138138
builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))
139139

140+
builderCtx.LibrariesDirs = paths.NewPathList(req.Library...)
141+
140142
if req.GetBuildPath() == "" {
141143
builderCtx.BuildPath = bldr.GenBuildPath(sketch.FullPath)
142144
} else {

legacy/builder/libraries_loader.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error {
6161
return errors.WithStack(err)
6262
}
6363

64+
for _, dir := range ctx.LibrariesDirs {
65+
if err := lm.LoadLibraryFromDir(dir, libraries.User); err != nil {
66+
return err
67+
}
68+
}
69+
6470
if debugLevel > 0 {
6571
for _, lib := range lm.Libraries {
6672
for _, libAlt := range lib.Alternatives {

legacy/builder/types/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Context struct {
6767
BuiltInToolsDirs paths.PathList
6868
BuiltInLibrariesDirs paths.PathList
6969
OtherLibrariesDirs paths.PathList
70+
LibrariesDirs paths.PathList // List of paths pointing to individual libraries root folders
7071
SketchLocation *paths.Path
7172
WatchedLocations paths.PathList
7273
ArduinoAPIVersion string

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 52 additions & 41 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/compile.proto

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ message CompileRequest {
5757
// If jobs is set to 0, it will use the number of available CPUs as the
5858
// maximum.
5959
int32 jobs = 14;
60-
// List of custom libraries paths separated by commas.
60+
// List of custom libraries dir paths.
6161
repeated string libraries = 15;
6262
// Optimize compile output for debug, not for release.
6363
bool optimize_for_debug = 16;
@@ -78,6 +78,8 @@ message CompileRequest {
7878
// When set to `true` the compiled binary will be copied to the export
7979
// directory.
8080
google.protobuf.BoolValue export_binaries = 23;
81+
// List of paths to libraries root folders
82+
repeated string library = 24;
8183
}
8284

8385
message CompileResponse {

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

Lines changed: 1 addition & 1 deletion
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.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/cc/arduino/cli/debug/v1/debug.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)