Skip to content

Commit ac89e36

Browse files
committed
CppIncludesFinder: Removing dependency on ctx (part 2)
1 parent f55dca8 commit ac89e36

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

legacy/builder/container_find_includes.go

+44-40
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,16 @@ func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
116116
if variantPath := ctx.BuildProperties.GetPath("build.variant.path"); variantPath != nil {
117117
finder.UseIncludeDir(variantPath)
118118
}
119+
finder.AddSourceFile(ctx.SketchBuildPath, ctx.SketchBuildPath, paths.New(ctx.Sketch.MainFile.Name.Base()+".cpp"))
120+
finder.AddSourceDir(ctx.SketchBuildPath, ctx.SketchBuildPath, false /* recurse */)
121+
if srcSubfolderPath := ctx.SketchBuildPath.Join("src"); srcSubfolderPath.IsDir() {
122+
finder.AddSourceDir(srcSubfolderPath, ctx.SketchBuildPath, true /* recurse */)
123+
}
124+
119125
if err := finder.DetectLibraries(); err != nil {
120126
return err
121127
}
128+
122129
ctx.IncludeFolders.AddAllMissing(finder.IncludeDirsFound)
123130
if err := runCommand(ctx, &FailIfImportedLibraryIsWrong{}); err != nil {
124131
return errors.WithStack(err)
@@ -134,19 +141,17 @@ type CppIncludesFinder struct {
134141
IncludeDirsFound paths.PathList
135142
ctx *types.Context
136143
cache *includeCache
137-
sketch *types.Sketch
138144
queue *UniqueSourceFileQueue
139145
log *logrus.Entry
140146
}
141147

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

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

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

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

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

0 commit comments

Comments
 (0)