diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index b620280940e..d6f273f5f4c 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -40,21 +40,18 @@ type SketchLibrariesDetector struct { verbose bool verboseInfoFn func(msg string) verboseWarnFn func(msg string) - - importedLibraries libraries.List - librariesResolutionResults map[string]libraryResolutionResult + importedLibraries libraries.List + librariesResolutionResults map[string]libraryResolutionResult } // NewSketchLibrariesDetector todo func NewSketchLibrariesDetector( lm *librariesmanager.LibrariesManager, libsResolver *librariesresolver.Cpp, - importedLibraries libraries.List, verbose, useCachedLibrariesResolution bool, verboseInfoFn func(msg string), verboseWarnFn func(msg string), ) *SketchLibrariesDetector { - return &SketchLibrariesDetector{ librariesManager: lm, librariesResolver: libsResolver, @@ -63,8 +60,7 @@ func NewSketchLibrariesDetector( verbose: verbose, verboseInfoFn: verboseInfoFn, verboseWarnFn: verboseWarnFn, - - importedLibraries: importedLibraries, + importedLibraries: libraries.List{}, } } @@ -112,15 +108,30 @@ func (l *SketchLibrariesDetector) ResolveLibrary(header, platformArch string) *l return selected } +// ImportedLibraries todo func (l *SketchLibrariesDetector) ImportedLibraries() libraries.List { // TODO understand if we have to do a deepcopy return l.importedLibraries } +// AppendImportedLibraries todo should rename this, probably after refactoring the +// container_find_includes command. +func (l *SketchLibrariesDetector) AppendImportedLibraries(library *libraries.Library) { + l.importedLibraries = append(l.importedLibraries, library) +} + +// UseCachedLibrariesResolution todo func (l *SketchLibrariesDetector) UseCachedLibrariesResolution() bool { return l.useCachedLibrariesResolution } +// AppendIncludeFolder todo should rename this, probably after refactoring the +// container_find_includes command. +//func (l *SketchLibrariesDetector) AppendIncludeFolder(ctx *types.Context, cache *includeCache, sourceFilePath *paths.Path, include string, folder *paths.Path) { +// ctx.IncludeFolders = append(ctx.IncludeFolders, folder) +// cache.ExpectEntry(sourceFilePath, include, folder) +//} + // LibrariesLoader todo func LibrariesLoader( useCachedLibrariesResolution bool, diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 56ea6728d0a..c5f36dbcc7c 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -269,7 +269,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.SketchLibrariesDetector = bldr.NewSketchLibrariesDetector( lm, libsResolver, - builderCtx.ImportedLibraries, builderCtx.Verbose, useCachedLibrariesResolution, func(msg string) { builderCtx.Info(msg) }, @@ -313,7 +312,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream defer func() { importedLibs := []*rpc.Library{} - for _, lib := range builderCtx.ImportedLibraries { + for _, lib := range builderCtx.SketchLibrariesDetector.ImportedLibraries() { rpcLib, err := lib.ToRPCLibrary() if err != nil { msg := tr("Error getting information for library %s", lib.Name) + ": " + err.Error() + "\n" diff --git a/legacy/builder/container_find_includes.go b/legacy/builder/container_find_includes.go index c810a30622a..b90c25d46d5 100644 --- a/legacy/builder/container_find_includes.go +++ b/legacy/builder/container_find_includes.go @@ -405,7 +405,7 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFileQu // Add this library to the list of libraries, the // include path and queue its source files for further // include scanning - ctx.ImportedLibraries = append(ctx.ImportedLibraries, library) + ctx.SketchLibrariesDetector.AppendImportedLibraries(library) appendIncludeFolder(ctx, cache, sourcePath, missingIncludeH, library.SourceDir) if library.Precompiled && library.PrecompiledWithSources { @@ -448,11 +448,11 @@ func ResolveLibrary(ctx *types.Context, header string) *libraries.Library { } func failIfImportedLibraryIsWrong(ctx *types.Context) error { - if len(ctx.ImportedLibraries) == 0 { + if len(ctx.SketchLibrariesDetector.ImportedLibraries()) == 0 { return nil } - for _, library := range ctx.ImportedLibraries { + for _, library := range ctx.SketchLibrariesDetector.ImportedLibraries() { if !library.IsLegacy { if library.InstallDir.Join("arch").IsDir() { return errors.New(tr("%[1]s folder is no longer supported! See %[2]s for more information", "'arch'", "http://goo.gl/gfFJzU")) diff --git a/legacy/builder/create_cmake_rule.go b/legacy/builder/create_cmake_rule.go index acbce09e2cb..37b60e92fac 100644 --- a/legacy/builder/create_cmake_rule.go +++ b/legacy/builder/create_cmake_rule.go @@ -198,7 +198,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { cmakeFile := cmakeFolder.Join("CMakeLists.txt") dynamicLibsFromPkgConfig := map[string]bool{} - for _, library := range ctx.ImportedLibraries { + for _, library := range ctx.SketchLibrariesDetector.ImportedLibraries() { // Copy used libraries in the correct folder libDir := libBaseFolder.Join(library.DirName) mcu := ctx.BuildProperties.Get(constants.BUILD_PROPERTIES_BUILD_MCU) diff --git a/legacy/builder/phases/libraries_builder.go b/legacy/builder/phases/libraries_builder.go index 5ee81bb4385..3d28fbf5520 100644 --- a/legacy/builder/phases/libraries_builder.go +++ b/legacy/builder/phases/libraries_builder.go @@ -38,7 +38,7 @@ func (s *LibrariesBuilder) Run(ctx *types.Context) error { librariesBuildPath := ctx.LibrariesBuildPath buildProperties := ctx.BuildProperties includes := f.Map(ctx.IncludeFolders.AsStrings(), utils.WrapWithHyphenI) - libs := ctx.ImportedLibraries + libs := ctx.SketchLibrariesDetector.ImportedLibraries() if err := librariesBuildPath.MkdirAll(); err != nil { return errors.WithStack(err) diff --git a/legacy/builder/print_used_libraries_if_verbose.go b/legacy/builder/print_used_libraries_if_verbose.go index 8c32c925995..15211a46519 100644 --- a/legacy/builder/print_used_libraries_if_verbose.go +++ b/legacy/builder/print_used_libraries_if_verbose.go @@ -24,11 +24,11 @@ import ( type PrintUsedLibrariesIfVerbose struct{} func (s *PrintUsedLibrariesIfVerbose) Run(ctx *types.Context) error { - if !ctx.Verbose || len(ctx.ImportedLibraries) == 0 { + if !ctx.Verbose || len(ctx.SketchLibrariesDetector.ImportedLibraries()) == 0 { return nil } - for _, library := range ctx.ImportedLibraries { + for _, library := range ctx.SketchLibrariesDetector.ImportedLibraries() { legacy := "" if library.IsLegacy { legacy = tr("(legacy)") diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 621df16037d..95816e42a0e 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -137,7 +137,6 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.LibrariesManager = lm ctx.SketchLibrariesDetector = bldr.NewSketchLibrariesDetector( lm, libsResolver, - ctx.ImportedLibraries, ctx.Verbose, false, func(msg string) { ctx.Info(msg) }, diff --git a/legacy/builder/test/includes_to_include_folders_test.go b/legacy/builder/test/includes_to_include_folders_test.go index 127e953e2c0..ed6afc51d1e 100644 --- a/legacy/builder/test/includes_to_include_folders_test.go +++ b/legacy/builder/test/includes_to_include_folders_test.go @@ -44,7 +44,7 @@ func TestIncludesToIncludeFolders(t *testing.T) { NoError(t, err) } - importedLibraries := ctx.ImportedLibraries + importedLibraries := ctx.SketchLibrariesDetector.ImportedLibraries() require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "Bridge", importedLibraries[0].Name) } @@ -67,7 +67,7 @@ func TestIncludesToIncludeFoldersSketchWithIfDef(t *testing.T) { NoError(t, err) } - importedLibraries := ctx.ImportedLibraries + importedLibraries := ctx.SketchLibrariesDetector.ImportedLibraries() require.Equal(t, 0, len(importedLibraries)) } @@ -89,7 +89,7 @@ func TestIncludesToIncludeFoldersIRremoteLibrary(t *testing.T) { NoError(t, err) } - importedLibraries := ctx.ImportedLibraries + importedLibraries := ctx.SketchLibrariesDetector.ImportedLibraries() sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 2, len(importedLibraries)) require.Equal(t, "Bridge", importedLibraries[0].Name) @@ -114,7 +114,7 @@ func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) { NoError(t, err) } - importedLibraries := ctx.ImportedLibraries + importedLibraries := ctx.SketchLibrariesDetector.ImportedLibraries() sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 2, len(importedLibraries)) require.Equal(t, "ANewLibrary-master", importedLibraries[0].Name) @@ -144,7 +144,7 @@ func TestIncludesToIncludeFoldersDuplicateLibs(t *testing.T) { NoError(t, err) } - importedLibraries := ctx.ImportedLibraries + importedLibraries := ctx.SketchLibrariesDetector.ImportedLibraries() sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "SPI", importedLibraries[0].Name) @@ -175,7 +175,7 @@ func TestIncludesToIncludeFoldersDuplicateLibsWithConflictingLibsOutsideOfPlatfo NoError(t, err) } - importedLibraries := ctx.ImportedLibraries + importedLibraries := ctx.SketchLibrariesDetector.ImportedLibraries() sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "SPI", importedLibraries[0].Name) @@ -206,7 +206,7 @@ func TestIncludesToIncludeFoldersDuplicateLibs2(t *testing.T) { NoError(t, err) } - importedLibraries := ctx.ImportedLibraries + importedLibraries := ctx.SketchLibrariesDetector.ImportedLibraries() sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 1, len(importedLibraries)) require.Equal(t, "USBHost", importedLibraries[0].Name) @@ -232,7 +232,7 @@ func TestIncludesToIncludeFoldersSubfolders(t *testing.T) { NoError(t, err) } - importedLibraries := ctx.ImportedLibraries + importedLibraries := ctx.SketchLibrariesDetector.ImportedLibraries() sort.Sort(ByLibraryName(importedLibraries)) require.Equal(t, 3, len(importedLibraries)) require.Equal(t, "testlib1", importedLibraries[0].Name) diff --git a/legacy/builder/test/unused_compiled_libraries_remover_test.go b/legacy/builder/test/unused_compiled_libraries_remover_test.go index f6965cee0ff..5c04b1cee10 100644 --- a/legacy/builder/test/unused_compiled_libraries_remover_test.go +++ b/legacy/builder/test/unused_compiled_libraries_remover_test.go @@ -18,6 +18,7 @@ package test import ( "testing" + bldr "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/libraries" "github.com/arduino/arduino-cli/legacy/builder" "github.com/arduino/arduino-cli/legacy/builder/types" @@ -36,7 +37,10 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) { ctx := &types.Context{} ctx.LibrariesBuildPath = temp - ctx.ImportedLibraries = []*libraries.Library{{Name: "Bridge"}} + ctx.SketchLibrariesDetector = bldr.NewSketchLibrariesDetector( + nil, nil, false, false, nil, nil, + ) + ctx.SketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) cmd := builder.UnusedCompiledLibrariesRemover{} err = cmd.Run(ctx) @@ -56,7 +60,10 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) { func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { ctx := &types.Context{} ctx.LibrariesBuildPath = paths.TempDir().Join("test") - ctx.ImportedLibraries = []*libraries.Library{{Name: "Bridge"}} + ctx.SketchLibrariesDetector = bldr.NewSketchLibrariesDetector( + nil, nil, false, false, nil, nil, + ) + ctx.SketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) cmd := builder.UnusedCompiledLibrariesRemover{} err := cmd.Run(ctx) @@ -73,8 +80,10 @@ func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) ctx := &types.Context{} + ctx.SketchLibrariesDetector = bldr.NewSketchLibrariesDetector( + nil, nil, false, false, nil, nil, + ) ctx.LibrariesBuildPath = temp - ctx.ImportedLibraries = []*libraries.Library{} cmd := builder.UnusedCompiledLibrariesRemover{} err = cmd.Run(ctx) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index edf938877bb..beb4336fac8 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -25,7 +25,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" - "github.com/arduino/arduino-cli/arduino/libraries" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" "github.com/arduino/arduino-cli/arduino/sketch" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -104,7 +103,6 @@ type Context struct { // Libraries handling LibrariesManager *librariesmanager.LibrariesManager - ImportedLibraries libraries.List LibrariesResolutionResults map[string]LibraryResolutionResult IncludeFolders paths.PathList diff --git a/legacy/builder/unused_compiled_libraries_remover.go b/legacy/builder/unused_compiled_libraries_remover.go index d1b36b147ac..03ea5b85d01 100644 --- a/legacy/builder/unused_compiled_libraries_remover.go +++ b/legacy/builder/unused_compiled_libraries_remover.go @@ -31,7 +31,7 @@ func (s *UnusedCompiledLibrariesRemover) Run(ctx *types.Context) error { return nil } - libraryNames := toLibraryNames(ctx.ImportedLibraries) + libraryNames := toLibraryNames(ctx.SketchLibrariesDetector.ImportedLibraries()) files, err := librariesBuildPath.ReadDir() if err != nil { diff --git a/legacy/builder/warn_about_arch_incompatible_libraries.go b/legacy/builder/warn_about_arch_incompatible_libraries.go index 712f79b2fe5..c815a54f3aa 100644 --- a/legacy/builder/warn_about_arch_incompatible_libraries.go +++ b/legacy/builder/warn_about_arch_incompatible_libraries.go @@ -33,7 +33,7 @@ func (s *WarnAboutArchIncompatibleLibraries) Run(ctx *types.Context) error { archs = append(archs, strings.Split(overrides, ",")...) } - for _, importedLibrary := range ctx.ImportedLibraries { + for _, importedLibrary := range ctx.SketchLibrariesDetector.ImportedLibraries() { if !importedLibrary.SupportsAnyArchitectureIn(archs...) { ctx.Info( tr("WARNING: library %[1]s claims to run on %[2]s architecture(s) and may be incompatible with your current board which runs on %[3]s architecture(s).",