@@ -52,6 +52,7 @@ type SketchLibrariesDetector struct {
5252 librariesManager * librariesmanager.LibrariesManager
5353 librariesResolver * librariesresolver.Cpp
5454 useCachedLibrariesResolution bool
55+ cache * includeCache
5556 onlyUpdateCompilationDatabase bool
5657 importedLibraries libraries.List
5758 librariesResolutionResults map [string ]libraryResolutionResult
@@ -184,13 +185,12 @@ func (l *SketchLibrariesDetector) IncludeFolders() paths.PathList {
184185// and should be the empty string for the default include folders, like
185186// the core or variant.
186187func (l * SketchLibrariesDetector ) appendIncludeFolder (
187- cache * includeCache ,
188188 sourceFilePath * paths.Path ,
189189 include string ,
190190 folder * paths.Path ,
191191) {
192192 l .includeFolders = append (l .includeFolders , folder )
193- cache .ExpectEntry (sourceFilePath , include , folder )
193+ l . cache .ExpectEntry (sourceFilePath , include , folder )
194194}
195195
196196// FindIncludes todo
@@ -246,11 +246,11 @@ func (l *SketchLibrariesDetector) findIncludes(
246246 }
247247
248248 cachePath := buildPath .Join ("includes.cache" )
249- cache : = readCache (cachePath )
249+ l . cache = readCache (cachePath )
250250
251- l .appendIncludeFolder (cache , nil , "" , buildCorePath )
251+ l .appendIncludeFolder (nil , "" , buildCorePath )
252252 if buildVariantPath != nil {
253- l .appendIncludeFolder (cache , nil , "" , buildVariantPath )
253+ l .appendIncludeFolder (nil , "" , buildVariantPath )
254254 }
255255
256256 sourceFileQueue := & uniqueSourceFileQueue {}
@@ -270,16 +270,16 @@ func (l *SketchLibrariesDetector) findIncludes(
270270 }
271271
272272 for ! sourceFileQueue .Empty () {
273- err := l .findIncludesUntilDone (ctx , cache , sourceFileQueue , buildProperties , librariesBuildPath , platformArch )
273+ err := l .findIncludesUntilDone (ctx , sourceFileQueue , buildProperties , librariesBuildPath , platformArch )
274274 if err != nil {
275275 cachePath .Remove ()
276276 return err
277277 }
278278 }
279279
280280 // Finalize the cache
281- cache .ExpectEnd ()
282- if err := writeCache ( cache , cachePath ); err != nil {
281+ l . cache .ExpectEnd ()
282+ if err := l . cache . write ( cachePath ); err != nil {
283283 return err
284284 }
285285 }
@@ -299,7 +299,6 @@ func (l *SketchLibrariesDetector) findIncludes(
299299
300300func (l * SketchLibrariesDetector ) findIncludesUntilDone (
301301 ctx context.Context ,
302- cache * includeCache ,
303302 sourceFileQueue * uniqueSourceFileQueue ,
304303 buildProperties * properties.Map ,
305304 librariesBuildPath * paths.Path ,
@@ -330,7 +329,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
330329
331330 first := true
332331 for {
333- cache .ExpectFile (sourcePath )
332+ l . cache .ExpectFile (sourcePath )
334333
335334 // Libraries may require the "utility" directory to be added to the include
336335 // search path, but only for the source code of the library, so we temporary
@@ -345,8 +344,8 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
345344 var preprocFirstResult * runner.Result
346345
347346 var missingIncludeH string
348- if unchanged && cache .valid {
349- missingIncludeH = cache .Next ().Include
347+ if unchanged && l . cache .valid {
348+ missingIncludeH = l . cache .Next ().Include
350349 if first && l .logger .VerbosityLevel () == logger .VerbosityVerbose {
351350 l .logger .Info (i18n .Tr ("Using cached library dependencies for file: %[1]s" , sourcePath ))
352351 }
@@ -373,7 +372,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
373372
374373 if missingIncludeH == "" {
375374 // No missing includes found, we're done
376- cache .ExpectEntry (sourcePath , "" , nil )
375+ l . cache .ExpectEntry (sourcePath , "" , nil )
377376 return nil
378377 }
379378
@@ -406,7 +405,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
406405 // include path and queue its source files for further
407406 // include scanning
408407 l .AppendImportedLibraries (library )
409- l .appendIncludeFolder (cache , sourcePath , missingIncludeH , library .SourceDir )
408+ l .appendIncludeFolder (sourcePath , missingIncludeH , library .SourceDir )
410409
411410 if library .Precompiled && library .PrecompiledWithSources {
412411 // Fully precompiled libraries should have no dependencies to avoid ABI breakage
@@ -595,97 +594,3 @@ func (entry *includeCacheEntry) String() string {
595594func (entry * includeCacheEntry ) Equals (other * includeCacheEntry ) bool {
596595 return entry .String () == other .String ()
597596}
598-
599- type includeCache struct {
600- // Are the cache contents valid so far?
601- valid bool
602- // Index into entries of the next entry to be processed. Unused
603- // when the cache is invalid.
604- next int
605- entries []* includeCacheEntry
606- }
607-
608- // Next Return the next cache entry. Should only be called when the cache is
609- // valid and a next entry is available (the latter can be checked with
610- // ExpectFile). Does not advance the cache.
611- func (cache * includeCache ) Next () * includeCacheEntry {
612- return cache .entries [cache .next ]
613- }
614-
615- // ExpectFile check that the next cache entry is about the given file. If it is
616- // not, or no entry is available, the cache is invalidated. Does not
617- // advance the cache.
618- func (cache * includeCache ) ExpectFile (sourcefile * paths.Path ) {
619- if cache .valid && (cache .next >= len (cache .entries ) || ! cache .Next ().Sourcefile .EqualsTo (sourcefile )) {
620- cache .valid = false
621- cache .entries = cache .entries [:cache .next ]
622- }
623- }
624-
625- // ExpectEntry check that the next entry matches the given values. If so, advance
626- // the cache. If not, the cache is invalidated. If the cache is
627- // invalidated, or was already invalid, an entry with the given values
628- // is appended.
629- func (cache * includeCache ) ExpectEntry (sourcefile * paths.Path , include string , librarypath * paths.Path ) {
630- entry := & includeCacheEntry {Sourcefile : sourcefile , Include : include , Includepath : librarypath }
631- if cache .valid {
632- if cache .next < len (cache .entries ) && cache .Next ().Equals (entry ) {
633- cache .next ++
634- } else {
635- cache .valid = false
636- cache .entries = cache .entries [:cache .next ]
637- }
638- }
639-
640- if ! cache .valid {
641- cache .entries = append (cache .entries , entry )
642- }
643- }
644-
645- // ExpectEnd check that the cache is completely consumed. If not, the cache is
646- // invalidated.
647- func (cache * includeCache ) ExpectEnd () {
648- if cache .valid && cache .next < len (cache .entries ) {
649- cache .valid = false
650- cache .entries = cache .entries [:cache .next ]
651- }
652- }
653-
654- // Read the cache from the given file
655- func readCache (path * paths.Path ) * includeCache {
656- bytes , err := path .ReadFile ()
657- if err != nil {
658- // Return an empty, invalid cache
659- return & includeCache {}
660- }
661- result := & includeCache {}
662- err = json .Unmarshal (bytes , & result .entries )
663- if err != nil {
664- // Return an empty, invalid cache
665- return & includeCache {}
666- }
667- result .valid = true
668- return result
669- }
670-
671- // Write the given cache to the given file if it is invalidated. If the
672- // cache is still valid, just update the timestamps of the file.
673- func writeCache (cache * includeCache , path * paths.Path ) error {
674- // If the cache was still valid all the way, just touch its file
675- // (in case any source file changed without influencing the
676- // includes). If it was invalidated, overwrite the cache with
677- // the new contents.
678- if cache .valid {
679- path .Chtimes (time .Now (), time .Now ())
680- } else {
681- bytes , err := json .MarshalIndent (cache .entries , "" , " " )
682- if err != nil {
683- return err
684- }
685- err = path .WriteFile (bytes )
686- if err != nil {
687- return err
688- }
689- }
690- return nil
691- }
0 commit comments