Skip to content

Commit 4b0b588

Browse files
committed
CppIncludesFinder: Removing dependency on ctx (part 2)
1 parent e1a1581 commit 4b0b588

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

legacy/builder/container_find_includes.go

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ import (
9898
"os/exec"
9999
"time"
100100

101-
"github.com/arduino/arduino-cli/arduino/sketch"
102101
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
103102
"github.com/arduino/arduino-cli/legacy/builder/types"
104103
"github.com/arduino/arduino-cli/legacy/builder/utils"
@@ -127,9 +126,16 @@ func (s *ContainerFindIncludes) findIncludes(ctx *types.Context) error {
127126
if variantPath := ctx.BuildProperties.GetPath("build.variant.path"); variantPath != nil {
128127
finder.UseIncludeDir(variantPath)
129128
}
129+
finder.AddSourceFile(ctx.SketchBuildPath, ctx.SketchBuildPath, paths.New(ctx.Sketch.MainFile.Base()+".cpp"))
130+
finder.AddSourceDir(ctx.SketchBuildPath, ctx.SketchBuildPath, false /* recurse */)
131+
if srcSubfolderPath := ctx.SketchBuildPath.Join("src"); srcSubfolderPath.IsDir() {
132+
finder.AddSourceDir(srcSubfolderPath, ctx.SketchBuildPath, true /* recurse */)
133+
}
134+
130135
if err := finder.DetectLibraries(); err != nil {
131136
return err
132137
}
138+
133139
ctx.IncludeFolders.AddAllMissing(finder.IncludeDirsFound)
134140
if err := runCommand(ctx, &FailIfImportedLibraryIsWrong{}); err != nil {
135141
return errors.WithStack(err)
@@ -145,19 +151,17 @@ type CppIncludesFinder struct {
145151
IncludeDirsFound paths.PathList
146152
ctx *types.Context
147153
cache *includeCache
148-
sketch *sketch.Sketch
149154
queue *UniqueSourceFileQueue
150155
log *logrus.Entry
151156
}
152157

153158
// NewCppIncludesFinder create a new include
154159
func NewCppIncludesFinder(ctx *types.Context) *CppIncludesFinder {
155160
return &CppIncludesFinder{
156-
ctx: ctx,
157-
cache: loadCacheFrom(ctx.BuildPath.Join("includes.cache")),
158-
sketch: ctx.Sketch,
159-
queue: &UniqueSourceFileQueue{},
160-
log: logrus.WithField("task", "DetectingLibraries"),
161+
ctx: ctx,
162+
cache: loadCacheFrom(ctx.BuildPath.Join("includes.cache")),
163+
queue: &UniqueSourceFileQueue{},
164+
log: logrus.WithField("task", "DetectingLibraries"),
161165
}
162166
}
163167

@@ -168,19 +172,6 @@ func (f *CppIncludesFinder) DetectLibraries() error {
168172
f.cache.AddAndCheckEntry(nil, "", includeDir)
169173
}
170174

171-
mergedfile, err := MakeSourceFile(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, paths.New(f.sketch.MainFile.Base()+".cpp"))
172-
if err != nil {
173-
return errors.WithStack(err)
174-
}
175-
f.log.Debugf("Queueing merged sketch: %s", mergedfile)
176-
f.queue.Push(mergedfile)
177-
178-
f.queueSourceFilesFromFolder(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, false /* recurse */)
179-
srcSubfolderPath := f.ctx.SketchBuildPath.Join("src")
180-
if srcSubfolderPath.IsDir() {
181-
f.queueSourceFilesFromFolder(srcSubfolderPath, f.ctx.SketchBuildPath, true /* recurse */)
182-
}
183-
184175
for !f.queue.Empty() {
185176
if err := f.findIncludesUntilDone(f.queue.Pop()); err != nil {
186177
f.cache.Remove()
@@ -201,6 +192,38 @@ func (f *CppIncludesFinder) UseIncludeDir(includeDir *paths.Path) {
201192
f.IncludeDirsFound.Add(includeDir)
202193
}
203194

195+
// AddSourceFile adds a source file to be examined to look for library imports
196+
func (f *CppIncludesFinder) AddSourceFile(sourceRoot, buildRoot, srcPath *paths.Path) error {
197+
if file, err := MakeSourceFile(sourceRoot, buildRoot, srcPath); err == nil {
198+
f.log.Debugf("Queueing source file: %s", file)
199+
f.queue.Push(file)
200+
} else {
201+
return errors.WithStack(err)
202+
}
203+
return nil
204+
}
205+
206+
// AddSourceDir adds a directory of source file to be examined to look for library imports
207+
func (f *CppIncludesFinder) AddSourceDir(srcDir, buildDir *paths.Path, recurse bool) error {
208+
extensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS[ext] }
209+
f.log.Debugf(" Queueing source files from %s (recurse %v)", srcDir, recurse)
210+
filePaths, err := utils.FindFilesInFolder(srcDir.String(), extensions, recurse)
211+
if err != nil {
212+
return errors.WithStack(err)
213+
}
214+
215+
for _, filePath := range filePaths {
216+
sourceFile, err := MakeSourceFile(srcDir, buildDir, paths.New(filePath))
217+
if err != nil {
218+
return errors.WithStack(err)
219+
}
220+
f.log.Debugf(" Queuing %s", sourceFile)
221+
f.queue.Push(sourceFile)
222+
}
223+
224+
return nil
225+
}
226+
204227
func runCommand(ctx *types.Context, command types.Command) error {
205228
PrintRingNameIfDebug(ctx, command)
206229
err := command.Run(ctx)
@@ -444,33 +467,13 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
444467
f.ctx.Info(tr("Skipping dependencies detection for precompiled library %[1]s", library.Name))
445468
}
446469
} else {
447-
f.queueSourceFilesFromFolder(buildDir, sourceDir.Dir, sourceDir.Recurse)
470+
f.AddSourceDir(buildDir, sourceDir.Dir, sourceDir.Recurse)
448471
}
449472
}
450473
first = false
451474
}
452475
}
453476

454-
func (f *CppIncludesFinder) queueSourceFilesFromFolder(srcDir, buildDir *paths.Path, recurse bool) error {
455-
extensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS[ext] }
456-
f.log.Debugf(" Queueing source files from %s (recurse %v)", srcDir, recurse)
457-
filePaths, err := utils.FindFilesInFolder(srcDir.String(), extensions, recurse)
458-
if err != nil {
459-
return errors.WithStack(err)
460-
}
461-
462-
for _, filePath := range filePaths {
463-
sourceFile, err := MakeSourceFile(srcDir, buildDir, paths.New(filePath))
464-
if err != nil {
465-
return errors.WithStack(err)
466-
}
467-
f.log.Debugf(" Queuing %s", sourceFile)
468-
f.queue.Push(sourceFile)
469-
}
470-
471-
return nil
472-
}
473-
474477
// SourceFile represent a source file, it keeps a reference to the root source
475478
// directory and the corresponding build root directory.
476479
type SourceFile struct {

0 commit comments

Comments
 (0)