From 0c5888f425592109e2cefc580cc3ce32e02e6fb7 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 3 Jun 2025 11:03:38 +0200 Subject: [PATCH 1/2] Refactored function it will be useful in the next commits. --- .../builder/internal/detector/detector.go | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/internal/arduino/builder/internal/detector/detector.go b/internal/arduino/builder/internal/detector/detector.go index a983370b075..9cb32d742a9 100644 --- a/internal/arduino/builder/internal/detector/detector.go +++ b/internal/arduino/builder/internal/detector/detector.go @@ -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 @@ -405,20 +420,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 } } From 046eb740f93301faac0014f2dda79d0b5cf99fcc Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 3 Jun 2025 11:03:59 +0200 Subject: [PATCH 2/2] Always add to the build the libraries requested in the active sketch profile. This will improve sketch compile time. --- commands/instances.go | 4 ++-- commands/service_compile.go | 7 ++++--- .../builder/internal/detector/detector.go | 8 ++++++++ .../arduino/libraries/libraries_location.go | 11 ++++++++++ rpc/cc/arduino/cli/commands/v1/lib.pb.go | 20 ++++++++++++------- rpc/cc/arduino/cli/commands/v1/lib.proto | 2 ++ 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/commands/instances.go b/commands/instances.go index 0145f3639b5..0b5127f0b8b 100644 --- a/commands/instances.go +++ b/commands/instances.go @@ -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 @@ -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, }) } } diff --git a/commands/service_compile.go b/commands/service_compile.go index ea36641c5e3..1586ef6f953 100644 --- a/commands/service_compile.go +++ b/commands/service_compile.go @@ -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() } @@ -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 } diff --git a/internal/arduino/builder/internal/detector/detector.go b/internal/arduino/builder/internal/detector/detector.go index 9cb32d742a9..516a605821e 100644 --- a/internal/arduino/builder/internal/detector/detector.go +++ b/internal/arduino/builder/internal/detector/detector.go @@ -284,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 { diff --git a/internal/arduino/libraries/libraries_location.go b/internal/arduino/libraries/libraries_location.go index a2a4423a54d..d815157bef3 100644 --- a/internal/arduino/libraries/libraries_location.go +++ b/internal/arduino/libraries/libraries_location.go @@ -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 { @@ -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)) } @@ -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)) } @@ -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)) } @@ -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)) } diff --git a/rpc/cc/arduino/cli/commands/v1/lib.pb.go b/rpc/cc/arduino/cli/commands/v1/lib.pb.go index 0910be91d8a..cff75c6a941 100644 --- a/rpc/cc/arduino/cli/commands/v1/lib.pb.go +++ b/rpc/cc/arduino/cli/commands/v1/lib.pb.go @@ -200,6 +200,8 @@ const ( LibraryLocation_LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN LibraryLocation = 3 // Outside the `libraries` folders managed by the CLI. LibraryLocation_LIBRARY_LOCATION_UNMANAGED LibraryLocation = 4 + // Library is part of the sketch profile. + LibraryLocation_LIBRARY_LOCATION_PROFILE LibraryLocation = 5 ) // Enum value maps for LibraryLocation. @@ -210,6 +212,7 @@ var ( 2: "LIBRARY_LOCATION_PLATFORM_BUILTIN", 3: "LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN", 4: "LIBRARY_LOCATION_UNMANAGED", + 5: "LIBRARY_LOCATION_PROFILE", } LibraryLocation_value = map[string]int32{ "LIBRARY_LOCATION_BUILTIN": 0, @@ -217,6 +220,7 @@ var ( "LIBRARY_LOCATION_PLATFORM_BUILTIN": 2, "LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN": 3, "LIBRARY_LOCATION_UNMANAGED": 4, + "LIBRARY_LOCATION_PROFILE": 5, } ) @@ -3206,7 +3210,7 @@ var file_cc_arduino_cli_commands_v1_lib_proto_rawDesc = []byte{ 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x41, 0x59, 0x4f, 0x55, 0x54, 0x5f, 0x46, 0x4c, 0x41, 0x54, 0x10, 0x00, 0x12, 0x1c, 0x0a, 0x18, 0x4c, 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x41, 0x59, 0x4f, 0x55, 0x54, 0x5f, 0x52, 0x45, 0x43, 0x55, 0x52, 0x53, 0x49, 0x56, 0x45, - 0x10, 0x01, 0x2a, 0xc3, 0x01, 0x0a, 0x0f, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x4c, 0x6f, + 0x10, 0x01, 0x2a, 0xe1, 0x01, 0x0a, 0x0f, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1c, 0x0a, 0x18, 0x4c, 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x4f, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x42, 0x55, 0x49, 0x4c, 0x54, 0x49, 0x4e, 0x10, 0x00, 0x12, 0x19, 0x0a, 0x15, 0x4c, 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, @@ -3218,12 +3222,14 @@ var file_cc_arduino_cli_commands_v1_lib_proto_rawDesc = []byte{ 0x45, 0x4e, 0x43, 0x45, 0x44, 0x5f, 0x50, 0x4c, 0x41, 0x54, 0x46, 0x4f, 0x52, 0x4d, 0x5f, 0x42, 0x55, 0x49, 0x4c, 0x54, 0x49, 0x4e, 0x10, 0x03, 0x12, 0x1e, 0x0a, 0x1a, 0x4c, 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x4f, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x55, 0x4e, 0x4d, - 0x41, 0x4e, 0x41, 0x47, 0x45, 0x44, 0x10, 0x04, 0x42, 0x48, 0x5a, 0x46, 0x67, 0x69, 0x74, 0x68, - 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x61, - 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2d, 0x63, 0x6c, 0x69, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x63, - 0x63, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x63, 0x6c, 0x69, 0x2f, 0x63, 0x6f, - 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, - 0x64, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x41, 0x4e, 0x41, 0x47, 0x45, 0x44, 0x10, 0x04, 0x12, 0x1c, 0x0a, 0x18, 0x4c, 0x49, 0x42, 0x52, + 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x4f, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x50, 0x52, 0x4f, + 0x46, 0x49, 0x4c, 0x45, 0x10, 0x05, 0x42, 0x48, 0x5a, 0x46, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, + 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x61, 0x72, 0x64, + 0x75, 0x69, 0x6e, 0x6f, 0x2d, 0x63, 0x6c, 0x69, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x63, 0x63, 0x2f, + 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x63, 0x6c, 0x69, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, + 0x61, 0x6e, 0x64, 0x73, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/rpc/cc/arduino/cli/commands/v1/lib.proto b/rpc/cc/arduino/cli/commands/v1/lib.proto index 1878b07ec53..9350440bb37 100644 --- a/rpc/cc/arduino/cli/commands/v1/lib.proto +++ b/rpc/cc/arduino/cli/commands/v1/lib.proto @@ -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 {