Skip to content

Commit

Permalink
--build-cache-path now contains also the sketches
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Jul 25, 2024
1 parent 23888af commit 6a7d47f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
28 changes: 12 additions & 16 deletions commands/service_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu

// Generate or retrieve build path
var buildPath *paths.Path
isDefaultBuildPath := false
if buildPathArg := req.GetBuildPath(); buildPathArg != "" {
buildPath = paths.New(req.GetBuildPath()).Canonical()
if in, _ := buildPath.IsInsideDir(sk.FullPath); in && buildPath.IsDir() {
Expand All @@ -171,24 +170,10 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
}
}
}
if buildPath == nil {
buildPath = sk.DefaultBuildPath()
isDefaultBuildPath = true
}
if err = buildPath.MkdirAll(); err != nil {
return &cmderrors.PermissionDeniedError{Message: i18n.Tr("Cannot create build directory"), Cause: err}
}

var coreBuildCachePath *paths.Path
var extraCoreBuildCachePaths paths.PathList
if isDefaultBuildPath {
buildcache.New(buildPath.Parent()).GetOrCreate(buildPath.Base())
// cache is purged after compilation to not remove entries that might be required

defer maybePurgeBuildCache(
s.settings.GetCompilationsBeforeBuildCachePurge(),
s.settings.GetBuildCacheTTL().Abs())

if buildPath == nil {
var buildCachePath *paths.Path
if req.GetBuildCachePath() != "" {
p, err := paths.New(req.GetBuildCachePath()).Abs()
Expand All @@ -214,6 +199,17 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
for i, p := range extraCoreBuildCachePaths {
extraCoreBuildCachePaths[i] = p.Join("cores")
}

buildPath = sk.DefaultBuildPath(buildCachePath)
buildcache.New(buildPath.Parent()).GetOrCreate(buildPath.Base())

// cache is purged after compilation to not remove entries that might be required
defer maybePurgeBuildCache(
s.settings.GetCompilationsBeforeBuildCachePurge(),
s.settings.GetBuildCacheTTL().Abs())
}
if err = buildPath.MkdirAll(); err != nil {
return &cmderrors.PermissionDeniedError{Message: i18n.Tr("Cannot create build directory"), Cause: err}
}

if _, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion commands/service_debug_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Expl
}
sketchName = sk.Name
sketchDefaultFQBN = sk.GetDefaultFQBN()
sketchDefaultBuildPath = sk.DefaultBuildPath()
sketchDefaultBuildPath = sk.DefaultBuildPath(nil)
} else {
// Use placeholder sketch data
sketchName = "Sketch"
Expand Down
2 changes: 1 addition & 1 deletion commands/service_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ func determineBuildPathAndSketchName(importFile, importDir string, sk *sketch.Sk

// Case 4: only sketch specified. In this case we use the generated build path
// and the given sketch name.
return sk.DefaultBuildPath(), sk.Name + sk.MainFile.Ext(), nil
return sk.DefaultBuildPath(nil), sk.Name + sk.MainFile.Ext(), nil
}

func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions commands/service_upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
// 03: error: used both importPath and importFile
{"testdata/upload/build_path_2/Blink.ino.hex", "testdata/upload/build_path_2", nil, nil, "<nil>", ""},
// 04: only sketch without FQBN
{"", "", blonk, nil, blonk.DefaultBuildPath().String(), "Blonk.ino"},
{"", "", blonk, nil, blonk.DefaultBuildPath(nil).String(), "Blonk.ino"},
// 05: use importFile to detect build.path and project_name, sketch is ignored.
{"testdata/upload/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/upload/build_path_2", "Blink.ino"},
// 06: use importPath as build.path and Blink as project name, ignore the sketch Blonk
Expand All @@ -97,7 +97,7 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
// 11: error: used both importPath and importFile
{"testdata/upload/build_path_2/Blink.ino.hex", "testdata/upload/build_path_2", nil, fqbn, "<nil>", ""},
// 12: use sketch to determine project name and sketch+fqbn to determine build path
{"", "", blonk, fqbn, blonk.DefaultBuildPath().String(), "Blonk.ino"},
{"", "", blonk, fqbn, blonk.DefaultBuildPath(nil).String(), "Blonk.ino"},
// 13: use importFile to detect build.path and project_name, sketch+fqbn is ignored.
{"testdata/upload/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/upload/build_path_2", "Blink.ino"},
// 14: use importPath as build.path and Blink as project name, ignore the sketch Blonk, ignore fqbn
Expand Down
10 changes: 7 additions & 3 deletions internal/arduino/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,13 @@ func (e *InvalidSketchFolderNameError) Error() string {
}

// DefaultBuildPath generates the default build directory for a given sketch.
// The build path is in a temporary directory and is unique for each sketch.
func (s *Sketch) DefaultBuildPath() *paths.Path {
return paths.TempDir().Join("arduino", "sketches", s.Hash())
// The sketch build path is inside the build cache path and is unique for each sketch.
// If buildCachePath is nil the default build cache path is used.
func (s *Sketch) DefaultBuildPath(buildCachePath *paths.Path) *paths.Path {
if buildCachePath == nil {
return paths.TempDir().Join("arduino", "sketches", s.Hash())
}
return buildCachePath.Join("sketches", s.Hash())
}

// Hash generate a unique hash for the given sketch.
Expand Down
2 changes: 1 addition & 1 deletion internal/arduino/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestNewSketchFolderSymlink(t *testing.T) {

func TestGenBuildPath(t *testing.T) {
want := paths.TempDir().Join("arduino", "sketches", "ACBD18DB4CC2F85CEDEF654FCCC4A4D8")
assert.True(t, (&Sketch{FullPath: paths.New("foo")}).DefaultBuildPath().EquivalentTo(want))
assert.True(t, (&Sketch{FullPath: paths.New("foo")}).DefaultBuildPath(nil).EquivalentTo(want))
assert.Equal(t, "ACBD18DB4CC2F85CEDEF654FCCC4A4D8", (&Sketch{FullPath: paths.New("foo")}).Hash())
}

Expand Down

0 comments on commit 6a7d47f

Please sign in to comment.