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