@@ -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
@@ -593,97 +592,3 @@ func (entry *includeCacheEntry) String() string {
593
592
func (entry * includeCacheEntry ) Equals (other * includeCacheEntry ) bool {
594
593
return entry .String () == other .String ()
595
594
}
596
-
597
- type includeCache struct {
598
- // Are the cache contents valid so far?
599
- valid bool
600
- // Index into entries of the next entry to be processed. Unused
601
- // when the cache is invalid.
602
- next int
603
- entries []* includeCacheEntry
604
- }
605
-
606
- // Next Return the next cache entry. Should only be called when the cache is
607
- // valid and a next entry is available (the latter can be checked with
608
- // ExpectFile). Does not advance the cache.
609
- func (cache * includeCache ) Next () * includeCacheEntry {
610
- return cache .entries [cache .next ]
611
- }
612
-
613
- // ExpectFile check that the next cache entry is about the given file. If it is
614
- // not, or no entry is available, the cache is invalidated. Does not
615
- // advance the cache.
616
- func (cache * includeCache ) ExpectFile (sourcefile * paths.Path ) {
617
- if cache .valid && (cache .next >= len (cache .entries ) || ! cache .Next ().Sourcefile .EqualsTo (sourcefile )) {
618
- cache .valid = false
619
- cache .entries = cache .entries [:cache .next ]
620
- }
621
- }
622
-
623
- // ExpectEntry check that the next entry matches the given values. If so, advance
624
- // the cache. If not, the cache is invalidated. If the cache is
625
- // invalidated, or was already invalid, an entry with the given values
626
- // is appended.
627
- func (cache * includeCache ) ExpectEntry (sourcefile * paths.Path , include string , librarypath * paths.Path ) {
628
- entry := & includeCacheEntry {Sourcefile : sourcefile , Include : include , Includepath : librarypath }
629
- if cache .valid {
630
- if cache .next < len (cache .entries ) && cache .Next ().Equals (entry ) {
631
- cache .next ++
632
- } else {
633
- cache .valid = false
634
- cache .entries = cache .entries [:cache .next ]
635
- }
636
- }
637
-
638
- if ! cache .valid {
639
- cache .entries = append (cache .entries , entry )
640
- }
641
- }
642
-
643
- // ExpectEnd check that the cache is completely consumed. If not, the cache is
644
- // invalidated.
645
- func (cache * includeCache ) ExpectEnd () {
646
- if cache .valid && cache .next < len (cache .entries ) {
647
- cache .valid = false
648
- cache .entries = cache .entries [:cache .next ]
649
- }
650
- }
651
-
652
- // Read the cache from the given file
653
- func readCache (path * paths.Path ) * includeCache {
654
- bytes , err := path .ReadFile ()
655
- if err != nil {
656
- // Return an empty, invalid cache
657
- return & includeCache {}
658
- }
659
- result := & includeCache {}
660
- err = json .Unmarshal (bytes , & result .entries )
661
- if err != nil {
662
- // Return an empty, invalid cache
663
- return & includeCache {}
664
- }
665
- result .valid = true
666
- return result
667
- }
668
-
669
- // Write the given cache to the given file if it is invalidated. If the
670
- // cache is still valid, just update the timestamps of the file.
671
- func writeCache (cache * includeCache , path * paths.Path ) error {
672
- // If the cache was still valid all the way, just touch its file
673
- // (in case any source file changed without influencing the
674
- // includes). If it was invalidated, overwrite the cache with
675
- // the new contents.
676
- if cache .valid {
677
- path .Chtimes (time .Now (), time .Now ())
678
- } else {
679
- bytes , err := json .MarshalIndent (cache .entries , "" , " " )
680
- if err != nil {
681
- return err
682
- }
683
- err = path .WriteFile (bytes )
684
- if err != nil {
685
- return err
686
- }
687
- }
688
- return nil
689
- }
0 commit comments