Skip to content

Commit

Permalink
Move the detector inside arduino/builder
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 14, 2023
1 parent 5daa267 commit e22d5d3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
55 changes: 39 additions & 16 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -78,6 +79,7 @@ type Builder struct {

buildArtifacts *BuildArtifacts

*detector.SketchLibrariesDetector
*BuildOptionsManager
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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"),
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -346,15 +369,15 @@ 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
}
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()
Expand All @@ -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()
Expand Down
42 changes: 11 additions & 31 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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),
)
Expand All @@ -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()
Expand Down Expand Up @@ -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()}
}
Expand All @@ -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"
Expand Down Expand Up @@ -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()}
}

Expand Down

0 comments on commit e22d5d3

Please sign in to comment.