From 1709a8f2b5b0619b3ec382411885e773d6c65795 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 11:07:10 +0200 Subject: [PATCH 01/34] move customBuildProperties to arduino/builder --- arduino/builder/builder.go | 31 +++++++++++++++---- arduino/builder/sketch_test.go | 11 +++++-- commands/compile/compile.go | 16 +++------- legacy/builder/builder.go | 2 +- legacy/builder/test/builder_test.go | 7 +++-- .../test/create_build_options_map_test.go | 2 +- .../test/setup_build_properties_test.go | 8 ++--- .../test/store_build_options_map_test.go | 3 +- legacy/builder/types/context.go | 3 -- 9 files changed, 50 insertions(+), 33 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 32bd35d7e68..26e40018a66 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -16,6 +16,8 @@ package builder import ( + "fmt" + "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" @@ -29,6 +31,9 @@ type Builder struct { // Parallel processes jobs int + // Custom build properties defined by user (line by line as "key=value" pairs) + customBuildProperties []string + // core related coreBuildCachePath *paths.Path } @@ -41,7 +46,8 @@ func NewBuilder( optimizeForDebug bool, coreBuildCachePath *paths.Path, jobs int, -) *Builder { + requestBuildProperties []string, +) (*Builder, error) { buildProperties := properties.NewMap() if boardBuildProperties != nil { buildProperties.Merge(boardBuildProperties) @@ -64,12 +70,20 @@ func NewBuilder( } } - return &Builder{ - sketch: sk, - buildProperties: buildProperties, - coreBuildCachePath: coreBuildCachePath, - jobs: jobs, + // Add user provided custom build properties + customBuildProperties, err := properties.LoadFromSlice(requestBuildProperties) + if err != nil { + return nil, fmt.Errorf("invalid build properties: %w", err) } + buildProperties.Merge(customBuildProperties) + + return &Builder{ + sketch: sk, + buildProperties: buildProperties, + coreBuildCachePath: coreBuildCachePath, + jobs: jobs, + customBuildProperties: append(requestBuildProperties, "build.warn_data_percentage=75"), + }, nil } // GetBuildProperties returns the build properties for running this build @@ -81,3 +95,8 @@ func (b *Builder) GetBuildProperties() *properties.Map { func (b *Builder) Jobs() int { return b.jobs } + +// CustomBuildProperties returns user provided custom build properties +func (b *Builder) CustomBuildProperties() []string { + return b.customBuildProperties +} diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index 6aaedaaa0fc..461cc5c8c2c 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -48,7 +48,9 @@ func TestMergeSketchSources(t *testing.T) { } mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource) - b := NewBuilder(sk, nil, nil, false, nil, 0) + b, err := NewBuilder(sk, nil, nil, false, nil, 0, nil) + require.NoError(t, err) + offset, source, err := b.sketchMergeSources(nil) require.Nil(t, err) require.Equal(t, 2, offset) @@ -61,7 +63,9 @@ 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, 0) + b, err := NewBuilder(sk, nil, nil, false, nil, 0, nil) + require.NoError(t, err) + _, source, err := b.sketchMergeSources(nil) require.Nil(t, err) require.Equal(t, 1, strings.Count(source, "")) @@ -76,7 +80,8 @@ 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, 0) + b1, err := NewBuilder(sk1, nil, nil, false, nil, 0, nil) + require.NoError(t, err) // copy the sketch over, create a fake main file we don't care about it // but we need it for `SketchLoad` to succeed later diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 36bba118f92..248a273f3b5 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -41,7 +41,6 @@ import ( "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" - "github.com/arduino/go-properties-orderedmap" "github.com/sirupsen/logrus" ) @@ -171,25 +170,21 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream coreBuildCachePath = buildCachePath.Join("core") } - sketchBuilder := bldr.NewBuilder( + sketchBuilder, err := bldr.NewBuilder( sk, boardBuildProperties, buildPath, req.GetOptimizeForDebug(), coreBuildCachePath, int(req.GetJobs()), + req.GetBuildProperties(), ) - - buildProperties := sketchBuilder.GetBuildProperties() - - // Add user provided custom build properties - customBuildPropertiesArgs := append(req.GetBuildProperties(), "build.warn_data_percentage=75") - if customBuildProperties, err := properties.LoadFromSlice(req.GetBuildProperties()); err == nil { - buildProperties.Merge(customBuildProperties) - } else { + if err != nil { return nil, &arduino.InvalidArgumentError{Message: tr("Invalid build properties"), Cause: err} } + buildProperties := sketchBuilder.GetBuildProperties() + requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) if err != nil { return nil, err @@ -204,7 +199,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.ActualPlatform = buildPlatform builderCtx.RequiredTools = requiredTools builderCtx.BuildProperties = buildProperties - builderCtx.CustomBuildProperties = customBuildPropertiesArgs builderCtx.FQBN = fqbn builderCtx.BuildPath = buildPath builderCtx.ProgressCB = progressCB diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index c36335aa799..f3e83f794af 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -416,7 +416,7 @@ func containerBuildOptions(ctx *types.Context) types.BareCommand { // ctx.BuildProperties buildOptionsJSON, buildOptionsJSONPrevious, infoMessage, err := ContainerBuildOptions( ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, ctx.BuildPath, ctx.Builder.Sketch(), ctx.CustomBuildProperties, + ctx.BuiltInLibrariesDirs, ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.CustomBuildProperties(), ctx.FQBN.String(), ctx.Clean, ctx.BuildProperties, ) if infoMessage != "" { diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index e6af9105ebc..7de7cf6dd32 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -101,7 +101,8 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat builderLogger := logger.New(nil, nil, false, "") ctx.BuilderLogger = builderLogger - ctx.Builder = bldr.NewBuilder(sk, nil, nil, false, nil, 0) + ctx.Builder, err = bldr.NewBuilder(sk, nil, nil, false, nil, 0, nil) + require.NoError(t, err) if fqbn != "" { ctx.FQBN = parseFQBN(t, fqbn) targetPackage, targetPlatform, targetBoard, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(ctx.FQBN) @@ -109,7 +110,9 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) require.NoError(t, err) - ctx.Builder = bldr.NewBuilder(sk, boardBuildProperties, ctx.BuildPath, false /*OptimizeForDebug*/, nil, 0) + ctx.Builder, err = bldr.NewBuilder(sk, boardBuildProperties, ctx.BuildPath, false /*OptimizeForDebug*/, nil, 0, nil) + require.NoError(t, err) + ctx.PackageManager = pme ctx.TargetBoard = targetBoard ctx.BuildProperties = ctx.Builder.GetBuildProperties() diff --git a/legacy/builder/test/create_build_options_map_test.go b/legacy/builder/test/create_build_options_map_test.go index d3c5ce42580..ac9dada0038 100644 --- a/legacy/builder/test/create_build_options_map_test.go +++ b/legacy/builder/test/create_build_options_map_test.go @@ -38,7 +38,7 @@ func TestCreateBuildOptionsMap(t *testing.T) { buildPropertiesJSON, err := builder.CreateBuildOptionsMap( ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, ctx.CustomBuildProperties, + ctx.BuiltInLibrariesDirs, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, nil, ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"), ) require.NoError(t, err) diff --git a/legacy/builder/test/setup_build_properties_test.go b/legacy/builder/test/setup_build_properties_test.go index 9b8d7236a31..62d28e7f157 100644 --- a/legacy/builder/test/setup_build_properties_test.go +++ b/legacy/builder/test/setup_build_properties_test.go @@ -73,13 +73,13 @@ func TestSetupBuildProperties(t *testing.T) { func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - CustomBuildProperties: []string{"name=fake name", "tools.avrdude.config.path=non existent path with space and a ="}, + HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), + BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), } ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno") defer cleanUpBuilderTestContext(t, ctx) - customProps, err := properties.LoadFromSlice(ctx.CustomBuildProperties) + customBuildProp := []string{"name=fake name", "tools.avrdude.config.path=non existent path with space and a ="} + customProps, err := properties.LoadFromSlice(customBuildProp) require.NoError(t, err) ctx.BuildProperties.Merge(customProps) diff --git a/legacy/builder/test/store_build_options_map_test.go b/legacy/builder/test/store_build_options_map_test.go index c9782e4dc40..55831b8e578 100644 --- a/legacy/builder/test/store_build_options_map_test.go +++ b/legacy/builder/test/store_build_options_map_test.go @@ -34,7 +34,6 @@ func TestStoreBuildOptionsMap(t *testing.T) { BuiltInLibrariesDirs: paths.New("built-in libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), FQBN: parseFQBN(t, "my:nice:fqbn"), - CustomBuildProperties: []string{"custom=prop"}, BuildProperties: properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"}), } @@ -43,7 +42,7 @@ func TestStoreBuildOptionsMap(t *testing.T) { buildPropertiesJSON, err := builder.CreateBuildOptionsMap( ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, ctx.CustomBuildProperties, + ctx.BuiltInLibrariesDirs, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, []string{"custom=prop"}, ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"), ) require.NoError(t, err) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 61eff082d7a..685d4a2057b 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -72,9 +72,6 @@ type Context struct { // Send progress events to this callback ProgressCB rpc.TaskProgressCB - // Custom build properties defined by user (line by line as "key=value" pairs) - CustomBuildProperties []string - // Sizer results ExecutableSectionsSize sizer.ExecutablesFileSections From c19a712f074c6fc09828889b47dcba0c659c8a7e Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 11:21:30 +0200 Subject: [PATCH 02/34] remove BuildProperties from context --- commands/compile/compile.go | 15 ++++------ legacy/builder/builder.go | 30 +++++++++---------- legacy/builder/test/builder_test.go | 1 - .../test/create_build_options_map_test.go | 4 +-- .../test/merge_sketch_with_bootloader_test.go | 14 ++++----- .../test/setup_build_properties_test.go | 9 +++--- .../test/store_build_options_map_test.go | 14 ++++----- ...uild_path_if_build_options_changed_test.go | 4 +-- legacy/builder/types/context.go | 2 -- 9 files changed, 43 insertions(+), 50 deletions(-) diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 248a273f3b5..106dc98f92e 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -183,8 +183,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream return nil, &arduino.InvalidArgumentError{Message: tr("Invalid build properties"), Cause: err} } - buildProperties := sketchBuilder.GetBuildProperties() - requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) if err != nil { return nil, err @@ -198,7 +196,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.TargetPackage = targetPackage builderCtx.ActualPlatform = buildPlatform builderCtx.RequiredTools = requiredTools - builderCtx.BuildProperties = buildProperties builderCtx.FQBN = fqbn builderCtx.BuildPath = buildPath builderCtx.ProgressCB = progressCB @@ -277,7 +274,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream }() defer func() { - buildProperties := builderCtx.BuildProperties + buildProperties := sketchBuilder.GetBuildProperties() if buildProperties == nil { return } @@ -321,7 +318,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream // if it's a regular build, go on... if req.GetVerbose() { - core := buildProperties.Get("build.core") + core := sketchBuilder.GetBuildProperties().Get("build.core") if core == "" { core = "arduino" } @@ -340,7 +337,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream if !targetBoard.Properties.ContainsKey("build.board") { outStream.Write([]byte( tr("Warning: Board %[1]s doesn't define a %[2]s preference. Auto-set to: %[3]s", - targetBoard.String(), "'build.board'", buildProperties.Get("build.board")) + "\n")) + targetBoard.String(), "'build.board'", sketchBuilder.GetBuildProperties().Get("build.board")) + "\n")) } if err := builder.RunBuilder(builderCtx); err != nil { @@ -359,7 +356,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream err := builder.RecipeByPrefixSuffixRunner( "recipe.hooks.savehex.presavehex", ".pattern", false, builderCtx.OnlyUpdateCompilationDatabase, - builderCtx.BuildProperties, + sketchBuilder.GetBuildProperties(), builderLogger, ) if err != nil { @@ -380,7 +377,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream } // Copy all "sketch.ino.*" artifacts to the export directory - baseName, ok := builderCtx.BuildProperties.GetOk("build.project_name") // == "sketch.ino" + baseName, ok := sketchBuilder.GetBuildProperties().GetOk("build.project_name") // == "sketch.ino" if !ok { return r, &arduino.MissingPlatformPropertyError{Property: "build.project_name"} } @@ -403,7 +400,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream err = builder.RecipeByPrefixSuffixRunner( "recipe.hooks.savehex.postsavehex", ".pattern", false, builderCtx.OnlyUpdateCompilationDatabase, - builderCtx.BuildProperties, builderLogger, + sketchBuilder.GetBuildProperties(), builderLogger, ) if err != nil { return r, err diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index f3e83f794af..5595e2ca5d1 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -72,7 +72,7 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { sketchObjectFiles, err := builder.SketchBuilder( ctx.SketchBuildPath, - ctx.BuildProperties, + ctx.Builder.GetBuildProperties(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.OnlyUpdateCompilationDatabase, ctx.CompilationDatabase, @@ -106,7 +106,7 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { librariesObjectFiles, err := builder.LibrariesBuilder( ctx.LibrariesBuildPath, - ctx.BuildProperties, + ctx.Builder.GetBuildProperties(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries(), ctx.OnlyUpdateCompilationDatabase, @@ -134,7 +134,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.BuildProperties, + ctx.Builder.GetBuildProperties(), ctx.ActualPlatform, ctx.OnlyUpdateCompilationDatabase, ctx.Clean, ctx.CompilationDatabase, @@ -166,7 +166,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.CoreObjectsFiles, ctx.CoreArchiveFilePath, ctx.BuildPath, - ctx.BuildProperties, + ctx.Builder.GetBuildProperties(), ctx.BuilderLogger, ) if ctx.BuilderLogger.Verbose() { @@ -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.BuildProperties, + ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), ctx.BuilderLogger, ) }), @@ -238,7 +238,7 @@ func (s *Builder) Run(ctx *types.Context) error { mainErr != nil, ctx.BuildPath, ctx.SketchBuildPath, ctx.SketchLibrariesDetector.ImportedLibraries(), - ctx.BuildProperties, + ctx.Builder.GetBuildProperties(), ctx.Builder.Sketch(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, @@ -255,7 +255,7 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { executableSectionsSize, err := sizer.Size( ctx.OnlyUpdateCompilationDatabase, mainErr != nil, - ctx.BuildProperties, + ctx.Builder.GetBuildProperties(), ctx.BuilderLogger, ) ctx.ExecutableSectionsSize = executableSectionsSize @@ -284,7 +284,7 @@ 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.BuildProperties, ctx.OnlyUpdateCompilationDatabase) + ctx.Builder.GetBuildProperties(), ctx.OnlyUpdateCompilationDatabase) if ctx.BuilderLogger.Verbose() { ctx.BuilderLogger.WriteStdout(verboseOutput) } else { @@ -376,12 +376,12 @@ func findIncludes(ctx *types.Context) types.BareCommand { return types.BareCommand(func(ctx *types.Context) error { return ctx.SketchLibrariesDetector.FindIncludes( ctx.BuildPath, - ctx.BuildProperties.GetPath("build.core.path"), - ctx.BuildProperties.GetPath("build.variant.path"), + ctx.Builder.GetBuildProperties().GetPath("build.core.path"), + ctx.Builder.GetBuildProperties().GetPath("build.variant.path"), ctx.SketchBuildPath, ctx.Builder.Sketch(), ctx.LibrariesBuildPath, - ctx.BuildProperties, + ctx.Builder.GetBuildProperties(), ctx.TargetPlatform.Platform.Architecture, ) }) @@ -405,7 +405,7 @@ func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipI return RecipeByPrefixSuffixRunner( prefix, suffix, skipIfOnlyUpdatingCompilationDatabase, ctx.OnlyUpdateCompilationDatabase, - ctx.BuildProperties, + ctx.Builder.GetBuildProperties(), ctx.BuilderLogger, ) } @@ -413,11 +413,11 @@ func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipI func containerBuildOptions(ctx *types.Context) types.BareCommand { return types.BareCommand(func(ctx *types.Context) error { // TODO here we can pass only the properties we're reading from the - // ctx.BuildProperties + // 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.FQBN.String(), ctx.Clean, ctx.BuildProperties, + ctx.FQBN.String(), ctx.Clean, ctx.Builder.GetBuildProperties(), ) if infoMessage != "" { ctx.BuilderLogger.Info(infoMessage) @@ -435,7 +435,7 @@ func containerBuildOptions(ctx *types.Context) types.BareCommand { func warnAboutArchIncompatibleLibraries(ctx *types.Context) types.BareCommand { return types.BareCommand(func(ctx *types.Context) error { - overrides, _ := ctx.BuildProperties.GetOk("architecture.override_check") + overrides, _ := ctx.Builder.GetBuildProperties().GetOk("architecture.override_check") WarnAboutArchIncompatibleLibraries( ctx.TargetPlatform, overrides, diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 7de7cf6dd32..6d41313e390 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -115,7 +115,6 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.PackageManager = pme ctx.TargetBoard = targetBoard - ctx.BuildProperties = ctx.Builder.GetBuildProperties() ctx.TargetPlatform = targetPlatform ctx.TargetPackage = targetPackage ctx.ActualPlatform = buildPlatform diff --git a/legacy/builder/test/create_build_options_map_test.go b/legacy/builder/test/create_build_options_map_test.go index ac9dada0038..5370736fba5 100644 --- a/legacy/builder/test/create_build_options_map_test.go +++ b/legacy/builder/test/create_build_options_map_test.go @@ -33,13 +33,13 @@ func TestCreateBuildOptionsMap(t *testing.T) { OtherLibrariesDirs: paths.NewPathList("libraries"), FQBN: parseFQBN(t, "my:nice:fqbn"), BuildPath: paths.New("buildPath"), - BuildProperties: properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"}), } + 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")}, nil, - ctx.FQBN.String(), ctx.BuildProperties.Get("compiler.optimization_flags"), + ctx.FQBN.String(), buildProperties.Get("compiler.optimization_flags"), ) require.NoError(t, err) diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index f1d9224b840..6b96e924313 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -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.BuildProperties, + ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), builderLogger, ) require.NoError(t, err) @@ -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.BuildProperties, + ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), builderLogger, ) require.NoError(t, err) @@ -149,14 +149,14 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) buildPath := ctx.BuildPath - buildProperties := ctx.BuildProperties + 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.BuildProperties, + ctx.OnlyUpdateCompilationDatabase, + ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), builderLogger, ) require.NoError(t, err) @@ -215,8 +215,8 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { builderLogger := logger.New(nil, nil, false, "") err = builder.MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, - ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, + ctx.OnlyUpdateCompilationDatabase, + ctx.BuildPath, ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), builderLogger, ) require.NoError(t, err) diff --git a/legacy/builder/test/setup_build_properties_test.go b/legacy/builder/test/setup_build_properties_test.go index 62d28e7f157..b4acb7d2ecc 100644 --- a/legacy/builder/test/setup_build_properties_test.go +++ b/legacy/builder/test/setup_build_properties_test.go @@ -33,7 +33,7 @@ func TestSetupBuildProperties(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch1", "sketch1.ino"), "arduino:avr:uno") defer cleanUpBuilderTestContext(t, ctx) - buildProperties := ctx.BuildProperties + buildProperties := ctx.Builder.GetBuildProperties() require.Equal(t, "ARDUINO", buildProperties.Get("software")) @@ -81,9 +81,8 @@ func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { customBuildProp := []string{"name=fake name", "tools.avrdude.config.path=non existent path with space and a ="} customProps, err := properties.LoadFromSlice(customBuildProp) require.NoError(t, err) - ctx.BuildProperties.Merge(customProps) - buildProperties := ctx.BuildProperties + buildProperties := ctx.Builder.GetBuildProperties().Merge(customProps) require.Equal(t, "ARDUINO", buildProperties.Get("software")) require.Equal(t, "uno", buildProperties.Get("_id")) require.Equal(t, "fake name", buildProperties.Get("name")) @@ -99,7 +98,7 @@ func TestSetupBuildPropertiesUserHardware(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch1", "sketch1.ino"), "my_avr_platform:avr:custom_yun") defer cleanUpBuilderTestContext(t, ctx) - buildProperties := ctx.BuildProperties + buildProperties := ctx.Builder.GetBuildProperties() require.Equal(t, "ARDUINO", buildProperties.Get("software")) @@ -117,7 +116,7 @@ func TestSetupBuildPropertiesWithMissingPropsFromParentPlatformTxtFiles(t *testi ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch1", "sketch1.ino"), "my_avr_platform:avr:custom_yun") defer cleanUpBuilderTestContext(t, ctx) - buildProperties := ctx.BuildProperties + buildProperties := ctx.Builder.GetBuildProperties() require.Equal(t, "ARDUINO", buildProperties.Get("software")) diff --git a/legacy/builder/test/store_build_options_map_test.go b/legacy/builder/test/store_build_options_map_test.go index 55831b8e578..b35899d46d2 100644 --- a/legacy/builder/test/store_build_options_map_test.go +++ b/legacy/builder/test/store_build_options_map_test.go @@ -29,21 +29,21 @@ import ( 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"), - BuildProperties: properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"}), + 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) 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(), ctx.BuildProperties.Get("compiler.optimization_flags"), + ctx.FQBN.String(), buildProperties.Get("compiler.optimization_flags"), ) require.NoError(t, err) ctx.BuildOptionsJson = buildPropertiesJSON diff --git a/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go b/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go index 7df448e9b54..9083dcb8945 100644 --- a/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go +++ b/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go @@ -39,7 +39,7 @@ func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { ctx.BuildPath, ctx.BuildOptionsJson, ctx.BuildOptionsJsonPrevious, - ctx.BuildProperties, + nil, ) require.NoError(t, err) @@ -71,7 +71,7 @@ func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing. ctx.BuildPath, ctx.BuildOptionsJson, ctx.BuildOptionsJsonPrevious, - ctx.BuildProperties, + nil, ) require.NoError(t, err) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 685d4a2057b..84a27538d37 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -26,7 +26,6 @@ import ( "github.com/arduino/arduino-cli/arduino/cores/packagemanager" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" paths "github.com/arduino/go-paths-helper" - properties "github.com/arduino/go-properties-orderedmap" ) // Context structure @@ -54,7 +53,6 @@ type Context struct { TargetPlatform *cores.PlatformRelease ActualPlatform *cores.PlatformRelease - BuildProperties *properties.Map BuildPath *paths.Path SketchBuildPath *paths.Path CoreBuildPath *paths.Path From c22f1c7466a085224ae9bdb3f22ad3617f3d66ea Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 11:46:24 +0200 Subject: [PATCH 03/34] remove BuildPath from context --- arduino/builder/builder.go | 8 +++++ commands/compile/compile.go | 9 +++--- legacy/builder/builder.go | 18 +++++------ legacy/builder/test/builder_test.go | 17 ++++------ .../test/create_build_options_map_test.go | 1 - legacy/builder/test/helper.go | 4 +-- .../load_previous_build_options_map_test.go | 13 +++----- .../test/merge_sketch_with_bootloader_test.go | 16 +++++----- .../test/store_build_options_map_test.go | 25 +++++++-------- ...uild_path_if_build_options_changed_test.go | 31 ++++++++----------- legacy/builder/types/context.go | 1 - 11 files changed, 64 insertions(+), 79 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 26e40018a66..8053f99e443 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -28,6 +28,8 @@ type Builder struct { sketch *sketch.Sketch buildProperties *properties.Map + buildPath *paths.Path + // Parallel processes jobs int @@ -83,6 +85,7 @@ func NewBuilder( coreBuildCachePath: coreBuildCachePath, jobs: jobs, customBuildProperties: append(requestBuildProperties, "build.warn_data_percentage=75"), + buildPath: buildPath, }, nil } @@ -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 +} diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 106dc98f92e..db34183f7e4 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -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 @@ -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) @@ -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"), } @@ -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() } }() @@ -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} } diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 5595e2ca5d1..ea5eaa82ee0 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -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 } @@ -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, @@ -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, ) @@ -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, ) }), @@ -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(), @@ -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) @@ -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 } @@ -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, @@ -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 != "" { diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 6d41313e390..4847af100a3 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -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()) } } @@ -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() @@ -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 @@ -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] { diff --git a/legacy/builder/test/create_build_options_map_test.go b/legacy/builder/test/create_build_options_map_test.go index 5370736fba5..b21c814114c 100644 --- a/legacy/builder/test/create_build_options_map_test.go +++ b/legacy/builder/test/create_build_options_map_test.go @@ -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"}) diff --git a/legacy/builder/test/helper.go b/legacy/builder/test/helper.go index 33b9aa1e2cf..f2d0990b2e9 100644 --- a/legacy/builder/test/helper.go +++ b/legacy/builder/test/helper.go @@ -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" ) @@ -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 } diff --git a/legacy/builder/test/load_previous_build_options_map_test.go b/legacy/builder/test/load_previous_build_options_map_test.go index 88613fb9034..438b66f00ae 100644 --- a/legacy/builder/test/load_previous_build_options_map_test.go +++ b/legacy/builder/test/load_previous_build_options_map_test.go @@ -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) } diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index 6b96e924313..dc3ceba3392 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -148,7 +148,7 @@ 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) @@ -156,7 +156,7 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(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) @@ -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) @@ -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) diff --git a/legacy/builder/test/store_build_options_map_test.go b/legacy/builder/test/store_build_options_map_test.go index b35899d46d2..9ec6ff3f087 100644 --- a/legacy/builder/test/store_build_options_map_test.go +++ b/legacy/builder/test/store_build_options_map_test.go @@ -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() diff --git a/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go b/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go index 9083dcb8945..9df641637c3 100644 --- a/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go +++ b/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go @@ -19,26 +19,23 @@ import ( "testing" "github.com/arduino/arduino-cli/legacy/builder" - "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/stretchr/testify/require" ) func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { - ctx := &types.Context{} - - buildPath := SetupBuildPath(t, ctx) + buildPath := SetupBuildPath(t) defer buildPath.RemoveAll() - ctx.BuildOptionsJsonPrevious = "{ \"old\":\"old\" }" - ctx.BuildOptionsJson = "{ \"new\":\"new\" }" + buildOptionsJsonPrevious := "{ \"old\":\"old\" }" + buildOptionsJson := "{ \"new\":\"new\" }" buildPath.Join("should_be_deleted.txt").Truncate() _, err := builder.WipeoutBuildPathIfBuildOptionsChanged( - ctx.Clean, - ctx.BuildPath, - ctx.BuildOptionsJson, - ctx.BuildOptionsJsonPrevious, + false, + buildPath, + buildOptionsJson, + buildOptionsJsonPrevious, nil, ) require.NoError(t, err) @@ -57,20 +54,18 @@ func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { } func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing.T) { - ctx := &types.Context{} - - buildPath := SetupBuildPath(t, ctx) + buildPath := SetupBuildPath(t) defer buildPath.RemoveAll() - ctx.BuildOptionsJson = "{ \"new\":\"new\" }" + buildOptionsJson := "{ \"new\":\"new\" }" require.NoError(t, buildPath.Join("should_not_be_deleted.txt").Truncate()) _, err := builder.WipeoutBuildPathIfBuildOptionsChanged( - ctx.Clean, - ctx.BuildPath, - ctx.BuildOptionsJson, - ctx.BuildOptionsJsonPrevious, + false, + buildPath, + buildOptionsJson, + "", nil, ) require.NoError(t, err) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 84a27538d37..91dfa9d31e0 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -53,7 +53,6 @@ type Context struct { TargetPlatform *cores.PlatformRelease ActualPlatform *cores.PlatformRelease - BuildPath *paths.Path SketchBuildPath *paths.Path CoreBuildPath *paths.Path CoreArchiveFilePath *paths.Path From 2c22616daa9db0cb11a619482127c24283242d36 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 12:40:56 +0200 Subject: [PATCH 04/34] remove sketch,libraries,core build path from Context --- arduino/builder/builder.go | 48 ++++++++++++++- arduino/builder/sketch_test.go | 6 +- commands/compile/compile.go | 58 +++++++------------ legacy/builder/builder.go | 20 +++---- legacy/builder/test/builder_test.go | 35 ++++++----- .../unused_compiled_libraries_remover_test.go | 32 +++++----- legacy/builder/types/context.go | 3 - 7 files changed, 113 insertions(+), 89 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 8053f99e443..6f045169825 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -16,6 +16,7 @@ package builder import ( + "errors" "fmt" "github.com/arduino/arduino-cli/arduino/sketch" @@ -23,12 +24,18 @@ import ( "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 @@ -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 } @@ -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 +} diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index 461cc5c8c2c..16c5fd9a599 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -48,7 +48,7 @@ func TestMergeSketchSources(t *testing.T) { } mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource) - b, err := NewBuilder(sk, nil, nil, false, nil, 0, nil) + b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil) require.NoError(t, err) offset, source, err := b.sketchMergeSources(nil) @@ -63,7 +63,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { require.NotNil(t, sk) // ensure not to include Arduino.h when it's already there - b, err := NewBuilder(sk, nil, nil, false, nil, 0, nil) + b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil) require.NoError(t, err) _, source, err := b.sketchMergeSources(nil) @@ -80,7 +80,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, err := NewBuilder(sk1, nil, nil, false, nil, 0, nil) + b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil) require.NoError(t, err) // copy the sketch over, create a fake main file we don't care about it diff --git a/commands/compile/compile.go b/commands/compile/compile.go index db34183f7e4..484a0efbfa3 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -17,6 +17,7 @@ package compile import ( "context" + "errors" "fmt" "io" "sort" @@ -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" @@ -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 @@ -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() @@ -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 { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index ea5eaa82ee0..040d1ee7be1 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -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 }), @@ -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, @@ -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(), @@ -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, @@ -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(), @@ -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 }), @@ -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 } @@ -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, ) diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 4847af100a3..db356e2042f 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -17,6 +17,7 @@ package test import ( "fmt" + "os" "path/filepath" "testing" @@ -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" @@ -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. @@ -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) @@ -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, diff --git a/legacy/builder/test/unused_compiled_libraries_remover_test.go b/legacy/builder/test/unused_compiled_libraries_remover_test.go index 6e7ab70df1b..f0d9facacb3 100644 --- a/legacy/builder/test/unused_compiled_libraries_remover_test.go +++ b/legacy/builder/test/unused_compiled_libraries_remover_test.go @@ -22,7 +22,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/libraries" "github.com/arduino/arduino-cli/legacy/builder" - "github.com/arduino/arduino-cli/legacy/builder/types" paths "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) @@ -36,16 +35,15 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) { require.NoError(t, temp.Join("Bridge").MkdirAll()) require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) - ctx := &types.Context{} - ctx.LibrariesBuildPath = temp - ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( + librariesBuildPath := temp + sketchLibrariesDetector := detector.NewSketchLibrariesDetector( nil, nil, false, false, logger.New(nil, nil, false, ""), ) - ctx.SketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) + sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) err = builder.UnusedCompiledLibrariesRemover( - ctx.LibrariesBuildPath, - ctx.SketchLibrariesDetector.ImportedLibraries(), + librariesBuildPath, + sketchLibrariesDetector.ImportedLibraries(), ) require.NoError(t, err) @@ -61,16 +59,15 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) { } func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { - ctx := &types.Context{} - ctx.LibrariesBuildPath = paths.TempDir().Join("test") - ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( + librariesBuildPath := paths.TempDir().Join("test") + sketchLibrariesDetector := detector.NewSketchLibrariesDetector( nil, nil, false, false, logger.New(nil, nil, false, ""), ) - ctx.SketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) + sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) err := builder.UnusedCompiledLibrariesRemover( - ctx.LibrariesBuildPath, - ctx.SketchLibrariesDetector.ImportedLibraries(), + librariesBuildPath, + sketchLibrariesDetector.ImportedLibraries(), ) require.NoError(t, err) } @@ -84,15 +81,14 @@ func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { require.NoError(t, temp.Join("Bridge").MkdirAll()) require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) - ctx := &types.Context{} - ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( + sketchLibrariesDetector := detector.NewSketchLibrariesDetector( nil, nil, false, false, logger.New(nil, nil, false, ""), ) - ctx.LibrariesBuildPath = temp + librariesBuildPath := temp err = builder.UnusedCompiledLibrariesRemover( - ctx.LibrariesBuildPath, - ctx.SketchLibrariesDetector.ImportedLibraries(), + librariesBuildPath, + sketchLibrariesDetector.ImportedLibraries(), ) require.NoError(t, err) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 91dfa9d31e0..33d4d32c4e1 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -53,11 +53,8 @@ type Context struct { TargetPlatform *cores.PlatformRelease ActualPlatform *cores.PlatformRelease - SketchBuildPath *paths.Path - CoreBuildPath *paths.Path CoreArchiveFilePath *paths.Path CoreObjectsFiles paths.PathList - LibrariesBuildPath *paths.Path LibrariesObjectFiles paths.PathList SketchObjectFiles paths.PathList From 0afe884e948646ea70af53357766f258b7d4382c Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 12:54:53 +0200 Subject: [PATCH 05/34] remove buildPath parameter to PrepareSketchBuildPath func --- arduino/builder/sketch.go | 8 ++++---- legacy/builder/builder.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index 79779d5b4cc..422c3fd345a 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -48,8 +48,8 @@ func (b *Builder) Sketch() *sketch.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). -func (b *Builder) PrepareSketchBuildPath(sourceOverrides map[string]string, buildPath *paths.Path) (int, error) { - if err := buildPath.MkdirAll(); err != nil { +func (b *Builder) PrepareSketchBuildPath(sourceOverrides map[string]string) (int, error) { + if err := b.sketchBuildPath.MkdirAll(); err != nil { return 0, errors.Wrap(err, tr("unable to create a folder to save the sketch")) } @@ -58,12 +58,12 @@ func (b *Builder) PrepareSketchBuildPath(sourceOverrides map[string]string, buil return 0, err } - destFile := buildPath.Join(b.sketch.MainFile.Base() + ".cpp") + destFile := b.sketchBuildPath.Join(b.sketch.MainFile.Base() + ".cpp") if err := destFile.WriteFile([]byte(mergedSource)); err != nil { return 0, err } - if err := b.sketchCopyAdditionalFiles(buildPath, sourceOverrides); err != nil { + if err := b.sketchCopyAdditionalFiles(b.sketchBuildPath, sourceOverrides); err != nil { return 0, err } diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 040d1ee7be1..fb09a742088 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -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.Builder.GetSketchBuildPath()) + ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath(ctx.SourceOverride) return _err }), @@ -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.Builder.GetSketchBuildPath()) + ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath(ctx.SourceOverride) return _err }), From 7ff4427fc9f9d31a169946ebc1fb8de8ee4ec604 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 13:03:03 +0200 Subject: [PATCH 06/34] Make CoreBuilder a method recevier of arduino/builder --- arduino/builder/core.go | 61 +++++++++++++++------------------------ legacy/builder/builder.go | 5 +--- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 9bec697cb96..acd03762350 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -32,47 +32,37 @@ import ( f "github.com/arduino/arduino-cli/internal/algorithms" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-paths-helper" - "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) -// CoreBuildCachePath fixdoc -func (b *Builder) CoreBuildCachePath() *paths.Path { - return b.coreBuildCachePath -} - -// CoreBuilder fixdoc -func CoreBuilder( - buildPath, coreBuildPath, coreBuildCachePath *paths.Path, - buildProperties *properties.Map, +// BuildCore fixdoc +func (b *Builder) BuildCore( actualPlatform *cores.PlatformRelease, onlyUpdateCompilationDatabase, clean bool, compilationDatabase *compilation.Database, - jobs int, builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, *paths.Path, error) { - if err := coreBuildPath.MkdirAll(); err != nil { + if err := b.coreBuildPath.MkdirAll(); err != nil { return nil, nil, errors.WithStack(err) } - if coreBuildCachePath != nil { - if _, err := coreBuildCachePath.RelTo(buildPath); err != nil { + if b.coreBuildCachePath != nil { + if _, err := b.coreBuildCachePath.RelTo(b.buildPath); err != nil { builderLogger.Info(tr("Couldn't deeply cache core build: %[1]s", err)) builderLogger.Info(tr("Running normal build of the core...")) - coreBuildCachePath = nil - } else if err := coreBuildCachePath.MkdirAll(); err != nil { + // TODO decide if we want to override this or not. (It's only used by the + // compileCore function). + b.coreBuildCachePath = nil + } else if err := b.coreBuildCachePath.MkdirAll(); err != nil { return nil, nil, errors.WithStack(err) } } - archiveFile, objectFiles, err := compileCore( + archiveFile, objectFiles, err := b.compileCore( onlyUpdateCompilationDatabase, clean, actualPlatform, - coreBuildPath, coreBuildCachePath, - buildProperties, compilationDatabase, - jobs, builderLogger, progress, progressCB, ) @@ -83,19 +73,16 @@ func CoreBuilder( return objectFiles, archiveFile, nil } -func compileCore( +func (b *Builder) compileCore( onlyUpdateCompilationDatabase, clean bool, actualPlatform *cores.PlatformRelease, - buildPath, buildCachePath *paths.Path, - buildProperties *properties.Map, compilationDatabase *compilation.Database, - jobs int, builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (*paths.Path, paths.PathList, error) { - coreFolder := buildProperties.GetPath("build.core.path") - variantFolder := buildProperties.GetPath("build.variant.path") - targetCoreFolder := buildProperties.GetPath("runtime.platform.path") + coreFolder := b.buildProperties.GetPath("build.core.path") + variantFolder := b.buildProperties.GetPath("build.variant.path") + targetCoreFolder := b.buildProperties.GetPath("runtime.platform.path") includes := []string{coreFolder.String()} if variantFolder != nil && variantFolder.IsDir() { @@ -107,10 +94,10 @@ func compileCore( variantObjectFiles := paths.NewPathList() if variantFolder != nil && variantFolder.IsDir() { variantObjectFiles, err = utils.CompileFilesRecursive( - variantFolder, buildPath, buildProperties, includes, + variantFolder, b.coreBuildPath, b.buildProperties, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, + b.jobs, builderLogger, progress, progressCB, ) @@ -120,16 +107,16 @@ func compileCore( } var targetArchivedCore *paths.Path - if buildCachePath != nil { + if b.coreBuildCachePath != nil { realCoreFolder := coreFolder.Parent().Parent() archivedCoreName := GetCachedCoreArchiveDirName( - buildProperties.Get("build.fqbn"), - buildProperties.Get("compiler.optimization_flags"), + b.buildProperties.Get("build.fqbn"), + b.buildProperties.Get("compiler.optimization_flags"), realCoreFolder, ) - targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a") + targetArchivedCore = b.coreBuildCachePath.Join(archivedCoreName, "core.a") - if _, err := buildcache.New(buildCachePath).GetOrCreate(archivedCoreName); errors.Is(err, buildcache.CreateDirErr) { + if _, err := buildcache.New(b.coreBuildCachePath).GetOrCreate(archivedCoreName); errors.Is(err, buildcache.CreateDirErr) { return nil, nil, fmt.Errorf(tr("creating core cache folder: %s", err)) } @@ -158,10 +145,10 @@ func compileCore( } coreObjectFiles, err := utils.CompileFilesRecursive( - coreFolder, buildPath, buildProperties, includes, + coreFolder, b.coreBuildPath, b.buildProperties, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, + b.jobs, builderLogger, progress, progressCB, ) @@ -170,7 +157,7 @@ func compileCore( } archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( - buildPath, paths.New("core.a"), coreObjectFiles, buildProperties, + b.coreBuildPath, paths.New("core.a"), coreObjectFiles, b.buildProperties, onlyUpdateCompilationDatabase, builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), ) if builderLogger.Verbose() { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index fb09a742088..1faad1e67c5 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -132,13 +132,10 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - objectFiles, archiveFile, err := builder.CoreBuilder( - ctx.Builder.GetBuildPath(), ctx.Builder.GetCoreBuildPath(), ctx.Builder.CoreBuildCachePath(), - ctx.Builder.GetBuildProperties(), + objectFiles, archiveFile, err := ctx.Builder.BuildCore( ctx.ActualPlatform, ctx.OnlyUpdateCompilationDatabase, ctx.Clean, ctx.CompilationDatabase, - ctx.Builder.Jobs(), ctx.BuilderLogger, &ctx.Progress, ctx.ProgressCB, ) From 2297f2b1b90a35e2c7efb840a8488aa5d3b0ba70 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 13:39:25 +0200 Subject: [PATCH 07/34] Add BuilderLogger in arduino/builder --- arduino/builder/builder.go | 5 +++++ arduino/builder/sketch_test.go | 6 +++--- commands/compile/compile.go | 1 + legacy/builder/test/builder_test.go | 4 ++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 6f045169825..5e7954cd527 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" + "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" @@ -45,6 +46,8 @@ type Builder struct { // core related coreBuildCachePath *paths.Path + + logger *logger.BuilderLogger } // NewBuilder creates a sketch Builder. @@ -56,6 +59,7 @@ func NewBuilder( coreBuildCachePath *paths.Path, jobs int, requestBuildProperties []string, + logger *logger.BuilderLogger, ) (*Builder, error) { buildProperties := properties.NewMap() if boardBuildProperties != nil { @@ -113,6 +117,7 @@ func NewBuilder( jobs: jobs, customBuildProperties: append(requestBuildProperties, "build.warn_data_percentage=75"), coreBuildCachePath: coreBuildCachePath, + logger: logger, }, nil } diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index 16c5fd9a599..aa32e30fb40 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -48,7 +48,7 @@ func TestMergeSketchSources(t *testing.T) { } mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource) - b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil) + b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, nil) require.NoError(t, err) offset, source, err := b.sketchMergeSources(nil) @@ -63,7 +63,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { require.NotNil(t, sk) // ensure not to include Arduino.h when it's already there - b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil) + b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, nil) require.NoError(t, err) _, source, err := b.sketchMergeSources(nil) @@ -80,7 +80,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, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil) + b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil, nil) require.NoError(t, err) // copy the sketch over, create a fake main file we don't care about it diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 484a0efbfa3..71738d9dda7 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -208,6 +208,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream coreBuildCachePath, int(req.GetJobs()), req.GetBuildProperties(), + builderLogger, ) if err != nil { if strings.Contains(err.Error(), "invalid build properties") { diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index db356e2042f..6b581cb4c2d 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -103,7 +103,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat builderLogger := logger.New(nil, nil, false, "") ctx.BuilderLogger = builderLogger - ctx.Builder, err = bldr.NewBuilder(sk, nil, buildPath, false, nil, 0, nil) + ctx.Builder, err = bldr.NewBuilder(sk, nil, buildPath, false, nil, 0, nil, builderLogger) require.NoError(t, err) if fqbn != "" { ctx.FQBN = parseFQBN(t, fqbn) @@ -112,7 +112,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, buildPath, false /*OptimizeForDebug*/, nil, 0, nil) + ctx.Builder, err = bldr.NewBuilder(sk, boardBuildProperties, buildPath, false /*OptimizeForDebug*/, nil, 0, nil, builderLogger) require.NoError(t, err) ctx.PackageManager = pme From d704e4c1e699447dbbe3a57b20883f72370d53b2 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 13:51:53 +0200 Subject: [PATCH 08/34] Remove BuilderLogger from CoreBuild parameter --- arduino/builder/core.go | 30 +++++++++++++----------------- legacy/builder/builder.go | 1 - 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/arduino/builder/core.go b/arduino/builder/core.go index acd03762350..826a753e902 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -24,7 +24,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" - "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/cores" @@ -40,7 +39,6 @@ func (b *Builder) BuildCore( actualPlatform *cores.PlatformRelease, onlyUpdateCompilationDatabase, clean bool, compilationDatabase *compilation.Database, - builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, *paths.Path, error) { if err := b.coreBuildPath.MkdirAll(); err != nil { @@ -49,8 +47,8 @@ func (b *Builder) BuildCore( if b.coreBuildCachePath != nil { if _, err := b.coreBuildCachePath.RelTo(b.buildPath); err != nil { - builderLogger.Info(tr("Couldn't deeply cache core build: %[1]s", err)) - builderLogger.Info(tr("Running normal build of the core...")) + b.logger.Info(tr("Couldn't deeply cache core build: %[1]s", err)) + b.logger.Info(tr("Running normal build of the core...")) // TODO decide if we want to override this or not. (It's only used by the // compileCore function). b.coreBuildCachePath = nil @@ -63,7 +61,6 @@ func (b *Builder) BuildCore( onlyUpdateCompilationDatabase, clean, actualPlatform, compilationDatabase, - builderLogger, progress, progressCB, ) if err != nil { @@ -77,7 +74,6 @@ func (b *Builder) compileCore( onlyUpdateCompilationDatabase, clean bool, actualPlatform *cores.PlatformRelease, compilationDatabase *compilation.Database, - builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (*paths.Path, paths.PathList, error) { coreFolder := b.buildProperties.GetPath("build.core.path") @@ -98,7 +94,7 @@ func (b *Builder) compileCore( onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, - builderLogger, + b.logger, progress, progressCB, ) if err != nil { @@ -137,8 +133,8 @@ func (b *Builder) compileCore( if canUseArchivedCore { // use archived core - if builderLogger.Verbose() { - builderLogger.Info(tr("Using precompiled core: %[1]s", targetArchivedCore)) + if b.logger.Verbose() { + b.logger.Info(tr("Using precompiled core: %[1]s", targetArchivedCore)) } return targetArchivedCore, variantObjectFiles, nil } @@ -149,7 +145,7 @@ func (b *Builder) compileCore( onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, - builderLogger, + b.logger, progress, progressCB, ) if err != nil { @@ -158,10 +154,10 @@ func (b *Builder) compileCore( archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( b.coreBuildPath, paths.New("core.a"), coreObjectFiles, b.buildProperties, - onlyUpdateCompilationDatabase, builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), + onlyUpdateCompilationDatabase, b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), ) - if builderLogger.Verbose() { - builderLogger.Info(string(verboseInfo)) + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } if err != nil { return nil, nil, errors.WithStack(err) @@ -170,15 +166,15 @@ func (b *Builder) compileCore( // archive core.a if targetArchivedCore != nil && !onlyUpdateCompilationDatabase { err := archiveFile.CopyTo(targetArchivedCore) - if builderLogger.Verbose() { + if b.logger.Verbose() { if err == nil { - builderLogger.Info(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore)) + b.logger.Info(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore)) } else if os.IsNotExist(err) { - builderLogger.Info(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s", + b.logger.Info(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s", actualPlatform, "https://arduino.github.io/arduino-cli/latest/platform-specification/#recipes-to-build-the-corea-archive-file")) } else { - builderLogger.Info(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err)) + b.logger.Info(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err)) } } } diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 1faad1e67c5..455bb61bfc6 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -136,7 +136,6 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.ActualPlatform, ctx.OnlyUpdateCompilationDatabase, ctx.Clean, ctx.CompilationDatabase, - ctx.BuilderLogger, &ctx.Progress, ctx.ProgressCB, ) From f8cbfe52eae7f7eb0445e0e3544c19141f3da09e Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 13:53:51 +0200 Subject: [PATCH 09/34] Make getCachedCoreArchiveDirName unexported --- arduino/builder/core.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 826a753e902..9d3fa053eaa 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -105,7 +105,7 @@ func (b *Builder) compileCore( var targetArchivedCore *paths.Path if b.coreBuildCachePath != nil { realCoreFolder := coreFolder.Parent().Parent() - archivedCoreName := GetCachedCoreArchiveDirName( + archivedCoreName := getCachedCoreArchiveDirName( b.buildProperties.Get("build.fqbn"), b.buildProperties.Get("compiler.optimization_flags"), realCoreFolder, @@ -182,9 +182,9 @@ func (b *Builder) compileCore( return archiveFile, variantObjectFiles, nil } -// GetCachedCoreArchiveDirName returns the directory name to be used to store +// getCachedCoreArchiveDirName returns the directory name to be used to store // the global cached core.a. -func GetCachedCoreArchiveDirName(fqbn string, optimizationFlags string, coreFolder *paths.Path) string { +func getCachedCoreArchiveDirName(fqbn string, optimizationFlags string, coreFolder *paths.Path) string { fqbnToUnderscore := strings.ReplaceAll(fqbn, ":", "_") fqbnToUnderscore = strings.ReplaceAll(fqbnToUnderscore, "=", "_") if absCoreFolder, err := coreFolder.Abs(); err == nil { From 5916daa27541963783651b47345f7b1a8bf11b76 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 15:48:13 +0200 Subject: [PATCH 10/34] heavily refactored the ContainerBuildOptions --- arduino/builder/build_options_manager.go | 183 ++++++++++++++++++ arduino/builder/builder.go | 29 ++- arduino/builder/sketch_test.go | 10 +- commands/compile/compile.go | 6 + legacy/builder/builder.go | 19 +- legacy/builder/container_build_options.go | 60 ------ legacy/builder/create_build_options_map.go | 63 ------ legacy/builder/load_previous_build_options.go | 36 ---- legacy/builder/store_build_options_map.go | 25 --- legacy/builder/test/builder_test.go | 11 +- .../test/create_build_options_map_test.go | 55 ------ .../load_previous_build_options_map_test.go | 46 ----- .../test/store_build_options_map_test.go | 84 ++++++-- ...uild_path_if_build_options_changed_test.go | 84 -------- legacy/builder/types/context.go | 4 - ...out_build_path_if_build_options_changed.go | 94 --------- 16 files changed, 307 insertions(+), 502 deletions(-) create mode 100644 arduino/builder/build_options_manager.go delete mode 100644 legacy/builder/container_build_options.go delete mode 100644 legacy/builder/create_build_options_map.go delete mode 100644 legacy/builder/load_previous_build_options.go delete mode 100644 legacy/builder/store_build_options_map.go delete mode 100644 legacy/builder/test/create_build_options_map_test.go delete mode 100644 legacy/builder/test/load_previous_build_options_map_test.go delete mode 100644 legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go delete mode 100644 legacy/builder/wipeout_build_path_if_build_options_changed.go diff --git a/arduino/builder/build_options_manager.go b/arduino/builder/build_options_manager.go new file mode 100644 index 00000000000..db0aa8a5059 --- /dev/null +++ b/arduino/builder/build_options_manager.go @@ -0,0 +1,183 @@ +// This file is part of arduino-cli. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package builder + +import ( + "encoding/json" + "path/filepath" + "strings" + + "github.com/arduino/arduino-cli/arduino/builder/logger" + "github.com/arduino/arduino-cli/arduino/builder/utils" + "github.com/arduino/arduino-cli/arduino/sketch" + "github.com/arduino/go-paths-helper" + properties "github.com/arduino/go-properties-orderedmap" + "github.com/pkg/errors" +) + +// BuildOptionsManager fixdoc +type BuildOptionsManager struct { + currentOptions *properties.Map + currentBuildOptionsJSON []byte + + hardwareDirs paths.PathList + builtInToolsDirs paths.PathList + otherLibrariesDirs paths.PathList + builtInLibrariesDirs *paths.Path + buildPath *paths.Path + runtimePlatformPath *paths.Path + buildCorePath *paths.Path + sketch *sketch.Sketch + customBuildProperties []string + fqbn string + compilerOptimizationFlags string + clean bool + builderLogger *logger.BuilderLogger +} + +// NewBuildOptionsManager fixdoc +func NewBuildOptionsManager( + hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList, + builtInLibrariesDirs, buildPath *paths.Path, + sketch *sketch.Sketch, + customBuildProperties []string, + fqbn string, + clean bool, + compilerOptimizationFlags string, + runtimePlatformPath, buildCorePath *paths.Path, + buildLogger *logger.BuilderLogger, +) *BuildOptionsManager { + opts := properties.NewMap() + + opts.Set("hardwareFolders", strings.Join(hardwareDirs.AsStrings(), ",")) + opts.Set("builtInToolsFolders", strings.Join(builtInToolsDirs.AsStrings(), ",")) + opts.Set("otherLibrariesFolders", strings.Join(otherLibrariesDirs.AsStrings(), ",")) + opts.SetPath("sketchLocation", sketch.FullPath) + opts.Set("fqbn", fqbn) + opts.Set("customBuildProperties", strings.Join(customBuildProperties, ",")) + opts.Set("compiler.optimization_flags", compilerOptimizationFlags) + + if builtInLibrariesDirs != nil { + opts.Set("builtInLibrariesFolders", builtInLibrariesDirs.String()) + } + + absPath := sketch.FullPath.Parent() + var additionalFilesRelative []string + for _, f := range sketch.AdditionalFiles { + relPath, err := f.RelTo(absPath) + if err != nil { + continue // ignore + } + additionalFilesRelative = append(additionalFilesRelative, relPath.String()) + } + opts.Set("additionalFiles", strings.Join(additionalFilesRelative, ",")) + + return &BuildOptionsManager{ + currentOptions: opts, + hardwareDirs: hardwareDirs, + builtInToolsDirs: builtInToolsDirs, + otherLibrariesDirs: otherLibrariesDirs, + builtInLibrariesDirs: builtInLibrariesDirs, + buildPath: buildPath, + runtimePlatformPath: runtimePlatformPath, + buildCorePath: buildCorePath, + sketch: sketch, + customBuildProperties: customBuildProperties, + fqbn: fqbn, + compilerOptimizationFlags: compilerOptimizationFlags, + clean: clean, + builderLogger: buildLogger, + } +} + +// WipeBuildPath fixdoc +func (m *BuildOptionsManager) WipeBuildPath() error { + buildOptionsJSON, err := json.MarshalIndent(m.currentOptions, "", " ") + if err != nil { + return errors.WithStack(err) + } + m.currentBuildOptionsJSON = buildOptionsJSON + + if err := m.wipeBuildPath(); err != nil { + return errors.WithStack(err) + } + return m.buildPath.Join("build.options.json").WriteFile(buildOptionsJSON) +} + +func (m *BuildOptionsManager) wipeBuildPath() error { + wipe := func() error { + // FIXME: this should go outside legacy and behind a `logrus` call so users can + // control when this should be printed. + // logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_BUILD_OPTIONS_CHANGED + constants.MSG_REBUILD_ALL) + if err := m.buildPath.RemoveAll(); err != nil { + return errors.WithMessage(err, tr("cleaning build path")) + } + if err := m.buildPath.MkdirAll(); err != nil { + return errors.WithMessage(err, tr("cleaning build path")) + } + return nil + } + + if m.clean { + return wipe() + } + + // Load previous build options map + var buildOptionsJSONPrevious []byte + var _err error + if buildOptionsFile := m.buildPath.Join("build.options.json"); buildOptionsFile.Exist() { + buildOptionsJSONPrevious, _err = buildOptionsFile.ReadFile() + if _err != nil { + return errors.WithStack(_err) + } + } + + if len(buildOptionsJSONPrevious) == 0 { + return nil + } + + var prevOpts *properties.Map + if err := json.Unmarshal(buildOptionsJSONPrevious, &prevOpts); err != nil || prevOpts == nil { + m.builderLogger.Info(tr("%[1]s invalid, rebuilding all", "build.options.json")) + return wipe() + } + + // If SketchLocation path is different but filename is the same, consider it equal + if filepath.Base(m.currentOptions.Get("sketchLocation")) == filepath.Base(prevOpts.Get("sketchLocation")) { + m.currentOptions.Remove("sketchLocation") + prevOpts.Remove("sketchLocation") + } + + // If options are not changed check if core has + if m.currentOptions.Equals(prevOpts) { + // check if any of the files contained in the core folders has changed + // since the json was generated - like platform.txt or similar + // if so, trigger a "safety" wipe + targetCoreFolder := m.runtimePlatformPath + coreFolder := m.buildCorePath + realCoreFolder := coreFolder.Parent().Parent() + jsonPath := m.buildPath.Join("build.options.json") + coreUnchanged, _ := utils.DirContentIsOlderThan(realCoreFolder, jsonPath, ".txt") + if coreUnchanged && targetCoreFolder != nil && !realCoreFolder.EqualsTo(targetCoreFolder) { + coreUnchanged, _ = utils.DirContentIsOlderThan(targetCoreFolder, jsonPath, ".txt") + } + if coreUnchanged { + return nil + } + } + + return wipe() +} diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 5e7954cd527..b8802a48bfc 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -48,6 +48,16 @@ type Builder struct { coreBuildCachePath *paths.Path logger *logger.BuilderLogger + + *BuildOptionsManager + + hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList + builtInLibrariesDirs *paths.Path + fqbn string + clean bool + + compilerOptimizationFlags string + runtimePlatformPath, buildCorePath *paths.Path } // NewBuilder creates a sketch Builder. @@ -59,6 +69,10 @@ func NewBuilder( coreBuildCachePath *paths.Path, jobs int, requestBuildProperties []string, + hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList, + builtInLibrariesDirs *paths.Path, + fqbn string, + clean bool, logger *logger.BuilderLogger, ) (*Builder, error) { buildProperties := properties.NewMap() @@ -89,6 +103,7 @@ func NewBuilder( return nil, fmt.Errorf("invalid build properties: %w", err) } buildProperties.Merge(customBuildProperties) + customBuildPropertiesArgs := append(requestBuildProperties, "build.warn_data_percentage=75") sketchBuildPath, err := buildPath.Join("sketch").Abs() if err != nil { @@ -115,9 +130,21 @@ func NewBuilder( coreBuildPath: coreBuildPath, librariesBuildPath: librariesBuildPath, jobs: jobs, - customBuildProperties: append(requestBuildProperties, "build.warn_data_percentage=75"), + customBuildProperties: customBuildPropertiesArgs, coreBuildCachePath: coreBuildCachePath, logger: logger, + BuildOptionsManager: NewBuildOptionsManager( + hardwareDirs, builtInToolsDirs, otherLibrariesDirs, + builtInLibrariesDirs, buildPath, + sk, + customBuildPropertiesArgs, + fqbn, + clean, + buildProperties.Get("compiler.optimization_flags"), + buildProperties.GetPath("runtime.platform.path"), + buildProperties.GetPath("build.core.path"), // TODO can we buildCorePath ? + logger, + ), }, nil } diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index aa32e30fb40..d01bc5e7a97 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -48,7 +48,9 @@ func TestMergeSketchSources(t *testing.T) { } mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource) - b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, nil) + b, err := NewBuilder( + sk, nil, paths.New("testdata"), false, nil, 0, nil, + nil, nil, nil, nil, "", false, nil) require.NoError(t, err) offset, source, err := b.sketchMergeSources(nil) @@ -63,7 +65,8 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { require.NotNil(t, sk) // ensure not to include Arduino.h when it's already there - b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, nil) + b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, + nil, nil, nil, nil, "", false, nil) require.NoError(t, err) _, source, err := b.sketchMergeSources(nil) @@ -80,7 +83,8 @@ 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, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil, nil) + b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil, + nil, nil, nil, nil, "", false, nil) require.NoError(t, err) // copy the sketch over, create a fake main file we don't care about it diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 71738d9dda7..df520d8fbdc 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -208,6 +208,12 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream coreBuildCachePath, int(req.GetJobs()), req.GetBuildProperties(), + builderCtx.HardwareDirs, + builderCtx.BuiltInToolsDirs, + builderCtx.OtherLibrariesDirs, + builderCtx.BuiltInLibrariesDirs, + builderCtx.FQBN.String(), + builderCtx.Clean, builderLogger, ) if err != nil { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 455bb61bfc6..be668d8a21c 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -408,24 +408,7 @@ func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipI func containerBuildOptions(ctx *types.Context) types.BareCommand { return types.BareCommand(func(ctx *types.Context) error { - // TODO here we can pass only the properties we're reading from the - // ctx.Builder.GetBuildProperties() - buildOptionsJSON, buildOptionsJSONPrevious, infoMessage, err := ContainerBuildOptions( - ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.CustomBuildProperties(), - ctx.FQBN.String(), ctx.Clean, ctx.Builder.GetBuildProperties(), - ) - if infoMessage != "" { - ctx.BuilderLogger.Info(infoMessage) - } - if err != nil { - return err - } - - ctx.BuildOptionsJson = buildOptionsJSON - ctx.BuildOptionsJsonPrevious = buildOptionsJSONPrevious - - return nil + return ctx.Builder.BuildOptionsManager.WipeBuildPath() }) } diff --git a/legacy/builder/container_build_options.go b/legacy/builder/container_build_options.go deleted file mode 100644 index 6302bc558f7..00000000000 --- a/legacy/builder/container_build_options.go +++ /dev/null @@ -1,60 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/go-paths-helper" - properties "github.com/arduino/go-properties-orderedmap" - "github.com/pkg/errors" -) - -func ContainerBuildOptions( - hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList, - builtInLibrariesDirs, buildPath *paths.Path, - sketch *sketch.Sketch, - customBuildProperties []string, - fqbn string, - clean bool, - buildProperties *properties.Map, -) (string, string, string, error) { - buildOptionsJSON, err := CreateBuildOptionsMap( - hardwareDirs, builtInToolsDirs, otherLibrariesDirs, - builtInLibrariesDirs, sketch, customBuildProperties, - fqbn, buildProperties.Get("compiler.optimization_flags"), - ) - if err != nil { - return "", "", "", errors.WithStack(err) - } - - buildOptionsJSONPrevious, err := LoadPreviousBuildOptionsMap(buildPath) - if err != nil { - return "", "", "", errors.WithStack(err) - } - - infoOut, err := WipeoutBuildPathIfBuildOptionsChanged( - clean, - buildPath, - buildOptionsJSON, - buildOptionsJSONPrevious, - buildProperties, - ) - if err != nil { - return "", "", "", errors.WithStack(err) - } - - return buildOptionsJSON, buildOptionsJSONPrevious, infoOut, StoreBuildOptionsMap(buildPath, buildOptionsJSON) -} diff --git a/legacy/builder/create_build_options_map.go b/legacy/builder/create_build_options_map.go deleted file mode 100644 index 23240f56bc1..00000000000 --- a/legacy/builder/create_build_options_map.go +++ /dev/null @@ -1,63 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "encoding/json" - "strings" - - "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/go-paths-helper" - properties "github.com/arduino/go-properties-orderedmap" - "github.com/pkg/errors" -) - -func CreateBuildOptionsMap( - hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList, - builtInLibrariesDirs *paths.Path, - sketch *sketch.Sketch, - customBuildProperties []string, - fqbn, compilerOptimizationFlags string, -) (string, error) { - opts := properties.NewMap() - opts.Set("hardwareFolders", strings.Join(hardwareDirs.AsStrings(), ",")) - opts.Set("builtInToolsFolders", strings.Join(builtInToolsDirs.AsStrings(), ",")) - if builtInLibrariesDirs != nil { - opts.Set("builtInLibrariesFolders", builtInLibrariesDirs.String()) - } - opts.Set("otherLibrariesFolders", strings.Join(otherLibrariesDirs.AsStrings(), ",")) - opts.SetPath("sketchLocation", sketch.FullPath) - var additionalFilesRelative []string - absPath := sketch.FullPath.Parent() - for _, f := range sketch.AdditionalFiles { - relPath, err := f.RelTo(absPath) - if err != nil { - continue // ignore - } - additionalFilesRelative = append(additionalFilesRelative, relPath.String()) - } - opts.Set("fqbn", fqbn) - opts.Set("customBuildProperties", strings.Join(customBuildProperties, ",")) - opts.Set("additionalFiles", strings.Join(additionalFilesRelative, ",")) - opts.Set("compiler.optimization_flags", compilerOptimizationFlags) - - buildOptionsJSON, err := json.MarshalIndent(opts, "", " ") - if err != nil { - return "", errors.WithStack(err) - } - - return string(buildOptionsJSON), nil -} diff --git a/legacy/builder/load_previous_build_options.go b/legacy/builder/load_previous_build_options.go deleted file mode 100644 index 63dd83804e4..00000000000 --- a/legacy/builder/load_previous_build_options.go +++ /dev/null @@ -1,36 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "github.com/arduino/arduino-cli/legacy/builder/constants" - "github.com/arduino/go-paths-helper" - "github.com/pkg/errors" -) - -func LoadPreviousBuildOptionsMap(buildPath *paths.Path) (string, error) { - buildOptionsFile := buildPath.Join(constants.BUILD_OPTIONS_FILE) - - if buildOptionsFile.NotExist() { - return "", nil - } - - buildOptionsJsonPrevious, err := buildOptionsFile.ReadFile() - if err != nil { - return "", errors.WithStack(err) - } - return string(buildOptionsJsonPrevious), nil -} diff --git a/legacy/builder/store_build_options_map.go b/legacy/builder/store_build_options_map.go deleted file mode 100644 index bc872d76d63..00000000000 --- a/legacy/builder/store_build_options_map.go +++ /dev/null @@ -1,25 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "github.com/arduino/arduino-cli/legacy/builder/constants" - "github.com/arduino/go-paths-helper" -) - -func StoreBuildOptionsMap(buildPath *paths.Path, buildOptionsJson string) error { - return buildPath.Join(constants.BUILD_OPTIONS_FILE).WriteFile([]byte(buildOptionsJson)) -} diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 6b581cb4c2d..56aba6d3d7c 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -103,7 +103,11 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat builderLogger := logger.New(nil, nil, false, "") ctx.BuilderLogger = builderLogger - ctx.Builder, err = bldr.NewBuilder(sk, nil, buildPath, false, nil, 0, nil, builderLogger) + ctx.Builder, err = bldr.NewBuilder( + sk, nil, buildPath, false, nil, 0, nil, + ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, + ctx.BuiltInLibrariesDirs, fqbn, ctx.Clean, builderLogger, + ) require.NoError(t, err) if fqbn != "" { ctx.FQBN = parseFQBN(t, fqbn) @@ -112,7 +116,10 @@ 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, buildPath, false /*OptimizeForDebug*/, nil, 0, nil, builderLogger) + ctx.Builder, err = bldr.NewBuilder( + sk, boardBuildProperties, buildPath, false, nil, 0, nil, + ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, + ctx.BuiltInLibrariesDirs, fqbn, ctx.Clean, builderLogger) require.NoError(t, err) ctx.PackageManager = pme diff --git a/legacy/builder/test/create_build_options_map_test.go b/legacy/builder/test/create_build_options_map_test.go deleted file mode 100644 index b21c814114c..00000000000 --- a/legacy/builder/test/create_build_options_map_test.go +++ /dev/null @@ -1,55 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package test - -import ( - "testing" - - "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/arduino-cli/legacy/builder" - "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/arduino/go-paths-helper" - "github.com/arduino/go-properties-orderedmap" - "github.com/stretchr/testify/require" -) - -func TestCreateBuildOptionsMap(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("hardware", "hardware2"), - BuiltInToolsDirs: paths.NewPathList("tools"), - OtherLibrariesDirs: paths.NewPathList("libraries"), - FQBN: parseFQBN(t, "my:nice:fqbn"), - } - - 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")}, nil, - ctx.FQBN.String(), buildProperties.Get("compiler.optimization_flags"), - ) - require.NoError(t, err) - - require.Equal(t, `{ - "additionalFiles": "", - "builtInToolsFolders": "tools", - "compiler.optimization_flags": "-Os", - "customBuildProperties": "", - "fqbn": "my:nice:fqbn", - "hardwareFolders": "hardware,hardware2", - "otherLibrariesFolders": "libraries", - "sketchLocation": "sketchLocation" -}`, buildPropertiesJSON) -} diff --git a/legacy/builder/test/load_previous_build_options_map_test.go b/legacy/builder/test/load_previous_build_options_map_test.go deleted file mode 100644 index 438b66f00ae..00000000000 --- a/legacy/builder/test/load_previous_build_options_map_test.go +++ /dev/null @@ -1,46 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package test - -import ( - "testing" - - "github.com/arduino/arduino-cli/legacy/builder" - "github.com/arduino/arduino-cli/legacy/builder/constants" - "github.com/stretchr/testify/require" -) - -func TestLoadPreviousBuildOptionsMap(t *testing.T) { - buildPath := SetupBuildPath(t) - defer buildPath.RemoveAll() - - err := buildPath.Join(constants.BUILD_OPTIONS_FILE).WriteFile([]byte("test")) - require.NoError(t, err) - - buildOptionsJsonPrevious, err := builder.LoadPreviousBuildOptionsMap(buildPath) - require.NoError(t, err) - - require.Equal(t, "test", buildOptionsJsonPrevious) -} - -func TestLoadPreviousBuildOptionsMapMissingFile(t *testing.T) { - buildPath := SetupBuildPath(t) - defer buildPath.RemoveAll() - - buildOptionsJsonPrevious, err := builder.LoadPreviousBuildOptionsMap(buildPath) - require.NoError(t, err) - require.Empty(t, buildOptionsJsonPrevious) -} diff --git a/legacy/builder/test/store_build_options_map_test.go b/legacy/builder/test/store_build_options_map_test.go index 9ec6ff3f087..970358a2877 100644 --- a/legacy/builder/test/store_build_options_map_test.go +++ b/legacy/builder/test/store_build_options_map_test.go @@ -18,15 +18,14 @@ package test import ( "testing" + "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/arduino-cli/legacy/builder" - "github.com/arduino/arduino-cli/legacy/builder/constants" paths "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" "github.com/stretchr/testify/require" ) -func TestStoreBuildOptionsMap(t *testing.T) { +func TestCheckIfBuildOptionsChanged(t *testing.T) { hardwareDirs := paths.NewPathList("hardware") builtInToolsDirs := paths.NewPathList("tools") builtInLibrariesDirs := paths.New("built-in libraries") @@ -37,22 +36,24 @@ func TestStoreBuildOptionsMap(t *testing.T) { defer buildPath.RemoveAll() buildProperties := properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"}) - buildPropertiesJSON, err := builder.CreateBuildOptionsMap( + buildOptionsManager := builder.NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, - builtInLibrariesDirs, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, []string{"custom=prop"}, - fqbn.String(), buildProperties.Get("compiler.optimization_flags"), + builtInLibrariesDirs, buildPath, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, []string{"custom=prop"}, + fqbn.String(), false, + buildProperties.Get("compiler.optimization_flags"), + buildProperties.GetPath("runtime.platform.path"), + buildProperties.GetPath("build.core.path"), + nil, ) - require.NoError(t, err) - buildOptionsJson := buildPropertiesJSON - err = builder.StoreBuildOptionsMap(buildPath, buildOptionsJson) + err := buildOptionsManager.WipeBuildPath() require.NoError(t, err) - exist, err := buildPath.Join(constants.BUILD_OPTIONS_FILE).ExistCheck() + exist, err := buildPath.Join("build.options.json").ExistCheck() require.NoError(t, err) require.True(t, exist) - bytes, err := buildPath.Join(constants.BUILD_OPTIONS_FILE).ReadFile() + bytes, err := buildPath.Join("build.options.json").ReadFile() require.NoError(t, err) require.Equal(t, `{ @@ -67,3 +68,64 @@ func TestStoreBuildOptionsMap(t *testing.T) { "sketchLocation": "sketchLocation" }`, string(bytes)) } + +//func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { +// buildPath := SetupBuildPath(t) +// defer buildPath.RemoveAll() +// +// buildOptionsJsonPrevious := "{ \"old\":\"old\" }" +// buildOptionsJson := "{ \"new\":\"new\" }" +// +// buildPath.Join("should_be_deleted.txt").Truncate() +// +// _, err := builder.WipeoutBuildPathIfBuildOptionsChanged( +// false, +// buildPath, +// buildOptionsJson, +// buildOptionsJsonPrevious, +// nil, +// ) +// require.NoError(t, err) +// +// exist, err := buildPath.ExistCheck() +// require.NoError(t, err) +// require.True(t, exist) +// +// files, err := buildPath.ReadDir() +// require.NoError(t, err) +// require.Equal(t, 0, len(files)) +// +// exist, err = buildPath.Join("should_be_deleted.txt").ExistCheck() +// require.NoError(t, err) +// require.False(t, exist) +//} +// +//func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing.T) { +// buildPath := SetupBuildPath(t) +// defer buildPath.RemoveAll() +// +// buildOptionsJson := "{ \"new\":\"new\" }" +// +// require.NoError(t, buildPath.Join("should_not_be_deleted.txt").Truncate()) +// +// _, err := builder.WipeoutBuildPathIfBuildOptionsChanged( +// false, +// buildPath, +// buildOptionsJson, +// "", +// nil, +// ) +// require.NoError(t, err) +// +// exist, err := buildPath.ExistCheck() +// require.NoError(t, err) +// require.True(t, exist) +// +// files, err := buildPath.ReadDir() +// require.NoError(t, err) +// require.Equal(t, 1, len(files)) +// +// exist, err = buildPath.Join("should_not_be_deleted.txt").ExistCheck() +// require.NoError(t, err) +// require.True(t, exist) +//} diff --git a/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go b/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go deleted file mode 100644 index 9df641637c3..00000000000 --- a/legacy/builder/test/wipeout_build_path_if_build_options_changed_test.go +++ /dev/null @@ -1,84 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package test - -import ( - "testing" - - "github.com/arduino/arduino-cli/legacy/builder" - "github.com/stretchr/testify/require" -) - -func TestWipeoutBuildPathIfBuildOptionsChanged(t *testing.T) { - buildPath := SetupBuildPath(t) - defer buildPath.RemoveAll() - - buildOptionsJsonPrevious := "{ \"old\":\"old\" }" - buildOptionsJson := "{ \"new\":\"new\" }" - - buildPath.Join("should_be_deleted.txt").Truncate() - - _, err := builder.WipeoutBuildPathIfBuildOptionsChanged( - false, - buildPath, - buildOptionsJson, - buildOptionsJsonPrevious, - nil, - ) - require.NoError(t, err) - - exist, err := buildPath.ExistCheck() - require.NoError(t, err) - require.True(t, exist) - - files, err := buildPath.ReadDir() - require.NoError(t, err) - require.Equal(t, 0, len(files)) - - exist, err = buildPath.Join("should_be_deleted.txt").ExistCheck() - require.NoError(t, err) - require.False(t, exist) -} - -func TestWipeoutBuildPathIfBuildOptionsChangedNoPreviousBuildOptions(t *testing.T) { - buildPath := SetupBuildPath(t) - defer buildPath.RemoveAll() - - buildOptionsJson := "{ \"new\":\"new\" }" - - require.NoError(t, buildPath.Join("should_not_be_deleted.txt").Truncate()) - - _, err := builder.WipeoutBuildPathIfBuildOptionsChanged( - false, - buildPath, - buildOptionsJson, - "", - nil, - ) - require.NoError(t, err) - - exist, err := buildPath.ExistCheck() - require.NoError(t, err) - require.True(t, exist) - - files, err := buildPath.ReadDir() - require.NoError(t, err) - require.Equal(t, 1, len(files)) - - exist, err = buildPath.Join("should_not_be_deleted.txt").ExistCheck() - require.NoError(t, err) - require.True(t, exist) -} diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 33d4d32c4e1..3af0d1bd12d 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -42,10 +42,6 @@ type Context struct { FQBN *cores.FQBN Clean bool - // Build options are serialized here - BuildOptionsJson string - BuildOptionsJsonPrevious string - PackageManager *packagemanager.Explorer RequiredTools []*cores.ToolRelease TargetBoard *cores.Board diff --git a/legacy/builder/wipeout_build_path_if_build_options_changed.go b/legacy/builder/wipeout_build_path_if_build_options_changed.go deleted file mode 100644 index 108664978ca..00000000000 --- a/legacy/builder/wipeout_build_path_if_build_options_changed.go +++ /dev/null @@ -1,94 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "encoding/json" - "path/filepath" - - "github.com/arduino/arduino-cli/arduino/builder/utils" - "github.com/arduino/arduino-cli/legacy/builder/constants" - "github.com/arduino/go-paths-helper" - properties "github.com/arduino/go-properties-orderedmap" - "github.com/pkg/errors" -) - -func WipeoutBuildPathIfBuildOptionsChanged( - clean bool, - buildPath *paths.Path, - buildOptionsJson, buildOptionsJsonPrevious string, - buildProperties *properties.Map, -) (string, error) { - if clean { - return "", doCleanup(buildPath) - } - if buildOptionsJsonPrevious == "" { - return "", nil - } - - var opts *properties.Map - if err := json.Unmarshal([]byte(buildOptionsJson), &opts); err != nil || opts == nil { - panic(constants.BUILD_OPTIONS_FILE + " is invalid") - } - - var prevOpts *properties.Map - if err := json.Unmarshal([]byte(buildOptionsJsonPrevious), &prevOpts); err != nil || prevOpts == nil { - return tr("%[1]s invalid, rebuilding all", constants.BUILD_OPTIONS_FILE), doCleanup(buildPath) - } - - // If SketchLocation path is different but filename is the same, consider it equal - if filepath.Base(opts.Get("sketchLocation")) == filepath.Base(prevOpts.Get("sketchLocation")) { - opts.Remove("sketchLocation") - prevOpts.Remove("sketchLocation") - } - - // If options are not changed check if core has - if opts.Equals(prevOpts) { - // check if any of the files contained in the core folders has changed - // since the json was generated - like platform.txt or similar - // if so, trigger a "safety" wipe - targetCoreFolder := buildProperties.GetPath("runtime.platform.path") - coreFolder := buildProperties.GetPath("build.core.path") - realCoreFolder := coreFolder.Parent().Parent() - jsonPath := buildPath.Join(constants.BUILD_OPTIONS_FILE) - coreUnchanged, _ := utils.DirContentIsOlderThan(realCoreFolder, jsonPath, ".txt") - if coreUnchanged && targetCoreFolder != nil && !realCoreFolder.EqualsTo(targetCoreFolder) { - coreUnchanged, _ = utils.DirContentIsOlderThan(targetCoreFolder, jsonPath, ".txt") - } - if coreUnchanged { - return "", nil - } - } - - return "", doCleanup(buildPath) -} - -func doCleanup(buildPath *paths.Path) error { - // FIXME: this should go outside legacy and behind a `logrus` call so users can - // control when this should be printed. - // logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_BUILD_OPTIONS_CHANGED + constants.MSG_REBUILD_ALL) - - if files, err := buildPath.ReadDir(); err != nil { - return errors.WithMessage(err, tr("cleaning build path")) - } else { - for _, file := range files { - if err := file.RemoveAll(); err != nil { - return errors.WithMessage(err, tr("cleaning build path")) - } - } - } - return nil -} From 895c593d512b8a244f893c0405a5f9ddc443a5c8 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 16:18:55 +0200 Subject: [PATCH 11/34] remove fqbn from Context --- arduino/builder/build_options_manager.go | 7 +++---- arduino/builder/builder.go | 4 ++-- arduino/builder/sketch_test.go | 17 ++++++++++++++--- commands/compile/compile.go | 3 +-- legacy/builder/test/builder_test.go | 10 +++++----- .../test/store_build_options_map_test.go | 2 +- legacy/builder/types/context.go | 1 - 7 files changed, 26 insertions(+), 18 deletions(-) diff --git a/arduino/builder/build_options_manager.go b/arduino/builder/build_options_manager.go index db0aa8a5059..3079ecf9ea6 100644 --- a/arduino/builder/build_options_manager.go +++ b/arduino/builder/build_options_manager.go @@ -22,6 +22,7 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/utils" + "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" properties "github.com/arduino/go-properties-orderedmap" @@ -42,7 +43,6 @@ type BuildOptionsManager struct { buildCorePath *paths.Path sketch *sketch.Sketch customBuildProperties []string - fqbn string compilerOptimizationFlags string clean bool builderLogger *logger.BuilderLogger @@ -54,7 +54,7 @@ func NewBuildOptionsManager( builtInLibrariesDirs, buildPath *paths.Path, sketch *sketch.Sketch, customBuildProperties []string, - fqbn string, + fqbn *cores.FQBN, clean bool, compilerOptimizationFlags string, runtimePlatformPath, buildCorePath *paths.Path, @@ -66,7 +66,7 @@ func NewBuildOptionsManager( opts.Set("builtInToolsFolders", strings.Join(builtInToolsDirs.AsStrings(), ",")) opts.Set("otherLibrariesFolders", strings.Join(otherLibrariesDirs.AsStrings(), ",")) opts.SetPath("sketchLocation", sketch.FullPath) - opts.Set("fqbn", fqbn) + opts.Set("fqbn", fqbn.String()) opts.Set("customBuildProperties", strings.Join(customBuildProperties, ",")) opts.Set("compiler.optimization_flags", compilerOptimizationFlags) @@ -96,7 +96,6 @@ func NewBuildOptionsManager( buildCorePath: buildCorePath, sketch: sketch, customBuildProperties: customBuildProperties, - fqbn: fqbn, compilerOptimizationFlags: compilerOptimizationFlags, clean: clean, builderLogger: buildLogger, diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index b8802a48bfc..169d729e33a 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/arduino/arduino-cli/arduino/builder/logger" + "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" @@ -53,7 +54,6 @@ type Builder struct { hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList builtInLibrariesDirs *paths.Path - fqbn string clean bool compilerOptimizationFlags string @@ -71,7 +71,7 @@ func NewBuilder( requestBuildProperties []string, hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList, builtInLibrariesDirs *paths.Path, - fqbn string, + fqbn *cores.FQBN, clean bool, logger *logger.BuilderLogger, ) (*Builder, error) { diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index d01bc5e7a97..2b400faec54 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -21,6 +21,7 @@ import ( "strings" "testing" + "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" @@ -48,9 +49,12 @@ func TestMergeSketchSources(t *testing.T) { } mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource) + fqbn, err := cores.ParseFQBN("a:b:c") + require.NoError(t, err) + b, err := NewBuilder( sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, "", false, nil) + nil, nil, nil, nil, fqbn, false, nil) require.NoError(t, err) offset, source, err := b.sketchMergeSources(nil) @@ -64,9 +68,12 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { require.Nil(t, err) require.NotNil(t, sk) + fqbn, err := cores.ParseFQBN("a:b:c") + require.NoError(t, err) + // ensure not to include Arduino.h when it's already there b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, "", false, nil) + nil, nil, nil, nil, fqbn, false, nil) require.NoError(t, err) _, source, err := b.sketchMergeSources(nil) @@ -83,8 +90,12 @@ 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) + + fqbn, err := cores.ParseFQBN("a:b:c") + require.NoError(t, err) + b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, "", false, nil) + nil, nil, nil, nil, fqbn, false, nil) require.NoError(t, err) // copy the sketch over, create a fake main file we don't care about it diff --git a/commands/compile/compile.go b/commands/compile/compile.go index df520d8fbdc..46d4870875a 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -182,7 +182,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.TargetPackage = targetPackage builderCtx.ActualPlatform = buildPlatform builderCtx.RequiredTools = requiredTools - builderCtx.FQBN = fqbn builderCtx.ProgressCB = progressCB // FIXME: This will be redundant when arduino-builder will be part of the cli @@ -212,7 +211,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.BuiltInToolsDirs, builderCtx.OtherLibrariesDirs, builderCtx.BuiltInLibrariesDirs, - builderCtx.FQBN.String(), + fqbn, builderCtx.Clean, builderLogger, ) diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 56aba6d3d7c..4a33b8b6920 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -41,7 +41,7 @@ type skipContextPreparationStepName string const skipLibraries = skipContextPreparationStepName("libraries") -func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *paths.Path, fqbn string, skips ...skipContextPreparationStepName) *types.Context { +func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *paths.Path, fqbnString string, skips ...skipContextPreparationStepName) *types.Context { DownloadCoresAndToolsAndLibraries(t) stepToSkip := map[skipContextPreparationStepName]bool{} @@ -106,12 +106,12 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, nil, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, fqbn, ctx.Clean, builderLogger, + ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), ctx.Clean, builderLogger, ) require.NoError(t, err) - if fqbn != "" { - ctx.FQBN = parseFQBN(t, fqbn) - targetPackage, targetPlatform, targetBoard, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(ctx.FQBN) + if fqbnString != "" { + fqbn := parseFQBN(t, fqbnString) + targetPackage, targetPlatform, targetBoard, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn) require.NoError(t, err) requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) require.NoError(t, err) diff --git a/legacy/builder/test/store_build_options_map_test.go b/legacy/builder/test/store_build_options_map_test.go index 970358a2877..ad087a0c48b 100644 --- a/legacy/builder/test/store_build_options_map_test.go +++ b/legacy/builder/test/store_build_options_map_test.go @@ -39,7 +39,7 @@ func TestCheckIfBuildOptionsChanged(t *testing.T) { buildOptionsManager := builder.NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, builtInLibrariesDirs, buildPath, &sketch.Sketch{FullPath: paths.New("sketchLocation")}, []string{"custom=prop"}, - fqbn.String(), false, + fqbn, false, buildProperties.Get("compiler.optimization_flags"), buildProperties.GetPath("runtime.platform.path"), buildProperties.GetPath("build.core.path"), diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 3af0d1bd12d..31e3d941749 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -39,7 +39,6 @@ type Context struct { BuiltInToolsDirs paths.PathList BuiltInLibrariesDirs *paths.Path OtherLibrariesDirs paths.PathList - FQBN *cores.FQBN Clean bool PackageManager *packagemanager.Explorer From b4891ea559c27726a6556d513bc9d330abf694e3 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 16:24:02 +0200 Subject: [PATCH 12/34] remove clean from Context --- arduino/builder/builder.go | 9 ++------- arduino/builder/core.go | 8 ++++---- commands/compile/compile.go | 3 +-- legacy/builder/builder.go | 2 +- legacy/builder/test/builder_test.go | 4 ++-- legacy/builder/types/context.go | 1 - 6 files changed, 10 insertions(+), 17 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 169d729e33a..1644ad5b0fe 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -49,15 +49,9 @@ type Builder struct { coreBuildCachePath *paths.Path logger *logger.BuilderLogger + clean bool *BuildOptionsManager - - hardwareDirs, builtInToolsDirs, otherLibrariesDirs paths.PathList - builtInLibrariesDirs *paths.Path - clean bool - - compilerOptimizationFlags string - runtimePlatformPath, buildCorePath *paths.Path } // NewBuilder creates a sketch Builder. @@ -133,6 +127,7 @@ func NewBuilder( customBuildProperties: customBuildPropertiesArgs, coreBuildCachePath: coreBuildCachePath, logger: logger, + clean: clean, BuildOptionsManager: NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, builtInLibrariesDirs, buildPath, diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 9d3fa053eaa..4fc411235a0 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -37,7 +37,7 @@ import ( // BuildCore fixdoc func (b *Builder) BuildCore( actualPlatform *cores.PlatformRelease, - onlyUpdateCompilationDatabase, clean bool, + onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, *paths.Path, error) { @@ -58,7 +58,7 @@ func (b *Builder) BuildCore( } archiveFile, objectFiles, err := b.compileCore( - onlyUpdateCompilationDatabase, clean, + onlyUpdateCompilationDatabase, actualPlatform, compilationDatabase, progress, progressCB, @@ -71,7 +71,7 @@ func (b *Builder) BuildCore( } func (b *Builder) compileCore( - onlyUpdateCompilationDatabase, clean bool, + onlyUpdateCompilationDatabase bool, actualPlatform *cores.PlatformRelease, compilationDatabase *compilation.Database, progress *progress.Struct, progressCB rpc.TaskProgressCB, @@ -117,7 +117,7 @@ func (b *Builder) compileCore( } var canUseArchivedCore bool - if onlyUpdateCompilationDatabase || clean { + if onlyUpdateCompilationDatabase || b.clean { canUseArchivedCore = false } else if isOlder, err := utils.DirContentIsOlderThan(realCoreFolder, targetArchivedCore); err != nil || !isOlder { // Recreate the archive if ANY of the core files (including platform.txt) has changed diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 46d4870875a..6d5e293614d 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -192,7 +192,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) - builderCtx.Clean = req.GetClean() builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly() builderCtx.SourceOverride = req.GetSourceOverride() @@ -212,7 +211,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.OtherLibrariesDirs, builderCtx.BuiltInLibrariesDirs, fqbn, - builderCtx.Clean, + req.GetClean(), builderLogger, ) if err != nil { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index be668d8a21c..6feb0e4ce5f 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -134,7 +134,7 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { objectFiles, archiveFile, err := ctx.Builder.BuildCore( ctx.ActualPlatform, - ctx.OnlyUpdateCompilationDatabase, ctx.Clean, + ctx.OnlyUpdateCompilationDatabase, ctx.CompilationDatabase, &ctx.Progress, ctx.ProgressCB, ) diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 4a33b8b6920..7af31de9a53 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -106,7 +106,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, nil, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), ctx.Clean, builderLogger, + ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, builderLogger, ) require.NoError(t, err) if fqbnString != "" { @@ -119,7 +119,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, boardBuildProperties, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, fqbn, ctx.Clean, builderLogger) + ctx.BuiltInLibrariesDirs, fqbn, false, builderLogger) require.NoError(t, err) ctx.PackageManager = pme diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 31e3d941749..ed23bea3970 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -39,7 +39,6 @@ type Context struct { BuiltInToolsDirs paths.PathList BuiltInLibrariesDirs *paths.Path OtherLibrariesDirs paths.PathList - Clean bool PackageManager *packagemanager.Explorer RequiredTools []*cores.ToolRelease From 870f624b7d3f0c1497a58d874cf585055f98c4d2 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 16:32:39 +0200 Subject: [PATCH 13/34] remove unsued properties in Context --- commands/compile/compile.go | 8 ++------ legacy/builder/test/builder_test.go | 7 ++----- legacy/builder/types/context.go | 3 --- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 6d5e293614d..bb7f47a0565 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -99,7 +99,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream if err != nil { return nil, &arduino.InvalidFQBNError{Cause: err} } - targetPackage, targetPlatform, targetBoard, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn) + _, targetPlatform, targetBoard, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn) if err != nil { if targetPlatform == nil { return nil, &arduino.PlatformNotFoundError{ @@ -170,18 +170,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream coreBuildCachePath = buildCachePath.Join("core") } - requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) - if err != nil { + if _, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform); err != nil { return nil, err } builderCtx := &types.Context{} builderCtx.PackageManager = pme - builderCtx.TargetBoard = targetBoard builderCtx.TargetPlatform = targetPlatform - builderCtx.TargetPackage = targetPackage builderCtx.ActualPlatform = buildPlatform - builderCtx.RequiredTools = requiredTools builderCtx.ProgressCB = progressCB // FIXME: This will be redundant when arduino-builder will be part of the cli diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 7af31de9a53..9db68dc723d 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -111,9 +111,9 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat require.NoError(t, err) if fqbnString != "" { fqbn := parseFQBN(t, fqbnString) - targetPackage, targetPlatform, targetBoard, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn) + _, targetPlatform, _, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn) require.NoError(t, err) - requiredTools, err := pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) + _, err = pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) require.NoError(t, err) ctx.Builder, err = bldr.NewBuilder( @@ -123,11 +123,8 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat require.NoError(t, err) ctx.PackageManager = pme - ctx.TargetBoard = targetBoard ctx.TargetPlatform = targetPlatform - ctx.TargetPackage = targetPackage ctx.ActualPlatform = buildPlatform - ctx.RequiredTools = requiredTools } if !stepToSkip[skipLibraries] { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index ed23bea3970..0b7875994a7 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -41,9 +41,6 @@ type Context struct { OtherLibrariesDirs paths.PathList PackageManager *packagemanager.Explorer - RequiredTools []*cores.ToolRelease - TargetBoard *cores.Board - TargetPackage *cores.Package TargetPlatform *cores.PlatformRelease ActualPlatform *cores.PlatformRelease From a6e1ef2c34bfc1b725201c564dce273a6633d59b Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 17:00:25 +0200 Subject: [PATCH 14/34] remove sourceOverrides from Context --- arduino/builder/builder.go | 7 +++++++ arduino/builder/sketch.go | 6 +++--- arduino/builder/sketch_test.go | 6 +++--- commands/compile/compile.go | 2 +- legacy/builder/builder.go | 4 ++-- legacy/builder/test/builder_test.go | 4 ++-- legacy/builder/types/context.go | 5 ----- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 1644ad5b0fe..69776236508 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -51,6 +51,11 @@ type Builder struct { logger *logger.BuilderLogger clean bool + // Source code overrides (filename -> content map). + // The provided source data is used instead of reading it from disk. + // The keys of the map are paths relative to sketch folder. + sourceOverrides map[string]string + *BuildOptionsManager } @@ -67,6 +72,7 @@ func NewBuilder( builtInLibrariesDirs *paths.Path, fqbn *cores.FQBN, clean bool, + sourceOverrides map[string]string, logger *logger.BuilderLogger, ) (*Builder, error) { buildProperties := properties.NewMap() @@ -128,6 +134,7 @@ func NewBuilder( coreBuildCachePath: coreBuildCachePath, logger: logger, clean: clean, + sourceOverrides: sourceOverrides, BuildOptionsManager: NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, builtInLibrariesDirs, buildPath, diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index 422c3fd345a..4bdf6415f46 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -48,12 +48,12 @@ func (b *Builder) Sketch() *sketch.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). -func (b *Builder) PrepareSketchBuildPath(sourceOverrides map[string]string) (int, error) { +func (b *Builder) PrepareSketchBuildPath() (int, error) { if err := b.sketchBuildPath.MkdirAll(); err != nil { return 0, errors.Wrap(err, tr("unable to create a folder to save the sketch")) } - offset, mergedSource, err := b.sketchMergeSources(sourceOverrides) + offset, mergedSource, err := b.sketchMergeSources(b.sourceOverrides) if err != nil { return 0, err } @@ -63,7 +63,7 @@ func (b *Builder) PrepareSketchBuildPath(sourceOverrides map[string]string) (int return 0, err } - if err := b.sketchCopyAdditionalFiles(b.sketchBuildPath, sourceOverrides); err != nil { + if err := b.sketchCopyAdditionalFiles(b.sketchBuildPath, b.sourceOverrides); err != nil { return 0, err } diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index 2b400faec54..f7e8cc1d5b6 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -54,7 +54,7 @@ func TestMergeSketchSources(t *testing.T) { b, err := NewBuilder( sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil) + nil, nil, nil, nil, fqbn, false, nil, nil) require.NoError(t, err) offset, source, err := b.sketchMergeSources(nil) @@ -73,7 +73,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { // ensure not to include Arduino.h when it's already there b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil) + nil, nil, nil, nil, fqbn, false, nil, nil) require.NoError(t, err) _, source, err := b.sketchMergeSources(nil) @@ -95,7 +95,7 @@ func TestCopyAdditionalFiles(t *testing.T) { require.NoError(t, err) b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil) + nil, nil, nil, nil, fqbn, false, nil, nil) require.NoError(t, err) // copy the sketch over, create a fake main file we don't care about it diff --git a/commands/compile/compile.go b/commands/compile/compile.go index bb7f47a0565..96e9165a9d3 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -189,7 +189,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly() - builderCtx.SourceOverride = req.GetSourceOverride() builderLogger := logger.New(outStream, errStream, req.GetVerbose(), req.GetWarnings()) builderCtx.BuilderLogger = builderLogger @@ -208,6 +207,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.BuiltInLibrariesDirs, fqbn, req.GetClean(), + req.GetSourceOverride(), builderLogger, ) if err != nil { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 6feb0e4ce5f..11bf2457900 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -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.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath() return _err }), @@ -315,7 +315,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.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath() return _err }), diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 9db68dc723d..e6cfe93cf1b 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -106,7 +106,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, nil, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, builderLogger, + ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, nil, builderLogger, ) require.NoError(t, err) if fqbnString != "" { @@ -119,7 +119,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, boardBuildProperties, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, fqbn, false, builderLogger) + ctx.BuiltInLibrariesDirs, fqbn, false, nil, builderLogger) require.NoError(t, err) ctx.PackageManager = pme diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 0b7875994a7..c241d02997a 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -64,11 +64,6 @@ type Context struct { CompilationDatabase *compilation.Database // Set to true to skip build and produce only Compilation Database OnlyUpdateCompilationDatabase bool - - // Source code overrides (filename -> content map). - // The provided source data is used instead of reading it from disk. - // The keys of the map are paths relative to sketch folder. - SourceOverride map[string]string } func (ctx *Context) PushProgress() { From 776ed23aa6379b95673dd048443192540b09e2d7 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 17:03:59 +0200 Subject: [PATCH 15/34] make SketchBuilder a method recevier of arduino/builder --- arduino/builder/sketch.go | 26 ++++++++++---------------- legacy/builder/builder.go | 6 +----- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index 4bdf6415f46..675197d21f9 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -22,7 +22,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" - "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/sketch" @@ -30,7 +29,6 @@ import ( f "github.com/arduino/arduino-cli/internal/algorithms" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-paths-helper" - "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) @@ -179,29 +177,25 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error { return nil } -// SketchBuilder fixdoc -func SketchBuilder( - sketchBuildPath *paths.Path, - buildProperties *properties.Map, +// BuildSketch fixdoc +func (b *Builder) BuildSketch( includesFolders paths.PathList, onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, - jobs int, - builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) - if err := sketchBuildPath.MkdirAll(); err != nil { + if err := b.sketchBuildPath.MkdirAll(); err != nil { return nil, errors.WithStack(err) } sketchObjectFiles, err := utils.CompileFiles( - sketchBuildPath, sketchBuildPath, buildProperties, includes, + b.sketchBuildPath, b.sketchBuildPath, b.buildProperties, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, - builderLogger, + b.jobs, + b.builderLogger, progress, progressCB, ) if err != nil { @@ -209,14 +203,14 @@ func SketchBuilder( } // The "src/" subdirectory of a sketch is compiled recursively - sketchSrcPath := sketchBuildPath.Join("src") + sketchSrcPath := b.sketchBuildPath.Join("src") if sketchSrcPath.IsDir() { srcObjectFiles, err := utils.CompileFilesRecursive( - sketchSrcPath, sketchSrcPath, buildProperties, includes, + sketchSrcPath, sketchSrcPath, b.buildProperties, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, - builderLogger, + b.jobs, + b.builderLogger, progress, progressCB, ) if err != nil { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 11bf2457900..4acf2199c2f 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -70,14 +70,10 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - sketchObjectFiles, err := builder.SketchBuilder( - ctx.Builder.GetSketchBuildPath(), - ctx.Builder.GetBuildProperties(), + sketchObjectFiles, err := ctx.Builder.BuildSketch( ctx.SketchLibrariesDetector.IncludeFolders(), ctx.OnlyUpdateCompilationDatabase, ctx.CompilationDatabase, - ctx.Builder.Jobs(), - ctx.BuilderLogger, &ctx.Progress, ctx.ProgressCB, ) if err != nil { From 93a9cfa30fe0e0c57a2171959a6d0488fcbd9ccb Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 17:13:38 +0200 Subject: [PATCH 16/34] make BuildLibraries a method recevier of arduino/builder --- arduino/builder/libraries.go | 95 +++++++++++++++--------------------- legacy/builder/builder.go | 6 +-- 2 files changed, 41 insertions(+), 60 deletions(-) diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index af2e899840d..8308d55295d 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -20,7 +20,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" - "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/libraries" @@ -37,31 +36,25 @@ var ( FpuCflag = "fpu" ) -// LibrariesBuilder fixdoc -func LibrariesBuilder( - librariesBuildPath *paths.Path, - buildProperties *properties.Map, +// BuildLibraries fixdoc +func (b *Builder) BuildLibraries( includesFolders paths.PathList, importedLibraries libraries.List, onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, - jobs int, - builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) libs := importedLibraries - if err := librariesBuildPath.MkdirAll(); err != nil { + if err := b.librariesBuildPath.MkdirAll(); err != nil { return nil, errors.WithStack(err) } - librariesObjectFiles, err := compileLibraries( - libs, librariesBuildPath, buildProperties, includes, + librariesObjectFiles, err := b.compileLibraries( + libs, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, - builderLogger, progress, progressCB, ) if err != nil { @@ -79,10 +72,9 @@ func directoryContainsFile(folder *paths.Path) bool { return false } -func findExpectedPrecompiledLibFolder( +func (b *Builder) findExpectedPrecompiledLibFolder( library *libraries.Library, buildProperties *properties.Map, - builderLogger *logger.BuilderLogger, ) *paths.Path { mcu := buildProperties.Get("build.mcu") // Add fpu specifications if they exist @@ -109,34 +101,32 @@ func findExpectedPrecompiledLibFolder( } } - builderLogger.Info(tr("Library %[1]s has been declared precompiled:", library.Name)) + b.logger.Info(tr("Library %[1]s has been declared precompiled:", library.Name)) // Try directory with full fpuSpecs first, if available if len(fpuSpecs) > 0 { fpuSpecs = strings.TrimRight(fpuSpecs, "-") fullPrecompDir := library.SourceDir.Join(mcu).Join(fpuSpecs) if fullPrecompDir.Exist() && directoryContainsFile(fullPrecompDir) { - builderLogger.Info(tr("Using precompiled library in %[1]s", fullPrecompDir)) + b.logger.Info(tr("Using precompiled library in %[1]s", fullPrecompDir)) return fullPrecompDir } - builderLogger.Info(tr(`Precompiled library in "%[1]s" not found`, fullPrecompDir)) + b.logger.Info(tr(`Precompiled library in "%[1]s" not found`, fullPrecompDir)) } precompDir := library.SourceDir.Join(mcu) if precompDir.Exist() && directoryContainsFile(precompDir) { - builderLogger.Info(tr("Using precompiled library in %[1]s", precompDir)) + b.logger.Info(tr("Using precompiled library in %[1]s", precompDir)) return precompDir } - builderLogger.Info(tr(`Precompiled library in "%[1]s" not found`, precompDir)) + b.logger.Info(tr(`Precompiled library in "%[1]s" not found`, precompDir)) return nil } -func compileLibraries( - libraries libraries.List, buildPath *paths.Path, buildProperties *properties.Map, includes []string, +func (b *Builder) compileLibraries( + libraries libraries.List, includes []string, onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, - jobs int, - builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { progress.AddSubSteps(len(libraries)) @@ -144,12 +134,10 @@ func compileLibraries( objectFiles := paths.NewPathList() for _, library := range libraries { - libraryObjectFiles, err := compileLibrary( - library, buildPath, buildProperties, includes, + libraryObjectFiles, err := b.compileLibrary( + library, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, - builderLogger, progress, progressCB, ) if err != nil { @@ -170,18 +158,16 @@ func compileLibraries( return objectFiles, nil } -func compileLibrary( - library *libraries.Library, buildPath *paths.Path, buildProperties *properties.Map, includes []string, +func (b *Builder) compileLibrary( + library *libraries.Library, includes []string, onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, - jobs int, - builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { - if builderLogger.Verbose() { - builderLogger.Info(tr(`Compiling library "%[1]s"`, library.Name)) + if b.logger.Verbose() { + b.logger.Info(tr(`Compiling library "%[1]s"`, library.Name)) } - libraryBuildPath := buildPath.Join(library.DirName) + libraryBuildPath := b.librariesBuildPath.Join(library.DirName) if err := libraryBuildPath.MkdirAll(); err != nil { return nil, errors.WithStack(err) @@ -190,15 +176,14 @@ func compileLibrary( objectFiles := paths.NewPathList() if library.Precompiled { - coreSupportPrecompiled := buildProperties.ContainsKey("compiler.libraries.ldflags") - precompiledPath := findExpectedPrecompiledLibFolder( + coreSupportPrecompiled := b.buildProperties.ContainsKey("compiler.libraries.ldflags") + precompiledPath := b.findExpectedPrecompiledLibFolder( library, - buildProperties, - builderLogger, + b.buildProperties, ) if !coreSupportPrecompiled { - builderLogger.Info(tr("The platform does not support '%[1]s' for precompiled libraries.", "compiler.libraries.ldflags")) + b.logger.Info(tr("The platform does not support '%[1]s' for precompiled libraries.", "compiler.libraries.ldflags")) } else if precompiledPath != nil { // Find all libraries in precompiledPath libs, err := precompiledPath.ReadDir() @@ -217,8 +202,8 @@ func compileLibrary( } } - currLDFlags := buildProperties.Get("compiler.libraries.ldflags") - buildProperties.Set("compiler.libraries.ldflags", currLDFlags+" \"-L"+precompiledPath.String()+"\" "+libsCmd+" ") + currLDFlags := b.buildProperties.Get("compiler.libraries.ldflags") + b.buildProperties.Set("compiler.libraries.ldflags", currLDFlags+" \"-L"+precompiledPath.String()+"\" "+libsCmd+" ") // TODO: This codepath is just taken for .a with unusual names that would // be ignored by -L / -l methods. @@ -239,11 +224,11 @@ func compileLibrary( if library.Layout == libraries.RecursiveLayout { libObjectFiles, err := utils.CompileFilesRecursive( - library.SourceDir, libraryBuildPath, buildProperties, includes, + library.SourceDir, libraryBuildPath, b.buildProperties, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, - builderLogger, + b.jobs, + b.logger, progress, progressCB, ) if err != nil { @@ -251,12 +236,12 @@ func compileLibrary( } if library.DotALinkage { archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( - libraryBuildPath, paths.New(library.DirName+".a"), libObjectFiles, buildProperties, - onlyUpdateCompilationDatabase, builderLogger.Verbose(), - builderLogger.Stdout(), builderLogger.Stderr(), + libraryBuildPath, paths.New(library.DirName+".a"), libObjectFiles, b.buildProperties, + onlyUpdateCompilationDatabase, b.logger.Verbose(), + b.logger.Stdout(), b.logger.Stderr(), ) - if builderLogger.Verbose() { - builderLogger.Info(string(verboseInfo)) + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } if err != nil { return nil, errors.WithStack(err) @@ -270,11 +255,11 @@ func compileLibrary( includes = append(includes, cpp.WrapWithHyphenI(library.UtilityDir.String())) } libObjectFiles, err := utils.CompileFiles( - library.SourceDir, libraryBuildPath, buildProperties, includes, + library.SourceDir, libraryBuildPath, b.buildProperties, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, - builderLogger, + b.jobs, + b.logger, progress, progressCB, ) if err != nil { @@ -285,11 +270,11 @@ func compileLibrary( if library.UtilityDir != nil { utilityBuildPath := libraryBuildPath.Join("utility") utilityObjectFiles, err := utils.CompileFiles( - library.UtilityDir, utilityBuildPath, buildProperties, includes, + library.UtilityDir, utilityBuildPath, b.buildProperties, includes, onlyUpdateCompilationDatabase, compilationDatabase, - jobs, - builderLogger, + b.jobs, + b.logger, progress, progressCB, ) if err != nil { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 4acf2199c2f..fb5e71f79e7 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -100,15 +100,11 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - librariesObjectFiles, err := builder.LibrariesBuilder( - ctx.Builder.GetLibrariesBuildPath(), - ctx.Builder.GetBuildProperties(), + librariesObjectFiles, err := ctx.Builder.BuildLibraries( ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries(), ctx.OnlyUpdateCompilationDatabase, ctx.CompilationDatabase, - ctx.Builder.Jobs(), - ctx.BuilderLogger, &ctx.Progress, ctx.ProgressCB, ) if err != nil { From e9655990ca1e393145dbcf02780ea73501a2f901 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 17:22:40 +0200 Subject: [PATCH 17/34] make RunRecipe a method recevier of arduino/builder --- .../builder/recipe.go | 18 ++++++++---------- commands/compile/compile.go | 7 ++----- legacy/builder/builder.go | 4 +--- 3 files changed, 11 insertions(+), 18 deletions(-) rename legacy/builder/recipe_runner.go => arduino/builder/recipe.go (78%) diff --git a/legacy/builder/recipe_runner.go b/arduino/builder/recipe.go similarity index 78% rename from legacy/builder/recipe_runner.go rename to arduino/builder/recipe.go index 8af310bd4d3..7c26fd6101a 100644 --- a/legacy/builder/recipe_runner.go +++ b/arduino/builder/recipe.go @@ -20,23 +20,21 @@ import ( "sort" "strings" - "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/utils" properties "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) -func RecipeByPrefixSuffixRunner( +// RunRecipe fixdoc +func (b *Builder) RunRecipe( prefix, suffix string, skipIfOnlyUpdatingCompilationDatabase, onlyUpdateCompilationDatabase bool, - buildProps *properties.Map, - builderLogger *logger.BuilderLogger, ) error { logrus.Debugf(fmt.Sprintf("Looking for recipes like %s", prefix+"*"+suffix)) // TODO is it necessary to use Clone? - buildProperties := buildProps.Clone() + buildProperties := b.buildProperties.Clone() recipes := findRecipes(buildProperties, prefix, suffix) // TODO is it necessary to use Clone? @@ -50,15 +48,15 @@ func RecipeByPrefixSuffixRunner( } if onlyUpdateCompilationDatabase && skipIfOnlyUpdatingCompilationDatabase { - if builderLogger.Verbose() { - builderLogger.Info(tr("Skipping: %[1]s", strings.Join(command.GetArgs(), " "))) + if b.logger.Verbose() { + b.logger.Info(tr("Skipping: %[1]s", strings.Join(command.GetArgs(), " "))) } return nil } - verboseInfo, _, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */) - if builderLogger.Verbose() { - builderLogger.Info(string(verboseInfo)) + verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */) + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } if err != nil { return errors.WithStack(err) diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 96e9165a9d3..5eee0981eeb 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -339,11 +339,9 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream exportBinaries = false } if exportBinaries { - err := builder.RecipeByPrefixSuffixRunner( + err := sketchBuilder.RunRecipe( "recipe.hooks.savehex.presavehex", ".pattern", false, builderCtx.OnlyUpdateCompilationDatabase, - sketchBuilder.GetBuildProperties(), - builderLogger, ) if err != nil { return r, err @@ -383,10 +381,9 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream } } - err = builder.RecipeByPrefixSuffixRunner( + err = sketchBuilder.RunRecipe( "recipe.hooks.savehex.postsavehex", ".pattern", false, builderCtx.OnlyUpdateCompilationDatabase, - sketchBuilder.GetBuildProperties(), builderLogger, ) if err != nil { return r, err diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index fb5e71f79e7..ad7ab116362 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -390,11 +390,9 @@ func logIfVerbose(warn bool, msg string) types.BareCommand { } func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipIfOnlyUpdatingCompilationDatabase bool) error { - return RecipeByPrefixSuffixRunner( + return ctx.Builder.RunRecipe( prefix, suffix, skipIfOnlyUpdatingCompilationDatabase, ctx.OnlyUpdateCompilationDatabase, - ctx.Builder.GetBuildProperties(), - ctx.BuilderLogger, ) } From f1fdf1754f64f5ca61fab92d1fc28f46b405020f Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 17:42:10 +0200 Subject: [PATCH 18/34] make RemoveUnusedCompiledLibraries a method recevier of arduino/builder --- arduino/builder/libraries.go | 34 ++++ legacy/builder/builder.go | 3 +- .../unused_compiled_libraries_remover_test.go | 163 +++++++++--------- .../unused_compiled_libraries_remover.go | 55 ------ 4 files changed, 112 insertions(+), 143 deletions(-) delete mode 100644 legacy/builder/unused_compiled_libraries_remover.go diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index 8308d55295d..445dcbd46af 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -28,6 +28,7 @@ import ( "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" + "golang.org/x/exp/slices" ) // nolint @@ -286,3 +287,36 @@ func (b *Builder) compileLibrary( return objectFiles, nil } + +// RemoveUnusedCompiledLibraries fixdoc +func (b *Builder) RemoveUnusedCompiledLibraries(importedLibraries libraries.List) error { + if b.librariesBuildPath.NotExist() { + return nil + } + + toLibraryNames := func(libraries []*libraries.Library) []string { + libraryNames := []string{} + for _, library := range libraries { + libraryNames = append(libraryNames, library.Name) + } + return libraryNames + } + + files, err := b.librariesBuildPath.ReadDir() + if err != nil { + return errors.WithStack(err) + } + + libraryNames := toLibraryNames(importedLibraries) + for _, file := range files { + if file.IsDir() { + if !slices.Contains(libraryNames, file.Base()) { + if err := file.RemoveAll(); err != nil { + return errors.WithStack(err) + } + } + } + } + + return nil +} diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index ad7ab116362..83a757db73d 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -93,8 +93,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - return UnusedCompiledLibrariesRemover( - ctx.Builder.GetLibrariesBuildPath(), + return ctx.Builder.RemoveUnusedCompiledLibraries( ctx.SketchLibrariesDetector.ImportedLibraries(), ) }), diff --git a/legacy/builder/test/unused_compiled_libraries_remover_test.go b/legacy/builder/test/unused_compiled_libraries_remover_test.go index f0d9facacb3..a9e122195ca 100644 --- a/legacy/builder/test/unused_compiled_libraries_remover_test.go +++ b/legacy/builder/test/unused_compiled_libraries_remover_test.go @@ -15,90 +15,81 @@ package test -import ( - "testing" +// TODO to be ported in the E2E suite - "github.com/arduino/arduino-cli/arduino/builder/detector" - "github.com/arduino/arduino-cli/arduino/builder/logger" - "github.com/arduino/arduino-cli/arduino/libraries" - "github.com/arduino/arduino-cli/legacy/builder" - paths "github.com/arduino/go-paths-helper" - "github.com/stretchr/testify/require" -) - -func TestUnusedCompiledLibrariesRemover(t *testing.T) { - temp, err := paths.MkTempDir("", "test") - require.NoError(t, err) - defer temp.RemoveAll() - - require.NoError(t, temp.Join("SPI").MkdirAll()) - require.NoError(t, temp.Join("Bridge").MkdirAll()) - require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) - - librariesBuildPath := temp - sketchLibrariesDetector := detector.NewSketchLibrariesDetector( - nil, nil, false, false, logger.New(nil, nil, false, ""), - ) - sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) - - err = builder.UnusedCompiledLibrariesRemover( - librariesBuildPath, - sketchLibrariesDetector.ImportedLibraries(), - ) - require.NoError(t, err) - - exist, err := temp.Join("SPI").ExistCheck() - require.NoError(t, err) - require.False(t, exist) - exist, err = temp.Join("Bridge").ExistCheck() - require.NoError(t, err) - require.True(t, exist) - exist, err = temp.Join("dummy_file").ExistCheck() - require.NoError(t, err) - require.True(t, exist) -} - -func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { - librariesBuildPath := paths.TempDir().Join("test") - sketchLibrariesDetector := detector.NewSketchLibrariesDetector( - nil, nil, false, false, logger.New(nil, nil, false, ""), - ) - sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) - - err := builder.UnusedCompiledLibrariesRemover( - librariesBuildPath, - sketchLibrariesDetector.ImportedLibraries(), - ) - require.NoError(t, err) -} - -func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { - temp, err := paths.MkTempDir("", "test") - require.NoError(t, err) - defer temp.RemoveAll() - - require.NoError(t, temp.Join("SPI").MkdirAll()) - require.NoError(t, temp.Join("Bridge").MkdirAll()) - require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) - - sketchLibrariesDetector := detector.NewSketchLibrariesDetector( - nil, nil, false, false, logger.New(nil, nil, false, ""), - ) - librariesBuildPath := temp - - err = builder.UnusedCompiledLibrariesRemover( - librariesBuildPath, - sketchLibrariesDetector.ImportedLibraries(), - ) - require.NoError(t, err) - - exist, err := temp.Join("SPI").ExistCheck() - require.NoError(t, err) - require.False(t, exist) - exist, err = temp.Join("Bridge").ExistCheck() - require.NoError(t, err) - require.False(t, exist) - exist, err = temp.Join("dummy_file").ExistCheck() - require.NoError(t, err) - require.True(t, exist) -} +//func TestUnusedCompiledLibrariesRemover(t *testing.T) { +// temp, err := paths.MkTempDir("", "test") +// require.NoError(t, err) +// defer temp.RemoveAll() +// +// require.NoError(t, temp.Join("SPI").MkdirAll()) +// require.NoError(t, temp.Join("Bridge").MkdirAll()) +// require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) +// +// librariesBuildPath := temp +// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( +// nil, nil, false, false, logger.New(nil, nil, false, ""), +// ) +// sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) +// +// err = builder.UnusedCompiledLibrariesRemover( +// librariesBuildPath, +// sketchLibrariesDetector.ImportedLibraries(), +// ) +// require.NoError(t, err) +// +// exist, err := temp.Join("SPI").ExistCheck() +// require.NoError(t, err) +// require.False(t, exist) +// exist, err = temp.Join("Bridge").ExistCheck() +// require.NoError(t, err) +// require.True(t, exist) +// exist, err = temp.Join("dummy_file").ExistCheck() +// require.NoError(t, err) +// require.True(t, exist) +//} +// +//func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { +// librariesBuildPath := paths.TempDir().Join("test") +// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( +// nil, nil, false, false, logger.New(nil, nil, false, ""), +// ) +// sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) +// +// err := builder.UnusedCompiledLibrariesRemover( +// librariesBuildPath, +// sketchLibrariesDetector.ImportedLibraries(), +// ) +// require.NoError(t, err) +//} +// +//func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { +// temp, err := paths.MkTempDir("", "test") +// require.NoError(t, err) +// defer temp.RemoveAll() +// +// require.NoError(t, temp.Join("SPI").MkdirAll()) +// require.NoError(t, temp.Join("Bridge").MkdirAll()) +// require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) +// +// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( +// nil, nil, false, false, logger.New(nil, nil, false, ""), +// ) +// librariesBuildPath := temp +// +// err = builder.UnusedCompiledLibrariesRemover( +// librariesBuildPath, +// sketchLibrariesDetector.ImportedLibraries(), +// ) +// require.NoError(t, err) +// +// exist, err := temp.Join("SPI").ExistCheck() +// require.NoError(t, err) +// require.False(t, exist) +// exist, err = temp.Join("Bridge").ExistCheck() +// require.NoError(t, err) +// require.False(t, exist) +// exist, err = temp.Join("dummy_file").ExistCheck() +// require.NoError(t, err) +// require.True(t, exist) +//} diff --git a/legacy/builder/unused_compiled_libraries_remover.go b/legacy/builder/unused_compiled_libraries_remover.go deleted file mode 100644 index cea88bd9db6..00000000000 --- a/legacy/builder/unused_compiled_libraries_remover.go +++ /dev/null @@ -1,55 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "github.com/arduino/arduino-cli/arduino/libraries" - "github.com/arduino/go-paths-helper" - "github.com/pkg/errors" - "golang.org/x/exp/slices" -) - -func UnusedCompiledLibrariesRemover(librariesBuildPath *paths.Path, importedLibraries libraries.List) error { - if librariesBuildPath.NotExist() { - return nil - } - - files, err := librariesBuildPath.ReadDir() - if err != nil { - return errors.WithStack(err) - } - - libraryNames := toLibraryNames(importedLibraries) - for _, file := range files { - if file.IsDir() { - if !slices.Contains(libraryNames, file.Base()) { - if err := file.RemoveAll(); err != nil { - return errors.WithStack(err) - } - } - } - } - - return nil -} - -func toLibraryNames(libraries []*libraries.Library) []string { - libraryNames := []string{} - for _, library := range libraries { - libraryNames = append(libraryNames, library.Name) - } - return libraryNames -} From 41b47b7d370c8bc1d4cd814a64cbada8867356dd Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 17:54:12 +0200 Subject: [PATCH 19/34] make MergeSketchWithBootloader a method recevier of arduino/builder --- arduino/builder/sketch.go | 129 ++++++++++++++ legacy/builder/builder.go | 6 +- .../builder/merge_sketch_with_bootloader.go | 160 ------------------ .../test/merge_sketch_with_bootloader_test.go | 30 +--- 4 files changed, 134 insertions(+), 191 deletions(-) delete mode 100644 legacy/builder/merge_sketch_with_bootloader.go diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index 675197d21f9..60d6c6bb84c 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -18,7 +18,10 @@ package builder import ( "bytes" "fmt" + "math" "regexp" + "strconv" + "strings" "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" @@ -29,6 +32,7 @@ import ( f "github.com/arduino/arduino-cli/internal/algorithms" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-paths-helper" + "github.com/marcinbor85/gohex" "github.com/pkg/errors" ) @@ -221,3 +225,128 @@ func (b *Builder) BuildSketch( return sketchObjectFiles, nil } + +// MergeSketchWithBootloader fixdoc +func (b *Builder) MergeSketchWithBootloader(onlyUpdateCompilationDatabase bool) error { + if onlyUpdateCompilationDatabase { + return nil + } + + if !b.buildProperties.ContainsKey("bootloader.noblink") && !b.buildProperties.ContainsKey("bootloader.file") { + return nil + } + + sketchFileName := b.sketch.MainFile.Base() + sketchInBuildPath := b.buildPath.Join(sketchFileName + ".hex") + sketchInSubfolder := b.buildPath.Join("sketch", sketchFileName+".hex") + + var builtSketchPath *paths.Path + if sketchInBuildPath.Exist() { + builtSketchPath = sketchInBuildPath + } else if sketchInSubfolder.Exist() { + builtSketchPath = sketchInSubfolder + } else { + return nil + } + + bootloader := "" + if bootloaderNoBlink, ok := b.buildProperties.GetOk("bootloader.noblink"); ok { + bootloader = bootloaderNoBlink + } else { + bootloader = b.buildProperties.Get("bootloader.file") + } + bootloader = b.buildProperties.ExpandPropsInString(bootloader) + + bootloaderPath := b.buildProperties.GetPath("runtime.platform.path").Join("bootloaders", bootloader) + if bootloaderPath.NotExist() { + if b.logger.Verbose() { + b.logger.Warn(tr("Bootloader file specified but missing: %[1]s", bootloaderPath)) + } + return nil + } + + mergedSketchPath := builtSketchPath.Parent().Join(sketchFileName + ".with_bootloader.hex") + + // Ignore merger errors for the first iteration + maximumBinSize := 16000000 + if uploadMaxSize, ok := b.buildProperties.GetOk("upload.maximum_size"); ok { + maximumBinSize, _ = strconv.Atoi(uploadMaxSize) + maximumBinSize *= 2 + } + err := merge(builtSketchPath, bootloaderPath, mergedSketchPath, maximumBinSize) + if err != nil && b.logger.Verbose() { + b.logger.Info(err.Error()) + } + + return nil +} + +func merge(builtSketchPath, bootloaderPath, mergedSketchPath *paths.Path, maximumBinSize int) error { + if bootloaderPath.Ext() == ".bin" { + bootloaderPath = paths.New(strings.TrimSuffix(bootloaderPath.String(), ".bin") + ".hex") + } + + memBoot := gohex.NewMemory() + if bootFile, err := bootloaderPath.Open(); err == nil { + defer bootFile.Close() + if err := memBoot.ParseIntelHex(bootFile); err != nil { + return errors.New(bootFile.Name() + " " + err.Error()) + } + } else { + return err + } + + memSketch := gohex.NewMemory() + if buildFile, err := builtSketchPath.Open(); err == nil { + defer buildFile.Close() + if err := memSketch.ParseIntelHex(buildFile); err != nil { + return errors.New(buildFile.Name() + " " + err.Error()) + } + } else { + return err + } + + memMerged := gohex.NewMemory() + initialAddress := uint32(math.MaxUint32) + lastAddress := uint32(0) + + for _, segment := range memBoot.GetDataSegments() { + if err := memMerged.AddBinary(segment.Address, segment.Data); err != nil { + continue + } + if segment.Address < initialAddress { + initialAddress = segment.Address + } + if segment.Address+uint32(len(segment.Data)) > lastAddress { + lastAddress = segment.Address + uint32(len(segment.Data)) + } + } + for _, segment := range memSketch.GetDataSegments() { + if err := memMerged.AddBinary(segment.Address, segment.Data); err != nil { + continue + } + if segment.Address < initialAddress { + initialAddress = segment.Address + } + if segment.Address+uint32(len(segment.Data)) > lastAddress { + lastAddress = segment.Address + uint32(len(segment.Data)) + } + } + + if mergeFile, err := mergedSketchPath.Create(); err == nil { + defer mergeFile.Close() + memMerged.DumpIntelHex(mergeFile, 16) + } else { + return err + } + + // Write out a .bin if the addresses doesn't go too far away from origin + // (and consequently produce a very large bin) + size := lastAddress - initialAddress + if size > uint32(maximumBinSize) { + return nil + } + mergedSketchPathBin := paths.New(strings.TrimSuffix(mergedSketchPath.String(), ".hex") + ".bin") + data := memMerged.ToBinary(initialAddress, size, 0xFF) + return mergedSketchPathBin.WriteFile(data) +} diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 83a757db73d..5abe9bf3294 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -177,11 +177,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - return MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, - ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), - ctx.BuilderLogger, - ) + return ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) }), types.BareCommand(func(ctx *types.Context) error { diff --git a/legacy/builder/merge_sketch_with_bootloader.go b/legacy/builder/merge_sketch_with_bootloader.go deleted file mode 100644 index 7d77204de33..00000000000 --- a/legacy/builder/merge_sketch_with_bootloader.go +++ /dev/null @@ -1,160 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "math" - "strconv" - "strings" - - "github.com/arduino/arduino-cli/arduino/builder/logger" - "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/arduino-cli/legacy/builder/constants" - "github.com/arduino/go-paths-helper" - "github.com/arduino/go-properties-orderedmap" - "github.com/marcinbor85/gohex" - "github.com/pkg/errors" -) - -func MergeSketchWithBootloader( - onlyUpdateCompilationDatabase bool, - buildPath *paths.Path, - sketch *sketch.Sketch, - buildProperties *properties.Map, - builderLogger *logger.BuilderLogger, -) error { - if onlyUpdateCompilationDatabase { - return nil - } - - if !buildProperties.ContainsKey(constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK) && !buildProperties.ContainsKey(constants.BUILD_PROPERTIES_BOOTLOADER_FILE) { - return nil - } - - sketchFileName := sketch.MainFile.Base() - sketchInBuildPath := buildPath.Join(sketchFileName + ".hex") - sketchInSubfolder := buildPath.Join(constants.FOLDER_SKETCH, sketchFileName+".hex") - - var builtSketchPath *paths.Path - if sketchInBuildPath.Exist() { - builtSketchPath = sketchInBuildPath - } else if sketchInSubfolder.Exist() { - builtSketchPath = sketchInSubfolder - } else { - return nil - } - - bootloader := "" - if bootloaderNoBlink, ok := buildProperties.GetOk(constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK); ok { - bootloader = bootloaderNoBlink - } else { - bootloader = buildProperties.Get(constants.BUILD_PROPERTIES_BOOTLOADER_FILE) - } - bootloader = buildProperties.ExpandPropsInString(bootloader) - - bootloaderPath := buildProperties.GetPath("runtime.platform.path").Join(constants.FOLDER_BOOTLOADERS, bootloader) - if bootloaderPath.NotExist() { - if builderLogger.Verbose() { - builderLogger.Warn(tr("Bootloader file specified but missing: %[1]s", bootloaderPath)) - } - return nil - } - - mergedSketchPath := builtSketchPath.Parent().Join(sketchFileName + ".with_bootloader.hex") - - // Ignore merger errors for the first iteration - maximumBinSize := 16000000 - if uploadMaxSize, ok := buildProperties.GetOk("upload.maximum_size"); ok { - maximumBinSize, _ = strconv.Atoi(uploadMaxSize) - maximumBinSize *= 2 - } - err := merge(builtSketchPath, bootloaderPath, mergedSketchPath, maximumBinSize) - if err != nil && builderLogger.Verbose() { - builderLogger.Info(err.Error()) - } - - return nil -} - -func merge(builtSketchPath, bootloaderPath, mergedSketchPath *paths.Path, maximumBinSize int) error { - if bootloaderPath.Ext() == ".bin" { - bootloaderPath = paths.New(strings.TrimSuffix(bootloaderPath.String(), ".bin") + ".hex") - } - - memBoot := gohex.NewMemory() - if bootFile, err := bootloaderPath.Open(); err == nil { - defer bootFile.Close() - if err := memBoot.ParseIntelHex(bootFile); err != nil { - return errors.New(bootFile.Name() + " " + err.Error()) - } - } else { - return err - } - - memSketch := gohex.NewMemory() - if buildFile, err := builtSketchPath.Open(); err == nil { - defer buildFile.Close() - if err := memSketch.ParseIntelHex(buildFile); err != nil { - return errors.New(buildFile.Name() + " " + err.Error()) - } - } else { - return err - } - - memMerged := gohex.NewMemory() - initialAddress := uint32(math.MaxUint32) - lastAddress := uint32(0) - - for _, segment := range memBoot.GetDataSegments() { - if err := memMerged.AddBinary(segment.Address, segment.Data); err != nil { - continue - } - if segment.Address < initialAddress { - initialAddress = segment.Address - } - if segment.Address+uint32(len(segment.Data)) > lastAddress { - lastAddress = segment.Address + uint32(len(segment.Data)) - } - } - for _, segment := range memSketch.GetDataSegments() { - if err := memMerged.AddBinary(segment.Address, segment.Data); err != nil { - continue - } - if segment.Address < initialAddress { - initialAddress = segment.Address - } - if segment.Address+uint32(len(segment.Data)) > lastAddress { - lastAddress = segment.Address + uint32(len(segment.Data)) - } - } - - if mergeFile, err := mergedSketchPath.Create(); err == nil { - defer mergeFile.Close() - memMerged.DumpIntelHex(mergeFile, 16) - } else { - return err - } - - // Write out a .bin if the addresses doesn't go too far away from origin - // (and consequently produce a very large bin) - size := lastAddress - initialAddress - if size > uint32(maximumBinSize) { - return nil - } - mergedSketchPathBin := paths.New(strings.TrimSuffix(mergedSketchPath.String(), ".hex") + ".bin") - data := memMerged.ToBinary(initialAddress, size, 0xFF) - return mergedSketchPathBin.WriteFile(data) -} diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index dc3ceba3392..0478641d241 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -21,8 +21,6 @@ import ( "strings" "testing" - "github.com/arduino/arduino-cli/arduino/builder/logger" - "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" @@ -70,12 +68,7 @@ func TestMergeSketchWithBootloader(t *testing.T) { err = buildPath.Join("sketch", "sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) require.NoError(t, err) - builderLogger := logger.New(nil, nil, false, "") - err = builder.MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, - ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), - builderLogger, - ) + err = ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) require.NoError(t, err) bytes, err := buildPath.Join("sketch", "sketch1.ino.with_bootloader.hex").ReadFile() @@ -127,12 +120,7 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { err = buildPath.Join("sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) require.NoError(t, err) - builderLogger := logger.New(nil, nil, false, "") - err = builder.MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, - ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), - builderLogger, - ) + err = ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) require.NoError(t, err) bytes, err := buildPath.Join("sketch1.ino.with_bootloader.hex").ReadFile() @@ -153,12 +141,7 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { 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.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), - builderLogger, - ) + err := ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) require.NoError(t, err) exist, err := buildPath.Join("sketch.ino.with_bootloader.hex").ExistCheck() @@ -213,12 +196,7 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { err = buildPath.Join("sketch", "sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) require.NoError(t, err) - builderLogger := logger.New(nil, nil, false, "") - err = builder.MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, - ctx.Builder.GetBuildPath(), ctx.Builder.Sketch(), ctx.Builder.GetBuildProperties(), - builderLogger, - ) + err = ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) require.NoError(t, err) bytes, err := buildPath.Join("sketch", "sketch1.ino.with_bootloader.hex").ReadFile() From 4f99d330674fa6a840bc83ce91ab9713e27aa577 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 17:58:12 +0200 Subject: [PATCH 20/34] make WarnAboutArchIncompatibleLibraries a method recevier of arduino/builder --- arduino/builder/libraries.go | 23 ++++++++++ legacy/builder/builder.go | 5 +-- .../warn_about_arch_incompatible_libraries.go | 45 ------------------- 3 files changed, 24 insertions(+), 49 deletions(-) delete mode 100644 legacy/builder/warn_about_arch_incompatible_libraries.go diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index 445dcbd46af..fcd896fb9d9 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -22,6 +22,7 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/cpp" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" + "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/libraries" f "github.com/arduino/arduino-cli/internal/algorithms" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -320,3 +321,25 @@ func (b *Builder) RemoveUnusedCompiledLibraries(importedLibraries libraries.List return nil } + +// WarnAboutArchIncompatibleLibraries fixdoc +func (b *Builder) WarnAboutArchIncompatibleLibraries( + targetPlatform *cores.PlatformRelease, + importedLibraries libraries.List, +) { + archs := []string{targetPlatform.Platform.Architecture} + overrides, _ := b.buildProperties.GetOk("architecture.override_check") + if overrides != "" { + archs = append(archs, strings.Split(overrides, ",")...) + } + + for _, importedLibrary := range importedLibraries { + if !importedLibrary.SupportsAnyArchitectureIn(archs...) { + b.logger.Info( + tr("WARNING: library %[1]s claims to run on %[2]s architecture(s) and may be incompatible with your current board which runs on %[3]s architecture(s).", + importedLibrary.Name, + strings.Join(importedLibrary.Architectures, ", "), + strings.Join(archs, ", "))) + } + } +} diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 5abe9bf3294..751317e4524 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -399,12 +399,9 @@ func containerBuildOptions(ctx *types.Context) types.BareCommand { func warnAboutArchIncompatibleLibraries(ctx *types.Context) types.BareCommand { return types.BareCommand(func(ctx *types.Context) error { - overrides, _ := ctx.Builder.GetBuildProperties().GetOk("architecture.override_check") - WarnAboutArchIncompatibleLibraries( + ctx.Builder.WarnAboutArchIncompatibleLibraries( ctx.TargetPlatform, - overrides, ctx.SketchLibrariesDetector.ImportedLibraries(), - func(s string) { ctx.BuilderLogger.Info(s) }, ) return nil }) diff --git a/legacy/builder/warn_about_arch_incompatible_libraries.go b/legacy/builder/warn_about_arch_incompatible_libraries.go deleted file mode 100644 index ea7392560bc..00000000000 --- a/legacy/builder/warn_about_arch_incompatible_libraries.go +++ /dev/null @@ -1,45 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "strings" - - "github.com/arduino/arduino-cli/arduino/cores" - "github.com/arduino/arduino-cli/arduino/libraries" -) - -func WarnAboutArchIncompatibleLibraries( - targetPlatform *cores.PlatformRelease, - overrides string, - importedLibraries libraries.List, - printInfoFn func(string), -) { - archs := []string{targetPlatform.Platform.Architecture} - if overrides != "" { - archs = append(archs, strings.Split(overrides, ",")...) - } - - for _, importedLibrary := range importedLibraries { - if !importedLibrary.SupportsAnyArchitectureIn(archs...) { - printInfoFn( - tr("WARNING: library %[1]s claims to run on %[2]s architecture(s) and may be incompatible with your current board which runs on %[3]s architecture(s).", - importedLibrary.Name, - strings.Join(importedLibrary.Architectures, ", "), - strings.Join(archs, ", "))) - } - } -} From 16f7fe0d446a4567d481e46830abe5b2b5975eee Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 18:03:54 +0200 Subject: [PATCH 21/34] make PrintUsedLibraries a method recevier of arduino/builder --- arduino/builder/libraries.go | 34 ++++++++++++ legacy/builder/builder.go | 3 +- .../print_used_libraries_if_verbose.go | 54 ------------------- 3 files changed, 35 insertions(+), 56 deletions(-) delete mode 100644 legacy/builder/print_used_libraries_if_verbose.go diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index fcd896fb9d9..2c33073b0ba 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -17,6 +17,7 @@ package builder import ( "strings" + "time" "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" @@ -343,3 +344,36 @@ func (b *Builder) WarnAboutArchIncompatibleLibraries( } } } + +// PrintUsedLibraries fixdoc +// TODO here we can completly remove this part as it's duplicated in what we can +// read in the gRPC response +func (b *Builder) PrintUsedLibraries(importedLibraries libraries.List) { + if !b.logger.Verbose() || len(importedLibraries) == 0 { + return + } + + for _, library := range importedLibraries { + legacy := "" + if library.IsLegacy { + legacy = tr("(legacy)") + } + if library.Version.String() == "" { + b.logger.Info( + tr("Using library %[1]s in folder: %[2]s %[3]s", + library.Name, + library.InstallDir, + legacy)) + } else { + b.logger.Info( + tr("Using library %[1]s at version %[2]s in folder: %[3]s %[4]s", + library.Name, + library.Version, + library.InstallDir, + legacy)) + } + } + + // TODO Why is this here? + time.Sleep(100 * time.Millisecond) +} diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 751317e4524..5bfaa8ff9de 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -211,8 +211,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - infoOut, _ := PrintUsedLibrariesIfVerbose(ctx.BuilderLogger.Verbose(), ctx.SketchLibrariesDetector.ImportedLibraries()) - ctx.BuilderLogger.Info(string(infoOut)) + ctx.Builder.PrintUsedLibraries(ctx.SketchLibrariesDetector.ImportedLibraries()) return nil }), diff --git a/legacy/builder/print_used_libraries_if_verbose.go b/legacy/builder/print_used_libraries_if_verbose.go deleted file mode 100644 index 1ae369d30a2..00000000000 --- a/legacy/builder/print_used_libraries_if_verbose.go +++ /dev/null @@ -1,54 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "bytes" - "time" - - "github.com/arduino/arduino-cli/arduino/libraries" -) - -func PrintUsedLibrariesIfVerbose(verbose bool, importedLibraries libraries.List) ([]byte, error) { - if !verbose || len(importedLibraries) == 0 { - return nil, nil - } - - infoBuf := &bytes.Buffer{} - for _, library := range importedLibraries { - legacy := "" - if library.IsLegacy { - legacy = tr("(legacy)") - } - if library.Version.String() == "" { - infoBuf.WriteString( - tr("Using library %[1]s in folder: %[2]s %[3]s", - library.Name, - library.InstallDir, - legacy)) - } else { - infoBuf.WriteString( - tr("Using library %[1]s at version %[2]s in folder: %[3]s %[4]s", - library.Name, - library.Version, - library.InstallDir, - legacy)) - } - } - - time.Sleep(100 * time.Millisecond) - return infoBuf.Bytes(), nil -} From 97a15a42b0d0edf5899550010d7211381d6c32bc Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 18:16:07 +0200 Subject: [PATCH 22/34] make ExportCmake and PreprocessorSketch a method recevier of arduino/builder --- .../builder/export_cmake.go | 45 +++++++------------ arduino/builder/preprocess_sketch.go | 36 +++++++++++++++ legacy/builder/builder.go | 34 +------------- 3 files changed, 55 insertions(+), 60 deletions(-) rename legacy/builder/create_cmake_rule.go => arduino/builder/export_cmake.go (90%) create mode 100644 arduino/builder/preprocess_sketch.go diff --git a/legacy/builder/create_cmake_rule.go b/arduino/builder/export_cmake.go similarity index 90% rename from legacy/builder/create_cmake_rule.go rename to arduino/builder/export_cmake.go index d24b31df8ad..00535076b19 100644 --- a/legacy/builder/create_cmake_rule.go +++ b/arduino/builder/export_cmake.go @@ -30,22 +30,19 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/globals" "github.com/arduino/arduino-cli/arduino/libraries" - "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder/constants" ) var lineMatcher = regexp.MustCompile(`^#line\s\d+\s"`) -func ExportProjectCMake( +// ExportProjectCMake fixdoc +func (b *Builder) ExportProjectCMake( sketchError bool, // Was there an error while compiling the sketch? - buildPath, sketchBuildPath *paths.Path, importedLibraries libraries.List, - buildProperties *properties.Map, - sketch *sketch.Sketch, includeFolders paths.PathList, lineOffset int, onlyUpdateCompilationDatabase bool, -) ([]byte, []byte, error) { +) error { // copies the contents of the file named src to the file named // by dst. The file will be created if it does not already exist. If the // destination file exists, all it's contents will be replaced by the contents @@ -182,12 +179,12 @@ func ExportProjectCMake( var validStaticLibExtensions = []string{".a"} // If sketch error or cannot export Cmake project - if sketchError || buildProperties.Get("compiler.export_cmake") == "" { - return nil, nil, nil + if sketchError || b.buildProperties.Get("compiler.export_cmake") == "" { + return nil } // Create new cmake subFolder - clean if the folder is already there - cmakeFolder := buildPath.Join("_cmake") + cmakeFolder := b.buildPath.Join("_cmake") if _, err := cmakeFolder.Stat(); err == nil { cmakeFolder.RemoveAll() } @@ -207,7 +204,7 @@ func ExportProjectCMake( for _, library := range importedLibraries { // Copy used libraries in the correct folder libDir := libBaseFolder.Join(library.DirName) - mcu := buildProperties.Get("build.mcu") + mcu := b.buildProperties.Get("build.mcu") copyDir(library.InstallDir.String(), libDir.String(), validExportExtensions) // Read cmake options if available @@ -238,28 +235,20 @@ func ExportProjectCMake( } // Copy core + variant in use + preprocessed sketch in the correct folders - err := copyDir(buildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions) + err := copyDir(b.buildProperties.Get("build.core.path"), coreFolder.String(), validExportExtensions) if err != nil { fmt.Println(err) } - err = copyDir(buildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions) + err = copyDir(b.buildProperties.Get("build.variant.path"), coreFolder.Join("variant").String(), validExportExtensions) if err != nil { fmt.Println(err) } - normalOutput, verboseOutput, err := PreprocessSketch( - sketch, - buildPath, - includeFolders, - lineOffset, - buildProperties, - onlyUpdateCompilationDatabase, - ) - if err != nil { - return normalOutput, verboseOutput, err + if err := b.PreprocessSketch(includeFolders, lineOffset, onlyUpdateCompilationDatabase); err != nil { + return err } - err = copyDir(sketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions) + err = copyDir(b.sketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions) if err != nil { fmt.Println(err) } @@ -294,9 +283,9 @@ func ExportProjectCMake( var dynamicLibsFromGccMinusL []string var linkDirectories []string - extractCompileFlags(buildProperties, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) - extractCompileFlags(buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) - extractCompileFlags(buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) + extractCompileFlags(b.buildProperties, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) + extractCompileFlags(b.buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) + extractCompileFlags(b.buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) // Extract folders with .h in them for adding in include list headerFiles, _ := utils.FindFilesInFolder(cmakeFolder, true, validHeaderExtensions...) @@ -307,7 +296,7 @@ func ExportProjectCMake( // Generate the CMakeLists global file - projectName := sketch.Name + projectName := b.sketch.Name cmakelist := "cmake_minimum_required(VERSION 3.5.0)\n" cmakelist += "INCLUDE(FindPkgConfig)\n" @@ -364,7 +353,7 @@ func ExportProjectCMake( cmakeFile.WriteFile([]byte(cmakelist)) - return normalOutput, verboseOutput, nil + return nil } func extractCompileFlags(buildProperties *properties.Map, recipe string, defines, dynamicLibs, linkerflags, linkDirectories *[]string) { diff --git a/arduino/builder/preprocess_sketch.go b/arduino/builder/preprocess_sketch.go new file mode 100644 index 00000000000..5229feb3446 --- /dev/null +++ b/arduino/builder/preprocess_sketch.go @@ -0,0 +1,36 @@ +// This file is part of arduino-cli. +// +// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package builder + +import ( + "github.com/arduino/arduino-cli/arduino/builder/preprocessor" + "github.com/arduino/go-paths-helper" +) + +// PreprocessSketch fixdoc +func (b *Builder) PreprocessSketch(includes paths.PathList, lineOffset int, onlyUpdateCompilationDatabase bool) error { + // In the future we might change the preprocessor + normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags( + b.sketch, b.buildPath, includes, lineOffset, + b.buildProperties, onlyUpdateCompilationDatabase, + ) + if b.logger.Verbose() { + b.logger.WriteStdout(verboseOutput) + } + b.logger.WriteStdout(normalOutput) + + return err +} diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 5bfaa8ff9de..a901e5a112d 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -20,13 +20,9 @@ import ( "time" "github.com/arduino/arduino-cli/arduino/builder" - "github.com/arduino/arduino-cli/arduino/builder/preprocessor" "github.com/arduino/arduino-cli/arduino/builder/sizer" - "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/arduino/go-paths-helper" - properties "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -216,22 +212,13 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - normalOutput, verboseOutput, err := ExportProjectCMake( + return ctx.Builder.ExportProjectCMake( mainErr != nil, - ctx.Builder.GetBuildPath(), ctx.Builder.GetSketchBuildPath(), ctx.SketchLibrariesDetector.ImportedLibraries(), - ctx.Builder.GetBuildProperties(), - ctx.Builder.Sketch(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, ctx.OnlyUpdateCompilationDatabase, ) - if ctx.BuilderLogger.Verbose() { - ctx.BuilderLogger.WriteStdout(verboseOutput) - } else { - ctx.BuilderLogger.WriteStdout(normalOutput) - } - return err }), types.BareCommand(func(ctx *types.Context) error { @@ -264,27 +251,10 @@ 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.Builder.GetBuildPath(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, - ctx.Builder.GetBuildProperties(), ctx.OnlyUpdateCompilationDatabase) - if ctx.BuilderLogger.Verbose() { - ctx.BuilderLogger.WriteStdout(verboseOutput) - } else { - ctx.BuilderLogger.WriteStdout(normalOutput) - } - return err + return ctx.Builder.PreprocessSketch(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, ctx.OnlyUpdateCompilationDatabase) } } -func PreprocessSketch( - sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList, lineOffset int, - buildProperties *properties.Map, onlyUpdateCompilationDatabase bool, -) ([]byte, []byte, error) { - // In the future we might change the preprocessor - preprocessorImpl := preprocessor.PreprocessSketchWithCtags - return preprocessorImpl(sketch, buildPath, includes, lineOffset, buildProperties, onlyUpdateCompilationDatabase) -} - type Preprocess struct{} func (s *Preprocess) Run(ctx *types.Context) error { From f4f729812bd0309b15ec7dba1dd98ce7420cc219 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 18:24:10 +0200 Subject: [PATCH 23/34] remove legacy/constans pkg --- arduino/builder/export_cmake.go | 3 +- legacy/builder/constants/constants.go | 47 ------------------- .../builder/test/helper_tools_downloader.go | 21 ++++----- legacy/builder/test/libraries_loader_test.go | 5 +- .../test/merge_sketch_with_bootloader_test.go | 5 +- 5 files changed, 15 insertions(+), 66 deletions(-) delete mode 100644 legacy/builder/constants/constants.go diff --git a/arduino/builder/export_cmake.go b/arduino/builder/export_cmake.go index 00535076b19..6be00fb5175 100644 --- a/arduino/builder/export_cmake.go +++ b/arduino/builder/export_cmake.go @@ -30,7 +30,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/globals" "github.com/arduino/arduino-cli/arduino/libraries" - "github.com/arduino/arduino-cli/legacy/builder/constants" ) var lineMatcher = regexp.MustCompile(`^#line\s\d+\s"`) @@ -283,7 +282,7 @@ func (b *Builder) ExportProjectCMake( var dynamicLibsFromGccMinusL []string var linkDirectories []string - extractCompileFlags(b.buildProperties, constants.RECIPE_C_COMBINE_PATTERN, &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) + extractCompileFlags(b.buildProperties, "recipe.c.combine.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) extractCompileFlags(b.buildProperties, "recipe.c.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) extractCompileFlags(b.buildProperties, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories) diff --git a/legacy/builder/constants/constants.go b/legacy/builder/constants/constants.go deleted file mode 100644 index 31287e769da..00000000000 --- a/legacy/builder/constants/constants.go +++ /dev/null @@ -1,47 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// Copyright 2015 Matthijs Kooijman -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package constants - -const BUILD_OPTIONS_FILE = "build.options.json" -const BUILD_PROPERTIES_ARCH_OVERRIDE_CHECK = "architecture.override_check" -const BUILD_PROPERTIES_BOOTLOADER_FILE = "bootloader.file" -const BUILD_PROPERTIES_BOOTLOADER_NOBLINK = "bootloader.noblink" -const BUILD_PROPERTIES_BUILD_BOARD = "build.board" -const BUILD_PROPERTIES_COMPILER_LDFLAGS = "compiler.ldflags" -const BUILD_PROPERTIES_COMPILER_CPP_FLAGS = "compiler.cpp.flags" -const FOLDER_BOOTLOADERS = "bootloaders" -const FOLDER_CORE = "core" -const FOLDER_SKETCH = "sketch" -const FOLDER_TOOLS = "tools" -const FOLDER_LIBRARIES = "libraries" -const LIBRARY_ALL_ARCHS = "*" -const LIBRARY_EMAIL = "email" -const LIBRARY_FOLDER_SRC = "src" -const LOG_LEVEL_DEBUG = "debug" -const LOG_LEVEL_ERROR = "error" -const LOG_LEVEL_INFO = "info" -const LOG_LEVEL_WARN = "warn" - -const PACKAGE_NAME = "name" -const PACKAGE_TOOLS = "tools" -const PLATFORM_ARCHITECTURE = "architecture" -const PLATFORM_URL = "url" -const PLATFORM_VERSION = "version" -const RECIPE_C_COMBINE_PATTERN = "recipe.c.combine.pattern" -const TOOL_NAME = "name" -const TOOL_URL = "url" -const TOOL_VERSION = "version" diff --git a/legacy/builder/test/helper_tools_downloader.go b/legacy/builder/test/helper_tools_downloader.go index 2d80f0417fe..ee4b60df572 100644 --- a/legacy/builder/test/helper_tools_downloader.go +++ b/legacy/builder/test/helper_tools_downloader.go @@ -26,7 +26,6 @@ import ( "strings" "testing" - "github.com/arduino/arduino-cli/legacy/builder/constants" "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" @@ -264,12 +263,12 @@ func findCoreUrl(index map[string]interface{}, core Core) (string, error) { packages := index["packages"].([]interface{}) for _, p := range packages { pack := p.(map[string]interface{}) - if pack[constants.PACKAGE_NAME].(string) == core.Maintainer { + if pack["name"].(string) == core.Maintainer { packagePlatforms := pack["platforms"].([]interface{}) for _, pt := range packagePlatforms { packagePlatform := pt.(map[string]interface{}) - if packagePlatform[constants.PLATFORM_ARCHITECTURE] == core.Arch && packagePlatform[constants.PLATFORM_VERSION] == core.Version { - return packagePlatform[constants.PLATFORM_URL].(string), nil + if packagePlatform["architecture"] == core.Arch && packagePlatform["version"] == core.Version { + return packagePlatform["url"].(string), nil } } } @@ -394,7 +393,7 @@ func allBoardsManagerToolsAlreadyDownloadedAndUnpacked(targetPath *paths.Path, t } func boardManagerToolAlreadyDownloadedAndUnpacked(targetPath *paths.Path, tool Tool) bool { - return targetPath.Join(tool.Package, constants.FOLDER_TOOLS, tool.Name, tool.Version).Exist() + return targetPath.Join(tool.Package, "tools", tool.Name, tool.Version).Exist() } func allToolsAlreadyDownloadedAndUnpacked(targetPath *paths.Path, tools []Tool) bool { @@ -513,10 +512,10 @@ func downloadAndUnpackBoardsManagerTool(tool Tool, url string, targetPath *paths } defer unpackFolder.RemoveAll() - if err := targetPath.Join(tool.Package, constants.FOLDER_TOOLS, tool.Name).MkdirAll(); err != nil { + if err := targetPath.Join(tool.Package, "tools", tool.Name).MkdirAll(); err != nil { return errors.WithStack(err) } - if err := unpackFolder.Join(files[0].Base()).CopyDirTo(targetPath.Join(tool.Package, constants.FOLDER_TOOLS, tool.Name, tool.Version)); err != nil { + if err := unpackFolder.Join(files[0].Base()).CopyDirTo(targetPath.Join(tool.Package, "tools", tool.Name, tool.Version)); err != nil { return errors.WithStack(err) } return nil @@ -646,17 +645,17 @@ func findToolUrl(index map[string]interface{}, tool Tool, host []string) (string packages := index["packages"].([]interface{}) for _, p := range packages { pack := p.(map[string]interface{}) - packageTools := pack[constants.PACKAGE_TOOLS].([]interface{}) + packageTools := pack["tools"].([]interface{}) for _, pt := range packageTools { packageTool := pt.(map[string]interface{}) - name := packageTool[constants.TOOL_NAME].(string) - version := packageTool[constants.TOOL_VERSION].(string) + name := packageTool["name"].(string) + version := packageTool["version"].(string) if name == tool.Name && version == tool.Version { systems := packageTool["systems"].([]interface{}) for _, s := range systems { system := s.(map[string]interface{}) if slices.Contains(host, system["host"].(string)) { - return system[constants.TOOL_URL].(string), nil + return system["url"].(string), nil } } } diff --git a/legacy/builder/test/libraries_loader_test.go b/legacy/builder/test/libraries_loader_test.go index 5866fbcfa98..eae7d3a121b 100644 --- a/legacy/builder/test/libraries_loader_test.go +++ b/legacy/builder/test/libraries_loader_test.go @@ -22,7 +22,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/libraries" - "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/stretchr/testify/require" @@ -74,7 +73,7 @@ func TestLoadLibrariesAVR(t *testing.T) { require.True(t, Abs(t, paths.New("downloaded_libraries/Adafruit_PN532")).EquivalentTo(libs[idx].InstallDir)) require.True(t, Abs(t, paths.New("downloaded_libraries/Adafruit_PN532")).EquivalentTo(libs[idx].SourceDir)) require.Equal(t, 1, len(libs[idx].Architectures)) - require.Equal(t, constants.LIBRARY_ALL_ARCHS, libs[idx].Architectures[0]) + require.Equal(t, "*", libs[idx].Architectures[0]) require.False(t, libs[idx].IsLegacy) idx++ @@ -90,7 +89,7 @@ func TestLoadLibrariesAVR(t *testing.T) { require.True(t, Abs(t, paths.New("downloaded_libraries/Bridge")).EquivalentTo(bridgeLib.InstallDir)) require.True(t, Abs(t, paths.New("downloaded_libraries/Bridge/src")).EquivalentTo(bridgeLib.SourceDir)) require.Equal(t, 1, len(bridgeLib.Architectures)) - require.Equal(t, constants.LIBRARY_ALL_ARCHS, bridgeLib.Architectures[0]) + require.Equal(t, "*", bridgeLib.Architectures[0]) require.Equal(t, "Arduino", bridgeLib.Author) require.Equal(t, "Arduino ", bridgeLib.Maintainer) diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index 0478641d241..a418b94543e 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -21,7 +21,6 @@ import ( "strings" "testing" - "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/stretchr/testify/require" @@ -138,8 +137,8 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { buildPath := ctx.Builder.GetBuildPath() buildProperties := ctx.Builder.GetBuildProperties() - buildProperties.Remove(constants.BUILD_PROPERTIES_BOOTLOADER_NOBLINK) - buildProperties.Remove(constants.BUILD_PROPERTIES_BOOTLOADER_FILE) + buildProperties.Remove("bootloader.noblink") + buildProperties.Remove("bootloader.file") err := ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) require.NoError(t, err) From 5e9f9ca22912bb59e0fa68b38fe70c026f604f64 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 18:56:39 +0200 Subject: [PATCH 24/34] make Linker a method recevier of arduino/builder --- arduino/builder/builder.go | 15 --------- arduino/builder/linker.go | 65 ++++++++++++++++---------------------- legacy/builder/builder.go | 10 +----- 3 files changed, 28 insertions(+), 62 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 69776236508..d2a30b03107 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -155,16 +155,6 @@ func (b *Builder) GetBuildProperties() *properties.Map { return b.buildProperties } -// Jobs number of parallel processes -func (b *Builder) Jobs() int { - return b.jobs -} - -// CustomBuildProperties returns user provided custom build properties -func (b *Builder) CustomBuildProperties() []string { - return b.customBuildProperties -} - // GetBuildPath returns the build path func (b *Builder) GetBuildPath() *paths.Path { return b.buildPath @@ -175,11 +165,6 @@ 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 diff --git a/arduino/builder/linker.go b/arduino/builder/linker.go index 528e159d0aa..7ae85d15d9b 100644 --- a/arduino/builder/linker.go +++ b/arduino/builder/linker.go @@ -16,33 +16,28 @@ package builder import ( - "bytes" "strings" - "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/utils" f "github.com/arduino/arduino-cli/internal/algorithms" "github.com/arduino/go-paths-helper" - "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) -// Linker fixdoc -func Linker( +// Link fixdoc +func (b *Builder) Link( onlyUpdateCompilationDatabase bool, sketchObjectFiles, librariesObjectFiles, coreObjectsFiles paths.PathList, - coreArchiveFilePath, buildPath *paths.Path, - buildProperties *properties.Map, - builderLogger *logger.BuilderLogger, -) ([]byte, error) { - verboseInfo := &bytes.Buffer{} + coreArchiveFilePath *paths.Path, +) error { if onlyUpdateCompilationDatabase { - if builderLogger.Verbose() { - verboseInfo.WriteString(tr("Skip linking of final executable.")) + if b.logger.Verbose() { + b.logger.Info(tr("Skip linking of final executable.")) } - return verboseInfo.Bytes(), nil + return nil } + // TODO can we remove this multiple assignations? objectFilesSketch := sketchObjectFiles objectFilesLibraries := librariesObjectFiles objectFilesCore := coreObjectsFiles @@ -52,25 +47,19 @@ func Linker( objectFiles.AddAll(objectFilesLibraries) objectFiles.AddAll(objectFilesCore) - coreDotARelPath, err := buildPath.RelTo(coreArchiveFilePath) + coreDotARelPath, err := b.buildPath.RelTo(coreArchiveFilePath) if err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } - verboseInfoOut, err := link(objectFiles, coreDotARelPath, coreArchiveFilePath, buildProperties, builderLogger) - verboseInfo.Write(verboseInfoOut) - if err != nil { - return verboseInfo.Bytes(), errors.WithStack(err) + if err := b.link(objectFiles, coreDotARelPath, coreArchiveFilePath); err != nil { + return errors.WithStack(err) } - return verboseInfo.Bytes(), nil + return nil } -func link( - objectFiles paths.PathList, coreDotARelPath *paths.Path, coreArchiveFilePath *paths.Path, buildProperties *properties.Map, - builderLogger *logger.BuilderLogger, -) ([]byte, error) { - verboseBuffer := &bytes.Buffer{} +func (b *Builder) link(objectFiles paths.PathList, coreDotARelPath *paths.Path, coreArchiveFilePath *paths.Path) error { wrapWithDoubleQuotes := func(value string) string { return "\"" + value + "\"" } objectFileList := strings.Join(f.Map(objectFiles.AsStrings(), wrapWithDoubleQuotes), " ") @@ -83,7 +72,7 @@ func link( // it may happen that a subdir/spi.o inside the archive may be overwritten by a anotherdir/spi.o // because thery are both named spi.o. - properties := buildProperties.Clone() + properties := b.buildProperties.Clone() archives := paths.NewPathList() for _, object := range objectFiles { if object.HasSuffix(".a") { @@ -102,14 +91,14 @@ func link( command, err := utils.PrepareCommandForRecipe(properties, "recipe.ar.pattern", false) if err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } - if verboseInfo, _, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil { - if builderLogger.Verbose() { - verboseBuffer.WriteString(string(verboseInfo)) + if verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil { + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } - return verboseBuffer.Bytes(), errors.WithStack(err) + return errors.WithStack(err) } } @@ -117,21 +106,21 @@ func link( objectFileList = "-Wl,--whole-archive " + objectFileList + " -Wl,--no-whole-archive" } - properties := buildProperties.Clone() + properties := b.buildProperties.Clone() properties.Set("compiler.c.elf.flags", properties.Get("compiler.c.elf.flags")) - properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+builderLogger.WarningsLevel())) + properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+b.logger.WarningsLevel())) properties.Set("archive_file", coreDotARelPath.String()) properties.Set("archive_file_path", coreArchiveFilePath.String()) properties.Set("object_files", objectFileList) command, err := utils.PrepareCommandForRecipe(properties, "recipe.c.combine.pattern", false) if err != nil { - return verboseBuffer.Bytes(), err + return err } - verboseInfo, _, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */) - if builderLogger.Verbose() { - verboseBuffer.WriteString(string(verboseInfo)) + verboseInfo, _, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */) + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } - return verboseBuffer.Bytes(), err + return err } diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index a901e5a112d..688a7913b28 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -19,7 +19,6 @@ import ( "reflect" "time" - "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/builder/sizer" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/legacy/builder/types" @@ -142,20 +141,13 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - verboseInfoOut, err := builder.Linker( + return ctx.Builder.Link( ctx.OnlyUpdateCompilationDatabase, ctx.SketchObjectFiles, ctx.LibrariesObjectFiles, ctx.CoreObjectsFiles, ctx.CoreArchiveFilePath, - ctx.Builder.GetBuildPath(), - ctx.Builder.GetBuildProperties(), - ctx.BuilderLogger, ) - if ctx.BuilderLogger.Verbose() { - ctx.BuilderLogger.Info(string(verboseInfoOut)) - } - return err }), types.BareCommand(func(ctx *types.Context) error { From 290e8b12b7594dab1a45bf19ef9dfcad10899001 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 19:05:13 +0200 Subject: [PATCH 25/34] make Size a method recevier of arduino/builder --- arduino/builder/{sizer => }/sizer.go | 64 ++++++++++------------- arduino/builder/{sizer => }/sizer_test.go | 2 +- legacy/builder/builder.go | 5 +- legacy/builder/types/context.go | 3 +- 4 files changed, 31 insertions(+), 43 deletions(-) rename arduino/builder/{sizer => }/sizer.go (71%) rename arduino/builder/{sizer => }/sizer_test.go (99%) diff --git a/arduino/builder/sizer/sizer.go b/arduino/builder/sizer.go similarity index 71% rename from arduino/builder/sizer/sizer.go rename to arduino/builder/sizer.go index 7533a8d740e..069ee527ff1 100644 --- a/arduino/builder/sizer/sizer.go +++ b/arduino/builder/sizer.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package sizer +package builder import ( "encoding/json" @@ -21,16 +21,12 @@ import ( "regexp" "strconv" - "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/utils" - "github.com/arduino/arduino-cli/i18n" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) -var tr = i18n.Tr - // ExecutableSectionSize represents a section of the executable output file type ExecutableSectionSize struct { Name string `json:"name"` @@ -55,31 +51,27 @@ func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.Execut } // Size fixdoc -func Size( - onlyUpdateCompilationDatabase, sketchError bool, - buildProperties *properties.Map, - builderLogger *logger.BuilderLogger, -) (ExecutablesFileSections, error) { +func (b *Builder) Size(onlyUpdateCompilationDatabase, sketchError bool) (ExecutablesFileSections, error) { if onlyUpdateCompilationDatabase || sketchError { return nil, nil } - if buildProperties.ContainsKey("recipe.advanced_size.pattern") { - return checkSizeAdvanced(buildProperties, builderLogger) + if b.buildProperties.ContainsKey("recipe.advanced_size.pattern") { + return b.checkSizeAdvanced() } - return checkSize(buildProperties, builderLogger) + return b.checkSize() } -func checkSizeAdvanced(buildProperties *properties.Map, builderLogger *logger.BuilderLogger) (ExecutablesFileSections, error) { - command, err := utils.PrepareCommandForRecipe(buildProperties, "recipe.advanced_size.pattern", false) +func (b *Builder) checkSizeAdvanced() (ExecutablesFileSections, error) { + command, err := utils.PrepareCommandForRecipe(b.buildProperties, "recipe.advanced_size.pattern", false) if err != nil { return nil, errors.New(tr("Error while determining sketch size: %s", err)) } - verboseInfo, out, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */) - if builderLogger.Verbose() { - builderLogger.Info(string(verboseInfo)) + verboseInfo, out, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */) + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } if err != nil { return nil, errors.New(tr("Error while determining sketch size: %s", err)) @@ -107,21 +99,21 @@ func checkSizeAdvanced(buildProperties *properties.Map, builderLogger *logger.Bu executableSectionsSize := resp.Sections switch resp.Severity { case "error": - builderLogger.Warn(resp.Output) + b.logger.Warn(resp.Output) return executableSectionsSize, errors.New(resp.ErrorMessage) case "warning": - builderLogger.Warn(resp.Output) + b.logger.Warn(resp.Output) case "info": - builderLogger.Info(resp.Output) + b.logger.Info(resp.Output) default: return executableSectionsSize, fmt.Errorf("invalid '%s' severity from sketch sizer: it must be 'error', 'warning' or 'info'", resp.Severity) } return executableSectionsSize, nil } -func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLogger) (ExecutablesFileSections, error) { - properties := buildProperties.Clone() - properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+builderLogger.WarningsLevel())) +func (b *Builder) checkSize() (ExecutablesFileSections, error) { + properties := b.buildProperties.Clone() + properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+b.logger.WarningsLevel())) maxTextSizeString := properties.Get("upload.maximum_size") maxDataSizeString := properties.Get("upload.maximum_data_size") @@ -143,25 +135,25 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog } } - textSize, dataSize, _, err := execSizeRecipe(properties, builderLogger) + textSize, dataSize, _, err := b.execSizeRecipe(properties) if err != nil { - builderLogger.Warn(tr("Couldn't determine program size")) + b.logger.Warn(tr("Couldn't determine program size")) return nil, nil } - builderLogger.Info(tr("Sketch uses %[1]s bytes (%[3]s%%) of program storage space. Maximum is %[2]s bytes.", + b.logger.Info(tr("Sketch uses %[1]s bytes (%[3]s%%) of program storage space. Maximum is %[2]s bytes.", strconv.Itoa(textSize), strconv.Itoa(maxTextSize), strconv.Itoa(textSize*100/maxTextSize))) if dataSize >= 0 { if maxDataSize > 0 { - builderLogger.Info(tr("Global variables use %[1]s bytes (%[3]s%%) of dynamic memory, leaving %[4]s bytes for local variables. Maximum is %[2]s bytes.", + b.logger.Info(tr("Global variables use %[1]s bytes (%[3]s%%) of dynamic memory, leaving %[4]s bytes for local variables. Maximum is %[2]s bytes.", strconv.Itoa(dataSize), strconv.Itoa(maxDataSize), strconv.Itoa(dataSize*100/maxDataSize), strconv.Itoa(maxDataSize-dataSize))) } else { - builderLogger.Info(tr("Global variables use %[1]s bytes of dynamic memory.", strconv.Itoa(dataSize))) + b.logger.Info(tr("Global variables use %[1]s bytes of dynamic memory.", strconv.Itoa(dataSize))) } } @@ -181,12 +173,12 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog } if textSize > maxTextSize { - builderLogger.Warn(tr("Sketch too big; see %[1]s for tips on reducing it.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) + b.logger.Warn(tr("Sketch too big; see %[1]s for tips on reducing it.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) return executableSectionsSize, errors.New(tr("text section exceeds available space in board")) } if maxDataSize > 0 && dataSize > maxDataSize { - builderLogger.Warn(tr("Not enough memory; see %[1]s for tips on reducing your footprint.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) + b.logger.Warn(tr("Not enough memory; see %[1]s for tips on reducing your footprint.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) return executableSectionsSize, errors.New(tr("data section exceeds available space in board")) } @@ -196,23 +188,23 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog return executableSectionsSize, err } if maxDataSize > 0 && dataSize > maxDataSize*warnDataPercentage/100 { - builderLogger.Warn(tr("Low memory available, stability problems may occur.")) + b.logger.Warn(tr("Low memory available, stability problems may occur.")) } } return executableSectionsSize, nil } -func execSizeRecipe(properties *properties.Map, builderLogger *logger.BuilderLogger) (textSize int, dataSize int, eepromSize int, resErr error) { +func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, dataSize int, eepromSize int, resErr error) { command, err := utils.PrepareCommandForRecipe(properties, "recipe.size.pattern", false) if err != nil { resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err) return } - verboseInfo, out, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */) - if builderLogger.Verbose() { - builderLogger.Info(string(verboseInfo)) + verboseInfo, out, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */) + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } if err != nil { resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err) diff --git a/arduino/builder/sizer/sizer_test.go b/arduino/builder/sizer_test.go similarity index 99% rename from arduino/builder/sizer/sizer_test.go rename to arduino/builder/sizer_test.go index 42c3b323d42..79514ae6862 100644 --- a/arduino/builder/sizer/sizer_test.go +++ b/arduino/builder/sizer_test.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package sizer +package builder import ( "testing" diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 688a7913b28..83f67c68d99 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -19,7 +19,6 @@ import ( "reflect" "time" - "github.com/arduino/arduino-cli/arduino/builder/sizer" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/pkg/errors" @@ -214,10 +213,8 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - executableSectionsSize, err := sizer.Size( + executableSectionsSize, err := ctx.Builder.Size( ctx.OnlyUpdateCompilationDatabase, mainErr != nil, - ctx.Builder.GetBuildProperties(), - ctx.BuilderLogger, ) ctx.ExecutableSectionsSize = executableSectionsSize return err diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index c241d02997a..b57a3076723 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -21,7 +21,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" - "github.com/arduino/arduino-cli/arduino/builder/sizer" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -58,7 +57,7 @@ type Context struct { ProgressCB rpc.TaskProgressCB // Sizer results - ExecutableSectionsSize sizer.ExecutablesFileSections + ExecutableSectionsSize builder.ExecutablesFileSections // Compilation Database to build/update CompilationDatabase *compilation.Database From 2279e9b816a36e61444e5a3558f81a06b8e9412f Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 19:16:45 +0200 Subject: [PATCH 26/34] remove onlyUpdateCompilationDatabase from Context --- arduino/builder/builder.go | 29 +++++++++++-------- arduino/builder/core.go | 13 ++++----- arduino/builder/export_cmake.go | 3 +- arduino/builder/libraries.go | 13 +++------ arduino/builder/linker.go | 8 ++--- arduino/builder/preprocess_sketch.go | 4 +-- arduino/builder/recipe.go | 4 +-- arduino/builder/sizer.go | 4 +-- arduino/builder/sketch.go | 9 +++--- arduino/builder/sketch_test.go | 6 ++-- commands/compile/compile.go | 13 ++------- legacy/builder/builder.go | 18 +++--------- legacy/builder/test/builder_test.go | 4 +-- .../test/merge_sketch_with_bootloader_test.go | 8 ++--- legacy/builder/types/context.go | 2 -- 15 files changed, 55 insertions(+), 83 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index d2a30b03107..5b1a0e46563 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -56,6 +56,9 @@ type Builder struct { // The keys of the map are paths relative to sketch folder. sourceOverrides map[string]string + // Set to true to skip build and produce only Compilation Database + onlyUpdateCompilationDatabase bool + *BuildOptionsManager } @@ -73,6 +76,7 @@ func NewBuilder( fqbn *cores.FQBN, clean bool, sourceOverrides map[string]string, + onlyUpdateCompilationDatabase bool, logger *logger.BuilderLogger, ) (*Builder, error) { buildProperties := properties.NewMap() @@ -123,18 +127,19 @@ func NewBuilder( } return &Builder{ - sketch: sk, - buildProperties: buildProperties, - buildPath: buildPath, - sketchBuildPath: sketchBuildPath, - coreBuildPath: coreBuildPath, - librariesBuildPath: librariesBuildPath, - jobs: jobs, - customBuildProperties: customBuildPropertiesArgs, - coreBuildCachePath: coreBuildCachePath, - logger: logger, - clean: clean, - sourceOverrides: sourceOverrides, + sketch: sk, + buildProperties: buildProperties, + buildPath: buildPath, + sketchBuildPath: sketchBuildPath, + coreBuildPath: coreBuildPath, + librariesBuildPath: librariesBuildPath, + jobs: jobs, + customBuildProperties: customBuildPropertiesArgs, + coreBuildCachePath: coreBuildCachePath, + logger: logger, + clean: clean, + sourceOverrides: sourceOverrides, + onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase, BuildOptionsManager: NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, builtInLibrariesDirs, buildPath, diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 4fc411235a0..d115bbdc919 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -37,7 +37,6 @@ import ( // BuildCore fixdoc func (b *Builder) BuildCore( actualPlatform *cores.PlatformRelease, - onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, *paths.Path, error) { @@ -58,7 +57,6 @@ func (b *Builder) BuildCore( } archiveFile, objectFiles, err := b.compileCore( - onlyUpdateCompilationDatabase, actualPlatform, compilationDatabase, progress, progressCB, @@ -71,7 +69,6 @@ func (b *Builder) BuildCore( } func (b *Builder) compileCore( - onlyUpdateCompilationDatabase bool, actualPlatform *cores.PlatformRelease, compilationDatabase *compilation.Database, progress *progress.Struct, progressCB rpc.TaskProgressCB, @@ -91,7 +88,7 @@ func (b *Builder) compileCore( if variantFolder != nil && variantFolder.IsDir() { variantObjectFiles, err = utils.CompileFilesRecursive( variantFolder, b.coreBuildPath, b.buildProperties, includes, - onlyUpdateCompilationDatabase, + b.onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, b.logger, @@ -117,7 +114,7 @@ func (b *Builder) compileCore( } var canUseArchivedCore bool - if onlyUpdateCompilationDatabase || b.clean { + if b.onlyUpdateCompilationDatabase || b.clean { canUseArchivedCore = false } else if isOlder, err := utils.DirContentIsOlderThan(realCoreFolder, targetArchivedCore); err != nil || !isOlder { // Recreate the archive if ANY of the core files (including platform.txt) has changed @@ -142,7 +139,7 @@ func (b *Builder) compileCore( coreObjectFiles, err := utils.CompileFilesRecursive( coreFolder, b.coreBuildPath, b.buildProperties, includes, - onlyUpdateCompilationDatabase, + b.onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, b.logger, @@ -154,7 +151,7 @@ func (b *Builder) compileCore( archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( b.coreBuildPath, paths.New("core.a"), coreObjectFiles, b.buildProperties, - onlyUpdateCompilationDatabase, b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), + b.onlyUpdateCompilationDatabase, b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), ) if b.logger.Verbose() { b.logger.Info(string(verboseInfo)) @@ -164,7 +161,7 @@ func (b *Builder) compileCore( } // archive core.a - if targetArchivedCore != nil && !onlyUpdateCompilationDatabase { + if targetArchivedCore != nil && !b.onlyUpdateCompilationDatabase { err := archiveFile.CopyTo(targetArchivedCore) if b.logger.Verbose() { if err == nil { diff --git a/arduino/builder/export_cmake.go b/arduino/builder/export_cmake.go index 6be00fb5175..1b10c7f4208 100644 --- a/arduino/builder/export_cmake.go +++ b/arduino/builder/export_cmake.go @@ -40,7 +40,6 @@ func (b *Builder) ExportProjectCMake( importedLibraries libraries.List, includeFolders paths.PathList, lineOffset int, - onlyUpdateCompilationDatabase bool, ) error { // copies the contents of the file named src to the file named // by dst. The file will be created if it does not already exist. If the @@ -243,7 +242,7 @@ func (b *Builder) ExportProjectCMake( fmt.Println(err) } - if err := b.PreprocessSketch(includeFolders, lineOffset, onlyUpdateCompilationDatabase); err != nil { + if err := b.PreprocessSketch(includeFolders, lineOffset); err != nil { return err } diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index 2c33073b0ba..ecd310d3402 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -43,7 +43,6 @@ var ( func (b *Builder) BuildLibraries( includesFolders paths.PathList, importedLibraries libraries.List, - onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { @@ -56,7 +55,6 @@ func (b *Builder) BuildLibraries( librariesObjectFiles, err := b.compileLibraries( libs, includes, - onlyUpdateCompilationDatabase, compilationDatabase, progress, progressCB, ) @@ -128,7 +126,6 @@ func (b *Builder) findExpectedPrecompiledLibFolder( func (b *Builder) compileLibraries( libraries libraries.List, includes []string, - onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { @@ -139,7 +136,6 @@ func (b *Builder) compileLibraries( for _, library := range libraries { libraryObjectFiles, err := b.compileLibrary( library, includes, - onlyUpdateCompilationDatabase, compilationDatabase, progress, progressCB, ) @@ -163,7 +159,6 @@ func (b *Builder) compileLibraries( func (b *Builder) compileLibrary( library *libraries.Library, includes []string, - onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { @@ -228,7 +223,7 @@ func (b *Builder) compileLibrary( if library.Layout == libraries.RecursiveLayout { libObjectFiles, err := utils.CompileFilesRecursive( library.SourceDir, libraryBuildPath, b.buildProperties, includes, - onlyUpdateCompilationDatabase, + b.onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, b.logger, @@ -240,7 +235,7 @@ func (b *Builder) compileLibrary( if library.DotALinkage { archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( libraryBuildPath, paths.New(library.DirName+".a"), libObjectFiles, b.buildProperties, - onlyUpdateCompilationDatabase, b.logger.Verbose(), + b.onlyUpdateCompilationDatabase, b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), ) if b.logger.Verbose() { @@ -259,7 +254,7 @@ func (b *Builder) compileLibrary( } libObjectFiles, err := utils.CompileFiles( library.SourceDir, libraryBuildPath, b.buildProperties, includes, - onlyUpdateCompilationDatabase, + b.onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, b.logger, @@ -274,7 +269,7 @@ func (b *Builder) compileLibrary( utilityBuildPath := libraryBuildPath.Join("utility") utilityObjectFiles, err := utils.CompileFiles( library.UtilityDir, utilityBuildPath, b.buildProperties, includes, - onlyUpdateCompilationDatabase, + b.onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, b.logger, diff --git a/arduino/builder/linker.go b/arduino/builder/linker.go index 7ae85d15d9b..5d88460fbfd 100644 --- a/arduino/builder/linker.go +++ b/arduino/builder/linker.go @@ -25,12 +25,8 @@ import ( ) // Link fixdoc -func (b *Builder) Link( - onlyUpdateCompilationDatabase bool, - sketchObjectFiles, librariesObjectFiles, coreObjectsFiles paths.PathList, - coreArchiveFilePath *paths.Path, -) error { - if onlyUpdateCompilationDatabase { +func (b *Builder) Link(sketchObjectFiles, librariesObjectFiles, coreObjectsFiles paths.PathList, coreArchiveFilePath *paths.Path) error { + if b.onlyUpdateCompilationDatabase { if b.logger.Verbose() { b.logger.Info(tr("Skip linking of final executable.")) } diff --git a/arduino/builder/preprocess_sketch.go b/arduino/builder/preprocess_sketch.go index 5229feb3446..63d472ed318 100644 --- a/arduino/builder/preprocess_sketch.go +++ b/arduino/builder/preprocess_sketch.go @@ -21,11 +21,11 @@ import ( ) // PreprocessSketch fixdoc -func (b *Builder) PreprocessSketch(includes paths.PathList, lineOffset int, onlyUpdateCompilationDatabase bool) error { +func (b *Builder) PreprocessSketch(includes paths.PathList, lineOffset int) error { // In the future we might change the preprocessor normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags( b.sketch, b.buildPath, includes, lineOffset, - b.buildProperties, onlyUpdateCompilationDatabase, + b.buildProperties, b.onlyUpdateCompilationDatabase, ) if b.logger.Verbose() { b.logger.WriteStdout(verboseOutput) diff --git a/arduino/builder/recipe.go b/arduino/builder/recipe.go index 7c26fd6101a..1102e8d7568 100644 --- a/arduino/builder/recipe.go +++ b/arduino/builder/recipe.go @@ -29,7 +29,7 @@ import ( // RunRecipe fixdoc func (b *Builder) RunRecipe( prefix, suffix string, - skipIfOnlyUpdatingCompilationDatabase, onlyUpdateCompilationDatabase bool, + skipIfOnlyUpdatingCompilationDatabase bool, ) error { logrus.Debugf(fmt.Sprintf("Looking for recipes like %s", prefix+"*"+suffix)) @@ -47,7 +47,7 @@ func (b *Builder) RunRecipe( return errors.WithStack(err) } - if onlyUpdateCompilationDatabase && skipIfOnlyUpdatingCompilationDatabase { + if b.onlyUpdateCompilationDatabase && skipIfOnlyUpdatingCompilationDatabase { if b.logger.Verbose() { b.logger.Info(tr("Skipping: %[1]s", strings.Join(command.GetArgs(), " "))) } diff --git a/arduino/builder/sizer.go b/arduino/builder/sizer.go index 069ee527ff1..7b984b24f67 100644 --- a/arduino/builder/sizer.go +++ b/arduino/builder/sizer.go @@ -51,8 +51,8 @@ func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.Execut } // Size fixdoc -func (b *Builder) Size(onlyUpdateCompilationDatabase, sketchError bool) (ExecutablesFileSections, error) { - if onlyUpdateCompilationDatabase || sketchError { +func (b *Builder) Size(sketchError bool) (ExecutablesFileSections, error) { + if b.onlyUpdateCompilationDatabase || sketchError { return nil, nil } diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index 60d6c6bb84c..5774da5e15a 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -184,7 +184,6 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error { // BuildSketch fixdoc func (b *Builder) BuildSketch( includesFolders paths.PathList, - onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { @@ -196,7 +195,7 @@ func (b *Builder) BuildSketch( sketchObjectFiles, err := utils.CompileFiles( b.sketchBuildPath, b.sketchBuildPath, b.buildProperties, includes, - onlyUpdateCompilationDatabase, + b.onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, b.builderLogger, @@ -211,7 +210,7 @@ func (b *Builder) BuildSketch( if sketchSrcPath.IsDir() { srcObjectFiles, err := utils.CompileFilesRecursive( sketchSrcPath, sketchSrcPath, b.buildProperties, includes, - onlyUpdateCompilationDatabase, + b.onlyUpdateCompilationDatabase, compilationDatabase, b.jobs, b.builderLogger, @@ -227,8 +226,8 @@ func (b *Builder) BuildSketch( } // MergeSketchWithBootloader fixdoc -func (b *Builder) MergeSketchWithBootloader(onlyUpdateCompilationDatabase bool) error { - if onlyUpdateCompilationDatabase { +func (b *Builder) MergeSketchWithBootloader() error { + if b.onlyUpdateCompilationDatabase { return nil } diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index f7e8cc1d5b6..9bf577a1686 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -54,7 +54,7 @@ func TestMergeSketchSources(t *testing.T) { b, err := NewBuilder( sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, nil) + nil, nil, nil, nil, fqbn, false, nil, false, nil) require.NoError(t, err) offset, source, err := b.sketchMergeSources(nil) @@ -73,7 +73,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { // ensure not to include Arduino.h when it's already there b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, nil) + nil, nil, nil, nil, fqbn, false, nil, false, nil) require.NoError(t, err) _, source, err := b.sketchMergeSources(nil) @@ -95,7 +95,7 @@ func TestCopyAdditionalFiles(t *testing.T) { require.NoError(t, err) b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, nil) + nil, nil, nil, nil, fqbn, false, nil, false, nil) require.NoError(t, err) // copy the sketch over, create a fake main file we don't care about it diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 5eee0981eeb..b17b130040d 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -188,8 +188,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) - builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly() - builderLogger := logger.New(outStream, errStream, req.GetVerbose(), req.GetWarnings()) builderCtx.BuilderLogger = builderLogger @@ -208,6 +206,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream fqbn, req.GetClean(), req.GetSourceOverride(), + req.GetCreateCompilationDatabaseOnly(), builderLogger, ) if err != nil { @@ -339,10 +338,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream exportBinaries = false } if exportBinaries { - err := sketchBuilder.RunRecipe( - "recipe.hooks.savehex.presavehex", ".pattern", false, - builderCtx.OnlyUpdateCompilationDatabase, - ) + err := sketchBuilder.RunRecipe("recipe.hooks.savehex.presavehex", ".pattern", false) if err != nil { return r, err } @@ -381,10 +377,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream } } - err = sketchBuilder.RunRecipe( - "recipe.hooks.savehex.postsavehex", ".pattern", false, - builderCtx.OnlyUpdateCompilationDatabase, - ) + err = sketchBuilder.RunRecipe("recipe.hooks.savehex.postsavehex", ".pattern", false) if err != nil { return r, err } diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 83f67c68d99..9149f2bc309 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -66,7 +66,6 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { sketchObjectFiles, err := ctx.Builder.BuildSketch( ctx.SketchLibrariesDetector.IncludeFolders(), - ctx.OnlyUpdateCompilationDatabase, ctx.CompilationDatabase, &ctx.Progress, ctx.ProgressCB, ) @@ -96,7 +95,6 @@ func (s *Builder) Run(ctx *types.Context) error { librariesObjectFiles, err := ctx.Builder.BuildLibraries( ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries(), - ctx.OnlyUpdateCompilationDatabase, ctx.CompilationDatabase, &ctx.Progress, ctx.ProgressCB, ) @@ -119,7 +117,6 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { objectFiles, archiveFile, err := ctx.Builder.BuildCore( ctx.ActualPlatform, - ctx.OnlyUpdateCompilationDatabase, ctx.CompilationDatabase, &ctx.Progress, ctx.ProgressCB, ) @@ -141,7 +138,6 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { return ctx.Builder.Link( - ctx.OnlyUpdateCompilationDatabase, ctx.SketchObjectFiles, ctx.LibrariesObjectFiles, ctx.CoreObjectsFiles, @@ -164,7 +160,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - return ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) + return ctx.Builder.MergeSketchWithBootloader() }), types.BareCommand(func(ctx *types.Context) error { @@ -208,14 +204,11 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.SketchLibrariesDetector.ImportedLibraries(), ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, - ctx.OnlyUpdateCompilationDatabase, ) }), types.BareCommand(func(ctx *types.Context) error { - executableSectionsSize, err := ctx.Builder.Size( - ctx.OnlyUpdateCompilationDatabase, mainErr != nil, - ) + executableSectionsSize, err := ctx.Builder.Size(mainErr != nil) ctx.ExecutableSectionsSize = executableSectionsSize return err }), @@ -240,7 +233,7 @@ func (s *Builder) Run(ctx *types.Context) error { func preprocessSketchCommand(ctx *types.Context) types.BareCommand { return func(ctx *types.Context) error { - return ctx.Builder.PreprocessSketch(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, ctx.OnlyUpdateCompilationDatabase) + return ctx.Builder.PreprocessSketch(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset) } } @@ -343,10 +336,7 @@ func logIfVerbose(warn bool, msg string) types.BareCommand { } func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipIfOnlyUpdatingCompilationDatabase bool) error { - return ctx.Builder.RunRecipe( - prefix, suffix, skipIfOnlyUpdatingCompilationDatabase, - ctx.OnlyUpdateCompilationDatabase, - ) + return ctx.Builder.RunRecipe(prefix, suffix, skipIfOnlyUpdatingCompilationDatabase) } func containerBuildOptions(ctx *types.Context) types.BareCommand { diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index e6cfe93cf1b..11082e44a09 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -106,7 +106,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, nil, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, nil, builderLogger, + ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, nil, false, builderLogger, ) require.NoError(t, err) if fqbnString != "" { @@ -119,7 +119,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, boardBuildProperties, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, fqbn, false, nil, builderLogger) + ctx.BuiltInLibrariesDirs, fqbn, false, nil, false, builderLogger) require.NoError(t, err) ctx.PackageManager = pme diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index a418b94543e..b1d2a6df7ea 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -67,7 +67,7 @@ func TestMergeSketchWithBootloader(t *testing.T) { err = buildPath.Join("sketch", "sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) require.NoError(t, err) - err = ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) + err = ctx.Builder.MergeSketchWithBootloader() require.NoError(t, err) bytes, err := buildPath.Join("sketch", "sketch1.ino.with_bootloader.hex").ReadFile() @@ -119,7 +119,7 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { err = buildPath.Join("sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) require.NoError(t, err) - err = ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) + err = ctx.Builder.MergeSketchWithBootloader() require.NoError(t, err) bytes, err := buildPath.Join("sketch1.ino.with_bootloader.hex").ReadFile() @@ -140,7 +140,7 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { buildProperties.Remove("bootloader.noblink") buildProperties.Remove("bootloader.file") - err := ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) + err := ctx.Builder.MergeSketchWithBootloader() require.NoError(t, err) exist, err := buildPath.Join("sketch.ino.with_bootloader.hex").ExistCheck() @@ -195,7 +195,7 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { err = buildPath.Join("sketch", "sketch1.ino.hex").WriteFile([]byte(fakeSketchHex)) require.NoError(t, err) - err = ctx.Builder.MergeSketchWithBootloader(ctx.OnlyUpdateCompilationDatabase) + err = ctx.Builder.MergeSketchWithBootloader() require.NoError(t, err) bytes, err := buildPath.Join("sketch", "sketch1.ino.with_bootloader.hex").ReadFile() diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index b57a3076723..8769b643666 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -61,8 +61,6 @@ type Context struct { // Compilation Database to build/update CompilationDatabase *compilation.Database - // Set to true to skip build and produce only Compilation Database - OnlyUpdateCompilationDatabase bool } func (ctx *Context) PushProgress() { From a0f8e30acd0abb9e807b308948248cc6c2faae59 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 19:49:42 +0200 Subject: [PATCH 27/34] remove Progress from Context --- arduino/builder/builder.go | 10 ++++++++ arduino/builder/core.go | 14 +++-------- arduino/builder/libraries.go | 37 +++++++--------------------- arduino/builder/progress/progress.go | 18 ++++++++++++++ arduino/builder/sketch.go | 7 ++---- arduino/builder/sketch_test.go | 6 ++--- arduino/builder/utils/utils.go | 18 ++++---------- commands/compile/compile.go | 3 ++- legacy/builder/builder.go | 28 +++++++++------------ legacy/builder/test/builder_test.go | 4 +-- legacy/builder/types/context.go | 16 ------------ 11 files changed, 65 insertions(+), 96 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 5b1a0e46563..931c8b829c9 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/arduino/arduino-cli/arduino/builder/logger" + "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" @@ -59,6 +60,9 @@ type Builder struct { // Set to true to skip build and produce only Compilation Database onlyUpdateCompilationDatabase bool + // Progress of all various steps + Progress *progress.Struct + *BuildOptionsManager } @@ -78,6 +82,7 @@ func NewBuilder( sourceOverrides map[string]string, onlyUpdateCompilationDatabase bool, logger *logger.BuilderLogger, + progressStats *progress.Struct, ) (*Builder, error) { buildProperties := properties.NewMap() if boardBuildProperties != nil { @@ -126,6 +131,10 @@ func NewBuilder( return nil, ErrSketchCannotBeLocatedInBuildPath } + if progressStats == nil { + progressStats = progress.New(nil) + } + return &Builder{ sketch: sk, buildProperties: buildProperties, @@ -140,6 +149,7 @@ func NewBuilder( clean: clean, sourceOverrides: sourceOverrides, onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase, + Progress: progressStats, BuildOptionsManager: NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, builtInLibrariesDirs, buildPath, diff --git a/arduino/builder/core.go b/arduino/builder/core.go index d115bbdc919..8e266154594 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -24,12 +24,10 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" - "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/buildcache" f "github.com/arduino/arduino-cli/internal/algorithms" - rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-paths-helper" "github.com/pkg/errors" ) @@ -38,7 +36,6 @@ import ( func (b *Builder) BuildCore( actualPlatform *cores.PlatformRelease, compilationDatabase *compilation.Database, - progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, *paths.Path, error) { if err := b.coreBuildPath.MkdirAll(); err != nil { return nil, nil, errors.WithStack(err) @@ -56,11 +53,7 @@ func (b *Builder) BuildCore( } } - archiveFile, objectFiles, err := b.compileCore( - actualPlatform, - compilationDatabase, - progress, progressCB, - ) + archiveFile, objectFiles, err := b.compileCore(actualPlatform, compilationDatabase) if err != nil { return nil, nil, errors.WithStack(err) } @@ -71,7 +64,6 @@ func (b *Builder) BuildCore( func (b *Builder) compileCore( actualPlatform *cores.PlatformRelease, compilationDatabase *compilation.Database, - progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (*paths.Path, paths.PathList, error) { coreFolder := b.buildProperties.GetPath("build.core.path") variantFolder := b.buildProperties.GetPath("build.variant.path") @@ -92,7 +84,7 @@ func (b *Builder) compileCore( compilationDatabase, b.jobs, b.logger, - progress, progressCB, + b.Progress, ) if err != nil { return nil, nil, errors.WithStack(err) @@ -143,7 +135,7 @@ func (b *Builder) compileCore( compilationDatabase, b.jobs, b.logger, - progress, progressCB, + b.Progress, ) if err != nil { return nil, nil, errors.WithStack(err) diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index ecd310d3402..b0a559a3bcd 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -21,12 +21,10 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" - "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/libraries" f "github.com/arduino/arduino-cli/internal/algorithms" - rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" @@ -44,7 +42,6 @@ func (b *Builder) BuildLibraries( includesFolders paths.PathList, importedLibraries libraries.List, compilationDatabase *compilation.Database, - progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) libs := importedLibraries @@ -53,11 +50,7 @@ func (b *Builder) BuildLibraries( return nil, errors.WithStack(err) } - librariesObjectFiles, err := b.compileLibraries( - libs, includes, - compilationDatabase, - progress, progressCB, - ) + librariesObjectFiles, err := b.compileLibraries(libs, includes, compilationDatabase) if err != nil { return nil, errors.WithStack(err) } @@ -124,34 +117,23 @@ func (b *Builder) findExpectedPrecompiledLibFolder( return nil } -func (b *Builder) compileLibraries( - libraries libraries.List, includes []string, - compilationDatabase *compilation.Database, - progress *progress.Struct, progressCB rpc.TaskProgressCB, -) (paths.PathList, error) { - progress.AddSubSteps(len(libraries)) - defer progress.RemoveSubSteps() +func (b *Builder) compileLibraries(libraries libraries.List, includes []string, compilationDatabase *compilation.Database) (paths.PathList, error) { + b.Progress.AddSubSteps(len(libraries)) + defer b.Progress.RemoveSubSteps() objectFiles := paths.NewPathList() for _, library := range libraries { libraryObjectFiles, err := b.compileLibrary( library, includes, compilationDatabase, - progress, progressCB, ) if err != nil { return nil, errors.WithStack(err) } objectFiles.AddAll(libraryObjectFiles) - progress.CompleteStep() - // PushProgress - if progressCB != nil { - progressCB(&rpc.TaskProgress{ - Percent: progress.Progress, - Completed: progress.Progress >= 100.0, - }) - } + b.Progress.CompleteStep() + b.Progress.PushProgress() } return objectFiles, nil @@ -160,7 +142,6 @@ func (b *Builder) compileLibraries( func (b *Builder) compileLibrary( library *libraries.Library, includes []string, compilationDatabase *compilation.Database, - progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { if b.logger.Verbose() { b.logger.Info(tr(`Compiling library "%[1]s"`, library.Name)) @@ -227,7 +208,7 @@ func (b *Builder) compileLibrary( compilationDatabase, b.jobs, b.logger, - progress, progressCB, + b.Progress, ) if err != nil { return nil, errors.WithStack(err) @@ -258,7 +239,7 @@ func (b *Builder) compileLibrary( compilationDatabase, b.jobs, b.logger, - progress, progressCB, + b.Progress, ) if err != nil { return nil, errors.WithStack(err) @@ -273,7 +254,7 @@ func (b *Builder) compileLibrary( compilationDatabase, b.jobs, b.logger, - progress, progressCB, + b.Progress, ) if err != nil { return nil, errors.WithStack(err) diff --git a/arduino/builder/progress/progress.go b/arduino/builder/progress/progress.go index 2bdfaeb7539..a3fa9b09d9b 100644 --- a/arduino/builder/progress/progress.go +++ b/arduino/builder/progress/progress.go @@ -15,11 +15,19 @@ package progress +import rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" + // Struct fixdoc type Struct struct { Progress float32 StepAmount float32 Parent *Struct + callback rpc.TaskProgressCB +} + +// New fixdoc +func New(callback rpc.TaskProgressCB) *Struct { + return &Struct{callback: callback} } // AddSubSteps fixdoc @@ -46,3 +54,13 @@ func (p *Struct) RemoveSubSteps() { func (p *Struct) CompleteStep() { p.Progress += p.StepAmount } + +// PushProgress fixdoc +func (p *Struct) PushProgress() { + if p.callback != nil { + p.callback(&rpc.TaskProgress{ + Percent: p.Progress, + Completed: p.Progress >= 100.0, + }) + } +} diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index 5774da5e15a..ed279d6fbdb 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -25,12 +25,10 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" - "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/i18n" f "github.com/arduino/arduino-cli/internal/algorithms" - rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-paths-helper" "github.com/marcinbor85/gohex" @@ -185,7 +183,6 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error { func (b *Builder) BuildSketch( includesFolders paths.PathList, compilationDatabase *compilation.Database, - progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) @@ -199,7 +196,7 @@ func (b *Builder) BuildSketch( compilationDatabase, b.jobs, b.builderLogger, - progress, progressCB, + b.Progress, ) if err != nil { return nil, errors.WithStack(err) @@ -214,7 +211,7 @@ func (b *Builder) BuildSketch( compilationDatabase, b.jobs, b.builderLogger, - progress, progressCB, + b.Progress, ) if err != nil { return nil, errors.WithStack(err) diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index 9bf577a1686..86bf6e6fa16 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -54,7 +54,7 @@ func TestMergeSketchSources(t *testing.T) { b, err := NewBuilder( sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, false, nil) + nil, nil, nil, nil, fqbn, false, nil, false, nil, nil) require.NoError(t, err) offset, source, err := b.sketchMergeSources(nil) @@ -73,7 +73,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { // ensure not to include Arduino.h when it's already there b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, false, nil) + nil, nil, nil, nil, fqbn, false, nil, false, nil, nil) require.NoError(t, err) _, source, err := b.sketchMergeSources(nil) @@ -95,7 +95,7 @@ func TestCopyAdditionalFiles(t *testing.T) { require.NoError(t, err) b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, false, nil) + nil, nil, nil, nil, fqbn, false, nil, false, nil, nil) require.NoError(t, err) // copy the sketch over, create a fake main file we don't care about it diff --git a/arduino/builder/utils/utils.go b/arduino/builder/utils/utils.go index ea3f80c5326..f0ae458512a 100644 --- a/arduino/builder/utils/utils.go +++ b/arduino/builder/utils/utils.go @@ -33,7 +33,6 @@ import ( "github.com/arduino/arduino-cli/executils" "github.com/arduino/arduino-cli/i18n" f "github.com/arduino/arduino-cli/internal/algorithms" - rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" @@ -358,7 +357,7 @@ func CompileFiles( compilationDatabase *compilation.Database, jobs int, builderLogger *logger.BuilderLogger, - progress *progress.Struct, progressCB rpc.TaskProgressCB, + progress *progress.Struct, ) (paths.PathList, error) { return compileFiles( onlyUpdateCompilationDatabase, @@ -368,7 +367,7 @@ func CompileFiles( false, buildPath, buildProperties, includes, builderLogger, - progress, progressCB, + progress, ) } @@ -381,7 +380,7 @@ func CompileFilesRecursive( compilationDatabase *compilation.Database, jobs int, builderLogger *logger.BuilderLogger, - progress *progress.Struct, progressCB rpc.TaskProgressCB, + progress *progress.Struct, ) (paths.PathList, error) { return compileFiles( onlyUpdateCompilationDatabase, @@ -391,7 +390,7 @@ func CompileFilesRecursive( true, buildPath, buildProperties, includes, builderLogger, - progress, progressCB, + progress, ) } @@ -406,7 +405,6 @@ func compileFiles( includes []string, builderLogger *logger.BuilderLogger, progress *progress.Struct, - progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { validExtensions := []string{} for ext := range globals.SourceFilesValidExtensions { @@ -483,13 +481,7 @@ func compileFiles( queue <- source progress.CompleteStep() - // PushProgress - if progressCB != nil { - progressCB(&rpc.TaskProgress{ - Percent: progress.Progress, - Completed: progress.Progress >= 100.0, - }) - } + progress.PushProgress() } close(queue) wg.Wait() diff --git a/commands/compile/compile.go b/commands/compile/compile.go index b17b130040d..a599590a6b2 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -28,6 +28,7 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" + "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" "github.com/arduino/arduino-cli/arduino/sketch" @@ -178,7 +179,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.PackageManager = pme builderCtx.TargetPlatform = targetPlatform builderCtx.ActualPlatform = buildPlatform - builderCtx.ProgressCB = progressCB // FIXME: This will be redundant when arduino-builder will be part of the cli builderCtx.HardwareDirs = configuration.HardwareDirectories(configuration.Settings) @@ -208,6 +208,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream req.GetSourceOverride(), req.GetCreateCompilationDatabaseOnly(), builderLogger, + progress.New(progressCB), ) if err != nil { if strings.Contains(err.Error(), "invalid build properties") { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 9149f2bc309..6a5c9a9f3ca 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -67,7 +67,6 @@ func (s *Builder) Run(ctx *types.Context) error { sketchObjectFiles, err := ctx.Builder.BuildSketch( ctx.SketchLibrariesDetector.IncludeFolders(), ctx.CompilationDatabase, - &ctx.Progress, ctx.ProgressCB, ) if err != nil { return err @@ -96,7 +95,6 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries(), ctx.CompilationDatabase, - &ctx.Progress, ctx.ProgressCB, ) if err != nil { return err @@ -115,11 +113,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - objectFiles, archiveFile, err := ctx.Builder.BuildCore( - ctx.ActualPlatform, - ctx.CompilationDatabase, - &ctx.Progress, ctx.ProgressCB, - ) + objectFiles, archiveFile, err := ctx.Builder.BuildCore(ctx.ActualPlatform, ctx.CompilationDatabase) ctx.CoreObjectsFiles = objectFiles ctx.CoreArchiveFilePath = archiveFile @@ -168,8 +162,8 @@ func (s *Builder) Run(ctx *types.Context) error { }), } - ctx.Progress.AddSubSteps(len(commands) + 5) - defer ctx.Progress.RemoveSubSteps() + ctx.Builder.Progress.AddSubSteps(len(commands) + 5) + defer ctx.Builder.Progress.RemoveSubSteps() for _, command := range commands { PrintRingNameIfDebug(ctx, command) @@ -178,8 +172,8 @@ func (s *Builder) Run(ctx *types.Context) error { mainErr = errors.WithStack(err) break } - ctx.Progress.CompleteStep() - ctx.PushProgress() + ctx.Builder.Progress.CompleteStep() + ctx.Builder.Progress.PushProgress() } if ctx.CompilationDatabase != nil { @@ -220,8 +214,8 @@ func (s *Builder) Run(ctx *types.Context) error { otherErr = errors.WithStack(err) break } - ctx.Progress.CompleteStep() - ctx.PushProgress() + ctx.Builder.Progress.CompleteStep() + ctx.Builder.Progress.PushProgress() } if mainErr != nil { @@ -278,8 +272,8 @@ func (s *Preprocess) Run(ctx *types.Context) error { } func runCommands(ctx *types.Context, commands []types.Command) error { - ctx.Progress.AddSubSteps(len(commands)) - defer ctx.Progress.RemoveSubSteps() + ctx.Builder.Progress.AddSubSteps(len(commands)) + defer ctx.Builder.Progress.RemoveSubSteps() for _, command := range commands { PrintRingNameIfDebug(ctx, command) @@ -287,8 +281,8 @@ func runCommands(ctx *types.Context, commands []types.Command) error { if err != nil { return errors.WithStack(err) } - ctx.Progress.CompleteStep() - ctx.PushProgress() + ctx.Builder.Progress.CompleteStep() + ctx.Builder.Progress.PushProgress() } return nil } diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 11082e44a09..7262e990efa 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -106,7 +106,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, nil, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, nil, false, builderLogger, + ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, nil, false, builderLogger, nil, ) require.NoError(t, err) if fqbnString != "" { @@ -119,7 +119,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, boardBuildProperties, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, fqbn, false, nil, false, builderLogger) + ctx.BuiltInLibrariesDirs, fqbn, false, nil, false, builderLogger, nil) require.NoError(t, err) ctx.PackageManager = pme diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 8769b643666..cc8f45c8b33 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -20,10 +20,8 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" - "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" - rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" paths "github.com/arduino/go-paths-helper" ) @@ -51,23 +49,9 @@ type Context struct { // C++ Parsing LineOffset int - // Dry run, only create progress map - Progress progress.Struct - // Send progress events to this callback - ProgressCB rpc.TaskProgressCB - // Sizer results ExecutableSectionsSize builder.ExecutablesFileSections // Compilation Database to build/update CompilationDatabase *compilation.Database } - -func (ctx *Context) PushProgress() { - if ctx.ProgressCB != nil { - ctx.ProgressCB(&rpc.TaskProgress{ - Percent: ctx.Progress.Progress, - Completed: ctx.Progress.Progress >= 100.0, - }) - } -} From aa904b6d366cc475df2dcdd8811c6aa105983ad4 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 20:12:50 +0200 Subject: [PATCH 28/34] remove ExecutableSectionSize from Context --- arduino/builder/builder.go | 8 ++++++++ arduino/builder/sizer.go | 16 ++++++++++++---- commands/compile/compile.go | 2 +- legacy/builder/builder.go | 4 +--- legacy/builder/types/context.go | 3 --- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 931c8b829c9..93c5579b205 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -63,6 +63,9 @@ type Builder struct { // Progress of all various steps Progress *progress.Struct + // Sizer results + executableSectionsSize ExecutablesFileSections + *BuildOptionsManager } @@ -184,3 +187,8 @@ func (b *Builder) GetSketchBuildPath() *paths.Path { func (b *Builder) GetLibrariesBuildPath() *paths.Path { return b.librariesBuildPath } + +// ExecutableSectionsSize fixdoc +func (b *Builder) ExecutableSectionsSize() ExecutablesFileSections { + return b.executableSectionsSize +} diff --git a/arduino/builder/sizer.go b/arduino/builder/sizer.go index 7b984b24f67..7f461f8292e 100644 --- a/arduino/builder/sizer.go +++ b/arduino/builder/sizer.go @@ -51,16 +51,24 @@ func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.Execut } // Size fixdoc -func (b *Builder) Size(sketchError bool) (ExecutablesFileSections, error) { +func (b *Builder) Size(sketchError bool) error { if b.onlyUpdateCompilationDatabase || sketchError { - return nil, nil + return nil } + check := b.checkSize if b.buildProperties.ContainsKey("recipe.advanced_size.pattern") { - return b.checkSizeAdvanced() + check = b.checkSizeAdvanced } - return b.checkSize() + result, err := check() + if err != nil { + return err + } + + b.executableSectionsSize = result + + return nil } func (b *Builder) checkSizeAdvanced() (ExecutablesFileSections, error) { diff --git a/commands/compile/compile.go b/commands/compile/compile.go index a599590a6b2..71ff699dcb2 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -384,7 +384,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream } } - r.ExecutableSectionsSize = builderCtx.ExecutableSectionsSize.ToRPCExecutableSectionSizeArray() + r.ExecutableSectionsSize = sketchBuilder.ExecutableSectionsSize().ToRPCExecutableSectionSizeArray() logrus.Tracef("Compile %s for %s successful", sk.Name, fqbnIn) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 6a5c9a9f3ca..b1c7b29e5fa 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -202,9 +202,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - executableSectionsSize, err := ctx.Builder.Size(mainErr != nil) - ctx.ExecutableSectionsSize = executableSectionsSize - return err + return ctx.Builder.Size(mainErr != nil) }), } for _, command := range commands { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index cc8f45c8b33..3e6dd46ba26 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -49,9 +49,6 @@ type Context struct { // C++ Parsing LineOffset int - // Sizer results - ExecutableSectionsSize builder.ExecutablesFileSections - // Compilation Database to build/update CompilationDatabase *compilation.Database } From 34e6204e68a1b2a3f7c281fbe98f50ea50964d15 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 20:23:48 +0200 Subject: [PATCH 29/34] remove CompilationDatabase from Context --- arduino/builder/builder.go | 11 +++++++++++ arduino/builder/core.go | 17 +++++------------ arduino/builder/libraries.go | 27 ++++++++------------------- arduino/builder/sketch.go | 10 +++------- commands/compile/compile.go | 5 ----- legacy/builder/builder.go | 12 +++--------- legacy/builder/types/context.go | 4 ---- 7 files changed, 30 insertions(+), 56 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 93c5579b205..4c0f0a435b1 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -19,6 +19,7 @@ import ( "errors" "fmt" + "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/cores" @@ -59,6 +60,8 @@ type Builder struct { // Set to true to skip build and produce only Compilation Database onlyUpdateCompilationDatabase bool + // Compilation Database to build/update + compilationDatabase *compilation.Database // Progress of all various steps Progress *progress.Struct @@ -152,6 +155,7 @@ func NewBuilder( clean: clean, sourceOverrides: sourceOverrides, onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase, + compilationDatabase: compilation.NewDatabase(buildPath.Join("compile_commands.json")), Progress: progressStats, BuildOptionsManager: NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, @@ -192,3 +196,10 @@ func (b *Builder) GetLibrariesBuildPath() *paths.Path { func (b *Builder) ExecutableSectionsSize() ExecutablesFileSections { return b.executableSectionsSize } + +// SaveCompilationDatabase fixdoc +func (b *Builder) SaveCompilationDatabase() { + if b.compilationDatabase != nil { + b.compilationDatabase.SaveToFile() + } +} diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 8e266154594..16e25cdf1f3 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -22,7 +22,6 @@ import ( "os" "strings" - "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/cores" @@ -33,10 +32,7 @@ import ( ) // BuildCore fixdoc -func (b *Builder) BuildCore( - actualPlatform *cores.PlatformRelease, - compilationDatabase *compilation.Database, -) (paths.PathList, *paths.Path, error) { +func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) (paths.PathList, *paths.Path, error) { if err := b.coreBuildPath.MkdirAll(); err != nil { return nil, nil, errors.WithStack(err) } @@ -53,7 +49,7 @@ func (b *Builder) BuildCore( } } - archiveFile, objectFiles, err := b.compileCore(actualPlatform, compilationDatabase) + archiveFile, objectFiles, err := b.compileCore(actualPlatform) if err != nil { return nil, nil, errors.WithStack(err) } @@ -61,10 +57,7 @@ func (b *Builder) BuildCore( return objectFiles, archiveFile, nil } -func (b *Builder) compileCore( - actualPlatform *cores.PlatformRelease, - compilationDatabase *compilation.Database, -) (*paths.Path, paths.PathList, error) { +func (b *Builder) compileCore(actualPlatform *cores.PlatformRelease) (*paths.Path, paths.PathList, error) { coreFolder := b.buildProperties.GetPath("build.core.path") variantFolder := b.buildProperties.GetPath("build.variant.path") targetCoreFolder := b.buildProperties.GetPath("runtime.platform.path") @@ -81,7 +74,7 @@ func (b *Builder) compileCore( variantObjectFiles, err = utils.CompileFilesRecursive( variantFolder, b.coreBuildPath, b.buildProperties, includes, b.onlyUpdateCompilationDatabase, - compilationDatabase, + b.compilationDatabase, b.jobs, b.logger, b.Progress, @@ -132,7 +125,7 @@ func (b *Builder) compileCore( coreObjectFiles, err := utils.CompileFilesRecursive( coreFolder, b.coreBuildPath, b.buildProperties, includes, b.onlyUpdateCompilationDatabase, - compilationDatabase, + b.compilationDatabase, b.jobs, b.logger, b.Progress, diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index b0a559a3bcd..0c39353426f 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -19,7 +19,6 @@ import ( "strings" "time" - "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/cores" @@ -38,11 +37,7 @@ var ( ) // BuildLibraries fixdoc -func (b *Builder) BuildLibraries( - includesFolders paths.PathList, - importedLibraries libraries.List, - compilationDatabase *compilation.Database, -) (paths.PathList, error) { +func (b *Builder) BuildLibraries(includesFolders paths.PathList, importedLibraries libraries.List) (paths.PathList, error) { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) libs := importedLibraries @@ -50,7 +45,7 @@ func (b *Builder) BuildLibraries( return nil, errors.WithStack(err) } - librariesObjectFiles, err := b.compileLibraries(libs, includes, compilationDatabase) + librariesObjectFiles, err := b.compileLibraries(libs, includes) if err != nil { return nil, errors.WithStack(err) } @@ -117,16 +112,13 @@ func (b *Builder) findExpectedPrecompiledLibFolder( return nil } -func (b *Builder) compileLibraries(libraries libraries.List, includes []string, compilationDatabase *compilation.Database) (paths.PathList, error) { +func (b *Builder) compileLibraries(libraries libraries.List, includes []string) (paths.PathList, error) { b.Progress.AddSubSteps(len(libraries)) defer b.Progress.RemoveSubSteps() objectFiles := paths.NewPathList() for _, library := range libraries { - libraryObjectFiles, err := b.compileLibrary( - library, includes, - compilationDatabase, - ) + libraryObjectFiles, err := b.compileLibrary(library, includes) if err != nil { return nil, errors.WithStack(err) } @@ -139,10 +131,7 @@ func (b *Builder) compileLibraries(libraries libraries.List, includes []string, return objectFiles, nil } -func (b *Builder) compileLibrary( - library *libraries.Library, includes []string, - compilationDatabase *compilation.Database, -) (paths.PathList, error) { +func (b *Builder) compileLibrary(library *libraries.Library, includes []string) (paths.PathList, error) { if b.logger.Verbose() { b.logger.Info(tr(`Compiling library "%[1]s"`, library.Name)) } @@ -205,7 +194,7 @@ func (b *Builder) compileLibrary( libObjectFiles, err := utils.CompileFilesRecursive( library.SourceDir, libraryBuildPath, b.buildProperties, includes, b.onlyUpdateCompilationDatabase, - compilationDatabase, + b.compilationDatabase, b.jobs, b.logger, b.Progress, @@ -236,7 +225,7 @@ func (b *Builder) compileLibrary( libObjectFiles, err := utils.CompileFiles( library.SourceDir, libraryBuildPath, b.buildProperties, includes, b.onlyUpdateCompilationDatabase, - compilationDatabase, + b.compilationDatabase, b.jobs, b.logger, b.Progress, @@ -251,7 +240,7 @@ func (b *Builder) compileLibrary( utilityObjectFiles, err := utils.CompileFiles( library.UtilityDir, utilityBuildPath, b.buildProperties, includes, b.onlyUpdateCompilationDatabase, - compilationDatabase, + b.compilationDatabase, b.jobs, b.logger, b.Progress, diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index ed279d6fbdb..b5ddd148ccc 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -23,7 +23,6 @@ import ( "strconv" "strings" - "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/cpp" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/sketch" @@ -180,10 +179,7 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error { } // BuildSketch fixdoc -func (b *Builder) BuildSketch( - includesFolders paths.PathList, - compilationDatabase *compilation.Database, -) (paths.PathList, error) { +func (b *Builder) BuildSketch(includesFolders paths.PathList) (paths.PathList, error) { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) if err := b.sketchBuildPath.MkdirAll(); err != nil { @@ -193,7 +189,7 @@ func (b *Builder) BuildSketch( sketchObjectFiles, err := utils.CompileFiles( b.sketchBuildPath, b.sketchBuildPath, b.buildProperties, includes, b.onlyUpdateCompilationDatabase, - compilationDatabase, + b.compilationDatabase, b.jobs, b.builderLogger, b.Progress, @@ -208,7 +204,7 @@ func (b *Builder) BuildSketch( srcObjectFiles, err := utils.CompileFilesRecursive( sketchSrcPath, sketchSrcPath, b.buildProperties, includes, b.onlyUpdateCompilationDatabase, - compilationDatabase, + b.compilationDatabase, b.jobs, b.builderLogger, b.Progress, diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 71ff699dcb2..539332470d1 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -25,7 +25,6 @@ import ( "github.com/arduino/arduino-cli/arduino" bldr "github.com/arduino/arduino-cli/arduino/builder" - "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" @@ -223,10 +222,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream } builderCtx.Builder = sketchBuilder - builderCtx.CompilationDatabase = compilation.NewDatabase( - sketchBuilder.GetBuildPath().Join("compile_commands.json"), - ) - var libsManager *librariesmanager.LibrariesManager if pme.GetProfile() != nil { libsManager = lm diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index b1c7b29e5fa..d78e57b94da 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -64,10 +64,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - sketchObjectFiles, err := ctx.Builder.BuildSketch( - ctx.SketchLibrariesDetector.IncludeFolders(), - ctx.CompilationDatabase, - ) + sketchObjectFiles, err := ctx.Builder.BuildSketch(ctx.SketchLibrariesDetector.IncludeFolders()) if err != nil { return err } @@ -94,7 +91,6 @@ func (s *Builder) Run(ctx *types.Context) error { librariesObjectFiles, err := ctx.Builder.BuildLibraries( ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries(), - ctx.CompilationDatabase, ) if err != nil { return err @@ -113,7 +109,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - objectFiles, archiveFile, err := ctx.Builder.BuildCore(ctx.ActualPlatform, ctx.CompilationDatabase) + objectFiles, archiveFile, err := ctx.Builder.BuildCore(ctx.ActualPlatform) ctx.CoreObjectsFiles = objectFiles ctx.CoreArchiveFilePath = archiveFile @@ -176,9 +172,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.Builder.Progress.PushProgress() } - if ctx.CompilationDatabase != nil { - ctx.CompilationDatabase.SaveToFile() - } + ctx.Builder.SaveCompilationDatabase() var otherErr error commands = []types.Command{ diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 3e6dd46ba26..e0143582006 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -17,7 +17,6 @@ package types import ( "github.com/arduino/arduino-cli/arduino/builder" - "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/cores" @@ -48,7 +47,4 @@ type Context struct { // C++ Parsing LineOffset int - - // Compilation Database to build/update - CompilationDatabase *compilation.Database } From d5c040f3698616f9cad6a6f3b0fd6bf0004faf70 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 20:27:34 +0200 Subject: [PATCH 30/34] remove LineOffset from Context --- arduino/builder/builder.go | 3 +++ arduino/builder/export_cmake.go | 3 +-- arduino/builder/preprocess_sketch.go | 4 ++-- arduino/builder/sketch.go | 14 ++++++++------ legacy/builder/builder.go | 12 ++++-------- legacy/builder/types/context.go | 3 --- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 4c0f0a435b1..668e83496b3 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -69,6 +69,9 @@ type Builder struct { // Sizer results executableSectionsSize ExecutablesFileSections + // C++ Parsing + lineOffset int + *BuildOptionsManager } diff --git a/arduino/builder/export_cmake.go b/arduino/builder/export_cmake.go index 1b10c7f4208..de7bdf6db88 100644 --- a/arduino/builder/export_cmake.go +++ b/arduino/builder/export_cmake.go @@ -39,7 +39,6 @@ func (b *Builder) ExportProjectCMake( sketchError bool, // Was there an error while compiling the sketch? importedLibraries libraries.List, includeFolders paths.PathList, - lineOffset int, ) error { // copies the contents of the file named src to the file named // by dst. The file will be created if it does not already exist. If the @@ -242,7 +241,7 @@ func (b *Builder) ExportProjectCMake( fmt.Println(err) } - if err := b.PreprocessSketch(includeFolders, lineOffset); err != nil { + if err := b.PreprocessSketch(includeFolders); err != nil { return err } diff --git a/arduino/builder/preprocess_sketch.go b/arduino/builder/preprocess_sketch.go index 63d472ed318..cf6adb208a2 100644 --- a/arduino/builder/preprocess_sketch.go +++ b/arduino/builder/preprocess_sketch.go @@ -21,10 +21,10 @@ import ( ) // PreprocessSketch fixdoc -func (b *Builder) PreprocessSketch(includes paths.PathList, lineOffset int) error { +func (b *Builder) PreprocessSketch(includes paths.PathList) error { // In the future we might change the preprocessor normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags( - b.sketch, b.buildPath, includes, lineOffset, + b.sketch, b.buildPath, includes, b.lineOffset, b.buildProperties, b.onlyUpdateCompilationDatabase, ) if b.logger.Verbose() { diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index b5ddd148ccc..ddd0f60f2e7 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -47,26 +47,28 @@ func (b *Builder) Sketch() *sketch.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). -func (b *Builder) PrepareSketchBuildPath() (int, error) { +func (b *Builder) PrepareSketchBuildPath() error { if err := b.sketchBuildPath.MkdirAll(); err != nil { - return 0, errors.Wrap(err, tr("unable to create a folder to save the sketch")) + return errors.Wrap(err, tr("unable to create a folder to save the sketch")) } offset, mergedSource, err := b.sketchMergeSources(b.sourceOverrides) if err != nil { - return 0, err + return err } destFile := b.sketchBuildPath.Join(b.sketch.MainFile.Base() + ".cpp") if err := destFile.WriteFile([]byte(mergedSource)); err != nil { - return 0, err + return err } if err := b.sketchCopyAdditionalFiles(b.sketchBuildPath, b.sourceOverrides); err != nil { - return 0, err + return err } - return offset, nil + b.lineOffset = offset + + return nil } // sketchMergeSources merges all the .ino source files included in a sketch to produce diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index d78e57b94da..8f178efa0b1 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -36,7 +36,7 @@ func (s *Builder) Run(ctx *types.Context) error { return err } - var _err, mainErr error + var mainErr error commands := []types.Command{ containerBuildOptions(ctx), @@ -45,8 +45,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath() - return _err + return ctx.Builder.PrepareSketchBuildPath() }), logIfVerbose(false, tr("Detecting libraries used...")), @@ -191,7 +190,6 @@ func (s *Builder) Run(ctx *types.Context) error { mainErr != nil, ctx.SketchLibrariesDetector.ImportedLibraries(), ctx.SketchLibrariesDetector.IncludeFolders(), - ctx.LineOffset, ) }), @@ -219,7 +217,7 @@ func (s *Builder) Run(ctx *types.Context) error { func preprocessSketchCommand(ctx *types.Context) types.BareCommand { return func(ctx *types.Context) error { - return ctx.Builder.PreprocessSketch(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset) + return ctx.Builder.PreprocessSketch(ctx.SketchLibrariesDetector.IncludeFolders()) } } @@ -230,7 +228,6 @@ func (s *Preprocess) Run(ctx *types.Context) error { return err } - var _err error commands := []types.Command{ containerBuildOptions(ctx), @@ -239,8 +236,7 @@ func (s *Preprocess) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, _err = ctx.Builder.PrepareSketchBuildPath() - return _err + return ctx.Builder.PrepareSketchBuildPath() }), findIncludes(ctx), diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index e0143582006..851674e1d95 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -44,7 +44,4 @@ type Context struct { CoreObjectsFiles paths.PathList LibrariesObjectFiles paths.PathList SketchObjectFiles paths.PathList - - // C++ Parsing - LineOffset int } From 8995aff4cf134c9d1a99e7e07857beeed2e03040 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 20:41:02 +0200 Subject: [PATCH 31/34] introduce BuilderArtifacts to better isolate write operations --- arduino/builder/builder.go | 17 +++++++++++++++++ arduino/builder/core.go | 13 +++++++------ arduino/builder/libraries.go | 10 +++++----- arduino/builder/linker.go | 12 ++++++------ arduino/builder/sketch.go | 11 ++++++----- legacy/builder/builder.go | 32 ++++---------------------------- legacy/builder/types/context.go | 5 ----- 7 files changed, 45 insertions(+), 55 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index 668e83496b3..ec6dbb99800 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -72,9 +72,24 @@ type Builder struct { // C++ Parsing lineOffset int + buildArtifacts *BuildArtifacts + *BuildOptionsManager } +// BuildArtifacts contains the result of various build +type BuildArtifacts struct { + // populated by BuildCore + coreArchiveFilePath *paths.Path + coreObjectsFiles paths.PathList + + // populated by BuildLibraries + librariesObjectFiles paths.PathList + + // populated by BuildSketch + sketchObjectFiles paths.PathList +} + // NewBuilder creates a sketch Builder. func NewBuilder( sk *sketch.Sketch, @@ -160,6 +175,8 @@ func NewBuilder( onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase, compilationDatabase: compilation.NewDatabase(buildPath.Join("compile_commands.json")), Progress: progressStats, + executableSectionsSize: []ExecutableSectionSize{}, + buildArtifacts: &BuildArtifacts{}, BuildOptionsManager: NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, builtInLibrariesDirs, buildPath, diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 16e25cdf1f3..e02aeb72ce4 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -32,9 +32,9 @@ import ( ) // BuildCore fixdoc -func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) (paths.PathList, *paths.Path, error) { +func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) error { if err := b.coreBuildPath.MkdirAll(); err != nil { - return nil, nil, errors.WithStack(err) + return errors.WithStack(err) } if b.coreBuildCachePath != nil { @@ -45,16 +45,17 @@ func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) (paths.PathLi // compileCore function). b.coreBuildCachePath = nil } else if err := b.coreBuildCachePath.MkdirAll(); err != nil { - return nil, nil, errors.WithStack(err) + return errors.WithStack(err) } } archiveFile, objectFiles, err := b.compileCore(actualPlatform) if err != nil { - return nil, nil, errors.WithStack(err) + return errors.WithStack(err) } - - return objectFiles, archiveFile, nil + b.buildArtifacts.coreObjectsFiles = objectFiles + b.buildArtifacts.coreArchiveFilePath = archiveFile + return nil } func (b *Builder) compileCore(actualPlatform *cores.PlatformRelease) (*paths.Path, paths.PathList, error) { diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index 0c39353426f..bf52c920ffa 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -37,20 +37,20 @@ var ( ) // BuildLibraries fixdoc -func (b *Builder) BuildLibraries(includesFolders paths.PathList, importedLibraries libraries.List) (paths.PathList, error) { +func (b *Builder) BuildLibraries(includesFolders paths.PathList, importedLibraries libraries.List) error { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) libs := importedLibraries if err := b.librariesBuildPath.MkdirAll(); err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } librariesObjectFiles, err := b.compileLibraries(libs, includes) if err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } - - return librariesObjectFiles, nil + b.buildArtifacts.librariesObjectFiles = librariesObjectFiles + return nil } func directoryContainsFile(folder *paths.Path) bool { diff --git a/arduino/builder/linker.go b/arduino/builder/linker.go index 5d88460fbfd..b69e71aaefe 100644 --- a/arduino/builder/linker.go +++ b/arduino/builder/linker.go @@ -25,7 +25,7 @@ import ( ) // Link fixdoc -func (b *Builder) Link(sketchObjectFiles, librariesObjectFiles, coreObjectsFiles paths.PathList, coreArchiveFilePath *paths.Path) error { +func (b *Builder) Link() error { if b.onlyUpdateCompilationDatabase { if b.logger.Verbose() { b.logger.Info(tr("Skip linking of final executable.")) @@ -34,21 +34,21 @@ func (b *Builder) Link(sketchObjectFiles, librariesObjectFiles, coreObjectsFiles } // TODO can we remove this multiple assignations? - objectFilesSketch := sketchObjectFiles - objectFilesLibraries := librariesObjectFiles - objectFilesCore := coreObjectsFiles + objectFilesSketch := b.buildArtifacts.sketchObjectFiles + objectFilesLibraries := b.buildArtifacts.librariesObjectFiles + objectFilesCore := b.buildArtifacts.coreObjectsFiles objectFiles := paths.NewPathList() objectFiles.AddAll(objectFilesSketch) objectFiles.AddAll(objectFilesLibraries) objectFiles.AddAll(objectFilesCore) - coreDotARelPath, err := b.buildPath.RelTo(coreArchiveFilePath) + coreDotARelPath, err := b.buildPath.RelTo(b.buildArtifacts.coreArchiveFilePath) if err != nil { return errors.WithStack(err) } - if err := b.link(objectFiles, coreDotARelPath, coreArchiveFilePath); err != nil { + if err := b.link(objectFiles, coreDotARelPath, b.buildArtifacts.coreArchiveFilePath); err != nil { return errors.WithStack(err) } diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index ddd0f60f2e7..dbab5b521bc 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -181,11 +181,11 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error { } // BuildSketch fixdoc -func (b *Builder) BuildSketch(includesFolders paths.PathList) (paths.PathList, error) { +func (b *Builder) BuildSketch(includesFolders paths.PathList) error { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) if err := b.sketchBuildPath.MkdirAll(); err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } sketchObjectFiles, err := utils.CompileFiles( @@ -197,7 +197,7 @@ func (b *Builder) BuildSketch(includesFolders paths.PathList) (paths.PathList, e b.Progress, ) if err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } // The "src/" subdirectory of a sketch is compiled recursively @@ -212,12 +212,13 @@ func (b *Builder) BuildSketch(includesFolders paths.PathList) (paths.PathList, e b.Progress, ) if err != nil { - return nil, errors.WithStack(err) + return errors.WithStack(err) } sketchObjectFiles.AddAll(srcObjectFiles) } - return sketchObjectFiles, nil + b.buildArtifacts.sketchObjectFiles = sketchObjectFiles + return nil } // MergeSketchWithBootloader fixdoc diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 8f178efa0b1..a1dd20a283e 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -63,12 +63,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - sketchObjectFiles, err := ctx.Builder.BuildSketch(ctx.SketchLibrariesDetector.IncludeFolders()) - if err != nil { - return err - } - ctx.SketchObjectFiles = sketchObjectFiles - return nil + return ctx.Builder.BuildSketch(ctx.SketchLibrariesDetector.IncludeFolders()) }), types.BareCommand(func(ctx *types.Context) error { @@ -87,16 +82,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - librariesObjectFiles, err := ctx.Builder.BuildLibraries( - ctx.SketchLibrariesDetector.IncludeFolders(), - ctx.SketchLibrariesDetector.ImportedLibraries(), - ) - if err != nil { - return err - } - ctx.LibrariesObjectFiles = librariesObjectFiles - - return nil + return ctx.Builder.BuildLibraries(ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries()) }), types.BareCommand(func(ctx *types.Context) error { return recipeByPrefixSuffixRunner(ctx, "recipe.hooks.libraries.postbuild", ".pattern", true) @@ -108,12 +94,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - objectFiles, archiveFile, err := ctx.Builder.BuildCore(ctx.ActualPlatform) - - ctx.CoreObjectsFiles = objectFiles - ctx.CoreArchiveFilePath = archiveFile - - return err + return ctx.Builder.BuildCore(ctx.ActualPlatform) }), types.BareCommand(func(ctx *types.Context) error { @@ -126,12 +107,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - return ctx.Builder.Link( - ctx.SketchObjectFiles, - ctx.LibrariesObjectFiles, - ctx.CoreObjectsFiles, - ctx.CoreArchiveFilePath, - ) + return ctx.Builder.Link() }), types.BareCommand(func(ctx *types.Context) error { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 851674e1d95..5b5494ed0c9 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -39,9 +39,4 @@ type Context struct { PackageManager *packagemanager.Explorer TargetPlatform *cores.PlatformRelease ActualPlatform *cores.PlatformRelease - - CoreArchiveFilePath *paths.Path - CoreObjectsFiles paths.PathList - LibrariesObjectFiles paths.PathList - SketchObjectFiles paths.PathList } From e0055869bafcf53502735f51b638743eabd80419 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Wed, 13 Sep 2023 08:52:05 +0200 Subject: [PATCH 32/34] remove ActualPlatform and TargetPlatform from Context --- arduino/builder/builder.go | 11 ++++++++ arduino/builder/core.go | 9 +++---- arduino/builder/libraries.go | 8 ++---- arduino/builder/sketch_test.go | 27 ++++---------------- commands/compile/compile.go | 6 ++--- legacy/builder/builder.go | 5 ++-- legacy/builder/test/builder_test.go | 20 ++++++++++----- legacy/builder/test/libraries_loader_test.go | 17 +++++++++--- legacy/builder/types/context.go | 3 --- 9 files changed, 53 insertions(+), 53 deletions(-) diff --git a/arduino/builder/builder.go b/arduino/builder/builder.go index ec6dbb99800..a856106d574 100644 --- a/arduino/builder/builder.go +++ b/arduino/builder/builder.go @@ -72,6 +72,9 @@ type Builder struct { // C++ Parsing lineOffset int + targetPlatform *cores.PlatformRelease + actualPlatform *cores.PlatformRelease + buildArtifacts *BuildArtifacts *BuildOptionsManager @@ -105,6 +108,7 @@ func NewBuilder( clean bool, sourceOverrides map[string]string, onlyUpdateCompilationDatabase bool, + targetPlatform, actualPlatform *cores.PlatformRelease, logger *logger.BuilderLogger, progressStats *progress.Struct, ) (*Builder, error) { @@ -177,6 +181,8 @@ func NewBuilder( Progress: progressStats, executableSectionsSize: []ExecutableSectionSize{}, buildArtifacts: &BuildArtifacts{}, + targetPlatform: targetPlatform, + actualPlatform: actualPlatform, BuildOptionsManager: NewBuildOptionsManager( hardwareDirs, builtInToolsDirs, otherLibrariesDirs, builtInLibrariesDirs, buildPath, @@ -223,3 +229,8 @@ func (b *Builder) SaveCompilationDatabase() { b.compilationDatabase.SaveToFile() } } + +// TargetPlatform fixdoc +func (b *Builder) TargetPlatform() *cores.PlatformRelease { + return b.targetPlatform +} diff --git a/arduino/builder/core.go b/arduino/builder/core.go index e02aeb72ce4..2fcbb682a7c 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -24,7 +24,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/cpp" "github.com/arduino/arduino-cli/arduino/builder/utils" - "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/buildcache" f "github.com/arduino/arduino-cli/internal/algorithms" "github.com/arduino/go-paths-helper" @@ -32,7 +31,7 @@ import ( ) // BuildCore fixdoc -func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) error { +func (b *Builder) BuildCore() error { if err := b.coreBuildPath.MkdirAll(); err != nil { return errors.WithStack(err) } @@ -49,7 +48,7 @@ func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) error { } } - archiveFile, objectFiles, err := b.compileCore(actualPlatform) + archiveFile, objectFiles, err := b.compileCore() if err != nil { return errors.WithStack(err) } @@ -58,7 +57,7 @@ func (b *Builder) BuildCore(actualPlatform *cores.PlatformRelease) error { return nil } -func (b *Builder) compileCore(actualPlatform *cores.PlatformRelease) (*paths.Path, paths.PathList, error) { +func (b *Builder) compileCore() (*paths.Path, paths.PathList, error) { coreFolder := b.buildProperties.GetPath("build.core.path") variantFolder := b.buildProperties.GetPath("build.variant.path") targetCoreFolder := b.buildProperties.GetPath("runtime.platform.path") @@ -154,7 +153,7 @@ func (b *Builder) compileCore(actualPlatform *cores.PlatformRelease) (*paths.Pat b.logger.Info(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore)) } else if os.IsNotExist(err) { b.logger.Info(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s", - actualPlatform, + b.actualPlatform, "https://arduino.github.io/arduino-cli/latest/platform-specification/#recipes-to-build-the-corea-archive-file")) } else { b.logger.Info(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err)) diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index bf52c920ffa..f5b6a46957b 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -21,7 +21,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/cpp" "github.com/arduino/arduino-cli/arduino/builder/utils" - "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/libraries" f "github.com/arduino/arduino-cli/internal/algorithms" "github.com/arduino/go-paths-helper" @@ -289,11 +288,8 @@ func (b *Builder) RemoveUnusedCompiledLibraries(importedLibraries libraries.List } // WarnAboutArchIncompatibleLibraries fixdoc -func (b *Builder) WarnAboutArchIncompatibleLibraries( - targetPlatform *cores.PlatformRelease, - importedLibraries libraries.List, -) { - archs := []string{targetPlatform.Platform.Architecture} +func (b *Builder) WarnAboutArchIncompatibleLibraries(importedLibraries libraries.List) { + archs := []string{b.targetPlatform.Platform.Architecture} overrides, _ := b.buildProperties.GetOk("architecture.override_check") if overrides != "" { archs = append(archs, strings.Split(overrides, ",")...) diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index 86bf6e6fa16..1ae684e12ed 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -21,7 +21,6 @@ import ( "strings" "testing" - "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" @@ -49,13 +48,7 @@ func TestMergeSketchSources(t *testing.T) { } mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource) - fqbn, err := cores.ParseFQBN("a:b:c") - require.NoError(t, err) - - b, err := NewBuilder( - sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, false, nil, nil) - require.NoError(t, err) + b := Builder{sketch: sk} offset, source, err := b.sketchMergeSources(nil) require.Nil(t, err) @@ -68,13 +61,8 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { require.Nil(t, err) require.NotNil(t, sk) - fqbn, err := cores.ParseFQBN("a:b:c") - require.NoError(t, err) - // ensure not to include Arduino.h when it's already there - b, err := NewBuilder(sk, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, false, nil, nil) - require.NoError(t, err) + b := Builder{sketch: sk} _, source, err := b.sketchMergeSources(nil) require.Nil(t, err) @@ -91,16 +79,11 @@ func TestCopyAdditionalFiles(t *testing.T) { require.Nil(t, err) require.Equal(t, sk1.AdditionalFiles.Len(), 1) - fqbn, err := cores.ParseFQBN("a:b:c") - require.NoError(t, err) - - b1, err := NewBuilder(sk1, nil, paths.New("testdata"), false, nil, 0, nil, - nil, nil, nil, nil, fqbn, false, nil, false, nil, nil) - require.NoError(t, err) + b := Builder{sketch: sk1} // copy the sketch over, create a fake main file we don't care about it // but we need it for `SketchLoad` to succeed later - err = b1.sketchCopyAdditionalFiles(tmp, nil) + err = b.sketchCopyAdditionalFiles(tmp, nil) require.Nil(t, err) fakeIno := tmp.Join(fmt.Sprintf("%s.ino", tmp.Base())) require.Nil(t, fakeIno.WriteFile([]byte{})) @@ -115,7 +98,7 @@ func TestCopyAdditionalFiles(t *testing.T) { require.Nil(t, err) // copy again - err = b1.sketchCopyAdditionalFiles(tmp, nil) + err = b.sketchCopyAdditionalFiles(tmp, nil) require.Nil(t, err) // verify file hasn't changed diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 539332470d1..d0e84deb4c9 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -176,8 +176,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx := &types.Context{} builderCtx.PackageManager = pme - builderCtx.TargetPlatform = targetPlatform - builderCtx.ActualPlatform = buildPlatform + actualPlatform := buildPlatform // FIXME: This will be redundant when arduino-builder will be part of the cli builderCtx.HardwareDirs = configuration.HardwareDirectories(configuration.Settings) @@ -206,6 +205,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream req.GetClean(), req.GetSourceOverride(), req.GetCreateCompilationDatabaseOnly(), + actualPlatform, targetPlatform, builderLogger, progress.New(progressCB), ) @@ -231,7 +231,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream libsManager, libsResolver, verboseOut, err := detector.LibrariesLoader( useCachedLibrariesResolution, libsManager, builderCtx.BuiltInLibrariesDirs, libraryDir, builderCtx.OtherLibrariesDirs, - builderCtx.ActualPlatform, builderCtx.TargetPlatform, + actualPlatform, targetPlatform, ) if err != nil { return r, &arduino.CompileFailedError{Message: err.Error()} diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index a1dd20a283e..cf9551b0d99 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -94,7 +94,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - return ctx.Builder.BuildCore(ctx.ActualPlatform) + return ctx.Builder.BuildCore() }), types.BareCommand(func(ctx *types.Context) error { @@ -274,7 +274,7 @@ func findIncludes(ctx *types.Context) types.BareCommand { ctx.Builder.Sketch(), ctx.Builder.GetLibrariesBuildPath(), ctx.Builder.GetBuildProperties(), - ctx.TargetPlatform.Platform.Architecture, + ctx.Builder.TargetPlatform().Platform.Architecture, ) }) } @@ -306,7 +306,6 @@ func containerBuildOptions(ctx *types.Context) types.BareCommand { func warnAboutArchIncompatibleLibraries(ctx *types.Context) types.BareCommand { return types.BareCommand(func(ctx *types.Context) error { ctx.Builder.WarnAboutArchIncompatibleLibraries( - ctx.TargetPlatform, ctx.SketchLibrariesDetector.ImportedLibraries(), ) return nil diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 7262e990efa..b3819960cac 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -24,6 +24,7 @@ import ( bldr "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" + "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder/types" @@ -106,32 +107,37 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.Builder, err = bldr.NewBuilder( sk, nil, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, nil, false, builderLogger, nil, + ctx.BuiltInLibrariesDirs, parseFQBN(t, "a:b:c"), false, nil, false, + nil, nil, builderLogger, nil, ) require.NoError(t, err) + + var actualPlatform, targetPlatform *cores.PlatformRelease if fqbnString != "" { fqbn := parseFQBN(t, fqbnString) - _, targetPlatform, _, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn) + _, targetPlatfrm, _, boardBuildProperties, buildPlatform, err := pme.ResolveFQBN(fqbn) require.NoError(t, err) + targetPlatform = targetPlatfrm + actualPlatform = buildPlatform + _, err = pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) require.NoError(t, err) + ctx.Builder, err = bldr.NewBuilder( sk, boardBuildProperties, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, - ctx.BuiltInLibrariesDirs, fqbn, false, nil, false, builderLogger, nil) + ctx.BuiltInLibrariesDirs, fqbn, false, nil, false, targetPlatform, + actualPlatform, builderLogger, nil) require.NoError(t, err) - ctx.PackageManager = pme - ctx.TargetPlatform = targetPlatform - ctx.ActualPlatform = buildPlatform } if !stepToSkip[skipLibraries] { lm, libsResolver, _, err := detector.LibrariesLoader( false, nil, ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - ctx.ActualPlatform, ctx.TargetPlatform, + actualPlatform, targetPlatform, ) require.NoError(t, err) diff --git a/legacy/builder/test/libraries_loader_test.go b/legacy/builder/test/libraries_loader_test.go index eae7d3a121b..e0e0c40f56b 100644 --- a/legacy/builder/test/libraries_loader_test.go +++ b/legacy/builder/test/libraries_loader_test.go @@ -46,10 +46,13 @@ func TestLoadLibrariesAVR(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:avr:leonardo") defer cleanUpBuilderTestContext(t, ctx) + _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:avr:leonardo")) + require.NoError(t, err) + lm, libsResolver, _, err := detector.LibrariesLoader( false, nil, ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - ctx.ActualPlatform, ctx.TargetPlatform, + buildPlatform, targetPlatform, ) require.NoError(t, err) @@ -150,10 +153,12 @@ func TestLoadLibrariesSAM(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:sam:arduino_due_x_dbg") defer cleanUpBuilderTestContext(t, ctx) + _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:sam:arduino_due_x_dbg")) + require.NoError(t, err) lm, libsResolver, _, err := detector.LibrariesLoader( false, nil, ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - ctx.ActualPlatform, ctx.TargetPlatform, + buildPlatform, targetPlatform, ) require.NoError(t, err) @@ -227,10 +232,12 @@ func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:avr:leonardo") defer cleanUpBuilderTestContext(t, ctx) + _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:avr:leonardo")) + require.NoError(t, err) lm, _, _, err := detector.LibrariesLoader( false, nil, ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - ctx.ActualPlatform, ctx.TargetPlatform, + buildPlatform, targetPlatform, ) require.NoError(t, err) @@ -250,10 +257,12 @@ func TestLoadLibrariesMyAVRPlatform(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, nil, "my_avr_platform:avr:custom_yun") defer cleanUpBuilderTestContext(t, ctx) + _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "my_avr_platform:avr:custom_yun")) + require.NoError(t, err) lm, _, _, err := detector.LibrariesLoader( false, nil, ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - ctx.ActualPlatform, ctx.TargetPlatform, + buildPlatform, targetPlatform, ) require.NoError(t, err) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 5b5494ed0c9..77fb82432ce 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -19,7 +19,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" - "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" paths "github.com/arduino/go-paths-helper" ) @@ -37,6 +36,4 @@ type Context struct { OtherLibrariesDirs paths.PathList PackageManager *packagemanager.Explorer - TargetPlatform *cores.PlatformRelease - ActualPlatform *cores.PlatformRelease } From a7963f41a5c4e41f6d9d1b5587b11b3bce9d9ee8 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Wed, 13 Sep 2023 09:51:52 +0200 Subject: [PATCH 33/34] directly pass the remaining properties of Context in the builder constructor The remaning fields is used only by legacy tests --- commands/compile/compile.go | 22 ++++++++-------------- legacy/builder/types/context.go | 5 ++--- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/commands/compile/compile.go b/commands/compile/compile.go index d0e84deb4c9..5dcd028c34c 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -175,16 +175,10 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream } builderCtx := &types.Context{} - builderCtx.PackageManager = pme actualPlatform := buildPlatform - - // FIXME: This will be redundant when arduino-builder will be part of the cli - builderCtx.HardwareDirs = configuration.HardwareDirectories(configuration.Settings) - builderCtx.BuiltInToolsDirs = configuration.BuiltinToolsDirectories(configuration.Settings) - builderCtx.OtherLibrariesDirs = paths.NewPathList(req.GetLibraries()...) - builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings)) - - builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) + builtinLibrariesDir := configuration.IDEBuiltinLibrariesDir(configuration.Settings) + otherLibrariesDirs := paths.NewPathList(req.GetLibraries()...) + otherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings)) builderLogger := logger.New(outStream, errStream, req.GetVerbose(), req.GetWarnings()) builderCtx.BuilderLogger = builderLogger @@ -197,10 +191,10 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream coreBuildCachePath, int(req.GetJobs()), req.GetBuildProperties(), - builderCtx.HardwareDirs, - builderCtx.BuiltInToolsDirs, - builderCtx.OtherLibrariesDirs, - builderCtx.BuiltInLibrariesDirs, + configuration.HardwareDirectories(configuration.Settings), + configuration.BuiltinToolsDirectories(configuration.Settings), + otherLibrariesDirs, + builtinLibrariesDir, fqbn, req.GetClean(), req.GetSourceOverride(), @@ -230,7 +224,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream libraryDir := paths.NewPathList(req.Library...) libsManager, libsResolver, verboseOut, err := detector.LibrariesLoader( useCachedLibrariesResolution, libsManager, - builderCtx.BuiltInLibrariesDirs, libraryDir, builderCtx.OtherLibrariesDirs, + builtinLibrariesDir, libraryDir, otherLibrariesDirs, actualPlatform, targetPlatform, ) if err != nil { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 77fb82432ce..e720af613d1 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -29,11 +29,10 @@ type Context struct { SketchLibrariesDetector *detector.SketchLibrariesDetector BuilderLogger *logger.BuilderLogger - // Build options + // Used only by legacy tests HardwareDirs paths.PathList BuiltInToolsDirs paths.PathList BuiltInLibrariesDirs *paths.Path OtherLibrariesDirs paths.PathList - - PackageManager *packagemanager.Explorer + PackageManager *packagemanager.Explorer } From 60040575a31b606d85ab22dc4bd2244dbe6e0828 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Wed, 13 Sep 2023 11:22:02 +0200 Subject: [PATCH 34/34] polish legacy test --- arduino/builder/linker.go | 11 +- arduino/builder/utils/utils_test.go | 133 +++++++++ .../integrationtest/compile_4/compile_test.go | 2 + legacy/builder/test/builder_test.go | 38 +-- legacy/builder/test/builder_utils_test.go | 156 ---------- legacy/builder/test/helper.go | 19 -- legacy/builder/test/libraries_loader_test.go | 275 ------------------ .../test/merge_sketch_with_bootloader_test.go | 1 + .../test/setup_build_properties_test.go | 3 + legacy/builder/test/tools_loader_test.go | 154 ---------- .../try_build_of_problematic_sketch_test.go | 6 +- .../unused_compiled_libraries_remover_test.go | 95 ------ 12 files changed, 156 insertions(+), 737 deletions(-) delete mode 100644 legacy/builder/test/builder_utils_test.go delete mode 100644 legacy/builder/test/libraries_loader_test.go delete mode 100644 legacy/builder/test/unused_compiled_libraries_remover_test.go diff --git a/arduino/builder/linker.go b/arduino/builder/linker.go index b69e71aaefe..a4a2eba3bec 100644 --- a/arduino/builder/linker.go +++ b/arduino/builder/linker.go @@ -33,15 +33,10 @@ func (b *Builder) Link() error { return nil } - // TODO can we remove this multiple assignations? - objectFilesSketch := b.buildArtifacts.sketchObjectFiles - objectFilesLibraries := b.buildArtifacts.librariesObjectFiles - objectFilesCore := b.buildArtifacts.coreObjectsFiles - objectFiles := paths.NewPathList() - objectFiles.AddAll(objectFilesSketch) - objectFiles.AddAll(objectFilesLibraries) - objectFiles.AddAll(objectFilesCore) + objectFiles.AddAll(b.buildArtifacts.sketchObjectFiles) + objectFiles.AddAll(b.buildArtifacts.librariesObjectFiles) + objectFiles.AddAll(b.buildArtifacts.coreObjectsFiles) coreDotARelPath, err := b.buildPath.RelTo(b.buildArtifacts.coreArchiveFilePath) if err != nil { diff --git a/arduino/builder/utils/utils_test.go b/arduino/builder/utils/utils_test.go index 3dd4c1a2142..1c31160f5d0 100644 --- a/arduino/builder/utils/utils_test.go +++ b/arduino/builder/utils/utils_test.go @@ -16,8 +16,11 @@ package utils import ( + "os" "testing" + "time" + "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) @@ -39,3 +42,133 @@ func TestPrintableCommand(t *testing.T) { result := printableCommand(parts) require.Equal(t, correct, result) } + +func tempFile(t *testing.T, prefix string) *paths.Path { + file, err := os.CreateTemp("", prefix) + file.Close() + require.NoError(t, err) + return paths.New(file.Name()) +} + +func TestObjFileIsUpToDateObjMissing(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + upToDate, err := ObjFileIsUpToDate(sourceFile, nil, nil) + require.NoError(t, err) + require.False(t, upToDate) +} + +func TestObjFileIsUpToDateDepMissing(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, nil) + require.NoError(t, err) + require.False(t, upToDate) +} + +func TestObjFileIsUpToDateObjOlder(t *testing.T) { + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + time.Sleep(time.Second) + + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.False(t, upToDate) +} + +func TestObjFileIsUpToDateObjNewer(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + time.Sleep(time.Second) + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.True(t, upToDate) +} + +func TestObjFileIsUpToDateDepIsNewer(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + time.Sleep(time.Second) + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + time.Sleep(time.Second) + + headerFile := tempFile(t, "header") + defer headerFile.RemoveAll() + + data := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() + depFile.WriteFile([]byte(data)) + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.False(t, upToDate) +} + +func TestObjFileIsUpToDateDepIsOlder(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + headerFile := tempFile(t, "header") + defer headerFile.RemoveAll() + + time.Sleep(time.Second) + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + res := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() + depFile.WriteFile([]byte(res)) + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.True(t, upToDate) +} + +func TestObjFileIsUpToDateDepIsWrong(t *testing.T) { + sourceFile := tempFile(t, "source") + defer sourceFile.RemoveAll() + + time.Sleep(time.Second) + + objFile := tempFile(t, "obj") + defer objFile.RemoveAll() + depFile := tempFile(t, "dep") + defer depFile.RemoveAll() + + time.Sleep(time.Second) + + headerFile := tempFile(t, "header") + defer headerFile.RemoveAll() + + res := sourceFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() + depFile.WriteFile([]byte(res)) + + upToDate, err := ObjFileIsUpToDate(sourceFile, objFile, depFile) + require.NoError(t, err) + require.False(t, upToDate) +} diff --git a/internal/integrationtest/compile_4/compile_test.go b/internal/integrationtest/compile_4/compile_test.go index 09125b5e2e1..4210eaa931f 100644 --- a/internal/integrationtest/compile_4/compile_test.go +++ b/internal/integrationtest/compile_4/compile_test.go @@ -387,6 +387,7 @@ func testBuilderBridgeExample(t *testing.T, env *integrationtest.Environment, cl // Simulate a library use in libraries build path require.NoError(t, buildPath.Join("libraries", "SPI").MkdirAll()) + require.NoError(t, buildPath.Join("libraries", "dummy_file").WriteFile([]byte{})) // Build again... _, err = tryBuild(t, env, cli, "arduino:avr:leonardo", &buildOptions{NoClean: true}) @@ -394,6 +395,7 @@ func testBuilderBridgeExample(t *testing.T, env *integrationtest.Environment, cl require.False(t, buildPath.Join("libraries", "SPI").Exist()) require.True(t, buildPath.Join("libraries", "Bridge").Exist()) + require.True(t, buildPath.Join("libraries", "dummy_file").Exist()) }) t.Run("Preprocess", func(t *testing.T) { diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index b3819960cac..25df800ff5e 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -38,18 +38,9 @@ func cleanUpBuilderTestContext(t *testing.T, ctx *types.Context) { } } -type skipContextPreparationStepName string - -const skipLibraries = skipContextPreparationStepName("libraries") - -func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *paths.Path, fqbnString string, skips ...skipContextPreparationStepName) *types.Context { +func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *paths.Path, fqbnString string) *types.Context { DownloadCoresAndToolsAndLibraries(t) - stepToSkip := map[skipContextPreparationStepName]bool{} - for _, skip := range skips { - stepToSkip[skip] = true - } - if ctx == nil { ctx = &types.Context{} } @@ -123,7 +114,6 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat _, err = pme.FindToolsRequiredForBuild(targetPlatform, buildPlatform) require.NoError(t, err) - ctx.Builder, err = bldr.NewBuilder( sk, boardBuildProperties, buildPath, false, nil, 0, nil, ctx.HardwareDirs, ctx.BuiltInToolsDirs, ctx.OtherLibrariesDirs, @@ -133,21 +123,19 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.PackageManager = pme } - if !stepToSkip[skipLibraries] { - lm, libsResolver, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - actualPlatform, targetPlatform, - ) - require.NoError(t, err) + lm, libsResolver, _, err := detector.LibrariesLoader( + false, nil, + ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, + actualPlatform, targetPlatform, + ) + require.NoError(t, err) - ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( - lm, libsResolver, - false, - false, - builderLogger, - ) - } + ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( + lm, libsResolver, + false, + false, + builderLogger, + ) return ctx } diff --git a/legacy/builder/test/builder_utils_test.go b/legacy/builder/test/builder_utils_test.go deleted file mode 100644 index 8d5cb09de2e..00000000000 --- a/legacy/builder/test/builder_utils_test.go +++ /dev/null @@ -1,156 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package test - -import ( - "os" - "testing" - "time" - - "github.com/arduino/arduino-cli/arduino/builder/utils" - paths "github.com/arduino/go-paths-helper" - "github.com/stretchr/testify/require" -) - -func tempFile(t *testing.T, prefix string) *paths.Path { - file, err := os.CreateTemp("", prefix) - file.Close() - require.NoError(t, err) - return paths.New(file.Name()) -} - -func TestObjFileIsUpToDateObjMissing(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, nil, nil) - require.NoError(t, err) - require.False(t, upToDate) -} - -func TestObjFileIsUpToDateDepMissing(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, nil) - require.NoError(t, err) - require.False(t, upToDate) -} - -func TestObjFileIsUpToDateObjOlder(t *testing.T) { - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - time.Sleep(time.Second) - - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.False(t, upToDate) -} - -func TestObjFileIsUpToDateObjNewer(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - time.Sleep(time.Second) - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.True(t, upToDate) -} - -func TestObjFileIsUpToDateDepIsNewer(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - time.Sleep(time.Second) - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - time.Sleep(time.Second) - - headerFile := tempFile(t, "header") - defer headerFile.RemoveAll() - - data := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() - depFile.WriteFile([]byte(data)) - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.False(t, upToDate) -} - -func TestObjFileIsUpToDateDepIsOlder(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - headerFile := tempFile(t, "header") - defer headerFile.RemoveAll() - - time.Sleep(time.Second) - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - res := objFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() - depFile.WriteFile([]byte(res)) - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.True(t, upToDate) -} - -func TestObjFileIsUpToDateDepIsWrong(t *testing.T) { - sourceFile := tempFile(t, "source") - defer sourceFile.RemoveAll() - - time.Sleep(time.Second) - - objFile := tempFile(t, "obj") - defer objFile.RemoveAll() - depFile := tempFile(t, "dep") - defer depFile.RemoveAll() - - time.Sleep(time.Second) - - headerFile := tempFile(t, "header") - defer headerFile.RemoveAll() - - res := sourceFile.String() + ": \\\n\t" + sourceFile.String() + " \\\n\t" + headerFile.String() - depFile.WriteFile([]byte(res)) - - upToDate, err := utils.ObjFileIsUpToDate(sourceFile, objFile, depFile) - require.NoError(t, err) - require.False(t, upToDate) -} diff --git a/legacy/builder/test/helper.go b/legacy/builder/test/helper.go index f2d0990b2e9..746096c473f 100644 --- a/legacy/builder/test/helper.go +++ b/legacy/builder/test/helper.go @@ -20,17 +20,10 @@ import ( "testing" "github.com/arduino/arduino-cli/arduino/cores" - "github.com/arduino/arduino-cli/arduino/libraries" paths "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) -func Abs(t *testing.T, rel *paths.Path) *paths.Path { - absPath, err := rel.Abs() - require.NoError(t, err) - return absPath -} - func SetupBuildPath(t *testing.T) *paths.Path { buildPath, err := paths.MkTempDir("", "test_build_path") require.NoError(t, err) @@ -42,15 +35,3 @@ func parseFQBN(t *testing.T, fqbnIn string) *cores.FQBN { require.NoError(t, err) return fqbn } - -type ByLibraryName []*libraries.Library - -func (s ByLibraryName) Len() int { - return len(s) -} -func (s ByLibraryName) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} -func (s ByLibraryName) Less(i, j int) bool { - return s[i].Name < s[j].Name -} diff --git a/legacy/builder/test/libraries_loader_test.go b/legacy/builder/test/libraries_loader_test.go deleted file mode 100644 index e0e0c40f56b..00000000000 --- a/legacy/builder/test/libraries_loader_test.go +++ /dev/null @@ -1,275 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package test - -import ( - "path/filepath" - "sort" - "testing" - - "github.com/arduino/arduino-cli/arduino/builder/detector" - "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" -) - -func extractLibraries(libs map[string]libraries.List) []*libraries.Library { - res := []*libraries.Library{} - for _, lib := range libs { - for _, libAlternative := range lib { - res = append(res, libAlternative) - } - } - return res -} - -func TestLoadLibrariesAVR(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - OtherLibrariesDirs: paths.NewPathList("libraries"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:avr:leonardo") - defer cleanUpBuilderTestContext(t, ctx) - - _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:avr:leonardo")) - require.NoError(t, err) - - lm, libsResolver, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - buildPlatform, targetPlatform, - ) - require.NoError(t, err) - - librariesFolders := lm.LibrariesDir - require.Equal(t, 3, len(librariesFolders)) - require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path)) - require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path)) - require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2].Path)) - - libs := extractLibraries(lm.Libraries) - require.Equal(t, 24, len(libs)) - - sort.Sort(ByLibraryName(libs)) - - idx := 0 - - require.Equal(t, "ANewLibrary-master", libs[idx].Name) - - idx++ - require.Equal(t, "Adafruit PN532", libs[idx].Name) - require.True(t, Abs(t, paths.New("downloaded_libraries/Adafruit_PN532")).EquivalentTo(libs[idx].InstallDir)) - require.True(t, Abs(t, paths.New("downloaded_libraries/Adafruit_PN532")).EquivalentTo(libs[idx].SourceDir)) - require.Equal(t, 1, len(libs[idx].Architectures)) - require.Equal(t, "*", libs[idx].Architectures[0]) - require.False(t, libs[idx].IsLegacy) - - idx++ - require.Equal(t, "Audio", libs[idx].Name) - - idx++ - require.Equal(t, "Balanduino", libs[idx].Name) - require.True(t, libs[idx].IsLegacy) - - idx++ - bridgeLib := libs[idx] - require.Equal(t, "Bridge", bridgeLib.Name) - require.True(t, Abs(t, paths.New("downloaded_libraries/Bridge")).EquivalentTo(bridgeLib.InstallDir)) - require.True(t, Abs(t, paths.New("downloaded_libraries/Bridge/src")).EquivalentTo(bridgeLib.SourceDir)) - require.Equal(t, 1, len(bridgeLib.Architectures)) - require.Equal(t, "*", bridgeLib.Architectures[0]) - require.Equal(t, "Arduino", bridgeLib.Author) - require.Equal(t, "Arduino ", bridgeLib.Maintainer) - - idx++ - require.Equal(t, "CapacitiveSensor", libs[idx].Name) - idx++ - require.Equal(t, "EEPROM", libs[idx].Name) - idx++ - require.Equal(t, "Ethernet", libs[idx].Name) - idx++ - require.Equal(t, "FakeAudio", libs[idx].Name) - idx++ - require.Equal(t, "FastLED", libs[idx].Name) - idx++ - require.Equal(t, "HID", libs[idx].Name) - idx++ - require.Equal(t, "IRremote", libs[idx].Name) - idx++ - require.Equal(t, "Robot IR Remote", libs[idx].Name) - idx++ - require.Equal(t, "SPI", libs[idx].Name) - idx++ - require.Equal(t, "SPI", libs[idx].Name) - idx++ - require.Equal(t, "ShouldNotRecurseWithOldLibs", libs[idx].Name) - idx++ - require.Equal(t, "SoftwareSerial", libs[idx].Name) - idx++ - require.Equal(t, "USBHost", libs[idx].Name) - idx++ - require.Equal(t, "Wire", libs[idx].Name) - - libs = libsResolver.AlternativesFor("Audio.h") - require.Len(t, libs, 2) - sort.Sort(ByLibraryName(libs)) - require.Equal(t, "Audio", libs[0].Name) - require.Equal(t, "FakeAudio", libs[1].Name) - - libs = libsResolver.AlternativesFor("FakeAudio.h") - require.Len(t, libs, 1) - require.Equal(t, "FakeAudio", libs[0].Name) - - libs = libsResolver.AlternativesFor("Adafruit_PN532.h") - require.Len(t, libs, 1) - require.Equal(t, "Adafruit PN532", libs[0].Name) - - libs = libsResolver.AlternativesFor("IRremote.h") - require.Len(t, libs, 1) - require.Equal(t, "IRremote", libs[0].Name) -} - -func TestLoadLibrariesSAM(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - OtherLibrariesDirs: paths.NewPathList("libraries"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:sam:arduino_due_x_dbg") - defer cleanUpBuilderTestContext(t, ctx) - - _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:sam:arduino_due_x_dbg")) - require.NoError(t, err) - lm, libsResolver, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - buildPlatform, targetPlatform, - ) - require.NoError(t, err) - - librariesFolders := lm.LibrariesDir - require.Equal(t, 3, len(librariesFolders)) - require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path)) - require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "sam", "libraries")).EquivalentTo(librariesFolders[1].Path)) - require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2].Path)) - - libraries := extractLibraries(lm.Libraries) - require.Equal(t, 22, len(libraries)) - - sort.Sort(ByLibraryName(libraries)) - - idx := 0 - require.Equal(t, "ANewLibrary-master", libraries[idx].Name) - idx++ - require.Equal(t, "Adafruit PN532", libraries[idx].Name) - idx++ - require.Equal(t, "Audio", libraries[idx].Name) - idx++ - require.Equal(t, "Balanduino", libraries[idx].Name) - idx++ - require.Equal(t, "Bridge", libraries[idx].Name) - idx++ - require.Equal(t, "CapacitiveSensor", libraries[idx].Name) - idx++ - require.Equal(t, "Ethernet", libraries[idx].Name) - idx++ - require.Equal(t, "FakeAudio", libraries[idx].Name) - idx++ - require.Equal(t, "FastLED", libraries[idx].Name) - idx++ - require.Equal(t, "HID", libraries[idx].Name) - idx++ - require.Equal(t, "IRremote", libraries[idx].Name) - idx++ - require.Equal(t, "Robot IR Remote", libraries[idx].Name) - idx++ - require.Equal(t, "SPI", libraries[idx].Name) - idx++ - require.Equal(t, "SPI", libraries[idx].Name) - idx++ - require.Equal(t, "ShouldNotRecurseWithOldLibs", libraries[idx].Name) - idx++ - require.Equal(t, "USBHost", libraries[idx].Name) - idx++ - require.Equal(t, "Wire", libraries[idx].Name) - - libs := libsResolver.AlternativesFor("Audio.h") - require.Len(t, libs, 2) - sort.Sort(ByLibraryName(libs)) - require.Equal(t, "Audio", libs[0].Name) - require.Equal(t, "FakeAudio", libs[1].Name) - - libs = libsResolver.AlternativesFor("FakeAudio.h") - require.Len(t, libs, 1) - require.Equal(t, "FakeAudio", libs[0].Name) - - libs = libsResolver.AlternativesFor("IRremote.h") - require.Len(t, libs, 1) - require.Equal(t, "IRremote", libs[0].Name) -} - -func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - OtherLibrariesDirs: paths.NewPathList("libraries", filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:avr:leonardo") - defer cleanUpBuilderTestContext(t, ctx) - - _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "arduino:avr:leonardo")) - require.NoError(t, err) - lm, _, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - buildPlatform, targetPlatform, - ) - require.NoError(t, err) - - librariesFolders := lm.LibrariesDir - require.Equal(t, 3, len(librariesFolders)) - require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path)) - require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path)) - require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2].Path)) -} - -func TestLoadLibrariesMyAVRPlatform(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "user_hardware", "downloaded_hardware"), - BuiltInLibrariesDirs: paths.New("downloaded_libraries"), - OtherLibrariesDirs: paths.NewPathList("libraries", filepath.Join("downloaded_hardware", "arduino", "avr", "libraries")), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "my_avr_platform:avr:custom_yun") - defer cleanUpBuilderTestContext(t, ctx) - - _, targetPlatform, _, _, buildPlatform, err := ctx.PackageManager.ResolveFQBN(parseFQBN(t, "my_avr_platform:avr:custom_yun")) - require.NoError(t, err) - lm, _, _, err := detector.LibrariesLoader( - false, nil, - ctx.BuiltInLibrariesDirs, nil, ctx.OtherLibrariesDirs, - buildPlatform, targetPlatform, - ) - require.NoError(t, err) - - librariesFolders := lm.LibrariesDir - require.Equal(t, 4, len(librariesFolders)) - require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path)) - require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path)) - require.True(t, Abs(t, paths.New("user_hardware", "my_avr_platform", "avr", "libraries")).EquivalentTo(librariesFolders[2].Path)) - require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[3].Path)) -} diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index b1d2a6df7ea..f7c9a24024f 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -148,6 +148,7 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { require.False(t, exist) } +// TODO convert in a compile test and we check against the real .hex func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), diff --git a/legacy/builder/test/setup_build_properties_test.go b/legacy/builder/test/setup_build_properties_test.go index b4acb7d2ecc..68aaa6b0574 100644 --- a/legacy/builder/test/setup_build_properties_test.go +++ b/legacy/builder/test/setup_build_properties_test.go @@ -25,6 +25,7 @@ import ( "github.com/stretchr/testify/require" ) +// TODO maybe create a test that actually check all the keys func TestSetupBuildProperties(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), @@ -71,6 +72,7 @@ func TestSetupBuildProperties(t *testing.T) { require.True(t, buildProperties.ContainsKey("extra.time.dst")) } +// TODO make this integration tests func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), @@ -90,6 +92,7 @@ func TestSetupBuildPropertiesWithSomeCustomOverrides(t *testing.T) { require.Equal(t, "non existent path with space and a =", buildProperties.Get("tools.avrdude.config.path")) } +// TODO go to compile_4 that uses the custom_yum to compile and we also verify this properties func TestSetupBuildPropertiesUserHardware(t *testing.T) { ctx := &types.Context{ HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "user_hardware"), diff --git a/legacy/builder/test/tools_loader_test.go b/legacy/builder/test/tools_loader_test.go index e28f828401d..3460ac82c76 100644 --- a/legacy/builder/test/tools_loader_test.go +++ b/legacy/builder/test/tools_loader_test.go @@ -16,35 +16,12 @@ package test import ( - "path/filepath" - "sort" "testing" - "github.com/arduino/arduino-cli/arduino/cores" - "github.com/arduino/arduino-cli/arduino/cores/packagemanager" - "github.com/arduino/arduino-cli/legacy/builder/types" paths "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) -type ByToolIDAndVersion []*cores.ToolRelease - -func (s ByToolIDAndVersion) Len() int { - return len(s) -} -func (s ByToolIDAndVersion) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} -func (s ByToolIDAndVersion) Less(i, j int) bool { - if s[i].Tool.Name != s[j].Tool.Name { - return s[i].Tool.Name < s[j].Tool.Name - } - if !s[i].Version.Equal(s[j].Version) { - return s[i].Version.LessThan(s[j].Version) - } - return s[i].InstallDir.String() < s[j].InstallDir.String() -} - func requireEquivalentPaths(t *testing.T, actual string, expected ...string) { if len(expected) == 1 { actualAbs, err := paths.New(actual).Abs() @@ -60,134 +37,3 @@ func requireEquivalentPaths(t *testing.T, actual string, expected ...string) { require.Contains(t, expectedAbs.AsStrings(), actualAbs.String()) } } - -func TestLoadTools(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"), - BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - tools := ctx.PackageManager.GetAllInstalledToolsReleases() - require.Equal(t, 9, len(tools)) - - sort.Sort(ByToolIDAndVersion(tools)) - - idx := 0 - require.Equal(t, ":arduino-preprocessor@0.1.5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/arduino-preprocessor/0.1.5") - idx++ - require.Equal(t, ":arm-none-eabi-gcc@4.8.3-2014q1", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/arm-none-eabi-gcc/4.8.3-2014q1") - idx++ - require.Equal(t, ":avr-gcc@4.8.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/avr-gcc/4.8.1-arduino5") - idx++ - require.Equal(t, "arduino:avr-gcc@4.8.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "tools_builtin/avr") - idx++ - require.Equal(t, ":avrdude@6.0.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/avrdude/6.0.1-arduino5") - idx++ - require.Equal(t, "arduino:avrdude@6.0.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "tools_builtin/avr") - idx++ - require.Equal(t, ":bossac@1.5-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/bossac/1.5-arduino") - idx++ - require.Equal(t, ":bossac@1.6.1-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/bossac/1.6.1-arduino") - idx++ - require.Equal(t, ":ctags@5.8-arduino11", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/ctags/5.8-arduino11") -} - -func TestLoadToolsWithBoardManagerFolderStructure(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("downloaded_board_manager_stuff"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - tools := ctx.PackageManager.GetAllInstalledToolsReleases() - require.Equal(t, 3, len(tools)) - - sort.Sort(ByToolIDAndVersion(tools)) - - idx := 0 - require.Equal(t, "arduino:CMSIS@4.0.0-atmel", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/arduino/tools/CMSIS/4.0.0-atmel") - idx++ - require.Equal(t, "RFduino:arm-none-eabi-gcc@4.8.3-2014q1", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/RFduino/tools/arm-none-eabi-gcc/4.8.3-2014q1") - idx++ - require.Equal(t, "arduino:openocd@0.9.0-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/arduino/tools/openocd/0.9.0-arduino") -} - -func TestLoadLotsOfTools(t *testing.T) { - ctx := &types.Context{ - HardwareDirs: paths.NewPathList("downloaded_board_manager_stuff"), - BuiltInToolsDirs: paths.NewPathList("downloaded_tools", "tools_builtin"), - } - ctx = prepareBuilderTestContext(t, ctx, nil, "", skipLibraries) - defer cleanUpBuilderTestContext(t, ctx) - - tools := ctx.PackageManager.GetAllInstalledToolsReleases() - require.Equal(t, 12, len(tools)) - - sort.Sort(ByToolIDAndVersion(tools)) - - idx := 0 - require.Equal(t, "arduino:CMSIS@4.0.0-atmel", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/arduino/tools/CMSIS/4.0.0-atmel") - idx++ - require.Equal(t, ":arduino-preprocessor@0.1.5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/arduino-preprocessor/0.1.5") - idx++ - require.Equal(t, "RFduino:arm-none-eabi-gcc@4.8.3-2014q1", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/RFduino/tools/arm-none-eabi-gcc/4.8.3-2014q1") - idx++ - require.Equal(t, ":arm-none-eabi-gcc@4.8.3-2014q1", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/arm-none-eabi-gcc/4.8.3-2014q1") - idx++ - require.Equal(t, ":avr-gcc@4.8.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/avr-gcc/4.8.1-arduino5") - idx++ - require.Equal(t, "arduino:avr-gcc@4.8.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "tools_builtin/avr") - idx++ - require.Equal(t, ":avrdude@6.0.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/avrdude/6.0.1-arduino5") - idx++ - require.Equal(t, "arduino:avrdude@6.0.1-arduino5", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "tools_builtin/avr") - idx++ - require.Equal(t, ":bossac@1.5-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/bossac/1.5-arduino") - idx++ - require.Equal(t, ":bossac@1.6.1-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), tools[idx].InstallDir.String(), "downloaded_tools/bossac/1.6.1-arduino") - idx++ - require.Equal(t, ":ctags@5.8-arduino11", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_tools/ctags/5.8-arduino11") - idx++ - require.Equal(t, "arduino:openocd@0.9.0-arduino", tools[idx].String()) - requireEquivalentPaths(t, tools[idx].InstallDir.String(), "downloaded_board_manager_stuff/arduino/tools/openocd/0.9.0-arduino") -} - -func TestAllToolsContextIsPopulated(t *testing.T) { - pmb := packagemanager.NewBuilder(nil, nil, nil, nil, "") - pmb.LoadHardwareFromDirectories(paths.NewPathList("downloaded_board_manager_stuff")) - pmb.LoadToolsFromBundleDirectory(paths.New("downloaded_tools", "tools_builtin")) - pm := pmb.Build() - pme, release := pm.NewExplorer() - defer release() - - ctx := &types.Context{ - PackageManager: pme, - } - - require.NotEmpty(t, ctx.PackageManager.GetAllInstalledToolsReleases()) -} diff --git a/legacy/builder/test/try_build_of_problematic_sketch_test.go b/legacy/builder/test/try_build_of_problematic_sketch_test.go index ab40de05714..6504bcd9b84 100644 --- a/legacy/builder/test/try_build_of_problematic_sketch_test.go +++ b/legacy/builder/test/try_build_of_problematic_sketch_test.go @@ -27,11 +27,7 @@ import ( "github.com/stretchr/testify/require" ) -// This is a sketch that fails to build on purpose -//func TestTryBuild016(t *testing.T) { -// tryBuild(t, paths.New("sketch_that_checks_if_SPI_has_transactions_and_includes_missing_Ethernet", "sketch.ino")) -//} - +// TODO add them in the compile_4 func TestTryBuild033(t *testing.T) { tryBuild(t, paths.New("sketch_that_includes_arduino_h", "sketch_that_includes_arduino_h.ino")) } diff --git a/legacy/builder/test/unused_compiled_libraries_remover_test.go b/legacy/builder/test/unused_compiled_libraries_remover_test.go deleted file mode 100644 index a9e122195ca..00000000000 --- a/legacy/builder/test/unused_compiled_libraries_remover_test.go +++ /dev/null @@ -1,95 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package test - -// TODO to be ported in the E2E suite - -//func TestUnusedCompiledLibrariesRemover(t *testing.T) { -// temp, err := paths.MkTempDir("", "test") -// require.NoError(t, err) -// defer temp.RemoveAll() -// -// require.NoError(t, temp.Join("SPI").MkdirAll()) -// require.NoError(t, temp.Join("Bridge").MkdirAll()) -// require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) -// -// librariesBuildPath := temp -// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( -// nil, nil, false, false, logger.New(nil, nil, false, ""), -// ) -// sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) -// -// err = builder.UnusedCompiledLibrariesRemover( -// librariesBuildPath, -// sketchLibrariesDetector.ImportedLibraries(), -// ) -// require.NoError(t, err) -// -// exist, err := temp.Join("SPI").ExistCheck() -// require.NoError(t, err) -// require.False(t, exist) -// exist, err = temp.Join("Bridge").ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -// exist, err = temp.Join("dummy_file").ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -//} -// -//func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { -// librariesBuildPath := paths.TempDir().Join("test") -// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( -// nil, nil, false, false, logger.New(nil, nil, false, ""), -// ) -// sketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) -// -// err := builder.UnusedCompiledLibrariesRemover( -// librariesBuildPath, -// sketchLibrariesDetector.ImportedLibraries(), -// ) -// require.NoError(t, err) -//} -// -//func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { -// temp, err := paths.MkTempDir("", "test") -// require.NoError(t, err) -// defer temp.RemoveAll() -// -// require.NoError(t, temp.Join("SPI").MkdirAll()) -// require.NoError(t, temp.Join("Bridge").MkdirAll()) -// require.NoError(t, temp.Join("dummy_file").WriteFile([]byte{})) -// -// sketchLibrariesDetector := detector.NewSketchLibrariesDetector( -// nil, nil, false, false, logger.New(nil, nil, false, ""), -// ) -// librariesBuildPath := temp -// -// err = builder.UnusedCompiledLibrariesRemover( -// librariesBuildPath, -// sketchLibrariesDetector.ImportedLibraries(), -// ) -// require.NoError(t, err) -// -// exist, err := temp.Join("SPI").ExistCheck() -// require.NoError(t, err) -// require.False(t, exist) -// exist, err = temp.Join("Bridge").ExistCheck() -// require.NoError(t, err) -// require.False(t, exist) -// exist, err = temp.Join("dummy_file").ExistCheck() -// require.NoError(t, err) -// require.True(t, exist) -//}