Skip to content

Commit f55dca8

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 a7c6530 commit f55dca8

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

legacy/builder/container_find_includes.go

+37-28
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ import (
111111
type ContainerFindIncludes struct{}
112112

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

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

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

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

188193
func runCommand(ctx *types.Context, command types.Command) error {
@@ -360,7 +365,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
360365
f.ctx.GetLogger().Println("info", "Using cached library dependencies for file: {0}", sourcePath)
361366
}
362367
} else {
363-
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.ctx.IncludeFolders)
368+
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.IncludeDirsFound)
364369
// Unwrap error and see if it is an ExitError.
365370
_, isExitError := errors.Cause(preprocErr).(*exec.ExitError)
366371
if preprocErr == nil {
@@ -392,7 +397,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
392397
// return errors.WithStack(err)
393398
if preprocErr == nil || preprocStderr == nil {
394399
// Filename came from cache, so run preprocessor to obtain error to show
395-
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.ctx.IncludeFolders)
400+
preprocStderr, preprocErr = GCCPreprocRunnerForDiscoveringIncludes(f.ctx, sourcePath, targetFilePath, f.IncludeDirsFound)
396401
if preprocErr == nil {
397402
// If there is a missing #include in the cache, but running
398403
// gcc does not reproduce that, there is something wrong.
@@ -409,10 +414,14 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
409414
// include path and queue its source files for further
410415
// include scanning
411416
f.ctx.ImportedLibraries = append(f.ctx.ImportedLibraries, library)
412-
f.appendIncludeFolder(sourcePath, include, library.SourceDir)
417+
418+
f.log.Debugf("Using library include folder: %s", library.SourceDir)
419+
f.IncludeDirsFound.Add(library.SourceDir)
420+
f.cache.AddAndCheckEntry(sourcePath, include, library.SourceDir)
421+
413422
if library.UtilityDir != nil {
414423
// TODO: Use library.SourceDirs() instead?
415-
f.ctx.IncludeFolders = append(f.ctx.IncludeFolders, library.UtilityDir)
424+
f.IncludeDirsFound.Add(library.UtilityDir)
416425
}
417426
sourceDirs := library.SourceDirs()
418427
buildDir := f.ctx.LibrariesBuildPath.Join(library.Name)

0 commit comments

Comments
 (0)