Skip to content

Commit 901a574

Browse files
move coreBuildCachePath in the arduino Builder
1 parent b73a681 commit 901a574

File tree

10 files changed

+97
-85
lines changed

10 files changed

+97
-85
lines changed

arduino/builder/builder.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
package builder
1717

18-
import "github.com/arduino/arduino-cli/arduino/sketch"
18+
import (
19+
"github.com/arduino/arduino-cli/arduino/sketch"
20+
"github.com/arduino/go-paths-helper"
21+
)
1922

2023
// nolint
2124
const (
@@ -31,11 +34,15 @@ const (
3134
// Builder is a Sketch builder.
3235
type Builder struct {
3336
sketch *sketch.Sketch
37+
38+
// core related
39+
coreBuildCachePath *paths.Path
3440
}
3541

3642
// NewBuilder creates a sketch Builder.
37-
func NewBuilder(sk *sketch.Sketch) *Builder {
43+
func NewBuilder(sk *sketch.Sketch, coreBuildCachePath *paths.Path) *Builder {
3844
return &Builder{
39-
sketch: sk,
45+
sketch: sk,
46+
coreBuildCachePath: coreBuildCachePath,
4047
}
4148
}

arduino/builder/core.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package builder
2+
3+
import "github.com/arduino/go-paths-helper"
4+
5+
// CoreBuildCachePath fixdoc
6+
func (b *Builder) CoreBuildCachePath() *paths.Path {
7+
return b.coreBuildCachePath
8+
}

arduino/builder/sketch_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestMergeSketchSources(t *testing.T) {
4848
}
4949
mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource)
5050

51-
b := NewBuilder(sk)
51+
b := NewBuilder(sk, nil)
5252
offset, source, err := b.sketchMergeSources(nil)
5353
require.Nil(t, err)
5454
require.Equal(t, 2, offset)
@@ -61,7 +61,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) {
6161
require.NotNil(t, sk)
6262

6363
// ensure not to include Arduino.h when it's already there
64-
b := NewBuilder(sk)
64+
b := NewBuilder(sk, nil)
6565
_, source, err := b.sketchMergeSources(nil)
6666
require.Nil(t, err)
6767
require.Equal(t, 1, strings.Count(source, "<Arduino.h>"))
@@ -76,7 +76,7 @@ func TestCopyAdditionalFiles(t *testing.T) {
7676
sk1, err := sketch.New(paths.New("testdata", t.Name()))
7777
require.Nil(t, err)
7878
require.Equal(t, sk1.AdditionalFiles.Len(), 1)
79-
b1 := NewBuilder(sk1)
79+
b1 := NewBuilder(sk1, nil)
8080

8181
// copy the sketch over, create a fake main file we don't care about it
8282
// but we need it for `SketchLoad` to succeed later

commands/compile/compile.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,21 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
155155
// cache is purged after compilation to not remove entries that might be required
156156
defer maybePurgeBuildCache()
157157

158-
sketchBuilder := bldr.NewBuilder(sk)
158+
var coreBuildCachePath *paths.Path
159+
if req.GetBuildCachePath() == "" {
160+
coreBuildCachePath = paths.TempDir().Join("arduino", "cores")
161+
} else {
162+
buildCachePath, err := paths.New(req.GetBuildCachePath()).Abs()
163+
if err != nil {
164+
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build cache directory"), Cause: err}
165+
}
166+
if err := buildCachePath.MkdirAll(); err != nil {
167+
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build cache directory"), Cause: err}
168+
}
169+
coreBuildCachePath = buildCachePath.Join("core")
170+
}
171+
172+
sketchBuilder := bldr.NewBuilder(sk, coreBuildCachePath)
159173

160174
// Add build properites related to sketch data
161175
buildProperties = sketchBuilder.SetupBuildProperties(buildProperties, buildPath, req.GetOptimizeForDebug())
@@ -206,19 +220,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
206220
builderCtx.WarningsLevel = builder.DEFAULT_WARNINGS_LEVEL
207221
}
208222

209-
if req.GetBuildCachePath() == "" {
210-
builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino", "cores")
211-
} else {
212-
buildCachePath, err := paths.New(req.GetBuildCachePath()).Abs()
213-
if err != nil {
214-
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build cache directory"), Cause: err}
215-
}
216-
if err := buildCachePath.MkdirAll(); err != nil {
217-
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build cache directory"), Cause: err}
218-
}
219-
builderCtx.CoreBuildCachePath = buildCachePath.Join("core")
220-
}
221-
222223
builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings)
223224

224225
builderCtx.Stdout = outStream

internal/integrationtest/compile_4/compile_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"testing"
2424
"text/template"
25+
"time"
2526

2627
"github.com/arduino/arduino-cli/arduino/builder/cpp"
2728
"github.com/arduino/arduino-cli/internal/integrationtest"
@@ -710,3 +711,55 @@ func comparePreprocessGoldenFile(t *testing.T, sketchDir *paths.Path, preprocess
710711

711712
require.Equal(t, buf.String(), strings.Replace(preprocessedSketch, "\r\n", "\n", -1))
712713
}
714+
715+
func TestCoreCaching(t *testing.T) {
716+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
717+
defer env.CleanUp()
718+
719+
sketchPath, err := paths.New("..", "testdata", "bare_minimum").Abs()
720+
require.NoError(t, err)
721+
722+
// Install Arduino AVR Boards
723+
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
724+
require.NoError(t, err)
725+
726+
// Create temporary cache dir
727+
buildCachePath, err := paths.MkTempDir("", "test_build_cache")
728+
require.NoError(t, err)
729+
defer buildCachePath.RemoveAll()
730+
731+
// Build first time
732+
_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String())
733+
require.NoError(t, err)
734+
735+
// Find cached core and save timestamp
736+
pathList, err := buildCachePath.ReadDirRecursiveFiltered(nil, paths.FilterPrefixes("core.a"))
737+
require.NoError(t, err)
738+
require.Len(t, pathList, 1)
739+
cachedCoreFile := pathList[0]
740+
lastUsedPath := cachedCoreFile.Parent().Join(".last-used")
741+
require.True(t, lastUsedPath.Exist())
742+
coreStatBefore, err := cachedCoreFile.Stat()
743+
require.NoError(t, err)
744+
745+
// Run build again and check timestamp is unchanged
746+
_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String())
747+
require.NoError(t, err)
748+
coreStatAfterRebuild, err := cachedCoreFile.Stat()
749+
require.NoError(t, err)
750+
require.Equal(t, coreStatBefore.ModTime(), coreStatAfterRebuild.ModTime())
751+
752+
// Touch a file of the core and check if the builder invalidate the cache
753+
time.Sleep(time.Second)
754+
now := time.Now().Local()
755+
coreFolder := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.8.6")
756+
err = coreFolder.Join("cores", "arduino", "Arduino.h").Chtimes(now, now)
757+
require.NoError(t, err)
758+
759+
// Run build again, to verify that the builder rebuilds core.a
760+
_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--build-cache-path", buildCachePath.String(), sketchPath.String())
761+
require.NoError(t, err)
762+
coreStatAfterTouch, err := cachedCoreFile.Stat()
763+
require.NoError(t, err)
764+
require.NotEqual(t, coreStatBefore.ModTime(), coreStatAfterTouch.ModTime())
765+
}

legacy/builder/builder.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func (s *Builder) Run(ctx *types.Context) error {
7373
&RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.core.prebuild", Suffix: ".pattern"},
7474

7575
types.BareCommand(func(ctx *types.Context) error {
76-
objectFiles, archiveFile, coreBuildCachePath, err := phases.CoreBuilder(
77-
ctx.BuildPath, ctx.CoreBuildPath, ctx.CoreBuildCachePath,
76+
objectFiles, archiveFile, err := phases.CoreBuilder(
77+
ctx.BuildPath, ctx.CoreBuildPath, ctx.Builder.CoreBuildCachePath(),
7878
ctx.BuildProperties,
7979
ctx.ActualPlatform,
8080
ctx.Verbose, ctx.OnlyUpdateCompilationDatabase, ctx.Clean,
@@ -90,7 +90,6 @@ func (s *Builder) Run(ctx *types.Context) error {
9090

9191
ctx.CoreObjectsFiles = objectFiles
9292
ctx.CoreArchiveFilePath = archiveFile
93-
ctx.CoreBuildCachePath = coreBuildCachePath
9493

9594
return err
9695
}),

legacy/builder/phases/core_builder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func CoreBuilder(
5252
verboseInfoFn func(msg string),
5353
verboseStdoutFn, verboseStderrFn func(data []byte),
5454
progress *progress.Struct, progressCB rpc.TaskProgressCB,
55-
) (paths.PathList, *paths.Path, *paths.Path, error) {
55+
) (paths.PathList, *paths.Path, error) {
5656
if err := coreBuildPath.MkdirAll(); err != nil {
57-
return nil, nil, coreBuildCachePath, errors.WithStack(err)
57+
return nil, nil, errors.WithStack(err)
5858
}
5959

6060
if coreBuildCachePath != nil {
@@ -63,7 +63,7 @@ func CoreBuilder(
6363
verboseInfoFn(tr("Running normal build of the core..."))
6464
coreBuildCachePath = nil
6565
} else if err := coreBuildCachePath.MkdirAll(); err != nil {
66-
return nil, nil, coreBuildCachePath, errors.WithStack(err)
66+
return nil, nil, errors.WithStack(err)
6767
}
6868
}
6969

@@ -81,10 +81,10 @@ func CoreBuilder(
8181
progress, progressCB,
8282
)
8383
if err != nil {
84-
return nil, nil, coreBuildCachePath, errors.WithStack(err)
84+
return nil, nil, errors.WithStack(err)
8585
}
8686

87-
return objectFiles, archiveFile, coreBuildCachePath, nil
87+
return objectFiles, archiveFile, nil
8888
}
8989

9090
func compileCore(

legacy/builder/test/builder_test.go

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ import (
1919
"fmt"
2020
"path/filepath"
2121
"testing"
22-
"time"
2322

2423
bldr "github.com/arduino/arduino-cli/arduino/builder"
2524
"github.com/arduino/arduino-cli/arduino/builder/detector"
2625
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2726
"github.com/arduino/arduino-cli/arduino/sketch"
2827
"github.com/arduino/arduino-cli/legacy/builder"
2928
"github.com/arduino/arduino-cli/legacy/builder/constants"
30-
"github.com/arduino/arduino-cli/legacy/builder/phases"
3129
"github.com/arduino/arduino-cli/legacy/builder/types"
3230
"github.com/arduino/go-paths-helper"
3331
"github.com/stretchr/testify/require"
@@ -100,7 +98,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat
10098
ctx.Sketch = sk
10199
}
102100

103-
ctx.Builder = bldr.NewBuilder(ctx.Sketch)
101+
ctx.Builder = bldr.NewBuilder(ctx.Sketch, nil)
104102
if fqbn != "" {
105103
ctx.FQBN = parseFQBN(t, fqbn)
106104
targetPackage, targetPlatform, targetBoard, buildProperties, buildPlatform, err := pme.ResolveFQBN(ctx.FQBN)
@@ -185,48 +183,3 @@ func TestBuilderWithBuildPathInSketchDir(t *testing.T) {
185183
err = command.Run(ctx)
186184
NoError(t, err)
187185
}
188-
189-
func TestBuilderCacheCoreAFile(t *testing.T) {
190-
ctx := prepareBuilderTestContext(t, nil, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno")
191-
defer cleanUpBuilderTestContext(t, ctx)
192-
193-
SetupBuildCachePath(t, ctx)
194-
defer ctx.CoreBuildCachePath.RemoveAll()
195-
196-
// Run build
197-
bldr := builder.Builder{}
198-
err := bldr.Run(ctx)
199-
NoError(t, err)
200-
201-
// Pick timestamp of cached core
202-
coreFolder := paths.New("downloaded_hardware", "arduino", "avr")
203-
coreFileName := phases.GetCachedCoreArchiveDirName(ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"), coreFolder)
204-
cachedCoreFile := ctx.CoreBuildCachePath.Join(coreFileName, "core.a")
205-
coreStatBefore, err := cachedCoreFile.Stat()
206-
require.NoError(t, err)
207-
lastUsedFile := ctx.CoreBuildCachePath.Join(coreFileName, ".last-used")
208-
_, err = lastUsedFile.Stat()
209-
require.NoError(t, err)
210-
211-
// Run build again, to verify that the builder skips rebuilding core.a
212-
err = bldr.Run(ctx)
213-
NoError(t, err)
214-
215-
coreStatAfterRebuild, err := cachedCoreFile.Stat()
216-
require.NoError(t, err)
217-
require.Equal(t, coreStatBefore.ModTime(), coreStatAfterRebuild.ModTime())
218-
219-
// Touch a file of the core and check if the builder invalidate the cache
220-
time.Sleep(time.Second)
221-
now := time.Now().Local()
222-
err = coreFolder.Join("cores", "arduino", "Arduino.h").Chtimes(now, now)
223-
require.NoError(t, err)
224-
225-
// Run build again, to verify that the builder rebuilds core.a
226-
err = bldr.Run(ctx)
227-
NoError(t, err)
228-
229-
coreStatAfterTouch, err := cachedCoreFile.Stat()
230-
require.NoError(t, err)
231-
require.NotEqual(t, coreStatBefore.ModTime(), coreStatAfterTouch.ModTime())
232-
}

legacy/builder/test/helper.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/arduino/arduino-cli/arduino/builder/cpp"
2727
"github.com/arduino/arduino-cli/arduino/cores"
2828
"github.com/arduino/arduino-cli/arduino/libraries"
29-
"github.com/arduino/arduino-cli/legacy/builder/constants"
3029
"github.com/arduino/arduino-cli/legacy/builder/types"
3130
paths "github.com/arduino/go-paths-helper"
3231
"github.com/stretchr/testify/assert"
@@ -70,13 +69,6 @@ func SetupBuildPath(t *testing.T, ctx *types.Context) *paths.Path {
7069
return buildPath
7170
}
7271

73-
func SetupBuildCachePath(t *testing.T, ctx *types.Context) *paths.Path {
74-
buildCachePath, err := paths.MkTempDir(constants.EMPTY_STRING, "test_build_cache")
75-
NoError(t, err)
76-
ctx.CoreBuildCachePath = buildCachePath
77-
return buildCachePath
78-
}
79-
8072
func parseFQBN(t *testing.T, fqbnIn string) *cores.FQBN {
8173
fqbn, err := cores.ParseFQBN(fqbnIn)
8274
require.NoError(t, err)

legacy/builder/types/context.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ type Context struct {
6161
BuildPath *paths.Path
6262
SketchBuildPath *paths.Path
6363
CoreBuildPath *paths.Path
64-
CoreBuildCachePath *paths.Path
6564
CoreArchiveFilePath *paths.Path
6665
CoreObjectsFiles paths.PathList
6766
LibrariesBuildPath *paths.Path

0 commit comments

Comments
 (0)