@@ -51,6 +51,7 @@ type libraryResolutionResult struct {
51
51
type SketchLibrariesDetector struct {
52
52
librariesResolver * librariesresolver.Cpp
53
53
useCachedLibrariesResolution bool
54
+ cache * includeCache
54
55
onlyUpdateCompilationDatabase bool
55
56
importedLibraries libraries.List
56
57
librariesResolutionResults map [string ]libraryResolutionResult
@@ -181,13 +182,12 @@ func (l *SketchLibrariesDetector) IncludeFolders() paths.PathList {
181
182
// and should be the empty string for the default include folders, like
182
183
// the core or variant.
183
184
func (l * SketchLibrariesDetector ) appendIncludeFolder (
184
- cache * includeCache ,
185
185
sourceFilePath * paths.Path ,
186
186
include string ,
187
187
folder * paths.Path ,
188
188
) {
189
189
l .includeFolders = append (l .includeFolders , folder )
190
- cache .ExpectEntry (sourceFilePath , include , folder )
190
+ l . cache .ExpectEntry (sourceFilePath , include , folder )
191
191
}
192
192
193
193
// FindIncludes todo
@@ -243,11 +243,11 @@ func (l *SketchLibrariesDetector) findIncludes(
243
243
}
244
244
245
245
cachePath := buildPath .Join ("includes.cache" )
246
- cache : = readCache (cachePath )
246
+ l . cache = readCache (cachePath )
247
247
248
- l .appendIncludeFolder (cache , nil , "" , buildCorePath )
248
+ l .appendIncludeFolder (nil , "" , buildCorePath )
249
249
if buildVariantPath != nil {
250
- l .appendIncludeFolder (cache , nil , "" , buildVariantPath )
250
+ l .appendIncludeFolder (nil , "" , buildVariantPath )
251
251
}
252
252
253
253
sourceFileQueue := & uniqueSourceFileQueue {}
@@ -267,16 +267,16 @@ func (l *SketchLibrariesDetector) findIncludes(
267
267
}
268
268
269
269
for ! sourceFileQueue .Empty () {
270
- err := l .findIncludesUntilDone (ctx , cache , sourceFileQueue , buildProperties , librariesBuildPath , platformArch )
270
+ err := l .findIncludesUntilDone (ctx , sourceFileQueue , buildProperties , librariesBuildPath , platformArch )
271
271
if err != nil {
272
272
cachePath .Remove ()
273
273
return err
274
274
}
275
275
}
276
276
277
277
// Finalize the cache
278
- cache .ExpectEnd ()
279
- if err := writeCache ( cache , cachePath ); err != nil {
278
+ l . cache .ExpectEnd ()
279
+ if err := l . cache . write ( cachePath ); err != nil {
280
280
return err
281
281
}
282
282
}
@@ -296,7 +296,6 @@ func (l *SketchLibrariesDetector) findIncludes(
296
296
297
297
func (l * SketchLibrariesDetector ) findIncludesUntilDone (
298
298
ctx context.Context ,
299
- cache * includeCache ,
300
299
sourceFileQueue * uniqueSourceFileQueue ,
301
300
buildProperties * properties.Map ,
302
301
librariesBuildPath * paths.Path ,
@@ -327,7 +326,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
327
326
328
327
first := true
329
328
for {
330
- cache .ExpectFile (sourcePath )
329
+ l . cache .ExpectFile (sourcePath )
331
330
332
331
// Libraries may require the "utility" directory to be added to the include
333
332
// search path, but only for the source code of the library, so we temporary
@@ -342,8 +341,8 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
342
341
var preprocFirstResult * runner.Result
343
342
344
343
var missingIncludeH string
345
- if unchanged && cache .valid {
346
- missingIncludeH = cache .Next ().Include
344
+ if unchanged && l . cache .valid {
345
+ missingIncludeH = l . cache .Next ().Include
347
346
if first && l .logger .Verbose () {
348
347
l .logger .Info (i18n .Tr ("Using cached library dependencies for file: %[1]s" , sourcePath ))
349
348
}
@@ -370,7 +369,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
370
369
371
370
if missingIncludeH == "" {
372
371
// No missing includes found, we're done
373
- cache .ExpectEntry (sourcePath , "" , nil )
372
+ l . cache .ExpectEntry (sourcePath , "" , nil )
374
373
return nil
375
374
}
376
375
@@ -403,7 +402,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
403
402
// include path and queue its source files for further
404
403
// include scanning
405
404
l .AppendImportedLibraries (library )
406
- l .appendIncludeFolder (cache , sourcePath , missingIncludeH , library .SourceDir )
405
+ l .appendIncludeFolder (sourcePath , missingIncludeH , library .SourceDir )
407
406
408
407
if library .Precompiled && library .PrecompiledWithSources {
409
408
// Fully precompiled libraries should have no dependencies to avoid ABI breakage
@@ -592,97 +591,3 @@ func (entry *includeCacheEntry) String() string {
592
591
func (entry * includeCacheEntry ) Equals (other * includeCacheEntry ) bool {
593
592
return entry .String () == other .String ()
594
593
}
595
-
596
- type includeCache struct {
597
- // Are the cache contents valid so far?
598
- valid bool
599
- // Index into entries of the next entry to be processed. Unused
600
- // when the cache is invalid.
601
- next int
602
- entries []* includeCacheEntry
603
- }
604
-
605
- // Next Return the next cache entry. Should only be called when the cache is
606
- // valid and a next entry is available (the latter can be checked with
607
- // ExpectFile). Does not advance the cache.
608
- func (cache * includeCache ) Next () * includeCacheEntry {
609
- return cache .entries [cache .next ]
610
- }
611
-
612
- // ExpectFile check that the next cache entry is about the given file. If it is
613
- // not, or no entry is available, the cache is invalidated. Does not
614
- // advance the cache.
615
- func (cache * includeCache ) ExpectFile (sourcefile * paths.Path ) {
616
- if cache .valid && (cache .next >= len (cache .entries ) || ! cache .Next ().Sourcefile .EqualsTo (sourcefile )) {
617
- cache .valid = false
618
- cache .entries = cache .entries [:cache .next ]
619
- }
620
- }
621
-
622
- // ExpectEntry check that the next entry matches the given values. If so, advance
623
- // the cache. If not, the cache is invalidated. If the cache is
624
- // invalidated, or was already invalid, an entry with the given values
625
- // is appended.
626
- func (cache * includeCache ) ExpectEntry (sourcefile * paths.Path , include string , librarypath * paths.Path ) {
627
- entry := & includeCacheEntry {Sourcefile : sourcefile , Include : include , Includepath : librarypath }
628
- if cache .valid {
629
- if cache .next < len (cache .entries ) && cache .Next ().Equals (entry ) {
630
- cache .next ++
631
- } else {
632
- cache .valid = false
633
- cache .entries = cache .entries [:cache .next ]
634
- }
635
- }
636
-
637
- if ! cache .valid {
638
- cache .entries = append (cache .entries , entry )
639
- }
640
- }
641
-
642
- // ExpectEnd check that the cache is completely consumed. If not, the cache is
643
- // invalidated.
644
- func (cache * includeCache ) ExpectEnd () {
645
- if cache .valid && cache .next < len (cache .entries ) {
646
- cache .valid = false
647
- cache .entries = cache .entries [:cache .next ]
648
- }
649
- }
650
-
651
- // Read the cache from the given file
652
- func readCache (path * paths.Path ) * includeCache {
653
- bytes , err := path .ReadFile ()
654
- if err != nil {
655
- // Return an empty, invalid cache
656
- return & includeCache {}
657
- }
658
- result := & includeCache {}
659
- err = json .Unmarshal (bytes , & result .entries )
660
- if err != nil {
661
- // Return an empty, invalid cache
662
- return & includeCache {}
663
- }
664
- result .valid = true
665
- return result
666
- }
667
-
668
- // Write the given cache to the given file if it is invalidated. If the
669
- // cache is still valid, just update the timestamps of the file.
670
- func writeCache (cache * includeCache , path * paths.Path ) error {
671
- // If the cache was still valid all the way, just touch its file
672
- // (in case any source file changed without influencing the
673
- // includes). If it was invalidated, overwrite the cache with
674
- // the new contents.
675
- if cache .valid {
676
- path .Chtimes (time .Now (), time .Now ())
677
- } else {
678
- bytes , err := json .MarshalIndent (cache .entries , "" , " " )
679
- if err != nil {
680
- return err
681
- }
682
- err = path .WriteFile (bytes )
683
- if err != nil {
684
- return err
685
- }
686
- }
687
- return nil
688
- }
0 commit comments