Skip to content

legacy: Builder refactorization (part 3...) #2302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Builder struct {
sketch *sketch.Sketch
buildProperties *properties.Map

// Parallel processes
jobs int

// core related
coreBuildCachePath *paths.Path
}
Expand All @@ -37,6 +40,7 @@ func NewBuilder(
buildPath *paths.Path,
optimizeForDebug bool,
coreBuildCachePath *paths.Path,
jobs int,
) *Builder {
buildProperties := properties.NewMap()
if boardBuildProperties != nil {
Expand Down Expand Up @@ -64,10 +68,16 @@ func NewBuilder(
sketch: sk,
buildProperties: buildProperties,
coreBuildCachePath: coreBuildCachePath,
jobs: jobs,
}
}

// GetBuildProperties returns the build properties for running this build
func (b *Builder) GetBuildProperties() *properties.Map {
return b.buildProperties
}

// Jobs number of parallel processes
func (b *Builder) Jobs() int {
return b.jobs
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package test
package detector_test

import (
"testing"
Expand Down
6 changes: 6 additions & 0 deletions arduino/builder/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"regexp"

"github.com/arduino/arduino-cli/arduino/builder/cpp"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/go-paths-helper"

Expand All @@ -32,6 +33,11 @@ var (
tr = i18n.Tr
)

// Sketch fixdoc
func (b *Builder) Sketch() *sketch.Sketch {
return b.sketch
}

// PrepareSketchBuildPath copies the sketch source files in the build path.
// The .ino files are merged together to create a .cpp file (by the way, the
// .cpp file still needs to be Arduino-preprocessed to compile).
Expand Down
6 changes: 3 additions & 3 deletions arduino/builder/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMergeSketchSources(t *testing.T) {
}
mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource)

b := NewBuilder(sk, nil, nil, false, nil)
b := NewBuilder(sk, nil, nil, false, nil, 0)
offset, source, err := b.sketchMergeSources(nil)
require.Nil(t, err)
require.Equal(t, 2, offset)
Expand All @@ -61,7 +61,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) {
require.NotNil(t, sk)

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

// copy the sketch over, create a fake main file we don't care about it
// but we need it for `SketchLoad` to succeed later
Expand Down
31 changes: 23 additions & 8 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
coreBuildCachePath = buildCachePath.Join("core")
}

sketchBuilder := bldr.NewBuilder(sk, boardBuildProperties, buildPath, req.GetOptimizeForDebug(), coreBuildCachePath)
sketchBuilder := bldr.NewBuilder(
sk,
boardBuildProperties,
buildPath,
req.GetOptimizeForDebug(),
coreBuildCachePath,
int(req.GetJobs()),
)

buildProperties := sketchBuilder.GetBuildProperties()

Expand Down Expand Up @@ -197,7 +204,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderCtx.BuildProperties = buildProperties
builderCtx.CustomBuildProperties = customBuildPropertiesArgs
builderCtx.FQBN = fqbn
builderCtx.Sketch = sk
builderCtx.BuildPath = buildPath
builderCtx.ProgressCB = progressCB

Expand All @@ -212,7 +218,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
)

builderCtx.Verbose = req.GetVerbose()
builderCtx.Jobs = int(req.GetJobs())

builderCtx.WarningsLevel = req.GetWarnings()
if builderCtx.WarningsLevel == "" {
Expand Down Expand Up @@ -243,7 +248,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderCtx.LibrariesBuildPath = librariesBuildPath
builderCtx.CoreBuildPath = coreBuildPath

if builderCtx.BuildPath.Canonical().EqualsTo(builderCtx.Sketch.FullPath.Canonical()) {
if builderCtx.BuildPath.Canonical().EqualsTo(sk.FullPath.Canonical()) {
return r, &arduino.CompileFailedError{
Message: tr("Sketch cannot be located in build path. Please specify a different build path"),
}
Expand Down Expand Up @@ -365,8 +370,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
exportBinaries = false
}
if exportBinaries {
presaveHex := builder.RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.savehex.presavehex", Suffix: ".pattern"}
if err := presaveHex.Run(builderCtx); err != nil {
err := builder.RecipeByPrefixSuffixRunner(
"recipe.hooks.savehex.presavehex", ".pattern", false,
builderCtx.OnlyUpdateCompilationDatabase, builderCtx.Verbose,
builderCtx.BuildProperties, builderCtx.Stdout, builderCtx.Stderr,
func(msg string) { builderCtx.Info(msg) },
)
if err != nil {
return r, err
}

Expand Down Expand Up @@ -404,8 +414,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
}
}

postsaveHex := builder.RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.savehex.postsavehex", Suffix: ".pattern"}
if err := postsaveHex.Run(builderCtx); err != nil {
err = builder.RecipeByPrefixSuffixRunner(
"recipe.hooks.savehex.postsavehex", ".pattern", false,
builderCtx.OnlyUpdateCompilationDatabase, builderCtx.Verbose,
builderCtx.BuildProperties, builderCtx.Stdout, builderCtx.Stderr,
func(msg string) { builderCtx.Info(msg) },
)
if err != nil {
return r, err
}
}
Expand Down
Loading