Skip to content

Commit e22d5d3

Browse files
Move the detector inside arduino/builder
1 parent 5daa267 commit e22d5d3

File tree

2 files changed

+50
-47
lines changed

2 files changed

+50
-47
lines changed

arduino/builder/builder.go

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/arduino/arduino-cli/arduino/builder/logger"
2525
"github.com/arduino/arduino-cli/arduino/builder/progress"
2626
"github.com/arduino/arduino-cli/arduino/cores"
27+
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2728
"github.com/arduino/arduino-cli/arduino/sketch"
2829
"github.com/arduino/go-paths-helper"
2930
"github.com/arduino/go-properties-orderedmap"
@@ -78,6 +79,7 @@ type Builder struct {
7879

7980
buildArtifacts *BuildArtifacts
8081

82+
*detector.SketchLibrariesDetector
8183
*BuildOptionsManager
8284
}
8385

@@ -110,6 +112,9 @@ func NewBuilder(
110112
sourceOverrides map[string]string,
111113
onlyUpdateCompilationDatabase bool,
112114
targetPlatform, actualPlatform *cores.PlatformRelease,
115+
useCachedLibrariesResolution bool,
116+
librariesManager *librariesmanager.LibrariesManager,
117+
libraryDirs paths.PathList,
113118
logger *logger.BuilderLogger,
114119
progressStats *progress.Struct,
115120
) (*Builder, error) {
@@ -164,6 +169,18 @@ func NewBuilder(
164169
progressStats = progress.New(nil)
165170
}
166171

172+
libsManager, libsResolver, verboseOut, err := detector.LibrariesLoader(
173+
useCachedLibrariesResolution, librariesManager,
174+
builtInLibrariesDirs, libraryDirs, otherLibrariesDirs,
175+
actualPlatform, targetPlatform,
176+
)
177+
if err != nil {
178+
return nil, err
179+
}
180+
if logger.Verbose() {
181+
logger.Warn(string(verboseOut))
182+
}
183+
167184
return &Builder{
168185
sketch: sk,
169186
buildProperties: buildProperties,
@@ -184,6 +201,12 @@ func NewBuilder(
184201
buildArtifacts: &BuildArtifacts{},
185202
targetPlatform: targetPlatform,
186203
actualPlatform: actualPlatform,
204+
SketchLibrariesDetector: detector.NewSketchLibrariesDetector(
205+
libsManager, libsResolver,
206+
useCachedLibrariesResolution,
207+
onlyUpdateCompilationDatabase,
208+
logger,
209+
),
187210
BuildOptionsManager: NewBuildOptionsManager(
188211
hardwareDirs, builtInToolsDirs, otherLibrariesDirs,
189212
builtInLibrariesDirs, buildPath,
@@ -230,13 +253,13 @@ func (b *Builder) TargetPlatform() *cores.PlatformRelease {
230253
}
231254

232255
// Preprocess fixdoc
233-
func (b *Builder) Preprocess(detector *detector.SketchLibrariesDetector) error {
256+
func (b *Builder) Preprocess() error {
234257
b.Progress.AddSubSteps(6)
235258
defer b.Progress.RemoveSubSteps()
236-
return b.preprocess(detector)
259+
return b.preprocess()
237260
}
238261

239-
func (b *Builder) preprocess(detector *detector.SketchLibrariesDetector) error {
262+
func (b *Builder) preprocess() error {
240263
if err := b.buildPath.MkdirAll(); err != nil {
241264
return err
242265
}
@@ -260,7 +283,7 @@ func (b *Builder) preprocess(detector *detector.SketchLibrariesDetector) error {
260283
b.Progress.PushProgress()
261284

262285
b.logIfVerbose(false, tr("Detecting libraries used..."))
263-
err := detector.FindIncludes(
286+
err := b.SketchLibrariesDetector.FindIncludes(
264287
b.GetBuildPath(),
265288
b.GetBuildProperties().GetPath("build.core.path"),
266289
b.GetBuildProperties().GetPath("build.variant.path"),
@@ -276,12 +299,12 @@ func (b *Builder) preprocess(detector *detector.SketchLibrariesDetector) error {
276299
b.Progress.CompleteStep()
277300
b.Progress.PushProgress()
278301

279-
b.WarnAboutArchIncompatibleLibraries(detector.ImportedLibraries())
302+
b.WarnAboutArchIncompatibleLibraries(b.SketchLibrariesDetector.ImportedLibraries())
280303
b.Progress.CompleteStep()
281304
b.Progress.PushProgress()
282305

283306
b.logIfVerbose(false, tr("Generating function prototypes..."))
284-
if err := b.PreprocessSketch(detector.IncludeFolders()); err != nil {
307+
if err := b.PreprocessSketch(b.SketchLibrariesDetector.IncludeFolders()); err != nil {
285308
return err
286309
}
287310
b.Progress.CompleteStep()
@@ -309,28 +332,28 @@ func (b *Builder) logIfVerbose(warn bool, msg string) {
309332
}
310333

311334
// Build fixdoc
312-
func (b *Builder) Build(detector *detector.SketchLibrariesDetector) error {
335+
func (b *Builder) Build() error {
313336
b.Progress.AddSubSteps(6 /** preprocess **/ + 21 /** build **/)
314337
defer b.Progress.RemoveSubSteps()
315338

316-
if err := b.preprocess(detector); err != nil {
339+
if err := b.preprocess(); err != nil {
317340
return err
318341
}
319342

320-
buildErr := b.build(detector)
343+
buildErr := b.build()
321344

322-
detector.PrintUsedAndNotUsedLibraries(buildErr != nil)
345+
b.SketchLibrariesDetector.PrintUsedAndNotUsedLibraries(buildErr != nil)
323346
b.Progress.CompleteStep()
324347
b.Progress.PushProgress()
325348

326-
b.PrintUsedLibraries(detector.ImportedLibraries())
349+
b.PrintUsedLibraries(b.SketchLibrariesDetector.ImportedLibraries())
327350
b.Progress.CompleteStep()
328351
b.Progress.PushProgress()
329352

330353
if buildErr != nil {
331354
return buildErr
332355
}
333-
if err := b.ExportProjectCMake(detector.ImportedLibraries(), detector.IncludeFolders()); err != nil {
356+
if err := b.ExportProjectCMake(b.SketchLibrariesDetector.ImportedLibraries(), b.SketchLibrariesDetector.IncludeFolders()); err != nil {
334357
return err
335358
}
336359
b.Progress.CompleteStep()
@@ -346,15 +369,15 @@ func (b *Builder) Build(detector *detector.SketchLibrariesDetector) error {
346369
}
347370

348371
// Build fixdoc
349-
func (b *Builder) build(detector *detector.SketchLibrariesDetector) error {
372+
func (b *Builder) build() error {
350373
b.logIfVerbose(false, tr("Compiling sketch..."))
351374
if err := b.RunRecipe("recipe.hooks.sketch.prebuild", ".pattern", false); err != nil {
352375
return err
353376
}
354377
b.Progress.CompleteStep()
355378
b.Progress.PushProgress()
356379

357-
if err := b.BuildSketch(detector.IncludeFolders()); err != nil {
380+
if err := b.BuildSketch(b.SketchLibrariesDetector.IncludeFolders()); err != nil {
358381
return err
359382
}
360383
b.Progress.CompleteStep()
@@ -373,13 +396,13 @@ func (b *Builder) build(detector *detector.SketchLibrariesDetector) error {
373396
b.Progress.CompleteStep()
374397
b.Progress.PushProgress()
375398

376-
if err := b.RemoveUnusedCompiledLibraries(detector.ImportedLibraries()); err != nil {
399+
if err := b.RemoveUnusedCompiledLibraries(b.SketchLibrariesDetector.ImportedLibraries()); err != nil {
377400
return err
378401
}
379402
b.Progress.CompleteStep()
380403
b.Progress.PushProgress()
381404

382-
if err := b.BuildLibraries(detector.IncludeFolders(), detector.ImportedLibraries()); err != nil {
405+
if err := b.BuildLibraries(b.SketchLibrariesDetector.IncludeFolders(), b.SketchLibrariesDetector.ImportedLibraries()); err != nil {
383406
return err
384407
}
385408
b.Progress.CompleteStep()

commands/compile/compile.go

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"github.com/arduino/arduino-cli/arduino"
2727
"github.com/arduino/arduino-cli/arduino/builder"
28-
"github.com/arduino/arduino-cli/arduino/builder/detector"
2928
"github.com/arduino/arduino-cli/arduino/builder/logger"
3029
"github.com/arduino/arduino-cli/arduino/builder/progress"
3130
"github.com/arduino/arduino-cli/arduino/cores"
@@ -179,6 +178,10 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
179178

180179
builderLogger := logger.New(outStream, errStream, req.GetVerbose(), req.GetWarnings())
181180

181+
var libsManager *librariesmanager.LibrariesManager
182+
if pme.GetProfile() != nil {
183+
libsManager = lm
184+
}
182185
sketchBuilder, err := builder.NewBuilder(
183186
sk,
184187
boardBuildProperties,
@@ -195,7 +198,10 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
195198
req.GetClean(),
196199
req.GetSourceOverride(),
197200
req.GetCreateCompilationDatabaseOnly(),
198-
actualPlatform, targetPlatform,
201+
targetPlatform, actualPlatform,
202+
req.GetSkipLibrariesDiscovery(),
203+
libsManager,
204+
paths.NewPathList(req.Library...),
199205
builderLogger,
200206
progress.New(progressCB),
201207
)
@@ -211,32 +217,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
211217
return r, &arduino.CompileFailedError{Message: err.Error()}
212218
}
213219

214-
var libsManager *librariesmanager.LibrariesManager
215-
if pme.GetProfile() != nil {
216-
libsManager = lm
217-
}
218-
useCachedLibrariesResolution := req.GetSkipLibrariesDiscovery()
219-
libraryDir := paths.NewPathList(req.Library...)
220-
libsManager, libsResolver, verboseOut, err := detector.LibrariesLoader(
221-
useCachedLibrariesResolution, libsManager,
222-
builtinLibrariesDir, libraryDir, otherLibrariesDirs,
223-
actualPlatform, targetPlatform,
224-
)
225-
if err != nil {
226-
return r, &arduino.CompileFailedError{Message: err.Error()}
227-
}
228-
229-
if builderLogger.Verbose() {
230-
builderLogger.Warn(string(verboseOut))
231-
}
232-
233-
sketchLibrariesDetector := detector.NewSketchLibrariesDetector(
234-
libsManager, libsResolver,
235-
useCachedLibrariesResolution,
236-
req.GetCreateCompilationDatabaseOnly(),
237-
builderLogger,
238-
)
239-
240220
defer func() {
241221
if p := sketchBuilder.GetBuildPath(); p != nil {
242222
r.BuildPath = p.String()
@@ -265,7 +245,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
265245

266246
if req.GetPreprocess() {
267247
// Just output preprocessed source code and exit
268-
compileErr := sketchBuilder.Preprocess(sketchLibrariesDetector)
248+
compileErr := sketchBuilder.Preprocess()
269249
if compileErr != nil {
270250
compileErr = &arduino.CompileFailedError{Message: compileErr.Error()}
271251
}
@@ -274,7 +254,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
274254

275255
defer func() {
276256
importedLibs := []*rpc.Library{}
277-
for _, lib := range sketchLibrariesDetector.ImportedLibraries() {
257+
for _, lib := range sketchBuilder.SketchLibrariesDetector.ImportedLibraries() {
278258
rpcLib, err := lib.ToRPCLibrary()
279259
if err != nil {
280260
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
310290
targetBoard.String(), "'build.board'", sketchBuilder.GetBuildProperties().Get("build.board")) + "\n"))
311291
}
312292

313-
if err := sketchBuilder.Build(sketchLibrariesDetector); err != nil {
293+
if err := sketchBuilder.Build(); err != nil {
314294
return r, &arduino.CompileFailedError{Message: err.Error()}
315295
}
316296

0 commit comments

Comments
 (0)