Skip to content

Commit 03d7b48

Browse files
committed
Moved includeCache in his own file and made it a field of detector
1 parent 4298a66 commit 03d7b48

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
@@ -51,6 +51,7 @@ type libraryResolutionResult struct {
5151
type 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.
183184
func (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

297297
func (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
@@ -592,97 +591,3 @@ func (entry *includeCacheEntry) String() string {
592591
func (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-
}
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)