Skip to content

Commit 3604d21

Browse files
committed
CppIncludesFinder: Removing dependency on ctx (part 2)
1 parent 3670504 commit 3604d21

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

legacy/builder/container_find_includes.go

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,16 @@ func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
117117
if variantPath := ctx.BuildProperties.GetPath("build.variant.path"); variantPath != nil {
118118
finder.UseIncludeDir(variantPath)
119119
}
120+
finder.AddSourceFile(ctx.SketchBuildPath, ctx.SketchBuildPath, paths.New(ctx.Sketch.MainFile.Name.Base()+".cpp"))
121+
finder.AddSourceDir(ctx.SketchBuildPath, ctx.SketchBuildPath, false /* recurse */)
122+
if srcSubfolderPath := ctx.SketchBuildPath.Join("src"); srcSubfolderPath.IsDir() {
123+
finder.AddSourceDir(srcSubfolderPath, ctx.SketchBuildPath, true /* recurse */)
124+
}
125+
120126
if err := finder.DetectLibraries(); err != nil {
121127
return err
122128
}
129+
123130
ctx.IncludeFolders.AddAllMissing(finder.IncludeDirsFound)
124131
if err := runCommand(ctx, &FailIfImportedLibraryIsWrong{}); err != nil {
125132
return i18n.WrapError(err)
@@ -135,19 +142,17 @@ type CppIncludesFinder struct {
135142
IncludeDirsFound paths.PathList
136143
ctx *types.Context
137144
cache *includeCache
138-
sketch *types.Sketch
139145
queue *UniqueSourceFileQueue
140146
log *logrus.Entry
141147
}
142148

143149
// NewCppIncludesFinder create a new include
144150
func NewCppIncludesFinder(ctx *types.Context) *CppIncludesFinder {
145151
return &CppIncludesFinder{
146-
ctx: ctx,
147-
cache: loadCacheFrom(ctx.BuildPath.Join("includes.cache")),
148-
sketch: ctx.Sketch,
149-
queue: &UniqueSourceFileQueue{},
150-
log: logrus.WithField("task", "DetectingLibraries"),
152+
ctx: ctx,
153+
cache: loadCacheFrom(ctx.BuildPath.Join("includes.cache")),
154+
queue: &UniqueSourceFileQueue{},
155+
log: logrus.WithField("task", "DetectingLibraries"),
151156
}
152157
}
153158

@@ -158,19 +163,6 @@ func (f *CppIncludesFinder) DetectLibraries() error {
158163
f.cache.AddAndCheckEntry(nil, "", includeDir)
159164
}
160165

161-
mergedfile, err := MakeSourceFile(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, paths.New(f.sketch.MainFile.Name.Base()+".cpp"))
162-
if err != nil {
163-
return i18n.WrapError(err)
164-
}
165-
f.log.Debugf("Queueing merged sketch: %s", mergedfile)
166-
f.queue.Push(mergedfile)
167-
168-
f.queueSourceFilesFromFolder(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, false /* recurse */)
169-
srcSubfolderPath := f.ctx.SketchBuildPath.Join("src")
170-
if srcSubfolderPath.IsDir() {
171-
f.queueSourceFilesFromFolder(srcSubfolderPath, f.ctx.SketchBuildPath, true /* recurse */)
172-
}
173-
174166
for !f.queue.Empty() {
175167
if err := f.findIncludesUntilDone(f.queue.Pop()); err != nil {
176168
f.cache.Remove()
@@ -191,6 +183,38 @@ func (f *CppIncludesFinder) UseIncludeDir(includeDir *paths.Path) {
191183
f.IncludeDirsFound.Add(includeDir)
192184
}
193185

186+
// AddSourceFile adds a source file to be examined to look for library imports
187+
func (f *CppIncludesFinder) AddSourceFile(sourceRoot, buildRoot, srcPath *paths.Path) error {
188+
if file, err := MakeSourceFile(sourceRoot, buildRoot, srcPath); err == nil {
189+
f.log.Debugf("Queueing source file: %s", file)
190+
f.queue.Push(file)
191+
} else {
192+
return i18n.WrapError(err)
193+
}
194+
return nil
195+
}
196+
197+
// AddSourceDir adds a directory of source file to be examined to look for library imports
198+
func (f *CppIncludesFinder) AddSourceDir(srcDir, buildDir *paths.Path, recurse bool) error {
199+
extensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS[ext] }
200+
f.log.Debugf(" Queueing source files from %s (recurse %v)", srcDir, recurse)
201+
filePaths, err := utils.FindFilesInFolder(srcDir.String(), extensions, recurse)
202+
if err != nil {
203+
return i18n.WrapError(err)
204+
}
205+
206+
for _, filePath := range filePaths {
207+
sourceFile, err := MakeSourceFile(srcDir, buildDir, paths.New(filePath))
208+
if err != nil {
209+
return i18n.WrapError(err)
210+
}
211+
f.log.Debugf(" Queuing %s", sourceFile)
212+
f.queue.Push(sourceFile)
213+
}
214+
215+
return nil
216+
}
217+
194218
func runCommand(ctx *types.Context, command types.Command) error {
195219
PrintRingNameIfDebug(ctx, command)
196220
err := command.Run(ctx)
@@ -427,32 +451,12 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
427451
sourceDirs := library.SourceDirs()
428452
buildDir := f.ctx.LibrariesBuildPath.Join(library.Name)
429453
for _, sourceDir := range sourceDirs {
430-
f.queueSourceFilesFromFolder(buildDir, sourceDir.Dir, sourceDir.Recurse)
454+
f.AddSourceDir(buildDir, sourceDir.Dir, sourceDir.Recurse)
431455
}
432456
first = false
433457
}
434458
}
435459

436-
func (f *CppIncludesFinder) queueSourceFilesFromFolder(srcDir, buildDir *paths.Path, recurse bool) error {
437-
extensions := func(ext string) bool { return ADDITIONAL_FILE_VALID_EXTENSIONS_NO_HEADERS[ext] }
438-
f.log.Debugf(" Queueing source files from %s (recurse %v)", srcDir, recurse)
439-
filePaths, err := utils.FindFilesInFolder(srcDir.String(), extensions, recurse)
440-
if err != nil {
441-
return i18n.WrapError(err)
442-
}
443-
444-
for _, filePath := range filePaths {
445-
sourceFile, err := MakeSourceFile(srcDir, buildDir, paths.New(filePath))
446-
if err != nil {
447-
return i18n.WrapError(err)
448-
}
449-
f.log.Debugf(" Queuing %s", sourceFile)
450-
f.queue.Push(sourceFile)
451-
}
452-
453-
return nil
454-
}
455-
456460
// SourceFile represent a source file, it keeps a reference to the root source
457461
// directory and the corresponding build root directory.
458462
type SourceFile struct {

0 commit comments

Comments
 (0)