From e22d5d305f1fe8c4ab53c566d9460b43ae66be1e Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Thu, 14 Sep 2023 14:45:34 +0200 Subject: [PATCH] Move the detector inside arduino/builder --- arduino/builder/builder.go | 55 ++++++++++++++++++++++++++----------- commands/compile/compile.go | 42 ++++++++-------------------- 2 files changed, 50 insertions(+), 47 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index af1ead37ec1..ea07343df0a 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -24,6 +24,7 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/cores" + "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" @@ -78,6 +79,7 @@ type Builder struct { buildArtifacts *BuildArtifacts + *detector.SketchLibrariesDetector *BuildOptionsManager } @@ -110,6 +112,9 @@ func NewBuilder( sourceOverrides map[string]string, onlyUpdateCompilationDatabase bool, targetPlatform, actualPlatform *cores.PlatformRelease, + useCachedLibrariesResolution bool, + librariesManager *librariesmanager.LibrariesManager, + libraryDirs paths.PathList, logger *logger.BuilderLogger, progressStats *progress.Struct, ) (*Builder, error) { @@ -164,6 +169,18 @@ func NewBuilder( progressStats = progress.New(nil) } + libsManager, libsResolver, verboseOut, err := detector.LibrariesLoader( + useCachedLibrariesResolution, librariesManager, + builtInLibrariesDirs, libraryDirs, otherLibrariesDirs, + actualPlatform, targetPlatform, + ) + if err != nil { + return nil, err + } + if logger.Verbose() { + logger.Warn(string(verboseOut)) + } + return &Builder{ sketch: sk, buildProperties: buildProperties, @@ -184,6 +201,12 @@ func NewBuilder( buildArtifacts: &BuildArtifacts{}, targetPlatform: targetPlatform, actualPlatform: actualPlatform, + SketchLibrariesDetector: detector.NewSketchLibrariesDetector( + libsManager, libsResolver, + useCachedLibrariesResolution, + onlyUpdateCompilationDatabase, + logger, + ), BuildOptionsManager: NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, builtInLibrariesDirs, buildPath, @@ -230,13 +253,13 @@ func (b *Builder) TargetPlatform() *cores.PlatformRelease { } // Preprocess fixdoc -func (b *Builder) Preprocess(detector *detector.SketchLibrariesDetector) error { +func (b *Builder) Preprocess() error { b.Progress.AddSubSteps(6) defer b.Progress.RemoveSubSteps() - return b.preprocess(detector) + return b.preprocess() } -func (b *Builder) preprocess(detector *detector.SketchLibrariesDetector) error { +func (b *Builder) preprocess() error { if err := b.buildPath.MkdirAll(); err != nil { return err } @@ -260,7 +283,7 @@ func (b *Builder) preprocess(detector *detector.SketchLibrariesDetector) error { b.Progress.PushProgress() b.logIfVerbose(false, tr("Detecting libraries used...")) - err := detector.FindIncludes( + err := b.SketchLibrariesDetector.FindIncludes( b.GetBuildPath(), b.GetBuildProperties().GetPath("build.core.path"), b.GetBuildProperties().GetPath("build.variant.path"), @@ -276,12 +299,12 @@ func (b *Builder) preprocess(detector *detector.SketchLibrariesDetector) error { b.Progress.CompleteStep() b.Progress.PushProgress() - b.WarnAboutArchIncompatibleLibraries(detector.ImportedLibraries()) + b.WarnAboutArchIncompatibleLibraries(b.SketchLibrariesDetector.ImportedLibraries()) b.Progress.CompleteStep() b.Progress.PushProgress() b.logIfVerbose(false, tr("Generating function prototypes...")) - if err := b.PreprocessSketch(detector.IncludeFolders()); err != nil { + if err := b.PreprocessSketch(b.SketchLibrariesDetector.IncludeFolders()); err != nil { return err } b.Progress.CompleteStep() @@ -309,28 +332,28 @@ func (b *Builder) logIfVerbose(warn bool, msg string) { } // Build fixdoc -func (b *Builder) Build(detector *detector.SketchLibrariesDetector) error { +func (b *Builder) Build() error { b.Progress.AddSubSteps(6 /** preprocess **/ + 21 /** build **/) defer b.Progress.RemoveSubSteps() - if err := b.preprocess(detector); err != nil { + if err := b.preprocess(); err != nil { return err } - buildErr := b.build(detector) + buildErr := b.build() - detector.PrintUsedAndNotUsedLibraries(buildErr != nil) + b.SketchLibrariesDetector.PrintUsedAndNotUsedLibraries(buildErr != nil) b.Progress.CompleteStep() b.Progress.PushProgress() - b.PrintUsedLibraries(detector.ImportedLibraries()) + b.PrintUsedLibraries(b.SketchLibrariesDetector.ImportedLibraries()) b.Progress.CompleteStep() b.Progress.PushProgress() if buildErr != nil { return buildErr } - if err := b.ExportProjectCMake(detector.ImportedLibraries(), detector.IncludeFolders()); err != nil { + if err := b.ExportProjectCMake(b.SketchLibrariesDetector.ImportedLibraries(), b.SketchLibrariesDetector.IncludeFolders()); err != nil { return err } b.Progress.CompleteStep() @@ -346,7 +369,7 @@ func (b *Builder) Build(detector *detector.SketchLibrariesDetector) error { } // Build fixdoc -func (b *Builder) build(detector *detector.SketchLibrariesDetector) error { +func (b *Builder) build() error { b.logIfVerbose(false, tr("Compiling sketch...")) if err := b.RunRecipe("recipe.hooks.sketch.prebuild", ".pattern", false); err != nil { return err @@ -354,7 +377,7 @@ func (b *Builder) build(detector *detector.SketchLibrariesDetector) error { b.Progress.CompleteStep() b.Progress.PushProgress() - if err := b.BuildSketch(detector.IncludeFolders()); err != nil { + if err := b.BuildSketch(b.SketchLibrariesDetector.IncludeFolders()); err != nil { return err } b.Progress.CompleteStep() @@ -373,13 +396,13 @@ func (b *Builder) build(detector *detector.SketchLibrariesDetector) error { b.Progress.CompleteStep() b.Progress.PushProgress() - if err := b.RemoveUnusedCompiledLibraries(detector.ImportedLibraries()); err != nil { + if err := b.RemoveUnusedCompiledLibraries(b.SketchLibrariesDetector.ImportedLibraries()); err != nil { return err } b.Progress.CompleteStep() b.Progress.PushProgress() - if err := b.BuildLibraries(detector.IncludeFolders(), detector.ImportedLibraries()); err != nil { + if err := b.BuildLibraries(b.SketchLibrariesDetector.IncludeFolders(), b.SketchLibrariesDetector.ImportedLibraries()); err != nil { return err } b.Progress.CompleteStep() diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 7b0462926db..fa14828919b 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -25,7 +25,6 @@ import ( "github.com/arduino/arduino-cli/arduino" "github.com/arduino/arduino-cli/arduino/builder" - "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/cores" @@ -179,6 +178,10 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderLogger := logger.New(outStream, errStream, req.GetVerbose(), req.GetWarnings()) + var libsManager *librariesmanager.LibrariesManager + if pme.GetProfile() != nil { + libsManager = lm + } sketchBuilder, err := builder.NewBuilder( sk, boardBuildProperties, @@ -195,7 +198,10 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream req.GetClean(), req.GetSourceOverride(), req.GetCreateCompilationDatabaseOnly(), - actualPlatform, targetPlatform, + targetPlatform, actualPlatform, + req.GetSkipLibrariesDiscovery(), + libsManager, + paths.NewPathList(req.Library...), builderLogger, progress.New(progressCB), ) @@ -211,32 +217,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream return r, &arduino.CompileFailedError{Message: err.Error()} } - var libsManager *librariesmanager.LibrariesManager - if pme.GetProfile() != nil { - libsManager = lm - } - useCachedLibrariesResolution := req.GetSkipLibrariesDiscovery() - libraryDir := paths.NewPathList(req.Library...) - libsManager, libsResolver, verboseOut, err := detector.LibrariesLoader( - useCachedLibrariesResolution, libsManager, - builtinLibrariesDir, libraryDir, otherLibrariesDirs, - actualPlatform, targetPlatform, - ) - if err != nil { - return r, &arduino.CompileFailedError{Message: err.Error()} - } - - if builderLogger.Verbose() { - builderLogger.Warn(string(verboseOut)) - } - - sketchLibrariesDetector := detector.NewSketchLibrariesDetector( - libsManager, libsResolver, - useCachedLibrariesResolution, - req.GetCreateCompilationDatabaseOnly(), - builderLogger, - ) - defer func() { if p := sketchBuilder.GetBuildPath(); p != nil { r.BuildPath = p.String() @@ -265,7 +245,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream if req.GetPreprocess() { // Just output preprocessed source code and exit - compileErr := sketchBuilder.Preprocess(sketchLibrariesDetector) + compileErr := sketchBuilder.Preprocess() if compileErr != nil { compileErr = &arduino.CompileFailedError{Message: compileErr.Error()} } @@ -274,7 +254,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream defer func() { importedLibs := []*rpc.Library{} - for _, lib := range sketchLibrariesDetector.ImportedLibraries() { + for _, lib := range sketchBuilder.SketchLibrariesDetector.ImportedLibraries() { rpcLib, err := lib.ToRPCLibrary() if err != nil { msg := tr("Error getting information for library %s", lib.Name) + ": " + err.Error() + "\n" @@ -310,7 +290,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream targetBoard.String(), "'build.board'", sketchBuilder.GetBuildProperties().Get("build.board")) + "\n")) } - if err := sketchBuilder.Build(sketchLibrariesDetector); err != nil { + if err := sketchBuilder.Build(); err != nil { return r, &arduino.CompileFailedError{Message: err.Error()} }