Skip to content

Commit e1a1581

Browse files
committed
CppIncludesFinder: Removing dependency on ctx (part 1)
This is a series of commit to make library detection context-agnostic. In this commit the IncludeFolders usage has been removed from the internal logic, a new field CppIncludesFinder.IncludeDirsFound has been made for this purpose. The new field is setup before by the caller before doing the actual discovery.
1 parent 3323453 commit e1a1581

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

legacy/builder/container_find_includes.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,15 @@ func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
122122
}
123123

124124
func (s *ContainerFindIncludes) findIncludes(ctx *types.Context) error {
125-
finder := &CppIncludesFinder{
126-
ctx: ctx,
125+
finder := NewCppIncludesFinder(ctx)
126+
finder.UseIncludeDir(ctx.BuildProperties.GetPath("build.core.path"))
127+
if variantPath := ctx.BuildProperties.GetPath("build.variant.path"); variantPath != nil {
128+
finder.UseIncludeDir(variantPath)
127129
}
128130
if err := finder.DetectLibraries(); err != nil {
129131
return err
130132
}
133+
ctx.IncludeFolders.AddAllMissing(finder.IncludeDirsFound)
131134
if err := runCommand(ctx, &FailIfImportedLibraryIsWrong{}); err != nil {
132135
return errors.WithStack(err)
133136
}
@@ -139,22 +142,30 @@ func (s *ContainerFindIncludes) findIncludes(ctx *types.Context) error {
139142
// libraries used in a sketch and a way to cache this result for
140143
// increasing detection speed on already processed sketches.
141144
type CppIncludesFinder struct {
142-
ctx *types.Context
143-
cache *includeCache
144-
sketch *sketch.Sketch
145-
queue *UniqueSourceFileQueue
146-
log *logrus.Entry
145+
IncludeDirsFound paths.PathList
146+
ctx *types.Context
147+
cache *includeCache
148+
sketch *sketch.Sketch
149+
queue *UniqueSourceFileQueue
150+
log *logrus.Entry
151+
}
152+
153+
// NewCppIncludesFinder create a new include
154+
func NewCppIncludesFinder(ctx *types.Context) *CppIncludesFinder {
155+
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+
}
147162
}
148163

164+
// DetectLibraries runs a library detection algorithm
149165
func (f *CppIncludesFinder) DetectLibraries() error {
150-
f.cache = loadCacheFrom(f.ctx.BuildPath.Join("includes.cache"))
151-
f.sketch = f.ctx.Sketch
152-
f.queue = &UniqueSourceFileQueue{}
153-
f.log = logrus.WithField("task", "DetectingLibraries")
154-
155-
f.appendIncludeFolder(nil, "", f.ctx.BuildProperties.GetPath("build.core.path"))
156-
if f.ctx.BuildProperties.Get("build.variant.path") != "" {
157-
f.appendIncludeFolder(nil, "", f.ctx.BuildProperties.GetPath("build.variant.path"))
166+
for _, includeDir := range f.IncludeDirsFound {
167+
f.log.Debugf("Using include directory: %s", includeDir)
168+
f.cache.AddAndCheckEntry(nil, "", includeDir)
158169
}
159170

160171
mergedfile, err := MakeSourceFile(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, paths.New(f.sketch.MainFile.Base()+".cpp"))
@@ -185,15 +196,9 @@ func (f *CppIncludesFinder) DetectLibraries() error {
185196
return nil
186197
}
187198

188-
// Append the given folder to the include path and match or append it to
189-
// the cache. sourceFilePath and include indicate the source of this
190-
// include (e.g. what #include line in what file it was resolved from)
191-
// and should be the empty string for the default include folders, like
192-
// the core or variant.
193-
func (f *CppIncludesFinder) appendIncludeFolder(sourceFilePath *paths.Path, include string, folder *paths.Path) {
194-
f.log.Debugf("Using include folder: %s", folder)
195-
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, folder)
196-
f.cache.AddAndCheckEntry(sourceFilePath, include, folder)
199+
// UseIncludeDir adds an include directory to the current library discovery
200+
func (f *CppIncludesFinder) UseIncludeDir(includeDir *paths.Path) {
201+
f.IncludeDirsFound.Add(includeDir)
197202
}
198203

199204
func runCommand(ctx *types.Context, command types.Command) error {
@@ -371,7 +376,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
371376
f.ctx.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath))
372377
}
373378
} else {
374-
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.ctx.IncludeFolders)
379+
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.IncludeDirsFound)
375380
// Unwrap error and see if it is an ExitError.
376381
_, isExitError := errors.Cause(preprocErr).(*exec.ExitError)
377382
if preprocErr == nil {
@@ -403,7 +408,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
403408
// return errors.WithStack(err)
404409
if preprocErr == nil || preprocStderr == nil {
405410
// Filename came from cache, so run preprocessor to obtain error to show
406-
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.ctx.IncludeFolders)
411+
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.IncludeDirsFound)
407412
if preprocErr == nil {
408413
// If there is a missing #include in the cache, but running
409414
// gcc does not reproduce that, there is something wrong.
@@ -420,10 +425,14 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
420425
// include path and queue its source files for further
421426
// include scanning
422427
f.ctx.ImportedLibraries = append(f.ctx.ImportedLibraries, library)
423-
f.appendIncludeFolder(sourcePath, include, library.SourceDir)
428+
429+
f.log.Debugf("Using library include folder: %s", library.SourceDir)
430+
f.IncludeDirsFound.Add(library.SourceDir)
431+
f.cache.AddAndCheckEntry(sourcePath, include, library.SourceDir)
432+
424433
if library.UtilityDir != nil {
425434
// TODO: Use library.SourceDirs() instead?
426-
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, library.UtilityDir)
435+
f.IncludeDirsFound.Add(library.UtilityDir)
427436
}
428437
sourceDirs := library.SourceDirs()
429438
buildDir := f.ctx.LibrariesBuildPath.Join(library.Name)

0 commit comments

Comments
 (0)