Skip to content

Commit

Permalink
remove BuildPath from context
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 12, 2023
1 parent c1be268 commit 9349db8
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 79 deletions.
8 changes: 8 additions & 0 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Builder struct {
sketch *sketch.Sketch
buildProperties *properties.Map

buildPath *paths.Path

// Parallel processes
jobs int

Expand Down Expand Up @@ -83,6 +85,7 @@ func NewBuilder(
coreBuildCachePath: coreBuildCachePath,
jobs: jobs,
customBuildProperties: append(requestBuildProperties, "build.warn_data_percentage=75"),
buildPath: buildPath,
}, nil
}

Expand All @@ -100,3 +103,8 @@ func (b *Builder) Jobs() int {
func (b *Builder) CustomBuildProperties() []string {
return b.customBuildProperties
}

// GetBuildPath returns the build path
func (b *Builder) GetBuildPath() *paths.Path {
return b.buildPath
}
9 changes: 4 additions & 5 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderCtx.ActualPlatform = buildPlatform
builderCtx.RequiredTools = requiredTools
builderCtx.FQBN = fqbn
builderCtx.BuildPath = buildPath
builderCtx.ProgressCB = progressCB

// FIXME: This will be redundant when arduino-builder will be part of the cli
Expand All @@ -207,7 +206,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))

builderCtx.CompilationDatabase = compilation.NewDatabase(
builderCtx.BuildPath.Join("compile_commands.json"),
sketchBuilder.GetBuildPath().Join("compile_commands.json"),
)

builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings)
Expand Down Expand Up @@ -235,7 +234,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderCtx.LibrariesBuildPath = librariesBuildPath
builderCtx.CoreBuildPath = coreBuildPath

if builderCtx.BuildPath.Canonical().EqualsTo(sk.FullPath.Canonical()) {
if sketchBuilder.GetBuildPath().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 @@ -268,7 +267,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
)

defer func() {
if p := builderCtx.BuildPath; p != nil {
if p := sketchBuilder.GetBuildPath(); p != nil {
r.BuildPath = p.String()
}
}()
Expand Down Expand Up @@ -381,7 +380,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
if !ok {
return r, &arduino.MissingPlatformPropertyError{Property: "build.project_name"}
}
buildFiles, err := builderCtx.BuildPath.ReadDir()
buildFiles, err := sketchBuilder.GetBuildPath().ReadDir()
if err != nil {
return r, &arduino.PermissionDeniedError{Message: tr("Error reading build directory"), Cause: err}
}
Expand Down
18 changes: 9 additions & 9 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const DEFAULT_DEBUG_LEVEL = 5
type Builder struct{}

func (s *Builder) Run(ctx *types.Context) error {
if err := ctx.BuildPath.MkdirAll(); err != nil {
if err := ctx.Builder.GetBuildPath().MkdirAll(); err != nil {
return err
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func (s *Builder) Run(ctx *types.Context) error {

types.BareCommand(func(ctx *types.Context) error {
objectFiles, archiveFile, err := builder.CoreBuilder(
ctx.BuildPath, ctx.CoreBuildPath, ctx.Builder.CoreBuildCachePath(),
ctx.Builder.GetBuildPath(), ctx.CoreBuildPath, ctx.Builder.CoreBuildCachePath(),
ctx.Builder.GetBuildProperties(),
ctx.ActualPlatform,
ctx.OnlyUpdateCompilationDatabase, ctx.Clean,
Expand Down Expand Up @@ -165,7 +165,7 @@ func (s *Builder) Run(ctx *types.Context) error {
ctx.LibrariesObjectFiles,
ctx.CoreObjectsFiles,
ctx.CoreArchiveFilePath,
ctx.BuildPath,
ctx.Builder.GetBuildPath(),
ctx.Builder.GetBuildProperties(),
ctx.BuilderLogger,
)
Expand All @@ -192,7 +192,7 @@ func (s *Builder) Run(ctx *types.Context) error {
types.BareCommand(func(ctx *types.Context) error {
return MergeSketchWithBootloader(
ctx.OnlyUpdateCompilationDatabase,
ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
ctx.BuilderLogger,
)
}),
Expand Down Expand Up @@ -236,7 +236,7 @@ func (s *Builder) Run(ctx *types.Context) error {
types.BareCommand(func(ctx *types.Context) error {
normalOutput, verboseOutput, err := ExportProjectCMake(
mainErr != nil,
ctx.BuildPath, ctx.SketchBuildPath,
ctx.Builder.GetBuildPath(), ctx.SketchBuildPath,
ctx.SketchLibrariesDetector.ImportedLibraries(),
ctx.Builder.GetBuildProperties(),
ctx.Builder.Sketch(),
Expand Down Expand Up @@ -283,7 +283,7 @@ func (s *Builder) Run(ctx *types.Context) error {
func preprocessSketchCommand(ctx *types.Context) types.BareCommand {
return func(ctx *types.Context) error {
normalOutput, verboseOutput, err := PreprocessSketch(
ctx.Builder.Sketch(), ctx.BuildPath, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset,
ctx.Builder.Sketch(), ctx.Builder.GetBuildPath(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset,
ctx.Builder.GetBuildProperties(), ctx.OnlyUpdateCompilationDatabase)
if ctx.BuilderLogger.Verbose() {
ctx.BuilderLogger.WriteStdout(verboseOutput)
Expand All @@ -306,7 +306,7 @@ func PreprocessSketch(
type Preprocess struct{}

func (s *Preprocess) Run(ctx *types.Context) error {
if err := ctx.BuildPath.MkdirAll(); err != nil {
if err := ctx.Builder.GetBuildPath().MkdirAll(); err != nil {
return err
}

Expand Down Expand Up @@ -375,7 +375,7 @@ func RunPreprocess(ctx *types.Context) error {
func findIncludes(ctx *types.Context) types.BareCommand {
return types.BareCommand(func(ctx *types.Context) error {
return ctx.SketchLibrariesDetector.FindIncludes(
ctx.BuildPath,
ctx.Builder.GetBuildPath(),
ctx.Builder.GetBuildProperties().GetPath("build.core.path"),
ctx.Builder.GetBuildProperties().GetPath("build.variant.path"),
ctx.SketchBuildPath,
Expand Down Expand Up @@ -416,7 +416,7 @@ func containerBuildOptions(ctx *types.Context) types.BareCommand {
// ctx.Builder.GetBuildProperties()
buildOptionsJSON, buildOptionsJSONPrevious, infoMessage, err := ContainerBuildOptions(
ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs,
ctx.BuiltInLibrariesDirs, ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.CustomBuildProperties(),
ctx.BuiltInLibrariesDirs, ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.CustomBuildProperties(),
ctx.FQBN.String(), ctx.Clean, ctx.Builder.GetBuildProperties(),
)
if infoMessage != "" {
Expand Down
17 changes: 6 additions & 11 deletions legacy/builder/test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ import (
)

func cleanUpBuilderTestContext(t *testing.T, ctx *types.Context) {
if ctx.BuildPath != nil {
err := ctx.BuildPath.RemoveAll()
require.NoError(t, err)
if ctx.Builder.GetBuildPath() != nil {
require.NoError(t, ctx.Builder.GetBuildPath().RemoveAll())
}
}

Expand All @@ -59,13 +58,9 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat
ctx.BuiltInLibrariesDirs = paths.New("downloaded_libraries")
ctx.OtherLibrariesDirs = paths.NewPathList("libraries")
}
if ctx.BuildPath == nil {
buildPath, err := paths.MkTempDir("", "test_build_path")
require.NoError(t, err)
ctx.BuildPath = buildPath
}

buildPath := ctx.BuildPath
buildPath, err := paths.MkTempDir("", "test_build_path")
require.NoError(t, err)
sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs()
require.NoError(t, err)
librariesBuildPath, err := buildPath.Join(constants.FOLDER_LIBRARIES).Abs()
Expand Down Expand Up @@ -110,7 +105,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat
requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform)
require.NoError(t, err)

ctx.Builder, err = bldr.NewBuilder(sk, boardBuildProperties, ctx.BuildPath, false /*OptimizeForDebug*/, nil, 0, nil)
ctx.Builder, err = bldr.NewBuilder(sk, boardBuildProperties, buildPath, false /*OptimizeForDebug*/, nil, 0, nil)
require.NoError(t, err)

ctx.PackageManager = pme
Expand All @@ -122,7 +117,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat
}

if sk != nil {
require.False(t, ctx.BuildPath.Canonical().EqualsTo(sk.FullPath.Canonical()))
require.False(t, ctx.Builder.GetBuildPath().Canonical().EqualsTo(sk.FullPath.Canonical()))
}

if !stepToSkip[skipLibraries] {
Expand Down
1 change: 0 additions & 1 deletion legacy/builder/test/create_build_options_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestCreateBuildOptionsMap(t *testing.T) {
BuiltInToolsDirs: paths.NewPathList("tools"),
OtherLibrariesDirs: paths.NewPathList("libraries"),
FQBN: parseFQBN(t, "my:nice:fqbn"),
BuildPath: paths.New("buildPath"),
}

buildProperties := properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"})
Expand Down
4 changes: 1 addition & 3 deletions legacy/builder/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/legacy/builder/types"
paths "github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)
Expand All @@ -32,10 +31,9 @@ func Abs(t *testing.T, rel *paths.Path) *paths.Path {
return absPath
}

func SetupBuildPath(t *testing.T, ctx *types.Context) *paths.Path {
func SetupBuildPath(t *testing.T) *paths.Path {
buildPath, err := paths.MkTempDir("", "test_build_path")
require.NoError(t, err)
ctx.BuildPath = buildPath
return buildPath
}

Expand Down
13 changes: 4 additions & 9 deletions legacy/builder/test/load_previous_build_options_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,27 @@ import (

"github.com/arduino/arduino-cli/legacy/builder"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/stretchr/testify/require"
)

func TestLoadPreviousBuildOptionsMap(t *testing.T) {
ctx := &types.Context{}

buildPath := SetupBuildPath(t, ctx)
buildPath := SetupBuildPath(t)
defer buildPath.RemoveAll()

err := buildPath.Join(constants.BUILD_OPTIONS_FILE).WriteFile([]byte("test"))
require.NoError(t, err)

buildOptionsJsonPrevious, err := builder.LoadPreviousBuildOptionsMap(ctx.BuildPath)
buildOptionsJsonPrevious, err := builder.LoadPreviousBuildOptionsMap(buildPath)
require.NoError(t, err)

require.Equal(t, "test", buildOptionsJsonPrevious)
}

func TestLoadPreviousBuildOptionsMapMissingFile(t *testing.T) {
ctx := &types.Context{}

buildPath := SetupBuildPath(t, ctx)
buildPath := SetupBuildPath(t)
defer buildPath.RemoveAll()

buildOptionsJsonPrevious, err := builder.LoadPreviousBuildOptionsMap(ctx.BuildPath)
buildOptionsJsonPrevious, err := builder.LoadPreviousBuildOptionsMap(buildPath)
require.NoError(t, err)
require.Empty(t, buildOptionsJsonPrevious)
}
16 changes: 8 additions & 8 deletions legacy/builder/test/merge_sketch_with_bootloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestMergeSketchWithBootloader(t *testing.T) {
ctx := prepareBuilderTestContext(t, nil, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno")
defer cleanUpBuilderTestContext(t, ctx)

buildPath := ctx.BuildPath
buildPath := ctx.Builder.GetBuildPath()
err := buildPath.Join("sketch").MkdirAll()
require.NoError(t, err)

Expand Down Expand Up @@ -73,7 +73,7 @@ func TestMergeSketchWithBootloader(t *testing.T) {
builderLogger := logger.New(nil, nil, false, "")
err = builder.MergeSketchWithBootloader(
ctx.OnlyUpdateCompilationDatabase,
ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
builderLogger,
)
require.NoError(t, err)
Expand All @@ -90,7 +90,7 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) {
ctx := prepareBuilderTestContext(t, nil, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno")
defer cleanUpBuilderTestContext(t, ctx)

buildPath := ctx.BuildPath
buildPath := ctx.Builder.GetBuildPath()
err := buildPath.Join("sketch").MkdirAll()
require.NoError(t, err)

Expand Down Expand Up @@ -130,7 +130,7 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) {
builderLogger := logger.New(nil, nil, false, "")
err = builder.MergeSketchWithBootloader(
ctx.OnlyUpdateCompilationDatabase,
ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
builderLogger,
)
require.NoError(t, err)
Expand All @@ -148,15 +148,15 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) {
ctx := prepareBuilderTestContext(t, nil, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno")
defer cleanUpBuilderTestContext(t, ctx)

buildPath := ctx.BuildPath
buildPath := ctx.Builder.GetBuildPath()
buildProperties := ctx.Builder.GetBuildProperties()
buildProperties.Remove(constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK)
buildProperties.Remove(constants.BUILD_PROPERTIES_BOOTLOADER_FILE)

builderLogger := logger.New(nil, nil, false, "")
err := builder.MergeSketchWithBootloader(
ctx.OnlyUpdateCompilationDatabase,
ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
builderLogger,
)
require.NoError(t, err)
Expand All @@ -176,7 +176,7 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) {
ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch1", "sketch1.ino"), "my_avr_platform:avr:mymega:cpu=atmega2560")
defer cleanUpBuilderTestContext(t, ctx)

buildPath := ctx.BuildPath
buildPath := ctx.Builder.GetBuildPath()
err := buildPath.Join("sketch").MkdirAll()
require.NoError(t, err)

Expand Down Expand Up @@ -216,7 +216,7 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) {
builderLogger := logger.New(nil, nil, false, "")
err = builder.MergeSketchWithBootloader(
ctx.OnlyUpdateCompilationDatabase,
ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(),
builderLogger,
)
require.NoError(t, err)
Expand Down
25 changes: 11 additions & 14 deletions legacy/builder/test/store_build_options_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,31 @@ import (
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/legacy/builder"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
paths "github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/stretchr/testify/require"
)

func TestStoreBuildOptionsMap(t *testing.T) {
ctx := &types.Context{
HardwareDirs: paths.NewPathList("hardware"),
BuiltInToolsDirs: paths.NewPathList("tools"),
BuiltInLibrariesDirs: paths.New("built-in libraries"),
OtherLibrariesDirs: paths.NewPathList("libraries"),
FQBN: parseFQBN(t, "my:nice:fqbn"),
}
hardwareDirs := paths.NewPathList("hardware")
builtInToolsDirs := paths.NewPathList("tools")
builtInLibrariesDirs := paths.New("built-in libraries")
otherLibrariesDirs := paths.NewPathList("libraries")
fqbn := parseFQBN(t, "my:nice:fqbn")

buildPath := SetupBuildPath(t, ctx)
buildPath := SetupBuildPath(t)
defer buildPath.RemoveAll()

buildProperties := properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"})
buildPropertiesJSON, err := builder.CreateBuildOptionsMap(
ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs,
ctx.BuiltInLibrariesDirs, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, []string{"custom=prop"},
ctx.FQBN.String(), buildProperties.Get("compiler.optimization_flags"),
hardwareDirs, builtInToolsDirs, otherLibrariesDirs,
builtInLibrariesDirs, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, []string{"custom=prop"},
fqbn.String(), buildProperties.Get("compiler.optimization_flags"),
)
require.NoError(t, err)
ctx.BuildOptionsJson = buildPropertiesJSON
buildOptionsJson := buildPropertiesJSON

err = builder.StoreBuildOptionsMap(ctx.BuildPath, ctx.BuildOptionsJson)
err = builder.StoreBuildOptionsMap(buildPath, buildOptionsJson)
require.NoError(t, err)

exist, err := buildPath.Join(constants.BUILD_OPTIONS_FILE).ExistCheck()
Expand Down
Loading

0 comments on commit 9349db8

Please sign in to comment.