Skip to content

Commit

Permalink
remove ImportedLibraries from context
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 1, 2023
1 parent 50e8370 commit cff9cdc
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 32 deletions.
25 changes: 18 additions & 7 deletions arduino/builder/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -63,8 +60,7 @@ func NewSketchLibrariesDetector(
verbose: verbose,
verboseInfoFn: verboseInfoFn,
verboseWarnFn: verboseWarnFn,

importedLibraries: importedLibraries,
importedLibraries: libraries.List{},
}
}

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions legacy/builder/container_find_includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/create_cmake_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/phases/libraries_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions legacy/builder/print_used_libraries_if_verbose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
1 change: 0 additions & 1 deletion legacy/builder/test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down
16 changes: 8 additions & 8 deletions legacy/builder/test/includes_to_include_folders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
}

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions legacy/builder/test/unused_compiled_libraries_remover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -104,7 +103,6 @@ type Context struct {

// Libraries handling
LibrariesManager *librariesmanager.LibrariesManager
ImportedLibraries libraries.List
LibrariesResolutionResults map[string]LibraryResolutionResult
IncludeFolders paths.PathList

Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/unused_compiled_libraries_remover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/warn_about_arch_incompatible_libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).",
Expand Down

0 comments on commit cff9cdc

Please sign in to comment.