@@ -111,12 +111,15 @@ import (
111
111
type ContainerFindIncludes struct {}
112
112
113
113
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 )
116
118
}
117
119
if err := finder .DetectLibraries (); err != nil {
118
120
return err
119
121
}
122
+ ctx .IncludeFolders .AddAllMissing (finder .IncludeDirsFound )
120
123
if err := runCommand (ctx , & FailIfImportedLibraryIsWrong {}); err != nil {
121
124
return errors .WithStack (err )
122
125
}
@@ -128,22 +131,30 @@ func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
128
131
// libraries used in a sketch and a way to cache this result for
129
132
// increasing detection speed on already processed sketches.
130
133
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
+ }
136
151
}
137
152
153
+ // DetectLibraries runs a library detection algorithm
138
154
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 )
147
158
}
148
159
149
160
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 {
174
185
return nil
175
186
}
176
187
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 )
186
191
}
187
192
188
193
func runCommand (ctx * types.Context , command types.Command ) error {
@@ -360,7 +365,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
360
365
f .ctx .GetLogger ().Println ("info" , "Using cached library dependencies for file: {0}" , sourcePath )
361
366
}
362
367
} else {
363
- preprocStderr , preprocErr = GCCPreprocRunnerForDiscoveringIncludes (f .ctx , sourcePath , targetFilePath , f .ctx . IncludeFolders )
368
+ preprocStderr , preprocErr = GCCPreprocRunnerForDiscoveringIncludes (f .ctx , sourcePath , targetFilePath , f .IncludeDirsFound )
364
369
// Unwrap error and see if it is an ExitError.
365
370
_ , isExitError := errors .Cause (preprocErr ).(* exec.ExitError )
366
371
if preprocErr == nil {
@@ -392,7 +397,7 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
392
397
// return errors.WithStack(err)
393
398
if preprocErr == nil || preprocStderr == nil {
394
399
// 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 )
396
401
if preprocErr == nil {
397
402
// If there is a missing #include in the cache, but running
398
403
// gcc does not reproduce that, there is something wrong.
@@ -409,10 +414,14 @@ func (f *CppIncludesFinder) findIncludesUntilDone(sourceFile *SourceFile) error
409
414
// include path and queue its source files for further
410
415
// include scanning
411
416
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
+
413
422
if library .UtilityDir != nil {
414
423
// TODO: Use library.SourceDirs() instead?
415
- f .ctx . IncludeFolders = append ( f . ctx . IncludeFolders , library .UtilityDir )
424
+ f .IncludeDirsFound . Add ( library .UtilityDir )
416
425
}
417
426
sourceDirs := library .SourceDirs ()
418
427
buildDir := f .ctx .LibrariesBuildPath .Join (library .Name )
0 commit comments