From b735f49a8d6cd781e0905f39a18824e3cc0cb66d Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 12:46:40 +0200 Subject: [PATCH 01/21] move CoreBuilder in arduino/builder and move CompilationDatabase in compilation pkg --- .../database.go} | 5 +- .../database_test.go} | 2 +- arduino/builder/core.go | 230 ++++++++++++++++- arduino/builder/utils/utils.go | 10 +- commands/compile/compile.go | 3 +- legacy/builder/builder.go | 3 +- legacy/builder/phases/core_builder.go | 234 ------------------ legacy/builder/phases/libraries_builder.go | 10 +- legacy/builder/phases/sketch_builder.go | 4 +- legacy/builder/types/context.go | 3 +- 10 files changed, 253 insertions(+), 251 deletions(-) rename arduino/builder/{compilation_database.go => compilation/database.go} (97%) rename arduino/builder/{compilation_database_test.go => compilation/database_test.go} (98%) delete mode 100644 legacy/builder/phases/core_builder.go diff --git a/arduino/builder/compilation_database.go b/arduino/builder/compilation/database.go similarity index 97% rename from arduino/builder/compilation_database.go rename to arduino/builder/compilation/database.go index 6a7e2e475dd..e008c773aa7 100644 --- a/arduino/builder/compilation_database.go +++ b/arduino/builder/compilation/database.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 builder +package compilation import ( "encoding/json" @@ -21,9 +21,12 @@ import ( "os" "github.com/arduino/arduino-cli/executils" + "github.com/arduino/arduino-cli/i18n" "github.com/arduino/go-paths-helper" ) +var tr = i18n.Tr + // CompilationDatabase keeps track of all the compile commands run by the builder type CompilationDatabase struct { Contents []CompilationCommand diff --git a/arduino/builder/compilation_database_test.go b/arduino/builder/compilation/database_test.go similarity index 98% rename from arduino/builder/compilation_database_test.go rename to arduino/builder/compilation/database_test.go index 8a715533617..7d856b51c77 100644 --- a/arduino/builder/compilation_database_test.go +++ b/arduino/builder/compilation/database_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 builder +package compilation import ( "testing" diff --git a/arduino/builder/core.go b/arduino/builder/core.go index c4d096f6f27..3fcf3094499 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -1,8 +1,236 @@ +// 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/go-paths-helper" +import ( + "crypto/md5" + "encoding/hex" + "fmt" + "io" + "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/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/arduino/go-properties-orderedmap" + "github.com/pkg/errors" +) // CoreBuildCachePath fixdoc func (b *Builder) CoreBuildCachePath() *paths.Path { return b.coreBuildCachePath } + +func CoreBuilder( + buildPath, coreBuildPath, coreBuildCachePath *paths.Path, + buildProperties *properties.Map, + actualPlatform *cores.PlatformRelease, + verbose, onlyUpdateCompilationDatabase, clean bool, + compilationDatabase *compilation.CompilationDatabase, + jobs int, + warningsLevel string, + stdoutWriter, stderrWriter io.Writer, + verboseInfoFn func(msg string), + verboseStdoutFn, verboseStderrFn func(data []byte), + progress *progress.Struct, progressCB rpc.TaskProgressCB, +) (paths.PathList, *paths.Path, error) { + if err := coreBuildPath.MkdirAll(); err != nil { + return nil, nil, errors.WithStack(err) + } + + if coreBuildCachePath != nil { + if _, err := coreBuildCachePath.RelTo(buildPath); err != nil { + verboseInfoFn(tr("Couldn't deeply cache core build: %[1]s", err)) + verboseInfoFn(tr("Running normal build of the core...")) + coreBuildCachePath = nil + } else if err := coreBuildCachePath.MkdirAll(); err != nil { + return nil, nil, errors.WithStack(err) + } + } + + archiveFile, objectFiles, err := compileCore( + verbose, onlyUpdateCompilationDatabase, clean, + actualPlatform, + coreBuildPath, coreBuildCachePath, + buildProperties, + compilationDatabase, + jobs, + warningsLevel, + stdoutWriter, stderrWriter, + verboseInfoFn, + verboseStdoutFn, verboseStderrFn, + progress, progressCB, + ) + if err != nil { + return nil, nil, errors.WithStack(err) + } + + return objectFiles, archiveFile, nil +} + +func compileCore( + verbose, onlyUpdateCompilationDatabase, clean bool, + actualPlatform *cores.PlatformRelease, + buildPath, buildCachePath *paths.Path, + buildProperties *properties.Map, + compilationDatabase *compilation.CompilationDatabase, + jobs int, + warningsLevel string, + stdoutWriter, stderrWriter io.Writer, + verboseInfoFn func(msg string), + verboseStdoutFn, verboseStderrFn func(data []byte), + 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") + + includes := []string{coreFolder.String()} + if variantFolder != nil && variantFolder.IsDir() { + includes = append(includes, variantFolder.String()) + } + includes = f.Map(includes, cpp.WrapWithHyphenI) + + var err error + variantObjectFiles := paths.NewPathList() + if variantFolder != nil && variantFolder.IsDir() { + variantObjectFiles, err = utils.CompileFilesRecursive( + variantFolder, buildPath, buildProperties, includes, + onlyUpdateCompilationDatabase, + compilationDatabase, + jobs, + verbose, + warningsLevel, + stdoutWriter, stderrWriter, + verboseInfoFn, verboseStdoutFn, verboseStderrFn, + progress, progressCB, + ) + if err != nil { + return nil, nil, errors.WithStack(err) + } + } + + var targetArchivedCore *paths.Path + if buildCachePath != nil { + realCoreFolder := coreFolder.Parent().Parent() + archivedCoreName := GetCachedCoreArchiveDirName( + buildProperties.Get("build.fqbn"), + buildProperties.Get("compiler.optimization_flags"), + realCoreFolder, + ) + targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a") + + if _, err := buildcache.New(buildCachePath).GetOrCreate(archivedCoreName); errors.Is(err, buildcache.CreateDirErr) { + return nil, nil, fmt.Errorf(tr("creating core cache folder: %s", err)) + } + + var canUseArchivedCore bool + if onlyUpdateCompilationDatabase || 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 + canUseArchivedCore = false + } else if targetCoreFolder == nil || realCoreFolder.EquivalentTo(targetCoreFolder) { + canUseArchivedCore = true + } else if isOlder, err := utils.DirContentIsOlderThan(targetCoreFolder, targetArchivedCore); err != nil || !isOlder { + // Recreate the archive if ANY of the build core files (including platform.txt) has changed + canUseArchivedCore = false + } else { + canUseArchivedCore = true + } + + if canUseArchivedCore { + // use archived core + if verbose { + verboseInfoFn(tr("Using precompiled core: %[1]s", targetArchivedCore)) + } + return targetArchivedCore, variantObjectFiles, nil + } + } + + coreObjectFiles, err := utils.CompileFilesRecursive( + coreFolder, buildPath, buildProperties, includes, + onlyUpdateCompilationDatabase, + compilationDatabase, + jobs, + verbose, + warningsLevel, + stdoutWriter, stderrWriter, + verboseInfoFn, verboseStdoutFn, verboseStderrFn, + progress, progressCB, + ) + if err != nil { + return nil, nil, errors.WithStack(err) + } + + archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( + buildPath, paths.New("core.a"), coreObjectFiles, buildProperties, + onlyUpdateCompilationDatabase, verbose, stdoutWriter, stderrWriter, + ) + if verbose { + verboseInfoFn(string(verboseInfo)) + } + if err != nil { + return nil, nil, errors.WithStack(err) + } + + // archive core.a + if targetArchivedCore != nil && !onlyUpdateCompilationDatabase { + err := archiveFile.CopyTo(targetArchivedCore) + if verbose { + if err == nil { + verboseInfoFn(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore)) + } else if os.IsNotExist(err) { + verboseInfoFn(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 { + verboseInfoFn(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err)) + } + } + } + + return archiveFile, variantObjectFiles, nil +} + +// 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 { + fqbnToUnderscore := strings.ReplaceAll(fqbn, ":", "_") + fqbnToUnderscore = strings.ReplaceAll(fqbnToUnderscore, "=", "_") + if absCoreFolder, err := coreFolder.Abs(); err == nil { + coreFolder = absCoreFolder + } // silently continue if absolute path can't be detected + + md5Sum := func(data []byte) string { + md5sumBytes := md5.Sum(data) + return hex.EncodeToString(md5sumBytes[:]) + } + hash := md5Sum([]byte(coreFolder.String() + optimizationFlags)) + realName := fqbnToUnderscore + "_" + hash + if len(realName) > 100 { + // avoid really long names, simply hash the name again + realName = md5Sum([]byte(realName)) + } + return realName +} diff --git a/arduino/builder/utils/utils.go b/arduino/builder/utils/utils.go index 10902c46ca0..80d0a9a32d2 100644 --- a/arduino/builder/utils/utils.go +++ b/arduino/builder/utils/utils.go @@ -11,7 +11,7 @@ import ( "sync" "unicode" - "github.com/arduino/arduino-cli/arduino/builder" + "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/globals" "github.com/arduino/arduino-cli/executils" @@ -339,7 +339,7 @@ func CompileFiles( buildProperties *properties.Map, includes []string, onlyUpdateCompilationDatabase bool, - compilationDatabase *builder.CompilationDatabase, + compilationDatabase *compilation.CompilationDatabase, jobs int, verbose bool, warningsLevel string, @@ -369,7 +369,7 @@ func CompileFilesRecursive( buildProperties *properties.Map, includes []string, onlyUpdateCompilationDatabase bool, - compilationDatabase *builder.CompilationDatabase, + compilationDatabase *compilation.CompilationDatabase, jobs int, verbose bool, warningsLevel string, @@ -395,7 +395,7 @@ func CompileFilesRecursive( func compileFiles( onlyUpdateCompilationDatabase bool, - compilationDatabase *builder.CompilationDatabase, + compilationDatabase *compilation.CompilationDatabase, jobs int, sourceDir *paths.Path, recurse bool, @@ -508,7 +508,7 @@ func compileFiles( func compileFileWithRecipe( stdoutWriter, stderrWriter io.Writer, warningsLevel string, - compilationDatabase *builder.CompilationDatabase, + compilationDatabase *compilation.CompilationDatabase, verbose, onlyUpdateCompilationDatabase bool, sourcePath *paths.Path, source *paths.Path, diff --git a/commands/compile/compile.go b/commands/compile/compile.go index f04a239a870..02b16fb0cf9 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -24,6 +24,7 @@ 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/cores" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" @@ -213,7 +214,7 @@ 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 = bldr.NewCompilationDatabase( + builderCtx.CompilationDatabase = compilation.NewCompilationDatabase( builderCtx.BuildPath.Join("compile_commands.json"), ) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 0e1156666e6..92e8d18afeb 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -19,6 +19,7 @@ import ( "reflect" "time" + "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/builder/preprocessor" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/i18n" @@ -143,7 +144,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - objectFiles, archiveFile, err := phases.CoreBuilder( + objectFiles, archiveFile, err := builder.CoreBuilder( ctx.BuildPath, ctx.CoreBuildPath, ctx.Builder.CoreBuildCachePath(), ctx.BuildProperties, ctx.ActualPlatform, diff --git a/legacy/builder/phases/core_builder.go b/legacy/builder/phases/core_builder.go deleted file mode 100644 index 9c49fe6d787..00000000000 --- a/legacy/builder/phases/core_builder.go +++ /dev/null @@ -1,234 +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 phases - -import ( - "crypto/md5" - "encoding/hex" - "fmt" - "io" - "os" - "strings" - - "github.com/arduino/arduino-cli/arduino/builder" - "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" - "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" -) - -var tr = i18n.Tr - -func CoreBuilder( - buildPath, coreBuildPath, coreBuildCachePath *paths.Path, - buildProperties *properties.Map, - actualPlatform *cores.PlatformRelease, - verbose, onlyUpdateCompilationDatabase, clean bool, - compilationDatabase *builder.CompilationDatabase, - jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), - progress *progress.Struct, progressCB rpc.TaskProgressCB, -) (paths.PathList, *paths.Path, error) { - if err := coreBuildPath.MkdirAll(); err != nil { - return nil, nil, errors.WithStack(err) - } - - if coreBuildCachePath != nil { - if _, err := coreBuildCachePath.RelTo(buildPath); err != nil { - verboseInfoFn(tr("Couldn't deeply cache core build: %[1]s", err)) - verboseInfoFn(tr("Running normal build of the core...")) - coreBuildCachePath = nil - } else if err := coreBuildCachePath.MkdirAll(); err != nil { - return nil, nil, errors.WithStack(err) - } - } - - archiveFile, objectFiles, err := compileCore( - verbose, onlyUpdateCompilationDatabase, clean, - actualPlatform, - coreBuildPath, coreBuildCachePath, - buildProperties, - compilationDatabase, - jobs, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, - verboseStdoutFn, verboseStderrFn, - progress, progressCB, - ) - if err != nil { - return nil, nil, errors.WithStack(err) - } - - return objectFiles, archiveFile, nil -} - -func compileCore( - verbose, onlyUpdateCompilationDatabase, clean bool, - actualPlatform *cores.PlatformRelease, - buildPath, buildCachePath *paths.Path, - buildProperties *properties.Map, - compilationDatabase *builder.CompilationDatabase, - jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), - 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") - - includes := []string{coreFolder.String()} - if variantFolder != nil && variantFolder.IsDir() { - includes = append(includes, variantFolder.String()) - } - includes = f.Map(includes, cpp.WrapWithHyphenI) - - var err error - variantObjectFiles := paths.NewPathList() - if variantFolder != nil && variantFolder.IsDir() { - variantObjectFiles, err = utils.CompileFilesRecursive( - variantFolder, buildPath, buildProperties, includes, - onlyUpdateCompilationDatabase, - compilationDatabase, - jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, - progress, progressCB, - ) - if err != nil { - return nil, nil, errors.WithStack(err) - } - } - - var targetArchivedCore *paths.Path - if buildCachePath != nil { - realCoreFolder := coreFolder.Parent().Parent() - archivedCoreName := GetCachedCoreArchiveDirName( - buildProperties.Get("build.fqbn"), - buildProperties.Get("compiler.optimization_flags"), - realCoreFolder, - ) - targetArchivedCore = buildCachePath.Join(archivedCoreName, "core.a") - - if _, err := buildcache.New(buildCachePath).GetOrCreate(archivedCoreName); errors.Is(err, buildcache.CreateDirErr) { - return nil, nil, fmt.Errorf(tr("creating core cache folder: %s", err)) - } - - var canUseArchivedCore bool - if onlyUpdateCompilationDatabase || 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 - canUseArchivedCore = false - } else if targetCoreFolder == nil || realCoreFolder.EquivalentTo(targetCoreFolder) { - canUseArchivedCore = true - } else if isOlder, err := utils.DirContentIsOlderThan(targetCoreFolder, targetArchivedCore); err != nil || !isOlder { - // Recreate the archive if ANY of the build core files (including platform.txt) has changed - canUseArchivedCore = false - } else { - canUseArchivedCore = true - } - - if canUseArchivedCore { - // use archived core - if verbose { - verboseInfoFn(tr("Using precompiled core: %[1]s", targetArchivedCore)) - } - return targetArchivedCore, variantObjectFiles, nil - } - } - - coreObjectFiles, err := utils.CompileFilesRecursive( - coreFolder, buildPath, buildProperties, includes, - onlyUpdateCompilationDatabase, - compilationDatabase, - jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, - progress, progressCB, - ) - if err != nil { - return nil, nil, errors.WithStack(err) - } - - archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( - buildPath, paths.New("core.a"), coreObjectFiles, buildProperties, - onlyUpdateCompilationDatabase, verbose, stdoutWriter, stderrWriter, - ) - if verbose { - verboseInfoFn(string(verboseInfo)) - } - if err != nil { - return nil, nil, errors.WithStack(err) - } - - // archive core.a - if targetArchivedCore != nil && !onlyUpdateCompilationDatabase { - err := archiveFile.CopyTo(targetArchivedCore) - if verbose { - if err == nil { - verboseInfoFn(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore)) - } else if os.IsNotExist(err) { - verboseInfoFn(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 { - verboseInfoFn(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err)) - } - } - } - - return archiveFile, variantObjectFiles, nil -} - -// 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 { - fqbnToUnderscore := strings.ReplaceAll(fqbn, ":", "_") - fqbnToUnderscore = strings.ReplaceAll(fqbnToUnderscore, "=", "_") - if absCoreFolder, err := coreFolder.Abs(); err == nil { - coreFolder = absCoreFolder - } // silently continue if absolute path can't be detected - - md5Sum := func(data []byte) string { - md5sumBytes := md5.Sum(data) - return hex.EncodeToString(md5sumBytes[:]) - } - hash := md5Sum([]byte(coreFolder.String() + optimizationFlags)) - realName := fqbnToUnderscore + "_" + hash - if len(realName) > 100 { - // avoid really long names, simply hash the name again - realName = md5Sum([]byte(realName)) - } - return realName -} diff --git a/legacy/builder/phases/libraries_builder.go b/legacy/builder/phases/libraries_builder.go index 9bd03c95f0a..0abe8cc174f 100644 --- a/legacy/builder/phases/libraries_builder.go +++ b/legacy/builder/phases/libraries_builder.go @@ -19,11 +19,12 @@ import ( "io" "strings" - "github.com/arduino/arduino-cli/arduino/builder" + "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/libraries" + "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" @@ -31,6 +32,7 @@ import ( "github.com/pkg/errors" ) +var tr = i18n.Tr var FLOAT_ABI_CFLAG = "float-abi" var FPU_CFLAG = "fpu" @@ -40,7 +42,7 @@ func LibrariesBuilder( includesFolders paths.PathList, importedLibraries libraries.List, verbose, onlyUpdateCompilationDatabase bool, - compilationDatabase *builder.CompilationDatabase, + compilationDatabase *compilation.CompilationDatabase, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, @@ -136,7 +138,7 @@ func findExpectedPrecompiledLibFolder( func compileLibraries( libraries libraries.List, buildPath *paths.Path, buildProperties *properties.Map, includes []string, verbose, onlyUpdateCompilationDatabase bool, - compilationDatabase *builder.CompilationDatabase, + compilationDatabase *compilation.CompilationDatabase, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, @@ -180,7 +182,7 @@ func compileLibraries( func compileLibrary( library *libraries.Library, buildPath *paths.Path, buildProperties *properties.Map, includes []string, verbose, onlyUpdateCompilationDatabase bool, - compilationDatabase *builder.CompilationDatabase, + compilationDatabase *compilation.CompilationDatabase, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, diff --git a/legacy/builder/phases/sketch_builder.go b/legacy/builder/phases/sketch_builder.go index 190604ddc0a..cc66d018135 100644 --- a/legacy/builder/phases/sketch_builder.go +++ b/legacy/builder/phases/sketch_builder.go @@ -18,7 +18,7 @@ package phases import ( "io" - "github.com/arduino/arduino-cli/arduino/builder" + "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" @@ -34,7 +34,7 @@ func SketchBuilder( buildProperties *properties.Map, includesFolders paths.PathList, onlyUpdateCompilationDatabase, verbose bool, - compilationDatabase *builder.CompilationDatabase, + compilationDatabase *compilation.CompilationDatabase, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 6d7ac15952f..e91604728e9 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -22,6 +22,7 @@ import ( "sync" "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/progress" "github.com/arduino/arduino-cli/arduino/cores" @@ -90,7 +91,7 @@ type Context struct { ExecutableSectionsSize builder.ExecutablesFileSections // Compilation Database to build/update - CompilationDatabase *builder.CompilationDatabase + CompilationDatabase *compilation.CompilationDatabase // Set to true to skip build and produce only Compilation Database OnlyUpdateCompilationDatabase bool From 76bb3f7a7ff950949bc22e884b8135e27eac97c4 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 12:49:01 +0200 Subject: [PATCH 02/21] rename CompilationDatabse in Database --- arduino/builder/compilation/database.go | 20 ++++++++++---------- arduino/builder/compilation/database_test.go | 4 ++-- arduino/builder/core.go | 4 ++-- arduino/builder/utils/utils.go | 8 ++++---- commands/compile/compile.go | 2 +- legacy/builder/phases/libraries_builder.go | 6 +++--- legacy/builder/phases/sketch_builder.go | 2 +- legacy/builder/types/context.go | 2 +- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/arduino/builder/compilation/database.go b/arduino/builder/compilation/database.go index e008c773aa7..81ea3daeb6f 100644 --- a/arduino/builder/compilation/database.go +++ b/arduino/builder/compilation/database.go @@ -27,8 +27,8 @@ import ( var tr = i18n.Tr -// CompilationDatabase keeps track of all the compile commands run by the builder -type CompilationDatabase struct { +// Database keeps track of all the compile commands run by the builder +type Database struct { Contents []CompilationCommand File *paths.Path } @@ -41,27 +41,27 @@ type CompilationCommand struct { File string `json:"file"` } -// NewCompilationDatabase creates an empty CompilationDatabase -func NewCompilationDatabase(filename *paths.Path) *CompilationDatabase { - return &CompilationDatabase{ +// NewDatabase creates an empty CompilationDatabase +func NewDatabase(filename *paths.Path) *Database { + return &Database{ File: filename, Contents: []CompilationCommand{}, } } -// LoadCompilationDatabase reads a compilation database from a file -func LoadCompilationDatabase(file *paths.Path) (*CompilationDatabase, error) { +// LoadDatabase reads a compilation database from a file +func LoadDatabase(file *paths.Path) (*Database, error) { f, err := file.ReadFile() if err != nil { return nil, err } - res := NewCompilationDatabase(file) + res := NewDatabase(file) return res, json.Unmarshal(f, &res.Contents) } // SaveToFile save the CompilationDatabase to file as a clangd-compatible compile_commands.json, // see https://clang.llvm.org/docs/JSONCompilationDatabase.html -func (db *CompilationDatabase) SaveToFile() { +func (db *Database) SaveToFile() { if jsonContents, err := json.MarshalIndent(db.Contents, "", " "); err != nil { fmt.Println(tr("Error serializing compilation database: %s", err)) return @@ -71,7 +71,7 @@ func (db *CompilationDatabase) SaveToFile() { } // Add adds a new CompilationDatabase entry -func (db *CompilationDatabase) Add(target *paths.Path, command *executils.Process) { +func (db *Database) Add(target *paths.Path, command *executils.Process) { commandDir := command.GetDir() if commandDir == "" { // This mimics what Cmd.Run also does: Use Dir if specified, diff --git a/arduino/builder/compilation/database_test.go b/arduino/builder/compilation/database_test.go index 7d856b51c77..26badfc6cb2 100644 --- a/arduino/builder/compilation/database_test.go +++ b/arduino/builder/compilation/database_test.go @@ -30,11 +30,11 @@ func TestCompilationDatabase(t *testing.T) { cmd, err := executils.NewProcess(nil, "gcc", "arg1", "arg2") require.NoError(t, err) - db := NewCompilationDatabase(tmpfile) + db := NewDatabase(tmpfile) db.Add(paths.New("test"), cmd) db.SaveToFile() - db2, err := LoadCompilationDatabase(tmpfile) + db2, err := LoadDatabase(tmpfile) require.NoError(t, err) require.Equal(t, db, db2) require.Len(t, db2.Contents, 1) diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 3fcf3094499..2d5cb6b9d9a 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -46,7 +46,7 @@ func CoreBuilder( buildProperties *properties.Map, actualPlatform *cores.PlatformRelease, verbose, onlyUpdateCompilationDatabase, clean bool, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, @@ -93,7 +93,7 @@ func compileCore( actualPlatform *cores.PlatformRelease, buildPath, buildCachePath *paths.Path, buildProperties *properties.Map, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, diff --git a/arduino/builder/utils/utils.go b/arduino/builder/utils/utils.go index 80d0a9a32d2..0d8e5bf85d3 100644 --- a/arduino/builder/utils/utils.go +++ b/arduino/builder/utils/utils.go @@ -339,7 +339,7 @@ func CompileFiles( buildProperties *properties.Map, includes []string, onlyUpdateCompilationDatabase bool, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, verbose bool, warningsLevel string, @@ -369,7 +369,7 @@ func CompileFilesRecursive( buildProperties *properties.Map, includes []string, onlyUpdateCompilationDatabase bool, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, verbose bool, warningsLevel string, @@ -395,7 +395,7 @@ func CompileFilesRecursive( func compileFiles( onlyUpdateCompilationDatabase bool, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, sourceDir *paths.Path, recurse bool, @@ -508,7 +508,7 @@ func compileFiles( func compileFileWithRecipe( stdoutWriter, stderrWriter io.Writer, warningsLevel string, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, verbose, onlyUpdateCompilationDatabase bool, sourcePath *paths.Path, source *paths.Path, diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 02b16fb0cf9..430968daa80 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -214,7 +214,7 @@ 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.NewCompilationDatabase( + builderCtx.CompilationDatabase = compilation.NewDatabase( builderCtx.BuildPath.Join("compile_commands.json"), ) diff --git a/legacy/builder/phases/libraries_builder.go b/legacy/builder/phases/libraries_builder.go index 0abe8cc174f..276ba619e19 100644 --- a/legacy/builder/phases/libraries_builder.go +++ b/legacy/builder/phases/libraries_builder.go @@ -42,7 +42,7 @@ func LibrariesBuilder( includesFolders paths.PathList, importedLibraries libraries.List, verbose, onlyUpdateCompilationDatabase bool, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, @@ -138,7 +138,7 @@ func findExpectedPrecompiledLibFolder( func compileLibraries( libraries libraries.List, buildPath *paths.Path, buildProperties *properties.Map, includes []string, verbose, onlyUpdateCompilationDatabase bool, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, @@ -182,7 +182,7 @@ func compileLibraries( func compileLibrary( library *libraries.Library, buildPath *paths.Path, buildProperties *properties.Map, includes []string, verbose, onlyUpdateCompilationDatabase bool, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, diff --git a/legacy/builder/phases/sketch_builder.go b/legacy/builder/phases/sketch_builder.go index cc66d018135..486aba8f1df 100644 --- a/legacy/builder/phases/sketch_builder.go +++ b/legacy/builder/phases/sketch_builder.go @@ -34,7 +34,7 @@ func SketchBuilder( buildProperties *properties.Map, includesFolders paths.PathList, onlyUpdateCompilationDatabase, verbose bool, - compilationDatabase *compilation.CompilationDatabase, + compilationDatabase *compilation.Database, jobs int, warningsLevel string, stdoutWriter, stderrWriter io.Writer, diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index e91604728e9..4b6383ad9f8 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -91,7 +91,7 @@ type Context struct { ExecutableSectionsSize builder.ExecutablesFileSections // Compilation Database to build/update - CompilationDatabase *compilation.CompilationDatabase + CompilationDatabase *compilation.Database // Set to true to skip build and produce only Compilation Database OnlyUpdateCompilationDatabase bool From 9a05ce505efd4332d98df7f926eb29d41bc8075e Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 12:52:06 +0200 Subject: [PATCH 03/21] move libraries phases in arduino/builder --- .../libraries_builder.go => arduino/builder/libraries.go | 5 ++--- legacy/builder/builder.go | 2 +- legacy/builder/phases/sizer.go | 3 +++ 3 files changed, 6 insertions(+), 4 deletions(-) rename legacy/builder/phases/libraries_builder.go => arduino/builder/libraries.go (99%) diff --git a/legacy/builder/phases/libraries_builder.go b/arduino/builder/libraries.go similarity index 99% rename from legacy/builder/phases/libraries_builder.go rename to arduino/builder/libraries.go index 276ba619e19..adc2af5a8f0 100644 --- a/legacy/builder/phases/libraries_builder.go +++ b/arduino/builder/libraries.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 phases +package builder import ( "io" @@ -24,7 +24,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/libraries" - "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" @@ -32,7 +31,7 @@ import ( "github.com/pkg/errors" ) -var tr = i18n.Tr + var FLOAT_ABI_CFLAG = "float-abi" var FPU_CFLAG = "fpu" diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 92e8d18afeb..0b977bd294e 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -110,7 +110,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - librariesObjectFiles, err := phases.LibrariesBuilder( + librariesObjectFiles, err := builder.LibrariesBuilder( ctx.LibrariesBuildPath, ctx.BuildProperties, ctx.SketchLibrariesDetector.IncludeFolders(), diff --git a/legacy/builder/phases/sizer.go b/legacy/builder/phases/sizer.go index cfc28f987c1..bb96a7aaeb2 100644 --- a/legacy/builder/phases/sizer.go +++ b/legacy/builder/phases/sizer.go @@ -24,10 +24,13 @@ import ( "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/builder/utils" + "github.com/arduino/arduino-cli/i18n" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) +var tr = i18n.Tr + func Sizer( onlyUpdateCompilationDatabase, sketchError, verbose bool, buildProperties *properties.Map, From 98ca4eefd9658137ec76a6ad2c4b6de1d3f8b830 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 12:52:48 +0200 Subject: [PATCH 04/21] move linker phases in arduino/builder --- {legacy/builder/phases => arduino/builder}/linker.go | 2 +- legacy/builder/builder.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename {legacy/builder/phases => arduino/builder}/linker.go (99%) diff --git a/legacy/builder/phases/linker.go b/arduino/builder/linker.go similarity index 99% rename from legacy/builder/phases/linker.go rename to arduino/builder/linker.go index f0c1f46e2f9..0a59bed213c 100644 --- a/legacy/builder/phases/linker.go +++ b/arduino/builder/linker.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 phases +package builder import ( "bytes" diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 0b977bd294e..6950e74aade 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -175,7 +175,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - verboseInfoOut, err := phases.Linker( + verboseInfoOut, err := builder.Linker( ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, ctx.SketchObjectFiles, From 8f8fe39e2f9f64c1085471708077e471001c3c03 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 12:54:25 +0200 Subject: [PATCH 05/21] move sketch_builder phases in arduino/builder --- arduino/builder/sketch.go | 68 ++++++++++++++++++ legacy/builder/builder.go | 2 +- legacy/builder/phases/sketch_builder.go | 91 ------------------------- 3 files changed, 69 insertions(+), 92 deletions(-) delete mode 100644 legacy/builder/phases/sketch_builder.go diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index f8eff7caafa..17b40130319 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -18,12 +18,19 @@ package builder import ( "bytes" "fmt" + "io" "regexp" + "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/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) @@ -171,3 +178,64 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error { // Source and destination are the same, don't write anything return nil } + +func SketchBuilder( + sketchBuildPath *paths.Path, + buildProperties *properties.Map, + includesFolders paths.PathList, + onlyUpdateCompilationDatabase, verbose bool, + compilationDatabase *compilation.Database, + jobs int, + warningsLevel string, + stdoutWriter, stderrWriter io.Writer, + verboseInfoFn func(msg string), + verboseStdoutFn, verboseStderrFn func(data []byte), + progress *progress.Struct, progressCB rpc.TaskProgressCB, +) (paths.PathList, error) { + includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) + + if err := sketchBuildPath.MkdirAll(); err != nil { + return nil, errors.WithStack(err) + } + + sketchObjectFiles, err := utils.CompileFiles( + sketchBuildPath, sketchBuildPath, buildProperties, includes, + onlyUpdateCompilationDatabase, + compilationDatabase, + jobs, + verbose, + warningsLevel, + stdoutWriter, stderrWriter, + verboseInfoFn, + verboseStdoutFn, + verboseStderrFn, + progress, progressCB, + ) + if err != nil { + return nil, errors.WithStack(err) + } + + // The "src/" subdirectory of a sketch is compiled recursively + sketchSrcPath := sketchBuildPath.Join("src") + if sketchSrcPath.IsDir() { + srcObjectFiles, err := utils.CompileFilesRecursive( + sketchSrcPath, sketchSrcPath, buildProperties, includes, + onlyUpdateCompilationDatabase, + compilationDatabase, + jobs, + verbose, + warningsLevel, + stdoutWriter, stderrWriter, + verboseInfoFn, + verboseStdoutFn, + verboseStderrFn, + progress, progressCB, + ) + if err != nil { + return nil, errors.WithStack(err) + } + sketchObjectFiles.AddAll(srcObjectFiles) + } + + return sketchObjectFiles, nil +} diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 6950e74aade..135644e29bc 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -71,7 +71,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - sketchObjectFiles, err := phases.SketchBuilder( + sketchObjectFiles, err := builder.SketchBuilder( ctx.SketchBuildPath, ctx.BuildProperties, ctx.SketchLibrariesDetector.IncludeFolders(), diff --git a/legacy/builder/phases/sketch_builder.go b/legacy/builder/phases/sketch_builder.go deleted file mode 100644 index 486aba8f1df..00000000000 --- a/legacy/builder/phases/sketch_builder.go +++ /dev/null @@ -1,91 +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 phases - -import ( - "io" - - "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" - 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" -) - -func SketchBuilder( - sketchBuildPath *paths.Path, - buildProperties *properties.Map, - includesFolders paths.PathList, - onlyUpdateCompilationDatabase, verbose bool, - compilationDatabase *compilation.Database, - jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), - progress *progress.Struct, progressCB rpc.TaskProgressCB, -) (paths.PathList, error) { - includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) - - if err := sketchBuildPath.MkdirAll(); err != nil { - return nil, errors.WithStack(err) - } - - sketchObjectFiles, err := utils.CompileFiles( - sketchBuildPath, sketchBuildPath, buildProperties, includes, - onlyUpdateCompilationDatabase, - compilationDatabase, - jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, - verboseStdoutFn, - verboseStderrFn, - progress, progressCB, - ) - if err != nil { - return nil, errors.WithStack(err) - } - - // The "src/" subdirectory of a sketch is compiled recursively - sketchSrcPath := sketchBuildPath.Join("src") - if sketchSrcPath.IsDir() { - srcObjectFiles, err := utils.CompileFilesRecursive( - sketchSrcPath, sketchSrcPath, buildProperties, includes, - onlyUpdateCompilationDatabase, - compilationDatabase, - jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, - verboseStdoutFn, - verboseStderrFn, - progress, progressCB, - ) - if err != nil { - return nil, errors.WithStack(err) - } - sketchObjectFiles.AddAll(srcObjectFiles) - } - - return sketchObjectFiles, nil -} From b13222c6709e378fa6631e349a0bf81ee940552d Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 12:59:25 +0200 Subject: [PATCH 06/21] move sizer phases in arduino/builder/sizer --- arduino/builder/sizer.go | 26 ------------ .../phases => arduino/builder/sizer}/sizer.go | 42 +++++++++++++++---- .../builder/sizer}/sizer_test.go | 2 +- legacy/builder/builder.go | 4 +- legacy/builder/types/context.go | 3 +- 5 files changed, 38 insertions(+), 39 deletions(-) delete mode 100644 arduino/builder/sizer.go rename {legacy/builder/phases => arduino/builder/sizer}/sizer.go (87%) rename {legacy/builder/phases => arduino/builder/sizer}/sizer_test.go (99%) diff --git a/arduino/builder/sizer.go b/arduino/builder/sizer.go deleted file mode 100644 index 911f261af67..00000000000 --- a/arduino/builder/sizer.go +++ /dev/null @@ -1,26 +0,0 @@ -package builder - -import rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" - -// ExecutableSectionSize represents a section of the executable output file -type ExecutableSectionSize struct { - Name string `json:"name"` - Size int `json:"size"` - MaxSize int `json:"max_size"` -} - -// ExecutablesFileSections is an array of ExecutablesFileSection -type ExecutablesFileSections []ExecutableSectionSize - -// ToRPCExecutableSectionSizeArray transforms this array into a []*rpc.ExecutableSectionSize -func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.ExecutableSectionSize { - res := []*rpc.ExecutableSectionSize{} - for _, section := range s { - res = append(res, &rpc.ExecutableSectionSize{ - Name: section.Name, - Size: int64(section.Size), - MaxSize: int64(section.MaxSize), - }) - } - return res -} diff --git a/legacy/builder/phases/sizer.go b/arduino/builder/sizer/sizer.go similarity index 87% rename from legacy/builder/phases/sizer.go rename to arduino/builder/sizer/sizer.go index bb96a7aaeb2..195ca3c1413 100644 --- a/legacy/builder/phases/sizer.go +++ b/arduino/builder/sizer/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 phases +package sizer import ( "encoding/json" @@ -22,22 +22,46 @@ import ( "regexp" "strconv" - "github.com/arduino/arduino-cli/arduino/builder" "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 -func Sizer( +// ExecutableSectionSize represents a section of the executable output file +type ExecutableSectionSize struct { + Name string `json:"name"` + Size int `json:"size"` + MaxSize int `json:"max_size"` +} + +// ExecutablesFileSections is an array of ExecutablesFileSection +type ExecutablesFileSections []ExecutableSectionSize + +// ToRPCExecutableSectionSizeArray transforms this array into a []*rpc.ExecutableSectionSize +func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.ExecutableSectionSize { + res := []*rpc.ExecutableSectionSize{} + for _, section := range s { + res = append(res, &rpc.ExecutableSectionSize{ + Name: section.Name, + Size: int64(section.Size), + MaxSize: int64(section.MaxSize), + }) + } + return res +} + +// Size fixdoc +func Size( onlyUpdateCompilationDatabase, sketchError, verbose bool, buildProperties *properties.Map, stdoutWriter, stderrWriter io.Writer, printInfoFn, printWarnFn func(msg string), warningsLevel string, -) (builder.ExecutablesFileSections, error) { +) (ExecutablesFileSections, error) { if onlyUpdateCompilationDatabase || sketchError { return nil, nil } @@ -53,7 +77,7 @@ func checkSizeAdvanced(buildProperties *properties.Map, verbose bool, stdoutWriter, stderrWriter io.Writer, printInfoFn, printWarnFn func(msg string), -) (builder.ExecutablesFileSections, error) { +) (ExecutablesFileSections, error) { command, err := utils.PrepareCommandForRecipe(buildProperties, "recipe.advanced_size.pattern", false) if err != nil { return nil, errors.New(tr("Error while determining sketch size: %s", err)) @@ -74,7 +98,7 @@ func checkSizeAdvanced(buildProperties *properties.Map, // likely be printed in red. Errors will stop build/upload. Severity string `json:"severity"` // Sections are the sections sizes for machine readable use - Sections []builder.ExecutableSectionSize `json:"sections"` + Sections []ExecutableSectionSize `json:"sections"` // ErrorMessage is a one line error message like: // "text section exceeds available space in board" // it must be set when Severity is "error" @@ -106,7 +130,7 @@ func checkSize(buildProperties *properties.Map, stdoutWriter, stderrWriter io.Writer, printInfoFn, printWarnFn func(msg string), warningsLevel string, -) (builder.ExecutablesFileSections, error) { +) (ExecutablesFileSections, error) { properties := buildProperties.Clone() properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+warningsLevel)) @@ -152,7 +176,7 @@ func checkSize(buildProperties *properties.Map, } } - executableSectionsSize := []builder.ExecutableSectionSize{ + executableSectionsSize := []ExecutableSectionSize{ { Name: "text", Size: textSize, @@ -160,7 +184,7 @@ func checkSize(buildProperties *properties.Map, }, } if maxDataSize > 0 { - executableSectionsSize = append(executableSectionsSize, builder.ExecutableSectionSize{ + executableSectionsSize = append(executableSectionsSize, ExecutableSectionSize{ Name: "data", Size: dataSize, MaxSize: maxDataSize, diff --git a/legacy/builder/phases/sizer_test.go b/arduino/builder/sizer/sizer_test.go similarity index 99% rename from legacy/builder/phases/sizer_test.go rename to arduino/builder/sizer/sizer_test.go index 42e26fdcfa3..42c3b323d42 100644 --- a/legacy/builder/phases/sizer_test.go +++ b/arduino/builder/sizer/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 phases +package sizer import ( "testing" diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 135644e29bc..a0657046922 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -21,9 +21,9 @@ import ( "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/phases" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/go-paths-helper" properties "github.com/arduino/go-properties-orderedmap" @@ -273,7 +273,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - executableSectionsSize, err := phases.Sizer( + executableSectionsSize, err := sizer.Size( ctx.OnlyUpdateCompilationDatabase, mainErr != nil, ctx.Verbose, ctx.BuildProperties, ctx.Stdout, ctx.Stderr, diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 4b6383ad9f8..7c5f394fa24 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -25,6 +25,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/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" @@ -88,7 +89,7 @@ type Context struct { stdLock sync.Mutex // Sizer results - ExecutableSectionsSize builder.ExecutablesFileSections + ExecutableSectionsSize sizer.ExecutablesFileSections // Compilation Database to build/update CompilationDatabase *compilation.Database From 9f489d6fd6343990c2e7543d70ffe516e54694c2 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 13:53:22 +0200 Subject: [PATCH 07/21] create bulder logger --- arduino/builder/logger/logger.go | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 arduino/builder/logger/logger.go diff --git a/arduino/builder/logger/logger.go b/arduino/builder/logger/logger.go new file mode 100644 index 00000000000..1162fe12f61 --- /dev/null +++ b/arduino/builder/logger/logger.go @@ -0,0 +1,64 @@ +package logger + +import ( + "fmt" + "io" + "os" + "sync" +) + +type BuilderLogger struct { + stdLock sync.Mutex + stdout io.Writer + stderr io.Writer + + verbose bool + warningsLevel string +} + +func New(stdout, stderr io.Writer, verbose bool, warningsLevel string) *BuilderLogger { + return &BuilderLogger{ + stdout: stdout, + stderr: stderr, + verbose: verbose, + warningsLevel: warningsLevel, + } +} + +func (l *BuilderLogger) Info(msg string) { + l.stdLock.Lock() + if l.stdout == nil { + fmt.Fprintln(os.Stdout, msg) + } else { + fmt.Fprintln(l.stdout, msg) + } + l.stdLock.Unlock() +} + +func (l *BuilderLogger) Warn(msg string) { + l.stdLock.Lock() + if l.stderr == nil { + fmt.Fprintln(os.Stderr, msg) + } else { + fmt.Fprintln(l.stderr, msg) + } + l.stdLock.Unlock() +} + +func (l *BuilderLogger) WriteStdout(data []byte) (int, error) { + l.stdLock.Lock() + defer l.stdLock.Unlock() + if l.stdout == nil { + return os.Stdout.Write(data) + } + return l.stdout.Write(data) +} + +func (l *BuilderLogger) WriteStderr(data []byte) (int, error) { + l.stdLock.Lock() + defer l.stdLock.Unlock() + if l.stderr == nil { + return os.Stderr.Write(data) + } + return l.stderr.Write(data) +} From 7e80526ad4937e797de8d397802325cfa49c257b Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 13:53:42 +0200 Subject: [PATCH 08/21] use builder logger in the sketLibrariesDetector --- arduino/builder/detector/detector.go | 62 ++++++++----------- arduino/builder/logger/logger.go | 44 +++++++------ commands/compile/compile.go | 9 ++- legacy/builder/test/builder_test.go | 8 +-- .../unused_compiled_libraries_remover_test.go | 7 ++- 5 files changed, 63 insertions(+), 67 deletions(-) diff --git a/arduino/builder/detector/detector.go b/arduino/builder/detector/detector.go index 5543236f18d..1de2017d773 100644 --- a/arduino/builder/detector/detector.go +++ b/arduino/builder/detector/detector.go @@ -26,6 +26,7 @@ import ( "golang.org/x/exp/slices" + "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/preprocessor" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/cores" @@ -52,41 +53,30 @@ type SketchLibrariesDetector struct { librariesManager *librariesmanager.LibrariesManager librariesResolver *librariesresolver.Cpp useCachedLibrariesResolution bool - verbose bool onlyUpdateCompilationDatabase bool - verboseInfoFn func(msg string) - verboseWarnFn func(msg string) - verboseStdoutFn func(data []byte) - verboseStderrFn func(data []byte) importedLibraries libraries.List librariesResolutionResults map[string]libraryResolutionResult includeFolders paths.PathList + logger *logger.BuilderLogger } // NewSketchLibrariesDetector todo func NewSketchLibrariesDetector( lm *librariesmanager.LibrariesManager, libsResolver *librariesresolver.Cpp, - verbose, useCachedLibrariesResolution bool, + useCachedLibrariesResolution bool, onlyUpdateCompilationDatabase bool, - verboseInfoFn func(msg string), - verboseWarnFn func(msg string), - verboseStdoutFn func(data []byte), - verboseStderrFn func(data []byte), + logger *logger.BuilderLogger, ) *SketchLibrariesDetector { return &SketchLibrariesDetector{ librariesManager: lm, librariesResolver: libsResolver, useCachedLibrariesResolution: useCachedLibrariesResolution, librariesResolutionResults: map[string]libraryResolutionResult{}, - verbose: verbose, - verboseInfoFn: verboseInfoFn, - verboseWarnFn: verboseWarnFn, - verboseStdoutFn: verboseStdoutFn, - verboseStderrFn: verboseStderrFn, importedLibraries: libraries.List{}, includeFolders: paths.PathList{}, onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase, + logger: logger, } } @@ -95,10 +85,10 @@ func (l *SketchLibrariesDetector) resolveLibrary(header, platformArch string) *l importedLibraries := l.importedLibraries candidates := l.librariesResolver.AlternativesFor(header) - if l.verbose { - l.verboseInfoFn(tr("Alternatives for %[1]s: %[2]s", header, candidates)) - l.verboseInfoFn(fmt.Sprintf("ResolveLibrary(%s)", header)) - l.verboseInfoFn(fmt.Sprintf(" -> %s: %s", tr("candidates"), candidates)) + if l.logger.Verbose() { + l.logger.Info(tr("Alternatives for %[1]s: %[2]s", header, candidates)) + l.logger.Info(fmt.Sprintf("ResolveLibrary(%s)", header)) + l.logger.Info(fmt.Sprintf(" -> %s: %s", tr("candidates"), candidates)) } if len(candidates) == 0 { @@ -152,7 +142,7 @@ func (l *SketchLibrariesDetector) PrintUsedAndNotUsedLibraries(sketchError bool) // - as warning, when the sketch didn't compile // - as info, when verbose is on // - otherwise, output nothing - if !sketchError && !l.verbose { + if !sketchError && !l.logger.Verbose() { return } @@ -169,9 +159,9 @@ func (l *SketchLibrariesDetector) PrintUsedAndNotUsedLibraries(sketchError bool) } res = strings.TrimSpace(res) if sketchError { - l.verboseWarnFn(res) + l.logger.Warn(res) } else { - l.verboseInfoFn(res) + l.logger.Info(res) } // todo why?? should we remove this? time.Sleep(100 * time.Millisecond) @@ -214,7 +204,7 @@ func (l *SketchLibrariesDetector) FindIncludes( ) error { err := l.findIncludes(buildPath, buildCorePath, buildVariantPath, sketchBuildPath, sketch, librariesBuildPath, buildProperties, platformArch) if err != nil && l.onlyUpdateCompilationDatabase { - l.verboseInfoFn( + l.logger.Info( fmt.Sprintf( "%s: %s", tr("An error occurred detecting libraries"), @@ -246,8 +236,8 @@ func (l *SketchLibrariesDetector) findIncludes( if err := json.Unmarshal(d, &includeFolders); err != nil { return err } - if l.verbose { - l.verboseInfoFn("Using cached library discovery: " + librariesResolutionCache.String()) + if l.logger.Verbose() { + l.logger.Info("Using cached library discovery: " + librariesResolutionCache.String()) } return nil } @@ -354,14 +344,14 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone( var missingIncludeH string if unchanged && cache.valid { missingIncludeH = cache.Next().Include - if first && l.verbose { - l.verboseInfoFn(tr("Using cached library dependencies for file: %[1]s", sourcePath)) + if first && l.logger.Verbose() { + l.logger.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath)) } } else { var preprocStdout []byte preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties) - if l.verbose { - l.verboseStdoutFn(preprocStdout) + if l.logger.Verbose() { + l.logger.WriteStdout(preprocStdout) } // Unwrap error and see if it is an ExitError. if preprocErr == nil { @@ -372,8 +362,8 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone( return errors.WithStack(preprocErr) } else { missingIncludeH = IncludesFinderWithRegExp(string(preprocStderr)) - if missingIncludeH == "" && l.verbose { - l.verboseInfoFn(tr("Error while detecting libraries included by %[1]s", sourcePath)) + if missingIncludeH == "" && l.logger.Verbose() { + l.logger.Info(tr("Error while detecting libraries included by %[1]s", sourcePath)) } } } @@ -391,8 +381,8 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone( // Filename came from cache, so run preprocessor to obtain error to show var preprocStdout []byte preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties) - if l.verbose { - l.verboseStdoutFn(preprocStdout) + if l.logger.Verbose() { + l.logger.WriteStdout(preprocStdout) } if preprocErr == nil { // If there is a missing #include in the cache, but running @@ -402,7 +392,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone( return errors.New(tr("Internal error in cache")) } } - l.verboseStderrFn(preprocStderr) + l.logger.WriteStderr(preprocStderr) return errors.WithStack(preprocErr) } @@ -414,8 +404,8 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone( if library.Precompiled && library.PrecompiledWithSources { // Fully precompiled libraries should have no dependencies to avoid ABI breakage - if l.verbose { - l.verboseInfoFn(tr("Skipping dependencies detection for precompiled library %[1]s", library.Name)) + if l.logger.Verbose() { + l.logger.Info(tr("Skipping dependencies detection for precompiled library %[1]s", library.Name)) } } else { for _, sourceDir := range library.SourceDirs() { diff --git a/arduino/builder/logger/logger.go b/arduino/builder/logger/logger.go index 1162fe12f61..2b4d09c08ed 100644 --- a/arduino/builder/logger/logger.go +++ b/arduino/builder/logger/logger.go @@ -7,6 +7,7 @@ import ( "sync" ) +// BuilderLogger fixdoc type BuilderLogger struct { stdLock sync.Mutex stdout io.Writer @@ -16,7 +17,14 @@ type BuilderLogger struct { warningsLevel string } +// New fixdoc func New(stdout, stderr io.Writer, verbose bool, warningsLevel string) *BuilderLogger { + if stdout == nil { + stdout = os.Stdout + } + if stderr == nil { + stderr = os.Stderr + } return &BuilderLogger{ stdout: stdout, stderr: stderr, @@ -25,40 +33,40 @@ func New(stdout, stderr io.Writer, verbose bool, warningsLevel string) *BuilderL } } +// Info fixdoc func (l *BuilderLogger) Info(msg string) { l.stdLock.Lock() - if l.stdout == nil { - fmt.Fprintln(os.Stdout, msg) - } else { - fmt.Fprintln(l.stdout, msg) - } - l.stdLock.Unlock() + defer l.stdLock.Unlock() + fmt.Fprintln(l.stdout, msg) } +// Warn fixdoc func (l *BuilderLogger) Warn(msg string) { l.stdLock.Lock() - if l.stderr == nil { - fmt.Fprintln(os.Stderr, msg) - } else { - fmt.Fprintln(l.stderr, msg) - } - l.stdLock.Unlock() + defer l.stdLock.Unlock() + fmt.Fprintln(l.stderr, msg) } +// WriteStdout fixdoc func (l *BuilderLogger) WriteStdout(data []byte) (int, error) { l.stdLock.Lock() defer l.stdLock.Unlock() - if l.stdout == nil { - return os.Stdout.Write(data) - } return l.stdout.Write(data) } +// WriteStderr fixdoc func (l *BuilderLogger) WriteStderr(data []byte) (int, error) { l.stdLock.Lock() defer l.stdLock.Unlock() - if l.stderr == nil { - return os.Stderr.Write(data) - } return l.stderr.Write(data) } + +// Verbose fixdoc +func (l *BuilderLogger) Verbose() bool { + return l.verbose +} + +// WarningsLevel fixdoc +func (l *BuilderLogger) WarningsLevel() string { + return l.warningsLevel +} diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 430968daa80..ce6d6ea82a3 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -26,6 +26,7 @@ import ( 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/cores" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" "github.com/arduino/arduino-cli/arduino/sketch" @@ -274,15 +275,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.Warn(string(verboseOut)) } + builderLogger := logger.New(outStream, errStream, builderCtx.Verbose, builderCtx.WarningsLevel) + builderCtx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( libsManager, libsResolver, - builderCtx.Verbose, useCachedLibrariesResolution, req.GetCreateCompilationDatabaseOnly(), - func(msg string) { builderCtx.Info(msg) }, - func(msg string) { builderCtx.Warn(msg) }, - func(data []byte) { builderCtx.WriteStdout(data) }, - func(data []byte) { builderCtx.WriteStderr(data) }, + builderLogger, ) defer func() { diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 4ec13627315..8e9cfb5e04a 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -22,6 +22,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/packagemanager" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder/constants" @@ -98,6 +99,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat sk = s } + builderLogger := logger.New(ctx.Stdout, ctx.Stderr, ctx.Verbose, ctx.WarningsLevel) ctx.Builder = bldr.NewBuilder(sk, nil, nil, false, nil, 0) if fqbn != "" { ctx.FQBN = parseFQBN(t, fqbn) @@ -130,13 +132,9 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( lm, libsResolver, - ctx.Verbose, false, false, - func(msg string) { ctx.Info(msg) }, - func(msg string) { ctx.Warn(msg) }, - func(data []byte) { ctx.WriteStdout(data) }, - func(data []byte) { ctx.WriteStderr(data) }, + builderLogger, ) } diff --git a/legacy/builder/test/unused_compiled_libraries_remover_test.go b/legacy/builder/test/unused_compiled_libraries_remover_test.go index 6f3217d505b..6e7ab70df1b 100644 --- a/legacy/builder/test/unused_compiled_libraries_remover_test.go +++ b/legacy/builder/test/unused_compiled_libraries_remover_test.go @@ -19,6 +19,7 @@ import ( "testing" "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" "github.com/arduino/arduino-cli/legacy/builder/types" @@ -38,7 +39,7 @@ func TestUnusedCompiledLibrariesRemover(t *testing.T) { ctx := &types.Context{} ctx.LibrariesBuildPath = temp ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( - nil, nil, false, false, false, nil, nil, nil, nil, + nil, nil, false, false, logger.New(nil, nil, false, ""), ) ctx.SketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) @@ -63,7 +64,7 @@ func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) { ctx := &types.Context{} ctx.LibrariesBuildPath = paths.TempDir().Join("test") ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( - nil, nil, false, false, false, nil, nil, nil, nil, + nil, nil, false, false, logger.New(nil, nil, false, ""), ) ctx.SketchLibrariesDetector.AppendImportedLibraries(&libraries.Library{Name: "Bridge"}) @@ -85,7 +86,7 @@ func TestUnusedCompiledLibrariesRemoverNoUsedLibraries(t *testing.T) { ctx := &types.Context{} ctx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( - nil, nil, false, false, false, nil, nil, nil, nil, + nil, nil, false, false, logger.New(nil, nil, false, ""), ) ctx.LibrariesBuildPath = temp From 35ffce99fff714b5b00bbf8b41296c0ce7336bf1 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:11:09 +0200 Subject: [PATCH 09/21] replace context Info logger with BuilderLogger --- commands/compile/compile.go | 19 +++++++++------- legacy/builder/builder.go | 22 +++++++++---------- legacy/builder/test/builder_test.go | 1 + .../test/merge_sketch_with_bootloader_test.go | 13 +++++++---- legacy/builder/types/context.go | 12 ++-------- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/commands/compile/compile.go b/commands/compile/compile.go index ce6d6ea82a3..a718278bde4 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -221,10 +221,12 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.Verbose = req.GetVerbose() - builderCtx.WarningsLevel = req.GetWarnings() - if builderCtx.WarningsLevel == "" { - builderCtx.WarningsLevel = builder.DEFAULT_WARNINGS_LEVEL + warningsLevel := req.GetWarnings() + // TODO move this inside the logger + if warningsLevel == "" { + warningsLevel = builder.DEFAULT_WARNINGS_LEVEL } + builderCtx.WarningsLevel = warningsLevel builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) @@ -234,6 +236,9 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly() builderCtx.SourceOverride = req.GetSourceOverride() + builderLogger := logger.New(outStream, errStream, builderCtx.Verbose, warningsLevel) + builderCtx.BuilderLogger = builderLogger + sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs() if err != nil { return r, &arduino.CompileFailedError{Message: err.Error()} @@ -272,11 +277,9 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream } if builderCtx.Verbose { - builderCtx.Warn(string(verboseOut)) + builderLogger.Warn(string(verboseOut)) } - builderLogger := logger.New(outStream, errStream, builderCtx.Verbose, builderCtx.WarningsLevel) - builderCtx.SketchLibrariesDetector = detector.NewSketchLibrariesDetector( libsManager, libsResolver, useCachedLibrariesResolution, @@ -374,7 +377,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream "recipe.hooks.savehex.presavehex", ".pattern", false, builderCtx.OnlyUpdateCompilationDatabase, builderCtx.Verbose, builderCtx.BuildProperties, builderCtx.Stdout, builderCtx.Stderr, - func(msg string) { builderCtx.Info(msg) }, + func(msg string) { builderLogger.Info(msg) }, ) if err != nil { return r, err @@ -418,7 +421,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream "recipe.hooks.savehex.postsavehex", ".pattern", false, builderCtx.OnlyUpdateCompilationDatabase, builderCtx.Verbose, builderCtx.BuildProperties, builderCtx.Stdout, builderCtx.Stderr, - func(msg string) { builderCtx.Info(msg) }, + func(msg string) { builderLogger.Info(msg) }, ) if err != nil { return r, err diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index a0657046922..a940a12fbde 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -81,7 +81,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.Builder.Jobs(), ctx.WarningsLevel, ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.Info(msg) }, + func(msg string) { ctx.BuilderLogger.Info(msg) }, func(data []byte) { ctx.WriteStdout(data) }, func(data []byte) { ctx.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, @@ -122,7 +122,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.WarningsLevel, ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.Info(msg) }, + func(msg string) { ctx.BuilderLogger.Info(msg) }, func(data []byte) { ctx.WriteStdout(data) }, func(data []byte) { ctx.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, @@ -153,7 +153,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.Builder.Jobs(), ctx.WarningsLevel, ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.Info(msg) }, + func(msg string) { ctx.BuilderLogger.Info(msg) }, func(data []byte) { ctx.WriteStdout(data) }, func(data []byte) { ctx.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, @@ -189,7 +189,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.WarningsLevel, ) if ctx.Verbose { - ctx.Info(string(verboseInfoOut)) + ctx.BuilderLogger.Info(string(verboseInfoOut)) } return err }), @@ -212,7 +212,7 @@ func (s *Builder) Run(ctx *types.Context) error { return MergeSketchWithBootloader( ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { ctx.Info(s) }, + func(s string) { ctx.BuilderLogger.Info(s) }, func(s string) { ctx.Warn(s) }, ) }), @@ -249,7 +249,7 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { infoOut, _ := PrintUsedLibrariesIfVerbose(ctx.Verbose, ctx.SketchLibrariesDetector.ImportedLibraries()) - ctx.Info(string(infoOut)) + ctx.BuilderLogger.Info(string(infoOut)) return nil }), @@ -277,7 +277,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.OnlyUpdateCompilationDatabase, mainErr != nil, ctx.Verbose, ctx.BuildProperties, ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.Info(msg) }, + func(msg string) { ctx.BuilderLogger.Info(msg) }, func(msg string) { ctx.Warn(msg) }, ctx.WarningsLevel, ) @@ -418,7 +418,7 @@ func logIfVerbose(warn bool, msg string) types.BareCommand { if warn { ctx.Warn(msg) } else { - ctx.Info(msg) + ctx.BuilderLogger.Info(msg) } return nil }) @@ -429,7 +429,7 @@ func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipI prefix, suffix, skipIfOnlyUpdatingCompilationDatabase, ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, ctx.BuildProperties, ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.Info(msg) }, + func(msg string) { ctx.BuilderLogger.Info(msg) }, ) } @@ -443,7 +443,7 @@ func containerBuildOptions(ctx *types.Context) types.BareCommand { ctx.FQBN.String(), ctx.Clean, ctx.BuildProperties, ) if infoMessage != "" { - ctx.Info(infoMessage) + ctx.BuilderLogger.Info(infoMessage) } if err != nil { return err @@ -463,7 +463,7 @@ func warnAboutArchIncompatibleLibraries(ctx *types.Context) types.BareCommand { ctx.TargetPlatform, overrides, ctx.SketchLibrariesDetector.ImportedLibraries(), - func(s string) { ctx.Info(s) }, + func(s string) { ctx.BuilderLogger.Info(s) }, ) return nil }) diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 8e9cfb5e04a..47e6476cda0 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -100,6 +100,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat } builderLogger := logger.New(ctx.Stdout, ctx.Stderr, ctx.Verbose, ctx.WarningsLevel) + ctx.BuilderLogger = builderLogger ctx.Builder = bldr.NewBuilder(sk, nil, nil, false, nil, 0) if fqbn != "" { ctx.FQBN = parseFQBN(t, fqbn) diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index 6305a174928..1e2db2ad8a4 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -21,6 +21,7 @@ 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" @@ -69,10 +70,11 @@ 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.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { ctx.Info(s) }, + func(s string) { builderLogger.Info(s) }, func(s string) { ctx.Warn(s) }, ) require.NoError(t, err) @@ -126,10 +128,11 @@ 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.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { ctx.Info(s) }, + func(s string) { builderLogger.Info(s) }, func(s string) { ctx.Warn(s) }, ) require.NoError(t, err) @@ -152,10 +155,11 @@ 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.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { ctx.Info(s) }, + func(s string) { builderLogger.Info(s) }, func(s string) { ctx.Warn(s) }, ) require.NoError(t, err) @@ -212,10 +216,11 @@ 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.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { ctx.Info(s) }, + func(s string) { builderLogger.Info(s) }, func(s string) { ctx.Warn(s) }, ) require.NoError(t, err) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 7c5f394fa24..e2e757ea5fc 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -24,6 +24,7 @@ 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/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/sizer" "github.com/arduino/arduino-cli/arduino/cores" @@ -37,6 +38,7 @@ import ( type Context struct { Builder *builder.Builder SketchLibrariesDetector *detector.SketchLibrariesDetector + BuilderLogger *logger.BuilderLogger // Build options HardwareDirs paths.PathList @@ -111,16 +113,6 @@ func (ctx *Context) PushProgress() { } } -func (ctx *Context) Info(msg string) { - ctx.stdLock.Lock() - if ctx.Stdout == nil { - fmt.Fprintln(os.Stdout, msg) - } else { - fmt.Fprintln(ctx.Stdout, msg) - } - ctx.stdLock.Unlock() -} - func (ctx *Context) Warn(msg string) { ctx.stdLock.Lock() if ctx.Stderr == nil { From b0add32b50fd43e0ba14830a63fd6ff8e95ddb4a Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:13:20 +0200 Subject: [PATCH 10/21] replace context Warn logger with BuilderLogger --- legacy/builder/builder.go | 6 +++--- .../builder/test/merge_sketch_with_bootloader_test.go | 8 ++++---- legacy/builder/types/context.go | 11 ----------- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index a940a12fbde..a1af5c4dacf 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -213,7 +213,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, func(s string) { ctx.BuilderLogger.Info(s) }, - func(s string) { ctx.Warn(s) }, + func(s string) { ctx.BuilderLogger.Warn(s) }, ) }), @@ -278,7 +278,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.BuildProperties, ctx.Stdout, ctx.Stderr, func(msg string) { ctx.BuilderLogger.Info(msg) }, - func(msg string) { ctx.Warn(msg) }, + func(msg string) { ctx.BuilderLogger.Warn(msg) }, ctx.WarningsLevel, ) ctx.ExecutableSectionsSize = executableSectionsSize @@ -416,7 +416,7 @@ func logIfVerbose(warn bool, msg string) types.BareCommand { return nil } if warn { - ctx.Warn(msg) + ctx.BuilderLogger.Warn(msg) } else { ctx.BuilderLogger.Info(msg) } diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index 1e2db2ad8a4..8f81ef240f6 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -75,7 +75,7 @@ func TestMergeSketchWithBootloader(t *testing.T) { ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, func(s string) { builderLogger.Info(s) }, - func(s string) { ctx.Warn(s) }, + func(s string) { builderLogger.Warn(s) }, ) require.NoError(t, err) @@ -133,7 +133,7 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, func(s string) { builderLogger.Info(s) }, - func(s string) { ctx.Warn(s) }, + func(s string) { builderLogger.Warn(s) }, ) require.NoError(t, err) @@ -160,7 +160,7 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, func(s string) { builderLogger.Info(s) }, - func(s string) { ctx.Warn(s) }, + func(s string) { builderLogger.Warn(s) }, ) require.NoError(t, err) @@ -221,7 +221,7 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, func(s string) { builderLogger.Info(s) }, - func(s string) { ctx.Warn(s) }, + func(s string) { builderLogger.Warn(s) }, ) require.NoError(t, err) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index e2e757ea5fc..12637b3c268 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -16,7 +16,6 @@ package types import ( - "fmt" "io" "os" "sync" @@ -113,16 +112,6 @@ func (ctx *Context) PushProgress() { } } -func (ctx *Context) Warn(msg string) { - ctx.stdLock.Lock() - if ctx.Stderr == nil { - fmt.Fprintln(os.Stderr, msg) - } else { - fmt.Fprintln(ctx.Stderr, msg) - } - ctx.stdLock.Unlock() -} - func (ctx *Context) WriteStdout(data []byte) (int, error) { ctx.stdLock.Lock() defer ctx.stdLock.Unlock() From 73ec65499719fbbb83944f7203003d76ab197535 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:14:18 +0200 Subject: [PATCH 11/21] replace context WriteStdout logger with BuilderLogger --- legacy/builder/builder.go | 16 ++++++++-------- legacy/builder/types/context.go | 9 --------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index a1af5c4dacf..65774f9c250 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -82,7 +82,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.WarningsLevel, ctx.Stdout, ctx.Stderr, func(msg string) { ctx.BuilderLogger.Info(msg) }, - func(data []byte) { ctx.WriteStdout(data) }, + func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, func(data []byte) { ctx.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, ) @@ -123,7 +123,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.Stdout, ctx.Stderr, func(msg string) { ctx.BuilderLogger.Info(msg) }, - func(data []byte) { ctx.WriteStdout(data) }, + func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, func(data []byte) { ctx.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, ) @@ -154,7 +154,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.WarningsLevel, ctx.Stdout, ctx.Stderr, func(msg string) { ctx.BuilderLogger.Info(msg) }, - func(data []byte) { ctx.WriteStdout(data) }, + func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, func(data []byte) { ctx.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, ) @@ -265,9 +265,9 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.OnlyUpdateCompilationDatabase, ) if ctx.Verbose { - ctx.WriteStdout(verboseOutput) + ctx.BuilderLogger.WriteStdout(verboseOutput) } else { - ctx.WriteStdout(normalOutput) + ctx.BuilderLogger.WriteStdout(normalOutput) } return err }), @@ -309,9 +309,9 @@ func preprocessSketchCommand(ctx *types.Context) types.BareCommand { ctx.Builder.Sketch(), ctx.BuildPath, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, ctx.BuildProperties, ctx.OnlyUpdateCompilationDatabase) if ctx.Verbose { - ctx.WriteStdout(verboseOutput) + ctx.BuilderLogger.WriteStdout(verboseOutput) } else { - ctx.WriteStdout(normalOutput) + ctx.BuilderLogger.WriteStdout(normalOutput) } return err } @@ -362,7 +362,7 @@ func (s *Preprocess) Run(ctx *types.Context) error { if err != nil { return err } - ctx.WriteStdout(preprocessedSketch) + ctx.BuilderLogger.WriteStdout(preprocessedSketch) return nil } diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 12637b3c268..6fadaa627b2 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -112,15 +112,6 @@ func (ctx *Context) PushProgress() { } } -func (ctx *Context) WriteStdout(data []byte) (int, error) { - ctx.stdLock.Lock() - defer ctx.stdLock.Unlock() - if ctx.Stdout == nil { - return os.Stdout.Write(data) - } - return ctx.Stdout.Write(data) -} - func (ctx *Context) WriteStderr(data []byte) (int, error) { ctx.stdLock.Lock() defer ctx.stdLock.Unlock() From 46aaac69c66983edc2ded3521e44e581c15842bd Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:15:01 +0200 Subject: [PATCH 12/21] replace context WriteStderr logger with BuilderLogger --- legacy/builder/builder.go | 6 +++--- legacy/builder/types/context.go | 9 --------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 65774f9c250..8c4e5c694c6 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -83,7 +83,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.Stdout, ctx.Stderr, func(msg string) { ctx.BuilderLogger.Info(msg) }, func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, - func(data []byte) { ctx.WriteStderr(data) }, + func(data []byte) { ctx.BuilderLogger.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, ) if err != nil { @@ -124,7 +124,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.Stderr, func(msg string) { ctx.BuilderLogger.Info(msg) }, func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, - func(data []byte) { ctx.WriteStderr(data) }, + func(data []byte) { ctx.BuilderLogger.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, ) if err != nil { @@ -155,7 +155,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.Stdout, ctx.Stderr, func(msg string) { ctx.BuilderLogger.Info(msg) }, func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, - func(data []byte) { ctx.WriteStderr(data) }, + func(data []byte) { ctx.BuilderLogger.WriteStderr(data) }, &ctx.Progress, ctx.ProgressCB, ) diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 6fadaa627b2..eb2470f0999 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -17,7 +17,6 @@ package types import ( "io" - "os" "sync" "github.com/arduino/arduino-cli/arduino/builder" @@ -112,11 +111,3 @@ func (ctx *Context) PushProgress() { } } -func (ctx *Context) WriteStderr(data []byte) (int, error) { - ctx.stdLock.Lock() - defer ctx.stdLock.Unlock() - if ctx.Stderr == nil { - return os.Stderr.Write(data) - } - return ctx.Stderr.Write(data) -} From 1a7dc47d8ca672ef380aa52a56ff20729993cec8 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:23:50 +0200 Subject: [PATCH 13/21] directly pass the BuilderLogger to RecipeByPrefixSuffixRunner func --- arduino/builder/logger/logger.go | 10 ++++++++++ commands/compile/compile.go | 11 +++++------ legacy/builder/builder.go | 6 +++--- legacy/builder/recipe_runner.go | 17 ++++++++--------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/arduino/builder/logger/logger.go b/arduino/builder/logger/logger.go index 2b4d09c08ed..34fc172f9c4 100644 --- a/arduino/builder/logger/logger.go +++ b/arduino/builder/logger/logger.go @@ -70,3 +70,13 @@ func (l *BuilderLogger) Verbose() bool { func (l *BuilderLogger) WarningsLevel() string { return l.warningsLevel } + +// Stdout fixdoc +func (l *BuilderLogger) Stdout() io.Writer { + return l.stdout +} + +// Stderr fixdoc +func (l *BuilderLogger) Stderr() io.Writer { + return l.stderr +} diff --git a/commands/compile/compile.go b/commands/compile/compile.go index a718278bde4..23ee0c916ea 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -375,9 +375,9 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream if exportBinaries { err := builder.RecipeByPrefixSuffixRunner( "recipe.hooks.savehex.presavehex", ".pattern", false, - builderCtx.OnlyUpdateCompilationDatabase, builderCtx.Verbose, - builderCtx.BuildProperties, builderCtx.Stdout, builderCtx.Stderr, - func(msg string) { builderLogger.Info(msg) }, + builderCtx.OnlyUpdateCompilationDatabase, + builderCtx.BuildProperties, + builderLogger, ) if err != nil { return r, err @@ -419,9 +419,8 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream err = builder.RecipeByPrefixSuffixRunner( "recipe.hooks.savehex.postsavehex", ".pattern", false, - builderCtx.OnlyUpdateCompilationDatabase, builderCtx.Verbose, - builderCtx.BuildProperties, builderCtx.Stdout, builderCtx.Stderr, - func(msg string) { builderLogger.Info(msg) }, + builderCtx.OnlyUpdateCompilationDatabase, + builderCtx.BuildProperties, builderLogger, ) if err != nil { return r, err diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 8c4e5c694c6..134a43b1340 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -427,9 +427,9 @@ func logIfVerbose(warn bool, msg string) types.BareCommand { func recipeByPrefixSuffixRunner(ctx *types.Context, prefix, suffix string, skipIfOnlyUpdatingCompilationDatabase bool) error { return RecipeByPrefixSuffixRunner( prefix, suffix, skipIfOnlyUpdatingCompilationDatabase, - ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, - ctx.BuildProperties, ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.BuilderLogger.Info(msg) }, + ctx.OnlyUpdateCompilationDatabase, + ctx.BuildProperties, + ctx.BuilderLogger, ) } diff --git a/legacy/builder/recipe_runner.go b/legacy/builder/recipe_runner.go index 78215bd211c..8af310bd4d3 100644 --- a/legacy/builder/recipe_runner.go +++ b/legacy/builder/recipe_runner.go @@ -17,10 +17,10 @@ package builder import ( "fmt" - "io" "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" @@ -29,10 +29,9 @@ import ( func RecipeByPrefixSuffixRunner( prefix, suffix string, - skipIfOnlyUpdatingCompilationDatabase, onlyUpdateCompilationDatabase, verbose bool, + skipIfOnlyUpdatingCompilationDatabase, onlyUpdateCompilationDatabase bool, buildProps *properties.Map, - stdoutWriter, stderrWriter io.Writer, - printInfoFn func(string), + builderLogger *logger.BuilderLogger, ) error { logrus.Debugf(fmt.Sprintf("Looking for recipes like %s", prefix+"*"+suffix)) @@ -51,15 +50,15 @@ func RecipeByPrefixSuffixRunner( } if onlyUpdateCompilationDatabase && skipIfOnlyUpdatingCompilationDatabase { - if verbose { - printInfoFn(tr("Skipping: %[1]s", strings.Join(command.GetArgs(), " "))) + if builderLogger.Verbose() { + builderLogger.Info(tr("Skipping: %[1]s", strings.Join(command.GetArgs(), " "))) } return nil } - verboseInfo, _, _, err := utils.ExecCommand(verbose, stdoutWriter, stderrWriter, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */) - if verbose { - printInfoFn(string(verboseInfo)) + verboseInfo, _, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */) + if builderLogger.Verbose() { + builderLogger.Info(string(verboseInfo)) } if err != nil { return errors.WithStack(err) From 9f5bd52dc990eeb5e4268f6e44bd5eaf3bea4a14 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:39:10 +0200 Subject: [PATCH 14/21] directly pass the BuilderLogger to part2 --- arduino/builder/core.go | 55 +++++++++-------------- arduino/builder/libraries.go | 80 ++++++++++++---------------------- arduino/builder/sketch.go | 23 +++------- arduino/builder/utils/utils.go | 61 ++++++++++---------------- legacy/builder/builder.go | 23 ++-------- 5 files changed, 81 insertions(+), 161 deletions(-) diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 2d5cb6b9d9a..5568a877640 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -19,12 +19,12 @@ import ( "crypto/md5" "encoding/hex" "fmt" - "io" "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/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" "github.com/arduino/arduino-cli/arduino/builder/utils" "github.com/arduino/arduino-cli/arduino/cores" @@ -45,13 +45,10 @@ func CoreBuilder( buildPath, coreBuildPath, coreBuildCachePath *paths.Path, buildProperties *properties.Map, actualPlatform *cores.PlatformRelease, - verbose, onlyUpdateCompilationDatabase, clean bool, + onlyUpdateCompilationDatabase, clean bool, compilationDatabase *compilation.Database, jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, *paths.Path, error) { if err := coreBuildPath.MkdirAll(); err != nil { @@ -60,8 +57,8 @@ func CoreBuilder( if coreBuildCachePath != nil { if _, err := coreBuildCachePath.RelTo(buildPath); err != nil { - verboseInfoFn(tr("Couldn't deeply cache core build: %[1]s", err)) - verboseInfoFn(tr("Running normal build of the core...")) + 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 { return nil, nil, errors.WithStack(err) @@ -69,16 +66,13 @@ func CoreBuilder( } archiveFile, objectFiles, err := compileCore( - verbose, onlyUpdateCompilationDatabase, clean, + onlyUpdateCompilationDatabase, clean, actualPlatform, coreBuildPath, coreBuildCachePath, buildProperties, compilationDatabase, jobs, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, - verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { @@ -89,16 +83,13 @@ func CoreBuilder( } func compileCore( - verbose, onlyUpdateCompilationDatabase, clean bool, + onlyUpdateCompilationDatabase, clean bool, actualPlatform *cores.PlatformRelease, buildPath, buildCachePath *paths.Path, buildProperties *properties.Map, compilationDatabase *compilation.Database, jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (*paths.Path, paths.PathList, error) { coreFolder := buildProperties.GetPath("build.core.path") @@ -119,10 +110,7 @@ func compileCore( onlyUpdateCompilationDatabase, compilationDatabase, jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { @@ -161,8 +149,8 @@ func compileCore( if canUseArchivedCore { // use archived core - if verbose { - verboseInfoFn(tr("Using precompiled core: %[1]s", targetArchivedCore)) + if builderLogger.Verbose() { + builderLogger.Info(tr("Using precompiled core: %[1]s", targetArchivedCore)) } return targetArchivedCore, variantObjectFiles, nil } @@ -173,10 +161,7 @@ func compileCore( onlyUpdateCompilationDatabase, compilationDatabase, jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { @@ -185,10 +170,10 @@ func compileCore( archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( buildPath, paths.New("core.a"), coreObjectFiles, buildProperties, - onlyUpdateCompilationDatabase, verbose, stdoutWriter, stderrWriter, + onlyUpdateCompilationDatabase, builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), ) - if verbose { - verboseInfoFn(string(verboseInfo)) + if builderLogger.Verbose() { + builderLogger.Info(string(verboseInfo)) } if err != nil { return nil, nil, errors.WithStack(err) @@ -197,15 +182,15 @@ func compileCore( // archive core.a if targetArchivedCore != nil && !onlyUpdateCompilationDatabase { err := archiveFile.CopyTo(targetArchivedCore) - if verbose { + if builderLogger.Verbose() { if err == nil { - verboseInfoFn(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore)) + builderLogger.Info(tr("Archiving built core (caching) in: %[1]s", targetArchivedCore)) } else if os.IsNotExist(err) { - verboseInfoFn(tr("Unable to cache built core, please tell %[1]s maintainers to follow %[2]s", + builderLogger.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 { - verboseInfoFn(tr("Error archiving built core (caching) in %[1]s: %[2]s", targetArchivedCore, err)) + builderLogger.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 adc2af5a8f0..3565c608276 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -16,11 +16,11 @@ package builder import ( - "io" "strings" "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" @@ -31,7 +31,6 @@ import ( "github.com/pkg/errors" ) - var FLOAT_ABI_CFLAG = "float-abi" var FPU_CFLAG = "fpu" @@ -40,13 +39,10 @@ func LibrariesBuilder( buildProperties *properties.Map, includesFolders paths.PathList, importedLibraries libraries.List, - verbose, onlyUpdateCompilationDatabase bool, + onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) @@ -58,13 +54,10 @@ func LibrariesBuilder( librariesObjectFiles, err := compileLibraries( libs, librariesBuildPath, buildProperties, includes, - verbose, onlyUpdateCompilationDatabase, + onlyUpdateCompilationDatabase, compilationDatabase, jobs, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, - verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { @@ -85,7 +78,7 @@ func directoryContainsFile(folder *paths.Path) bool { func findExpectedPrecompiledLibFolder( library *libraries.Library, buildProperties *properties.Map, - verboseInfoFn func(msg string), + builderLogger *logger.BuilderLogger, ) *paths.Path { mcu := buildProperties.Get("build.mcu") // Add fpu specifications if they exist @@ -112,37 +105,34 @@ func findExpectedPrecompiledLibFolder( } } - verboseInfoFn(tr("Library %[1]s has been declared precompiled:", library.Name)) + builderLogger.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) { - verboseInfoFn(tr("Using precompiled library in %[1]s", fullPrecompDir)) + builderLogger.Info(tr("Using precompiled library in %[1]s", fullPrecompDir)) return fullPrecompDir } - verboseInfoFn(tr(`Precompiled library in "%[1]s" not found`, fullPrecompDir)) + builderLogger.Info(tr(`Precompiled library in "%[1]s" not found`, fullPrecompDir)) } precompDir := library.SourceDir.Join(mcu) if precompDir.Exist() && directoryContainsFile(precompDir) { - verboseInfoFn(tr("Using precompiled library in %[1]s", precompDir)) + builderLogger.Info(tr("Using precompiled library in %[1]s", precompDir)) return precompDir } - verboseInfoFn(tr(`Precompiled library in "%[1]s" not found`, precompDir)) + builderLogger.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, - verbose, onlyUpdateCompilationDatabase bool, + onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { progress.AddSubSteps(len(libraries)) @@ -152,12 +142,10 @@ func compileLibraries( for _, library := range libraries { libraryObjectFiles, err := compileLibrary( library, buildPath, buildProperties, includes, - verbose, onlyUpdateCompilationDatabase, + onlyUpdateCompilationDatabase, compilationDatabase, jobs, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { @@ -180,17 +168,14 @@ func compileLibraries( func compileLibrary( library *libraries.Library, buildPath *paths.Path, buildProperties *properties.Map, includes []string, - verbose, onlyUpdateCompilationDatabase bool, + onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { - if verbose { - verboseInfoFn(tr(`Compiling library "%[1]s"`, library.Name)) + if builderLogger.Verbose() { + builderLogger.Info(tr(`Compiling library "%[1]s"`, library.Name)) } libraryBuildPath := buildPath.Join(library.DirName) @@ -205,11 +190,11 @@ func compileLibrary( precompiledPath := findExpectedPrecompiledLibFolder( library, buildProperties, - verboseInfoFn, + builderLogger, ) if !coreSupportPrecompiled { - verboseInfoFn(tr("The platform does not support '%[1]s' for precompiled libraries.", "compiler.libraries.ldflags")) + builderLogger.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() @@ -254,10 +239,7 @@ func compileLibrary( onlyUpdateCompilationDatabase, compilationDatabase, jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { @@ -266,11 +248,11 @@ func compileLibrary( if library.DotALinkage { archiveFile, verboseInfo, err := utils.ArchiveCompiledFiles( libraryBuildPath, paths.New(library.DirName+".a"), libObjectFiles, buildProperties, - onlyUpdateCompilationDatabase, verbose, - stdoutWriter, stderrWriter, + onlyUpdateCompilationDatabase, builderLogger.Verbose(), + builderLogger.Stdout(), builderLogger.Stderr(), ) - if verbose { - verboseInfoFn(string(verboseInfo)) + if builderLogger.Verbose() { + builderLogger.Info(string(verboseInfo)) } if err != nil { return nil, errors.WithStack(err) @@ -288,10 +270,7 @@ func compileLibrary( onlyUpdateCompilationDatabase, compilationDatabase, jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { @@ -306,10 +285,7 @@ func compileLibrary( onlyUpdateCompilationDatabase, compilationDatabase, jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index 17b40130319..4e78bc58cf2 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -18,11 +18,11 @@ package builder import ( "bytes" "fmt" - "io" "regexp" "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" @@ -183,13 +183,10 @@ func SketchBuilder( sketchBuildPath *paths.Path, buildProperties *properties.Map, includesFolders paths.PathList, - onlyUpdateCompilationDatabase, verbose bool, + onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, jobs int, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { includes := f.Map(includesFolders.AsStrings(), cpp.WrapWithHyphenI) @@ -203,12 +200,7 @@ func SketchBuilder( onlyUpdateCompilationDatabase, compilationDatabase, jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, - verboseStdoutFn, - verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { @@ -223,12 +215,7 @@ func SketchBuilder( onlyUpdateCompilationDatabase, compilationDatabase, jobs, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, - verboseStdoutFn, - verboseStderrFn, + builderLogger, progress, progressCB, ) if err != nil { diff --git a/arduino/builder/utils/utils.go b/arduino/builder/utils/utils.go index 0d8e5bf85d3..231c1252623 100644 --- a/arduino/builder/utils/utils.go +++ b/arduino/builder/utils/utils.go @@ -12,6 +12,7 @@ import ( "unicode" "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/globals" "github.com/arduino/arduino-cli/executils" @@ -341,11 +342,7 @@ func CompileFiles( onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, jobs int, - verbose bool, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { return compileFiles( @@ -355,10 +352,7 @@ func CompileFiles( sourceDir, false, buildPath, buildProperties, includes, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) } @@ -371,11 +365,7 @@ func CompileFilesRecursive( onlyUpdateCompilationDatabase bool, compilationDatabase *compilation.Database, jobs int, - verbose bool, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { return compileFiles( @@ -385,10 +375,7 @@ func CompileFilesRecursive( sourceDir, true, buildPath, buildProperties, includes, - verbose, - warningsLevel, - stdoutWriter, stderrWriter, - verboseInfoFn, verboseStdoutFn, verboseStderrFn, + builderLogger, progress, progressCB, ) } @@ -402,11 +389,7 @@ func compileFiles( buildPath *paths.Path, buildProperties *properties.Map, includes []string, - verbose bool, - warningsLevel string, - stdoutWriter, stderrWriter io.Writer, - verboseInfoFn func(msg string), - verboseStdoutFn, verboseStderrFn func(data []byte), + builderLogger *logger.BuilderLogger, progress *progress.Struct, progressCB rpc.TaskProgressCB, ) (paths.PathList, error) { @@ -438,18 +421,16 @@ func compileFiles( recipe = fmt.Sprintf("recipe%s.o.pattern", globals.SourceFilesValidExtensions[source.Ext()]) } objectFile, verboseInfo, verboseStdout, stderr, err := compileFileWithRecipe( - stdoutWriter, stderrWriter, - warningsLevel, compilationDatabase, - verbose, onlyUpdateCompilationDatabase, sourceDir, source, buildPath, buildProperties, includes, recipe, + builderLogger, ) - if verbose { - verboseStdoutFn(verboseStdout) - verboseInfoFn(string(verboseInfo)) + if builderLogger.Verbose() { + builderLogger.WriteStdout(verboseStdout) + builderLogger.Info(string(verboseInfo)) } - verboseStderrFn(stderr) + builderLogger.WriteStderr(stderr) if err != nil { errorsMux.Lock() errorsList = append(errorsList, err) @@ -506,21 +487,20 @@ func compileFiles( } func compileFileWithRecipe( - stdoutWriter, stderrWriter io.Writer, - warningsLevel string, compilationDatabase *compilation.Database, - verbose, onlyUpdateCompilationDatabase bool, + onlyUpdateCompilationDatabase bool, sourcePath *paths.Path, source *paths.Path, buildPath *paths.Path, buildProperties *properties.Map, includes []string, recipe string, + builderLogger *logger.BuilderLogger, ) (*paths.Path, []byte, []byte, []byte, error) { verboseStdout, verboseInfo, errOut := &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{} properties := buildProperties.Clone() - properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+warningsLevel)) + properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+builderLogger.WarningsLevel())) properties.Set("includes", strings.Join(includes, " ")) properties.SetPath("source_file", source) relativeSource, err := sourcePath.RelTo(source) @@ -550,9 +530,16 @@ func compileFileWithRecipe( } if !objIsUpToDate && !onlyUpdateCompilationDatabase { // Since this compile could be multithreaded, we first capture the command output - info, stdout, stderr, err := ExecCommand(verbose, stdoutWriter, stderrWriter, command, Capture, Capture) + info, stdout, stderr, err := ExecCommand( + builderLogger.Verbose(), + builderLogger.Stdout(), + builderLogger.Stderr(), + command, + Capture, + Capture, + ) // and transfer all at once at the end... - if verbose { + if builderLogger.Verbose() { verboseInfo.Write(info) verboseStdout.Write(stdout) } @@ -562,7 +549,7 @@ func compileFileWithRecipe( if err != nil { return nil, verboseInfo.Bytes(), verboseStdout.Bytes(), errOut.Bytes(), errors.WithStack(err) } - } else if verbose { + } else if builderLogger.Verbose() { if objIsUpToDate { verboseInfo.WriteString(tr("Using previously compiled file: %[1]s", objectFile)) } else { diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 134a43b1340..27ef47b26dd 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -76,14 +76,9 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.BuildProperties, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.OnlyUpdateCompilationDatabase, - ctx.Verbose, ctx.CompilationDatabase, ctx.Builder.Jobs(), - ctx.WarningsLevel, - ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.BuilderLogger.Info(msg) }, - func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, - func(data []byte) { ctx.BuilderLogger.WriteStderr(data) }, + ctx.BuilderLogger, &ctx.Progress, ctx.ProgressCB, ) if err != nil { @@ -115,16 +110,10 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.BuildProperties, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.SketchLibrariesDetector.ImportedLibraries(), - ctx.Verbose, ctx.OnlyUpdateCompilationDatabase, ctx.CompilationDatabase, ctx.Builder.Jobs(), - ctx.WarningsLevel, - ctx.Stdout, - ctx.Stderr, - func(msg string) { ctx.BuilderLogger.Info(msg) }, - func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, - func(data []byte) { ctx.BuilderLogger.WriteStderr(data) }, + ctx.BuilderLogger, &ctx.Progress, ctx.ProgressCB, ) if err != nil { @@ -148,14 +137,10 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.BuildPath, ctx.CoreBuildPath, ctx.Builder.CoreBuildCachePath(), ctx.BuildProperties, ctx.ActualPlatform, - ctx.Verbose, ctx.OnlyUpdateCompilationDatabase, ctx.Clean, + ctx.OnlyUpdateCompilationDatabase, ctx.Clean, ctx.CompilationDatabase, ctx.Builder.Jobs(), - ctx.WarningsLevel, - ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.BuilderLogger.Info(msg) }, - func(data []byte) { ctx.BuilderLogger.WriteStdout(data) }, - func(data []byte) { ctx.BuilderLogger.WriteStderr(data) }, + ctx.BuilderLogger, &ctx.Progress, ctx.ProgressCB, ) From 93d74debcfe7fe709b47901a0918c6ce0da6d60c Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:42:22 +0200 Subject: [PATCH 15/21] directly pass the BuilderLogger to Linker func --- arduino/builder/linker.go | 28 +++++++++++----------------- legacy/builder/builder.go | 5 +---- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/arduino/builder/linker.go b/arduino/builder/linker.go index 0a59bed213c..0082e1008b3 100644 --- a/arduino/builder/linker.go +++ b/arduino/builder/linker.go @@ -17,9 +17,9 @@ package builder import ( "bytes" - "io" "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" @@ -28,16 +28,15 @@ import ( ) func Linker( - onlyUpdateCompilationDatabase, verbose bool, + onlyUpdateCompilationDatabase bool, sketchObjectFiles, librariesObjectFiles, coreObjectsFiles paths.PathList, coreArchiveFilePath, buildPath *paths.Path, buildProperties *properties.Map, - stdoutWriter, stderrWriter io.Writer, - warningsLevel string, + builderLogger *logger.BuilderLogger, ) ([]byte, error) { verboseInfo := &bytes.Buffer{} if onlyUpdateCompilationDatabase { - if verbose { + if builderLogger.Verbose() { verboseInfo.WriteString(tr("Skip linking of final executable.")) } return verboseInfo.Bytes(), nil @@ -57,10 +56,7 @@ func Linker( return nil, errors.WithStack(err) } - verboseInfoOut, err := link( - objectFiles, coreDotARelPath, coreArchiveFilePath, buildProperties, - verbose, stdoutWriter, stderrWriter, warningsLevel, - ) + verboseInfoOut, err := link(objectFiles, coreDotARelPath, coreArchiveFilePath, buildProperties, builderLogger) verboseInfo.Write(verboseInfoOut) if err != nil { return verboseInfo.Bytes(), errors.WithStack(err) @@ -71,9 +67,7 @@ func Linker( func link( objectFiles paths.PathList, coreDotARelPath *paths.Path, coreArchiveFilePath *paths.Path, buildProperties *properties.Map, - verbose bool, - stdoutWriter, stderrWriter io.Writer, - warningsLevel string, + builderLogger *logger.BuilderLogger, ) ([]byte, error) { verboseBuffer := &bytes.Buffer{} wrapWithDoubleQuotes := func(value string) string { return "\"" + value + "\"" } @@ -110,8 +104,8 @@ func link( return nil, errors.WithStack(err) } - if verboseInfo, _, _, err := utils.ExecCommand(verbose, stdoutWriter, stderrWriter, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */); err != nil { - if verbose { + 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)) } return verboseBuffer.Bytes(), errors.WithStack(err) @@ -124,7 +118,7 @@ func link( properties := buildProperties.Clone() properties.Set("compiler.c.elf.flags", properties.Get("compiler.c.elf.flags")) - properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+warningsLevel)) + properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+builderLogger.WarningsLevel())) properties.Set("archive_file", coreDotARelPath.String()) properties.Set("archive_file_path", coreArchiveFilePath.String()) properties.Set("object_files", objectFileList) @@ -134,8 +128,8 @@ func link( return verboseBuffer.Bytes(), err } - verboseInfo, _, _, err := utils.ExecCommand(verbose, stdoutWriter, stderrWriter, command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */) - if verbose { + verboseInfo, _, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.ShowIfVerbose /* stdout */, utils.Show /* stderr */) + if builderLogger.Verbose() { verboseBuffer.WriteString(string(verboseInfo)) } return verboseBuffer.Bytes(), err diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 27ef47b26dd..ba5705eec97 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -162,16 +162,13 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { verboseInfoOut, err := builder.Linker( ctx.OnlyUpdateCompilationDatabase, - ctx.Verbose, ctx.SketchObjectFiles, ctx.LibrariesObjectFiles, ctx.CoreObjectsFiles, ctx.CoreArchiveFilePath, ctx.BuildPath, ctx.BuildProperties, - ctx.Stdout, - ctx.Stderr, - ctx.WarningsLevel, + ctx.BuilderLogger, ) if ctx.Verbose { ctx.BuilderLogger.Info(string(verboseInfoOut)) From 455ce61bb0adfe76108cc2ed35e0885d42601259 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:47:04 +0200 Subject: [PATCH 16/21] directly pass the BuilderLogger to sizer.Size func --- arduino/builder/sizer/sizer.go | 67 +++++++++++++--------------------- legacy/builder/builder.go | 7 +--- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/arduino/builder/sizer/sizer.go b/arduino/builder/sizer/sizer.go index 195ca3c1413..7533a8d740e 100644 --- a/arduino/builder/sizer/sizer.go +++ b/arduino/builder/sizer/sizer.go @@ -18,10 +18,10 @@ package sizer import ( "encoding/json" "fmt" - "io" "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" @@ -56,36 +56,30 @@ func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.Execut // Size fixdoc func Size( - onlyUpdateCompilationDatabase, sketchError, verbose bool, + onlyUpdateCompilationDatabase, sketchError bool, buildProperties *properties.Map, - stdoutWriter, stderrWriter io.Writer, - printInfoFn, printWarnFn func(msg string), - warningsLevel string, + builderLogger *logger.BuilderLogger, ) (ExecutablesFileSections, error) { if onlyUpdateCompilationDatabase || sketchError { return nil, nil } if buildProperties.ContainsKey("recipe.advanced_size.pattern") { - return checkSizeAdvanced(buildProperties, verbose, stdoutWriter, stderrWriter, printInfoFn, printWarnFn) + return checkSizeAdvanced(buildProperties, builderLogger) } - return checkSize(buildProperties, verbose, stdoutWriter, stderrWriter, printInfoFn, printWarnFn, warningsLevel) + return checkSize(buildProperties, builderLogger) } -func checkSizeAdvanced(buildProperties *properties.Map, - verbose bool, - stdoutWriter, stderrWriter io.Writer, - printInfoFn, printWarnFn func(msg string), -) (ExecutablesFileSections, error) { +func checkSizeAdvanced(buildProperties *properties.Map, builderLogger *logger.BuilderLogger) (ExecutablesFileSections, error) { command, err := utils.PrepareCommandForRecipe(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(verbose, stdoutWriter, stderrWriter, command, utils.Capture /* stdout */, utils.Show /* stderr */) - if verbose { - printInfoFn(string(verboseInfo)) + 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)) } if err != nil { return nil, errors.New(tr("Error while determining sketch size: %s", err)) @@ -113,26 +107,21 @@ func checkSizeAdvanced(buildProperties *properties.Map, executableSectionsSize := resp.Sections switch resp.Severity { case "error": - printWarnFn(resp.Output) + builderLogger.Warn(resp.Output) return executableSectionsSize, errors.New(resp.ErrorMessage) case "warning": - printWarnFn(resp.Output) + builderLogger.Warn(resp.Output) case "info": - printInfoFn(resp.Output) + builderLogger.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, - verbose bool, - stdoutWriter, stderrWriter io.Writer, - printInfoFn, printWarnFn func(msg string), - warningsLevel string, -) (ExecutablesFileSections, error) { +func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLogger) (ExecutablesFileSections, error) { properties := buildProperties.Clone() - properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+warningsLevel)) + properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+builderLogger.WarningsLevel())) maxTextSizeString := properties.Get("upload.maximum_size") maxDataSizeString := properties.Get("upload.maximum_data_size") @@ -154,25 +143,25 @@ func checkSize(buildProperties *properties.Map, } } - textSize, dataSize, _, err := execSizeRecipe(properties, verbose, stdoutWriter, stderrWriter, printInfoFn) + textSize, dataSize, _, err := execSizeRecipe(properties, builderLogger) if err != nil { - printWarnFn(tr("Couldn't determine program size")) + builderLogger.Warn(tr("Couldn't determine program size")) return nil, nil } - printInfoFn(tr("Sketch uses %[1]s bytes (%[3]s%%) of program storage space. Maximum is %[2]s bytes.", + builderLogger.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 { - printInfoFn(tr("Global variables use %[1]s bytes (%[3]s%%) of dynamic memory, leaving %[4]s bytes for local variables. Maximum is %[2]s bytes.", + 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.", strconv.Itoa(dataSize), strconv.Itoa(maxDataSize), strconv.Itoa(dataSize*100/maxDataSize), strconv.Itoa(maxDataSize-dataSize))) } else { - printInfoFn(tr("Global variables use %[1]s bytes of dynamic memory.", strconv.Itoa(dataSize))) + builderLogger.Info(tr("Global variables use %[1]s bytes of dynamic memory.", strconv.Itoa(dataSize))) } } @@ -192,12 +181,12 @@ func checkSize(buildProperties *properties.Map, } if textSize > maxTextSize { - printWarnFn(tr("Sketch too big; see %[1]s for tips on reducing it.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) + builderLogger.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 { - printWarnFn(tr("Not enough memory; see %[1]s for tips on reducing your footprint.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) + builderLogger.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")) } @@ -207,27 +196,23 @@ func checkSize(buildProperties *properties.Map, return executableSectionsSize, err } if maxDataSize > 0 && dataSize > maxDataSize*warnDataPercentage/100 { - printWarnFn(tr("Low memory available, stability problems may occur.")) + builderLogger.Warn(tr("Low memory available, stability problems may occur.")) } } return executableSectionsSize, nil } -func execSizeRecipe(properties *properties.Map, - verbose bool, - stdoutWriter, stderrWriter io.Writer, - printInfoFn func(msg string), -) (textSize int, dataSize int, eepromSize int, resErr error) { +func execSizeRecipe(properties *properties.Map, builderLogger *logger.BuilderLogger) (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(verbose, stdoutWriter, stderrWriter, command, utils.Capture /* stdout */, utils.Show /* stderr */) - if verbose { - printInfoFn(string(verboseInfo)) + 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)) } if err != nil { resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index ba5705eec97..f83e381551e 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -256,12 +256,9 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { executableSectionsSize, err := sizer.Size( - ctx.OnlyUpdateCompilationDatabase, mainErr != nil, ctx.Verbose, + ctx.OnlyUpdateCompilationDatabase, mainErr != nil, ctx.BuildProperties, - ctx.Stdout, ctx.Stderr, - func(msg string) { ctx.BuilderLogger.Info(msg) }, - func(msg string) { ctx.BuilderLogger.Warn(msg) }, - ctx.WarningsLevel, + ctx.BuilderLogger, ) ctx.ExecutableSectionsSize = executableSectionsSize return err From ce5276ca4dcf3837c1462d920f8ad775b6f388f4 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:48:46 +0200 Subject: [PATCH 17/21] remove Stdout, Stderr and stdlock from Context --- commands/compile/compile.go | 2 -- legacy/builder/test/builder_test.go | 2 +- legacy/builder/types/context.go | 9 --------- 3 files changed, 1 insertion(+), 12 deletions(-) diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 23ee0c916ea..a2f14f3801e 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -230,8 +230,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) - builderCtx.Stdout = outStream - builderCtx.Stderr = errStream builderCtx.Clean = req.GetClean() builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly() builderCtx.SourceOverride = req.GetSourceOverride() diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 47e6476cda0..56c92a29934 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -99,7 +99,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat sk = s } - builderLogger := logger.New(ctx.Stdout, ctx.Stderr, ctx.Verbose, ctx.WarningsLevel) + builderLogger := logger.New(nil, nil, ctx.Verbose, ctx.WarningsLevel) ctx.BuilderLogger = builderLogger ctx.Builder = bldr.NewBuilder(sk, nil, nil, false, nil, 0) if fqbn != "" { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index eb2470f0999..de14ea09497 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -16,9 +16,6 @@ package types import ( - "io" - "sync" - "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/builder/compilation" "github.com/arduino/arduino-cli/arduino/builder/detector" @@ -83,11 +80,6 @@ type Context struct { // Custom build properties defined by user (line by line as "key=value" pairs) CustomBuildProperties []string - // Out and Err stream to redirect all output - Stdout io.Writer - Stderr io.Writer - stdLock sync.Mutex - // Sizer results ExecutableSectionsSize sizer.ExecutablesFileSections @@ -110,4 +102,3 @@ func (ctx *Context) PushProgress() { }) } } - From 1dc6fcceb3736a0817d6ea7accf17d59a8f00b9d Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:51:49 +0200 Subject: [PATCH 18/21] appease golint --- arduino/builder/compilation/database.go | 10 +++++----- arduino/builder/core.go | 1 + arduino/builder/libraries.go | 12 ++++++++---- arduino/builder/linker.go | 1 + arduino/builder/sketch.go | 1 + 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/arduino/builder/compilation/database.go b/arduino/builder/compilation/database.go index 81ea3daeb6f..f6a6b1ab5f0 100644 --- a/arduino/builder/compilation/database.go +++ b/arduino/builder/compilation/database.go @@ -29,12 +29,12 @@ var tr = i18n.Tr // Database keeps track of all the compile commands run by the builder type Database struct { - Contents []CompilationCommand + Contents []Command File *paths.Path } -// CompilationCommand keeps track of a single run of a compile command -type CompilationCommand struct { +// Command keeps track of a single run of a compile command +type Command struct { Directory string `json:"directory"` Command string `json:"command,omitempty"` Arguments []string `json:"arguments,omitempty"` @@ -45,7 +45,7 @@ type CompilationCommand struct { func NewDatabase(filename *paths.Path) *Database { return &Database{ File: filename, - Contents: []CompilationCommand{}, + Contents: []Command{}, } } @@ -83,7 +83,7 @@ func (db *Database) Add(target *paths.Path, command *executils.Process) { commandDir = dir } - entry := CompilationCommand{ + entry := Command{ Directory: commandDir, Arguments: command.GetArgs(), File: target.String(), diff --git a/arduino/builder/core.go b/arduino/builder/core.go index 5568a877640..9bec697cb96 100644 --- a/arduino/builder/core.go +++ b/arduino/builder/core.go @@ -41,6 +41,7 @@ func (b *Builder) CoreBuildCachePath() *paths.Path { return b.coreBuildCachePath } +// CoreBuilder fixdoc func CoreBuilder( buildPath, coreBuildPath, coreBuildCachePath *paths.Path, buildProperties *properties.Map, diff --git a/arduino/builder/libraries.go b/arduino/builder/libraries.go index 3565c608276..af2e899840d 100644 --- a/arduino/builder/libraries.go +++ b/arduino/builder/libraries.go @@ -31,9 +31,13 @@ import ( "github.com/pkg/errors" ) -var FLOAT_ABI_CFLAG = "float-abi" -var FPU_CFLAG = "fpu" +// nolint +var ( + FloatAbiCflag = "float-abi" + FpuCflag = "fpu" +) +// LibrariesBuilder fixdoc func LibrariesBuilder( librariesBuildPath *paths.Path, buildProperties *properties.Map, @@ -87,7 +91,7 @@ func findExpectedPrecompiledLibFolder( command, _ := utils.PrepareCommandForRecipe(buildProperties, "recipe.cpp.o.pattern", true) fpuSpecs := "" for _, el := range command.GetArgs() { - if strings.Contains(el, FPU_CFLAG) { + if strings.Contains(el, FpuCflag) { toAdd := strings.Split(el, "=") if len(toAdd) > 1 { fpuSpecs += strings.TrimSpace(toAdd[1]) + "-" @@ -96,7 +100,7 @@ func findExpectedPrecompiledLibFolder( } } for _, el := range command.GetArgs() { - if strings.Contains(el, FLOAT_ABI_CFLAG) { + if strings.Contains(el, FloatAbiCflag) { toAdd := strings.Split(el, "=") if len(toAdd) > 1 { fpuSpecs += strings.TrimSpace(toAdd[1]) + "-" diff --git a/arduino/builder/linker.go b/arduino/builder/linker.go index 0082e1008b3..528e159d0aa 100644 --- a/arduino/builder/linker.go +++ b/arduino/builder/linker.go @@ -27,6 +27,7 @@ import ( "github.com/pkg/errors" ) +// Linker fixdoc func Linker( onlyUpdateCompilationDatabase bool, sketchObjectFiles, librariesObjectFiles, coreObjectsFiles paths.PathList, diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index 4e78bc58cf2..79779d5b4cc 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -179,6 +179,7 @@ func writeIfDifferent(source []byte, destPath *paths.Path) error { return nil } +// SketchBuilder fixdoc func SketchBuilder( sketchBuildPath *paths.Path, buildProperties *properties.Map, From 396faa8cf82c90167cb9bc564b9326e817a3c1ba Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 14:59:14 +0200 Subject: [PATCH 19/21] add missinig license headers --- arduino/builder/logger/logger.go | 15 +++++++++++++++ arduino/builder/progress/progress.go | 15 +++++++++++++++ arduino/builder/utils/utils.go | 15 +++++++++++++++ arduino/builder/utils/utils_test.go | 15 +++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/arduino/builder/logger/logger.go b/arduino/builder/logger/logger.go index 34fc172f9c4..51aa0277b29 100644 --- a/arduino/builder/logger/logger.go +++ b/arduino/builder/logger/logger.go @@ -1,3 +1,18 @@ +// 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 logger import ( diff --git a/arduino/builder/progress/progress.go b/arduino/builder/progress/progress.go index 53a64e803bf..2bdfaeb7539 100644 --- a/arduino/builder/progress/progress.go +++ b/arduino/builder/progress/progress.go @@ -1,3 +1,18 @@ +// 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 progress // Struct fixdoc diff --git a/arduino/builder/utils/utils.go b/arduino/builder/utils/utils.go index 231c1252623..ea3f80c5326 100644 --- a/arduino/builder/utils/utils.go +++ b/arduino/builder/utils/utils.go @@ -1,3 +1,18 @@ +// 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 utils import ( diff --git a/arduino/builder/utils/utils_test.go b/arduino/builder/utils/utils_test.go index 84cd9d699ec..3dd4c1a2142 100644 --- a/arduino/builder/utils/utils_test.go +++ b/arduino/builder/utils/utils_test.go @@ -1,3 +1,18 @@ +// 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 utils import ( From 91419cf1d2c6695f0f2345b26f44117c8d56fad3 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 15:01:50 +0200 Subject: [PATCH 20/21] remove WarningsLevel from context --- arduino/builder/logger/logger.go | 3 +++ commands/compile/compile.go | 9 +-------- legacy/builder/builder.go | 1 - legacy/builder/test/builder_test.go | 2 +- legacy/builder/types/context.go | 2 -- 5 files changed, 5 insertions(+), 12 deletions(-) diff --git a/arduino/builder/logger/logger.go b/arduino/builder/logger/logger.go index 51aa0277b29..992c15a25b2 100644 --- a/arduino/builder/logger/logger.go +++ b/arduino/builder/logger/logger.go @@ -40,6 +40,9 @@ func New(stdout, stderr io.Writer, verbose bool, warningsLevel string) *BuilderL if stderr == nil { stderr = os.Stderr } + if warningsLevel == "" { + warningsLevel = "none" + } return &BuilderLogger{ stdout: stdout, stderr: stderr, diff --git a/commands/compile/compile.go b/commands/compile/compile.go index a2f14f3801e..d1ad3f31db9 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -221,20 +221,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.Verbose = req.GetVerbose() - warningsLevel := req.GetWarnings() - // TODO move this inside the logger - if warningsLevel == "" { - warningsLevel = builder.DEFAULT_WARNINGS_LEVEL - } - builderCtx.WarningsLevel = warningsLevel - builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) builderCtx.Clean = req.GetClean() builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly() builderCtx.SourceOverride = req.GetSourceOverride() - builderLogger := logger.New(outStream, errStream, builderCtx.Verbose, warningsLevel) + builderLogger := logger.New(outStream, errStream, builderCtx.Verbose, req.GetWarnings()) builderCtx.BuilderLogger = builderLogger sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs() diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index f83e381551e..2b8cc802f4d 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -34,7 +34,6 @@ import ( var tr = i18n.Tr const DEFAULT_DEBUG_LEVEL = 5 -const DEFAULT_WARNINGS_LEVEL = "none" type Builder struct{} diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 56c92a29934..d09fc1522c3 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -99,7 +99,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat sk = s } - builderLogger := logger.New(nil, nil, ctx.Verbose, ctx.WarningsLevel) + builderLogger := logger.New(nil, nil, ctx.Verbose, "") ctx.BuilderLogger = builderLogger ctx.Builder = bldr.NewBuilder(sk, nil, nil, false, nil, 0) if fqbn != "" { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index de14ea09497..49f7a3bbf32 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -64,8 +64,6 @@ type Context struct { LibrariesObjectFiles paths.PathList SketchObjectFiles paths.PathList - WarningsLevel string - // C++ Parsing LineOffset int From 2ad132cdacaec6c773b90edc76dd9ae98d972799 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 11 Sep 2023 15:10:28 +0200 Subject: [PATCH 21/21] remove Verbose from context --- commands/compile/compile.go | 6 ++---- legacy/builder/builder.go | 15 +++++++------- .../builder/merge_sketch_with_bootloader.go | 13 ++++++------ legacy/builder/test/builder_test.go | 2 +- .../test/create_build_options_map_test.go | 1 - .../test/merge_sketch_with_bootloader_test.go | 20 ++++++++----------- .../test/store_build_options_map_test.go | 1 - .../try_build_of_problematic_sketch_test.go | 1 - legacy/builder/types/context.go | 3 --- 9 files changed, 25 insertions(+), 37 deletions(-) diff --git a/commands/compile/compile.go b/commands/compile/compile.go index d1ad3f31db9..36bba118f92 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -219,15 +219,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream builderCtx.BuildPath.Join("compile_commands.json"), ) - builderCtx.Verbose = req.GetVerbose() - builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings) builderCtx.Clean = req.GetClean() builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly() builderCtx.SourceOverride = req.GetSourceOverride() - builderLogger := logger.New(outStream, errStream, builderCtx.Verbose, req.GetWarnings()) + builderLogger := logger.New(outStream, errStream, req.GetVerbose(), req.GetWarnings()) builderCtx.BuilderLogger = builderLogger sketchBuildPath, err := buildPath.Join(constants.FOLDER_SKETCH).Abs() @@ -267,7 +265,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream return r, &arduino.CompileFailedError{Message: err.Error()} } - if builderCtx.Verbose { + if builderLogger.Verbose() { builderLogger.Warn(string(verboseOut)) } diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 2b8cc802f4d..c36335aa799 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -169,7 +169,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.BuildProperties, ctx.BuilderLogger, ) - if ctx.Verbose { + if ctx.BuilderLogger.Verbose() { ctx.BuilderLogger.Info(string(verboseInfoOut)) } return err @@ -191,10 +191,9 @@ func (s *Builder) Run(ctx *types.Context) error { types.BareCommand(func(ctx *types.Context) error { return MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, + ctx.OnlyUpdateCompilationDatabase, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { ctx.BuilderLogger.Info(s) }, - func(s string) { ctx.BuilderLogger.Warn(s) }, + ctx.BuilderLogger, ) }), @@ -229,7 +228,7 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - infoOut, _ := PrintUsedLibrariesIfVerbose(ctx.Verbose, ctx.SketchLibrariesDetector.ImportedLibraries()) + infoOut, _ := PrintUsedLibrariesIfVerbose(ctx.BuilderLogger.Verbose(), ctx.SketchLibrariesDetector.ImportedLibraries()) ctx.BuilderLogger.Info(string(infoOut)) return nil }), @@ -245,7 +244,7 @@ func (s *Builder) Run(ctx *types.Context) error { ctx.LineOffset, ctx.OnlyUpdateCompilationDatabase, ) - if ctx.Verbose { + if ctx.BuilderLogger.Verbose() { ctx.BuilderLogger.WriteStdout(verboseOutput) } else { ctx.BuilderLogger.WriteStdout(normalOutput) @@ -286,7 +285,7 @@ func preprocessSketchCommand(ctx *types.Context) types.BareCommand { normalOutput, verboseOutput, err := PreprocessSketch( ctx.Builder.Sketch(), ctx.BuildPath, ctx.SketchLibrariesDetector.IncludeFolders(), ctx.LineOffset, ctx.BuildProperties, ctx.OnlyUpdateCompilationDatabase) - if ctx.Verbose { + if ctx.BuilderLogger.Verbose() { ctx.BuilderLogger.WriteStdout(verboseOutput) } else { ctx.BuilderLogger.WriteStdout(normalOutput) @@ -390,7 +389,7 @@ func findIncludes(ctx *types.Context) types.BareCommand { func logIfVerbose(warn bool, msg string) types.BareCommand { return types.BareCommand(func(ctx *types.Context) error { - if !ctx.Verbose { + if !ctx.BuilderLogger.Verbose() { return nil } if warn { diff --git a/legacy/builder/merge_sketch_with_bootloader.go b/legacy/builder/merge_sketch_with_bootloader.go index a397ac102f4..7d77204de33 100644 --- a/legacy/builder/merge_sketch_with_bootloader.go +++ b/legacy/builder/merge_sketch_with_bootloader.go @@ -20,6 +20,7 @@ import ( "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" @@ -29,11 +30,11 @@ import ( ) func MergeSketchWithBootloader( - onlyUpdateCompilationDatabase, verbose bool, + onlyUpdateCompilationDatabase bool, buildPath *paths.Path, sketch *sketch.Sketch, buildProperties *properties.Map, - printInfoFn, printWarnFn func(string), + builderLogger *logger.BuilderLogger, ) error { if onlyUpdateCompilationDatabase { return nil @@ -66,8 +67,8 @@ func MergeSketchWithBootloader( bootloaderPath := buildProperties.GetPath("runtime.platform.path").Join(constants.FOLDER_BOOTLOADERS, bootloader) if bootloaderPath.NotExist() { - if verbose { - printWarnFn(tr("Bootloader file specified but missing: %[1]s", bootloaderPath)) + if builderLogger.Verbose() { + builderLogger.Warn(tr("Bootloader file specified but missing: %[1]s", bootloaderPath)) } return nil } @@ -81,8 +82,8 @@ func MergeSketchWithBootloader( maximumBinSize *= 2 } err := merge(builtSketchPath, bootloaderPath, mergedSketchPath, maximumBinSize) - if err != nil && verbose { - printInfoFn(err.Error()) + if err != nil && builderLogger.Verbose() { + builderLogger.Info(err.Error()) } return nil diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index d09fc1522c3..e6af9105ebc 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -99,7 +99,7 @@ func prepareBuilderTestContext(t *testing.T, ctx *types.Context, sketchPath *pat sk = s } - builderLogger := logger.New(nil, nil, ctx.Verbose, "") + builderLogger := logger.New(nil, nil, false, "") ctx.BuilderLogger = builderLogger ctx.Builder = bldr.NewBuilder(sk, nil, nil, false, nil, 0) if fqbn != "" { diff --git a/legacy/builder/test/create_build_options_map_test.go b/legacy/builder/test/create_build_options_map_test.go index b2d90ffa253..d3c5ce42580 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"), - Verbose: true, BuildPath: paths.New("buildPath"), BuildProperties: properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"}), } diff --git a/legacy/builder/test/merge_sketch_with_bootloader_test.go b/legacy/builder/test/merge_sketch_with_bootloader_test.go index 8f81ef240f6..f1d9224b840 100644 --- a/legacy/builder/test/merge_sketch_with_bootloader_test.go +++ b/legacy/builder/test/merge_sketch_with_bootloader_test.go @@ -72,10 +72,9 @@ func TestMergeSketchWithBootloader(t *testing.T) { builderLogger := logger.New(nil, nil, false, "") err = builder.MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, + ctx.OnlyUpdateCompilationDatabase, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { builderLogger.Info(s) }, - func(s string) { builderLogger.Warn(s) }, + builderLogger, ) require.NoError(t, err) @@ -130,10 +129,9 @@ func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) { builderLogger := logger.New(nil, nil, false, "") err = builder.MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, + ctx.OnlyUpdateCompilationDatabase, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { builderLogger.Info(s) }, - func(s string) { builderLogger.Warn(s) }, + builderLogger, ) require.NoError(t, err) @@ -157,10 +155,9 @@ func TestMergeSketchWithBootloaderWhenNoBootloaderAvailable(t *testing.T) { builderLogger := logger.New(nil, nil, false, "") err := builder.MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, + ctx.OnlyUpdateCompilationDatabase, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { builderLogger.Info(s) }, - func(s string) { builderLogger.Warn(s) }, + builderLogger, ) require.NoError(t, err) @@ -218,10 +215,9 @@ func TestMergeSketchWithBootloaderPathIsParameterized(t *testing.T) { builderLogger := logger.New(nil, nil, false, "") err = builder.MergeSketchWithBootloader( - ctx.OnlyUpdateCompilationDatabase, ctx.Verbose, + ctx.OnlyUpdateCompilationDatabase, ctx.BuildPath, ctx.Builder.Sketch(), ctx.BuildProperties, - func(s string) { builderLogger.Info(s) }, - func(s string) { builderLogger.Warn(s) }, + 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 a532ac5007a..c9782e4dc40 100644 --- a/legacy/builder/test/store_build_options_map_test.go +++ b/legacy/builder/test/store_build_options_map_test.go @@ -35,7 +35,6 @@ func TestStoreBuildOptionsMap(t *testing.T) { OtherLibrariesDirs: paths.NewPathList("libraries"), FQBN: parseFQBN(t, "my:nice:fqbn"), CustomBuildProperties: []string{"custom=prop"}, - Verbose: true, BuildProperties: properties.NewFromHashmap(map[string]string{"compiler.optimization_flags": "-Os"}), } 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 9a5b43a0e9f..ab40de05714 100644 --- a/legacy/builder/test/try_build_of_problematic_sketch_test.go +++ b/legacy/builder/test/try_build_of_problematic_sketch_test.go @@ -81,7 +81,6 @@ func makeDefaultContext() *types.Context { BuiltInToolsDirs: paths.NewPathList("downloaded_tools"), BuiltInLibrariesDirs: paths.New("downloaded_libraries"), OtherLibrariesDirs: paths.NewPathList("libraries"), - Verbose: true, } } diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 49f7a3bbf32..61eff082d7a 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -67,9 +67,6 @@ type Context struct { // C++ Parsing LineOffset int - // Verbosity settings - Verbose bool - // Dry run, only create progress map Progress progress.Struct // Send progress events to this callback