@@ -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 .VerbosityLevel () == logger .VerbosityVerbose {
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
@@ -592,97 +591,3 @@ func (entry *includeCacheEntry) String() string {
592591func (entry * includeCacheEntry ) Equals (other * includeCacheEntry ) bool {
593592 return entry .String () == other .String ()
594593}
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