Skip to content

Commit 134bb28

Browse files
committed
Moved includeCache in his own file and made it a field of detector
1 parent 53d45ca commit 134bb28

File tree

2 files changed

+130
-108
lines changed

2 files changed

+130
-108
lines changed

internal/arduino/builder/internal/detector/detector.go

Lines changed: 13 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
186187
func (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

300300
func (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 {
595594
func (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-
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package detector
17+
18+
import (
19+
"encoding/json"
20+
"time"
21+
22+
"github.com/arduino/go-paths-helper"
23+
)
24+
25+
type includeCache struct {
26+
// Are the cache contents valid so far?
27+
valid bool
28+
// Index into entries of the next entry to be processed. Unused
29+
// when the cache is invalid.
30+
next int
31+
entries []*includeCacheEntry
32+
}
33+
34+
// Next Return the next cache entry. Should only be called when the cache is
35+
// valid and a next entry is available (the latter can be checked with
36+
// ExpectFile). Does not advance the cache.
37+
func (cache *includeCache) Next() *includeCacheEntry {
38+
return cache.entries[cache.next]
39+
}
40+
41+
// ExpectFile check that the next cache entry is about the given file. If it is
42+
// not, or no entry is available, the cache is invalidated. Does not
43+
// advance the cache.
44+
func (cache *includeCache) ExpectFile(sourcefile *paths.Path) {
45+
if cache.valid && (cache.next >= len(cache.entries) || !cache.Next().Sourcefile.EqualsTo(sourcefile)) {
46+
cache.valid = false
47+
cache.entries = cache.entries[:cache.next]
48+
}
49+
}
50+
51+
// ExpectEntry check that the next entry matches the given values. If so, advance
52+
// the cache. If not, the cache is invalidated. If the cache is
53+
// invalidated, or was already invalid, an entry with the given values
54+
// is appended.
55+
func (cache *includeCache) ExpectEntry(sourcefile *paths.Path, include string, librarypath *paths.Path) {
56+
entry := &includeCacheEntry{Sourcefile: sourcefile, Include: include, Includepath: librarypath}
57+
if cache.valid {
58+
if cache.next < len(cache.entries) && cache.Next().Equals(entry) {
59+
cache.next++
60+
} else {
61+
cache.valid = false
62+
cache.entries = cache.entries[:cache.next]
63+
}
64+
}
65+
66+
if !cache.valid {
67+
cache.entries = append(cache.entries, entry)
68+
}
69+
}
70+
71+
// ExpectEnd check that the cache is completely consumed. If not, the cache is
72+
// invalidated.
73+
func (cache *includeCache) ExpectEnd() {
74+
if cache.valid && cache.next < len(cache.entries) {
75+
cache.valid = false
76+
cache.entries = cache.entries[:cache.next]
77+
}
78+
}
79+
80+
// Read the cache from the given file
81+
func readCache(path *paths.Path) *includeCache {
82+
bytes, err := path.ReadFile()
83+
if err != nil {
84+
// Return an empty, invalid cache
85+
return &includeCache{}
86+
}
87+
result := &includeCache{}
88+
err = json.Unmarshal(bytes, &result.entries)
89+
if err != nil {
90+
// Return an empty, invalid cache
91+
return &includeCache{}
92+
}
93+
result.valid = true
94+
return result
95+
}
96+
97+
// Write the given cache to the given file if it is invalidated. If the
98+
// cache is still valid, just update the timestamps of the file.
99+
func (cache *includeCache) write(path *paths.Path) error {
100+
// If the cache was still valid all the way, just touch its file
101+
// (in case any source file changed without influencing the
102+
// includes). If it was invalidated, overwrite the cache with
103+
// the new contents.
104+
if cache.valid {
105+
path.Chtimes(time.Now(), time.Now())
106+
} else {
107+
bytes, err := json.MarshalIndent(cache.entries, "", " ")
108+
if err != nil {
109+
return err
110+
}
111+
err = path.WriteFile(bytes)
112+
if err != nil {
113+
return err
114+
}
115+
}
116+
return nil
117+
}

0 commit comments

Comments
 (0)