Skip to content

Commit

Permalink
remove sketch,libraries,core build path from Context
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 12, 2023
1 parent 9349db8 commit b2d55da
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 86 deletions.
48 changes: 45 additions & 3 deletions arduino/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@
package builder

import (
"errors"
"fmt"

"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
)

// ErrSketchCannotBeLocatedInBuildPath fixdoc
var ErrSketchCannotBeLocatedInBuildPath = errors.New("sketch cannot be located in build path")

// Builder is a Sketch builder.
type Builder struct {
sketch *sketch.Sketch
buildProperties *properties.Map

buildPath *paths.Path
buildPath *paths.Path
sketchBuildPath *paths.Path
coreBuildPath *paths.Path
librariesBuildPath *paths.Path

// Parallel processes
jobs int
Expand Down Expand Up @@ -79,13 +86,33 @@ func NewBuilder(
}
buildProperties.Merge(customBuildProperties)

sketchBuildPath, err := buildPath.Join("sketch").Abs()
if err != nil {
return nil, err
}
librariesBuildPath, err := buildPath.Join("libraries").Abs()
if err != nil {
return nil, err
}
coreBuildPath, err := buildPath.Join("core").Abs()
if err != nil {
return nil, err
}

if buildPath.Canonical().EqualsTo(sk.FullPath.Canonical()) {
return nil, ErrSketchCannotBeLocatedInBuildPath
}

return &Builder{
sketch: sk,
buildProperties: buildProperties,
coreBuildCachePath: coreBuildCachePath,
buildPath: buildPath,
sketchBuildPath: sketchBuildPath,
coreBuildPath: coreBuildPath,
librariesBuildPath: librariesBuildPath,
jobs: jobs,
customBuildProperties: append(requestBuildProperties, "build.warn_data_percentage=75"),
buildPath: buildPath,
coreBuildCachePath: coreBuildCachePath,
}, nil
}

Expand All @@ -108,3 +135,18 @@ func (b *Builder) CustomBuildProperties() []string {
func (b *Builder) GetBuildPath() *paths.Path {
return b.buildPath
}

// GetSketchBuildPath returns the sketch build path
func (b *Builder) GetSketchBuildPath() *paths.Path {
return b.sketchBuildPath
}

// GetCoreBuildPath returns the core build path
func (b *Builder) GetCoreBuildPath() *paths.Path {
return b.coreBuildPath
}

// GetLibrariesBuildPath returns the libraries build path
func (b *Builder) GetLibrariesBuildPath() *paths.Path {
return b.librariesBuildPath
}
58 changes: 22 additions & 36 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package compile

import (
"context"
"errors"
"fmt"
"io"
"sort"
Expand All @@ -37,7 +38,6 @@ import (
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/internal/inventory"
"github.com/arduino/arduino-cli/legacy/builder"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
Expand Down Expand Up @@ -170,26 +170,12 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
coreBuildCachePath = buildCachePath.Join("core")
}

sketchBuilder, err := bldr.NewBuilder(
sk,
boardBuildProperties,
buildPath,
req.GetOptimizeForDebug(),
coreBuildCachePath,
int(req.GetJobs()),
req.GetBuildProperties(),
)
if err != nil {
return nil, &arduino.InvalidArgumentError{Message: tr("Invalid build properties"), Cause: err}
}

requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform)
if err != nil {
return nil, err
}

builderCtx := &types.Context{}
builderCtx.Builder = sketchBuilder
builderCtx.PackageManager = pme
builderCtx.TargetBoard = targetBoard
builderCtx.TargetPlatform = targetPlatform
Expand All @@ -205,10 +191,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderCtx.OtherLibrariesDirs = paths.NewPathList(req.GetLibraries()...)
builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))

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

builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings)

builderCtx.Clean = req.GetClean()
Expand All @@ -218,27 +200,31 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderLogger := logger.New(outStream, errStream, req.GetVerbose(), req.GetWarnings())
builderCtx.BuilderLogger = builderLogger

sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs()
if err != nil {
return r, &arduino.CompileFailedError{Message: err.Error()}
}
librariesBuildPath, err := buildPath.Join(constants.FOLDER_LIBRARIES).Abs()
if err != nil {
return r, &arduino.CompileFailedError{Message: err.Error()}
}
coreBuildPath, err := buildPath.Join(constants.FOLDER_CORE).Abs()
sketchBuilder, err := bldr.NewBuilder(
sk,
boardBuildProperties,
buildPath,
req.GetOptimizeForDebug(),
coreBuildCachePath,
int(req.GetJobs()),
req.GetBuildProperties(),
)
if err != nil {
if strings.Contains(err.Error(), "invalid build properties") {
return nil, &arduino.InvalidArgumentError{Message: tr("Invalid build properties"), Cause: err}
}
if errors.Is(err, bldr.ErrSketchCannotBeLocatedInBuildPath) {
return r, &arduino.CompileFailedError{
Message: tr("Sketch cannot be located in build path. Please specify a different build path"),
}
}
return r, &arduino.CompileFailedError{Message: err.Error()}
}
builderCtx.SketchBuildPath = sketchBuildPath
builderCtx.LibrariesBuildPath = librariesBuildPath
builderCtx.CoreBuildPath = coreBuildPath
builderCtx.Builder = sketchBuilder

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"),
}
}
builderCtx.CompilationDatabase = compilation.NewDatabase(
sketchBuilder.GetBuildPath().Join("compile_commands.json"),
)

var libsManager *librariesmanager.LibrariesManager
if pme.GetProfile() != nil {
Expand Down
20 changes: 10 additions & 10 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *Builder) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath(ctx.SourceOverride, ctx.SketchBuildPath)
ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath(ctx.SourceOverride, ctx.Builder.GetSketchBuildPath())
return _err
}),

Expand All @@ -71,7 +71,7 @@ func (s *Builder) Run(ctx *types.Context) error {

types.BareCommand(func(ctx *types.Context) error {
sketchObjectFiles, err := builder.SketchBuilder(
ctx.SketchBuildPath,
ctx.Builder.GetSketchBuildPath(),
ctx.Builder.GetBuildProperties(),
ctx.SketchLibrariesDetector.IncludeFolders(),
ctx.OnlyUpdateCompilationDatabase,
Expand All @@ -98,14 +98,14 @@ func (s *Builder) Run(ctx *types.Context) error {

types.BareCommand(func(ctx *types.Context) error {
return UnusedCompiledLibrariesRemover(
ctx.LibrariesBuildPath,
ctx.Builder.GetLibrariesBuildPath(),
ctx.SketchLibrariesDetector.ImportedLibraries(),
)
}),

types.BareCommand(func(ctx *types.Context) error {
librariesObjectFiles, err := builder.LibrariesBuilder(
ctx.LibrariesBuildPath,
ctx.Builder.GetLibrariesBuildPath(),
ctx.Builder.GetBuildProperties(),
ctx.SketchLibrariesDetector.IncludeFolders(),
ctx.SketchLibrariesDetector.ImportedLibraries(),
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.Builder.GetBuildPath(), ctx.CoreBuildPath, ctx.Builder.CoreBuildCachePath(),
ctx.Builder.GetBuildPath(), ctx.Builder.GetCoreBuildPath(), ctx.Builder.CoreBuildCachePath(),
ctx.Builder.GetBuildProperties(),
ctx.ActualPlatform,
ctx.OnlyUpdateCompilationDatabase, ctx.Clean,
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.Builder.GetBuildPath(), ctx.SketchBuildPath,
ctx.Builder.GetBuildPath(), ctx.Builder.GetSketchBuildPath(),
ctx.SketchLibrariesDetector.ImportedLibraries(),
ctx.Builder.GetBuildProperties(),
ctx.Builder.Sketch(),
Expand Down Expand Up @@ -319,7 +319,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
}),

types.BareCommand(func(ctx *types.Context) error {
ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath(ctx.SourceOverride, ctx.SketchBuildPath)
ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath(ctx.SourceOverride, ctx.Builder.GetSketchBuildPath())
return _err
}),

Expand All @@ -335,7 +335,7 @@ func (s *Preprocess) Run(ctx *types.Context) error {
}

// Output arduino-preprocessed source
preprocessedSketch, err := ctx.SketchBuildPath.Join(ctx.Builder.Sketch().MainFile.Base() + ".cpp").ReadFile()
preprocessedSketch, err := ctx.Builder.GetSketchBuildPath().Join(ctx.Builder.Sketch().MainFile.Base() + ".cpp").ReadFile()
if err != nil {
return err
}
Expand Down Expand Up @@ -378,9 +378,9 @@ func findIncludes(ctx *types.Context) types.BareCommand {
ctx.Builder.GetBuildPath(),
ctx.Builder.GetBuildProperties().GetPath("build.core.path"),
ctx.Builder.GetBuildProperties().GetPath("build.variant.path"),
ctx.SketchBuildPath,
ctx.Builder.GetSketchBuildPath(),
ctx.Builder.Sketch(),
ctx.LibrariesBuildPath,
ctx.Builder.GetLibrariesBuildPath(),
ctx.Builder.GetBuildProperties(),
ctx.TargetPlatform.Platform.Architecture,
)
Expand Down
35 changes: 19 additions & 16 deletions legacy/builder/test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package test

import (
"fmt"
"os"
"path/filepath"
"testing"

Expand All @@ -25,7 +26,6 @@ import (
"github.com/arduino/arduino-cli/arduino/builder/logger"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -61,16 +61,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat

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()
require.NoError(t, err)
coreBuildPath, err := buildPath.Join(constants.FOLDER_CORE).Abs()
require.NoError(t, err)

ctx.SketchBuildPath = sketchBuildPath
ctx.LibrariesBuildPath = librariesBuildPath
ctx.CoreBuildPath = coreBuildPath
require.NotNil(t, buildPath)

// Create a Package Manager from the given context
// This should happen only on legacy arduino-builder.
Expand All @@ -94,9 +85,25 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat
sk = s
}

// This is an hack to avoid panic when the `NewBuilder` assert a condition on sketch path.
// Since the test will be migrated soon in an E2E manner we temporary set the sketch to be empty.
// so this assertion inside the Builder:
// buildPath().Canonical().EqualsTo(sk.FullPath.Canonical())`
// Doesn't fail
if sk == nil {
sk = &sketch.Sketch{
MainFile: &paths.Path{},
FullPath: paths.New(os.TempDir()),
OtherSketchFiles: []*paths.Path{},
AdditionalFiles: []*paths.Path{},
RootFolderFiles: []*paths.Path{},
Project: &sketch.Project{},
}
}

builderLogger := logger.New(nil, nil, false, "")
ctx.BuilderLogger = builderLogger
ctx.Builder, err = bldr.NewBuilder(sk, nil, nil, false, nil, 0, nil)
ctx.Builder, err = bldr.NewBuilder(sk, nil, buildPath, false, nil, 0, nil)
require.NoError(t, err)
if fqbn != "" {
ctx.FQBN = parseFQBN(t, fqbn)
Expand All @@ -116,10 +123,6 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat
ctx.RequiredTools = requiredTools
}

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

if !stepToSkip[skipLibraries] {
lm, libsResolver, _, err := detector.LibrariesLoader(
false, nil,
Expand Down
Loading

0 comments on commit b2d55da

Please sign in to comment.