@@ -51,6 +51,7 @@ type libraryResolutionResult struct {
5151type SketchLibrariesDetector struct {
5252 librariesResolver * librariesresolver.Cpp
5353 useCachedLibrariesResolution bool
54+ cache * includeCache
5455 onlyUpdateCompilationDatabase bool
5556 importedLibraries libraries.List
5657 librariesResolutionResults map [string ]libraryResolutionResult
@@ -181,13 +182,12 @@ func (l *SketchLibrariesDetector) IncludeFolders() paths.PathList {
181182// and should be the empty string for the default include folders, like
182183// the core or variant.
183184func (l * SketchLibrariesDetector ) appendIncludeFolder (
184- cache * includeCache ,
185185 sourceFilePath * paths.Path ,
186186 include string ,
187187 folder * paths.Path ,
188188) {
189189 l .includeFolders = append (l .includeFolders , folder )
190- cache .ExpectEntry (sourceFilePath , include , folder )
190+ l . cache .ExpectEntry (sourceFilePath , include , folder )
191191}
192192
193193// FindIncludes todo
@@ -243,11 +243,11 @@ func (l *SketchLibrariesDetector) findIncludes(
243243 }
244244
245245 cachePath := buildPath .Join ("includes.cache" )
246- cache : = readCache (cachePath )
246+ l . cache = readCache (cachePath )
247247
248- l .appendIncludeFolder (cache , nil , "" , buildCorePath )
248+ l .appendIncludeFolder (nil , "" , buildCorePath )
249249 if buildVariantPath != nil {
250- l .appendIncludeFolder (cache , nil , "" , buildVariantPath )
250+ l .appendIncludeFolder (nil , "" , buildVariantPath )
251251 }
252252
253253 sourceFileQueue := & uniqueSourceFileQueue {}
@@ -267,16 +267,16 @@ func (l *SketchLibrariesDetector) findIncludes(
267267 }
268268
269269 for ! sourceFileQueue .Empty () {
270- err := l .findIncludesUntilDone (ctx , cache , sourceFileQueue , buildProperties , librariesBuildPath , platformArch )
270+ err := l .findIncludesUntilDone (ctx , sourceFileQueue , buildProperties , librariesBuildPath , platformArch )
271271 if err != nil {
272272 cachePath .Remove ()
273273 return err
274274 }
275275 }
276276
277277 // 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 {
280280 return err
281281 }
282282 }
@@ -296,7 +296,6 @@ func (l *SketchLibrariesDetector) findIncludes(
296296
297297func (l * SketchLibrariesDetector ) findIncludesUntilDone (
298298 ctx context.Context ,
299- cache * includeCache ,
300299 sourceFileQueue * uniqueSourceFileQueue ,
301300 buildProperties * properties.Map ,
302301 librariesBuildPath * paths.Path ,
@@ -327,7 +326,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
327326
328327 first := true
329328 for {
330- cache .ExpectFile (sourcePath )
329+ l . cache .ExpectFile (sourcePath )
331330
332331 // Libraries may require the "utility" directory to be added to the include
333332 // search path, but only for the source code of the library, so we temporary
@@ -342,8 +341,8 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
342341 var preprocFirstResult * runner.Result
343342
344343 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
347346 if first && l .logger .Verbose () {
348347 l .logger .Info (i18n .Tr ("Using cached library dependencies for file: %[1]s" , sourcePath ))
349348 }
@@ -370,7 +369,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
370369
371370 if missingIncludeH == "" {
372371 // No missing includes found, we're done
373- cache .ExpectEntry (sourcePath , "" , nil )
372+ l . cache .ExpectEntry (sourcePath , "" , nil )
374373 return nil
375374 }
376375
@@ -403,7 +402,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
403402 // include path and queue its source files for further
404403 // include scanning
405404 l .AppendImportedLibraries (library )
406- l .appendIncludeFolder (cache , sourcePath , missingIncludeH , library .SourceDir )
405+ l .appendIncludeFolder (sourcePath , missingIncludeH , library .SourceDir )
407406
408407 if library .Precompiled && library .PrecompiledWithSources {
409408 // Fully precompiled libraries should have no dependencies to avoid ABI breakage
@@ -593,97 +592,3 @@ func (entry *includeCacheEntry) String() string {
593592func (entry * includeCacheEntry ) Equals (other * includeCacheEntry ) bool {
594593 return entry .String () == other .String ()
595594}
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