Skip to content

Auto-import libraries based on sketch profile. #2945

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
}
lmb.AddLibrariesDir(librariesmanager.LibrariesDir{
Path: libDir,
Location: libraries.Unmanaged,
Location: libraries.Profile,
IsSingleLibrary: true,
})
continue
Expand Down Expand Up @@ -428,7 +428,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor

lmb.AddLibrariesDir(librariesmanager.LibrariesDir{
Path: libRoot,
Location: libraries.User,
Location: libraries.Profile,
})
}
}
Expand Down
7 changes: 4 additions & 3 deletions commands/service_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
return &cmderrors.CantOpenSketchError{Cause: err}
}

profile := pme.GetProfile()
fqbnIn := req.GetFqbn()
if fqbnIn == "" && sk != nil {
if pme.GetProfile() != nil {
fqbnIn = pme.GetProfile().FQBN
if profile != nil {
fqbnIn = profile.FQBN
} else {
fqbnIn = sk.GetDefaultFQBN()
}
Expand Down Expand Up @@ -224,7 +225,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
otherLibrariesDirs.Add(s.settings.LibrariesDir())

var libsManager *librariesmanager.LibrariesManager
if pme.GetProfile() != nil {
if profile != nil {
libsManager = lm
}

Expand Down
42 changes: 27 additions & 15 deletions internal/arduino/builder/internal/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,25 @@ func (l *SketchLibrariesDetector) ImportedLibraries() libraries.List {
return l.importedLibraries
}

// AppendImportedLibraries todo should rename this, probably after refactoring the
// container_find_includes command.
func (l *SketchLibrariesDetector) AppendImportedLibraries(library *libraries.Library) {
// addAndBuildLibrary adds the given library to the imported libraries list and queues its source files
// for further processing.
func (l *SketchLibrariesDetector) addAndBuildLibrary(sourceFileQueue *uniqueSourceFileQueue, librariesBuildPath *paths.Path, library *libraries.Library) {
l.importedLibraries = append(l.importedLibraries, library)
if library.Precompiled && library.PrecompiledWithSources {
// Fully precompiled libraries should have no dependencies to avoid ABI breakage
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
l.logger.Info(i18n.Tr("Skipping dependencies detection for precompiled library %[1]s", library.Name))
}
} else {
for _, sourceDir := range library.SourceDirs() {
l.queueSourceFilesFromFolder(
sourceFileQueue,
sourceDir.Dir, sourceDir.Recurse,
library.SourceDir,
librariesBuildPath.Join(library.DirName),
library.UtilityDir)
}
}
}

// PrintUsedAndNotUsedLibraries todo
Expand Down Expand Up @@ -269,6 +284,14 @@ func (l *SketchLibrariesDetector) findIncludes(
l.queueSourceFilesFromFolder(sourceFileQueue, srcSubfolderPath, true /* recurse */, sketchBuildPath, sketchBuildPath)
}

for _, library := range l.librariesManager.FindAllInstalled() {
if library.Location == libraries.Profile {
l.logger.Info(i18n.Tr("The library %[1]s has been automatically added from sketch project.", library.Name))
l.addAndBuildLibrary(sourceFileQueue, librariesBuildPath, library)
l.appendIncludeFolder(cache, mergedfile.SourcePath(), "", library.SourceDir)
}
}

for !sourceFileQueue.empty() {
err := l.findIncludesUntilDone(ctx, cache, sourceFileQueue, buildProperties, librariesBuildPath, platformArch)
if err != nil {
Expand Down Expand Up @@ -405,20 +428,9 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
// Add this library to the list of libraries, the
// include path and queue its source files for further
// include scanning
l.AppendImportedLibraries(library)
l.addAndBuildLibrary(sourceFileQueue, librariesBuildPath, library)
l.appendIncludeFolder(cache, sourcePath, missingIncludeH, library.SourceDir)

if library.Precompiled && library.PrecompiledWithSources {
// Fully precompiled libraries should have no dependencies to avoid ABI breakage
if l.logger.VerbosityLevel() == logger.VerbosityVerbose {
l.logger.Info(i18n.Tr("Skipping dependencies detection for precompiled library %[1]s", library.Name))
}
} else {
for _, sourceDir := range library.SourceDirs() {
l.queueSourceFilesFromFolder(sourceFileQueue, sourceDir.Dir, sourceDir.Recurse,
library.SourceDir, librariesBuildPath.Join(library.DirName), library.UtilityDir)
}
}
first = false
}
}
Expand Down
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
// Profile is for libraries that are part of a sketch profile
Profile
)

func (d *LibraryLocation) String() string {
Expand All @@ -54,6 +56,8 @@ func (d *LibraryLocation) String() string {
return "user"
case Unmanaged:
return "unmanaged"
case Profile:
return "profile"
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 "profile":
*d = Profile
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 Profile:
return rpc.LibraryLocation_LIBRARY_LOCATION_PROFILE
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_PROFILE:
return Profile
default:
panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l))
}
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;
// Library is part of the sketch profile.
LIBRARY_LOCATION_PROFILE = 5;
}

message ZipLibraryInstallRequest {
Expand Down
Loading