Skip to content

Commit 3670504

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 b04be66 commit 3670504

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
@@ -112,12 +112,15 @@ import (
112112
type ContainerFindIncludes struct{}
113113

114114
func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
115-
finder := &CppIncludesFinder{
116-
ctx: ctx,
115+
finder := NewCppIncludesFinder(ctx)
116+
finder.UseIncludeDir(ctx.BuildProperties.GetPath("build.core.path"))
117+
if variantPath := ctx.BuildProperties.GetPath("build.variant.path"); variantPath != nil {
118+
finder.UseIncludeDir(variantPath)
117119
}
118120
if err := finder.DetectLibraries(); err != nil {
119121
return err
120122
}
123+
ctx.IncludeFolders.AddAllMissing(finder.IncludeDirsFound)
121124
if err := runCommand(ctx, &FailIfImportedLibraryIsWrong{}); err != nil {
122125
return i18n.WrapError(err)
123126
}
@@ -129,22 +132,30 @@ func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
129132
// libraries used in a sketch and a way to cache this result for
130133
// increasing detection speed on already processed sketches.
131134
type CppIncludesFinder struct {
132-
ctx *types.Context
133-
cache *includeCache
134-
sketch *types.Sketch
135-
queue *UniqueSourceFileQueue
136-
log *logrus.Entry
135+
IncludeDirsFound paths.PathList
136+
ctx *types.Context
137+
cache *includeCache
138+
sketch *types.Sketch
139+
queue *UniqueSourceFileQueue
140+
log *logrus.Entry
141+
}
142+
143+
// NewCppIncludesFinder create a new include
144+
func NewCppIncludesFinder(ctx *types.Context) *CppIncludesFinder {
145+
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"),
151+
}
137152
}
138153

154+
// DetectLibraries runs a library detection algorithm
139155
func (f *CppIncludesFinder) DetectLibraries() error {
140-
f.cache = loadCacheFrom(f.ctx.BuildPath.Join("includes.cache"))
141-
f.sketch = f.ctx.Sketch
142-
f.queue = &UniqueSourceFileQueue{}
143-
f.log = logrus.WithField("task", "DetectingLibraries")
144-
145-
f.appendIncludeFolder(nil, "", f.ctx.BuildProperties.GetPath("build.core.path"))
146-
if f.ctx.BuildProperties.Get("build.variant.path") != "" {
147-
f.appendIncludeFolder(nil, "", f.ctx.BuildProperties.GetPath("build.variant.path"))
156+
for _, includeDir := range f.IncludeDirsFound {
157+
f.log.Debugf("Using include directory: %s", includeDir)
158+
f.cache.AddAndCheckEntry(nil, "", includeDir)
148159
}
149160

150161
mergedfile, err := MakeSourceFile(f.ctx.SketchBuildPath, f.ctx.SketchBuildPath, paths.New(f.sketch.MainFile.Name.Base()+".cpp"))
@@ -175,15 +186,9 @@ func (f *CppIncludesFinder) DetectLibraries() error {
175186
return nil
176187
}
177188

178-
// Append the given folder to the include path and match or append it to
179-
// the cache. sourceFilePath and include indicate the source of this
180-
// include (e.g. what #include line in what file it was resolved from)
181-
// and should be the empty string for the default include folders, like
182-
// the core or variant.
183-
func (f *CppIncludesFinder) appendIncludeFolder(sourceFilePath *paths.Path, include string, folder *paths.Path) {
184-
f.log.Debugf("Using include folder: %s", folder)
185-
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, folder)
186-
f.cache.AddAndCheckEntry(sourceFilePath, include, folder)
189+
// UseIncludeDir adds an include directory to the current library discovery
190+
func (f *CppIncludesFinder) UseIncludeDir(includeDir *paths.Path) {
191+
f.IncludeDirsFound.Add(includeDir)
187192
}
188193

189194
func runCommand(ctx *types.Context, command types.Command) error {
@@ -361,7 +366,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
361366
f.ctx.GetLogger().Println("info", "Using cached library dependencies for file: {0}", sourcePath)
362367
}
363368
} else {
364-
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.ctx.IncludeFolders)
369+
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.IncludeDirsFound)
365370
// Unwrap error and see if it is an ExitError.
366371
_, isExitError := i18n.UnwrapError(preprocErr).(*exec.ExitError)
367372
if preprocErr == nil {
@@ -393,7 +398,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
393398
// return i18n.WrapError(err)
394399
if preprocErr == nil || preprocStderr == nil {
395400
// Filename came from cache, so run preprocessor to obtain error to show
396-
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.ctx.IncludeFolders)
401+
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.IncludeDirsFound)
397402
if preprocErr == nil {
398403
// If there is a missing #include in the cache, but running
399404
// gcc does not reproduce that, there is something wrong.
@@ -410,10 +415,14 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
410415
// include path and queue its source files for further
411416
// include scanning
412417
f.ctx.ImportedLibraries = append(f.ctx.ImportedLibraries, library)
413-
f.appendIncludeFolder(sourcePath, include, library.SourceDir)
418+
419+
f.log.Debugf("Using library include folder: %s", library.SourceDir)
420+
f.IncludeDirsFound.Add(library.SourceDir)
421+
f.cache.AddAndCheckEntry(sourcePath, include, library.SourceDir)
422+
414423
if library.UtilityDir != nil {
415424
// TODO: Use library.SourceDirs() instead?
416-
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, library.UtilityDir)
425+
f.IncludeDirsFound.Add(library.UtilityDir)
417426
}
418427
sourceDirs := library.SourceDirs()
419428
buildDir := f.ctx.LibrariesBuildPath.Join(library.Name)

0 commit comments

Comments
 (0)