From 4c85a16d98ceca441756de167b55ac1e1a1d12bf Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 12 Sep 2023 19:05:13 +0200 Subject: [PATCH] make Size a method recevier of arduino/builder --- arduino/builder/{sizer => }/sizer.go | 64 ++++++++++------------- arduino/builder/{sizer => }/sizer_test.go | 2 +- legacy/builder/builder.go | 5 +- legacy/builder/types/context.go | 3 +- 4 files changed, 31 insertions(+), 43 deletions(-) rename arduino/builder/{sizer => }/sizer.go (71%) rename arduino/builder/{sizer => }/sizer_test.go (99%) diff --git a/arduino/builder/sizer/sizer.go b/arduino/builder/sizer.go similarity index 71% rename from arduino/builder/sizer/sizer.go rename to arduino/builder/sizer.go index 7533a8d740e..069ee527ff1 100644 --- a/arduino/builder/sizer/sizer.go +++ b/arduino/builder/sizer.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package sizer +package builder import ( "encoding/json" @@ -21,16 +21,12 @@ import ( "regexp" "strconv" - "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/utils" - "github.com/arduino/arduino-cli/i18n" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" ) -var tr = i18n.Tr - // ExecutableSectionSize represents a section of the executable output file type ExecutableSectionSize struct { Name string `json:"name"` @@ -55,31 +51,27 @@ func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.Execut } // Size fixdoc -func Size( - onlyUpdateCompilationDatabase, sketchError bool, - buildProperties *properties.Map, - builderLogger *logger.BuilderLogger, -) (ExecutablesFileSections, error) { +func (b *Builder) Size(onlyUpdateCompilationDatabase, sketchError bool) (ExecutablesFileSections, error) { if onlyUpdateCompilationDatabase || sketchError { return nil, nil } - if buildProperties.ContainsKey("recipe.advanced_size.pattern") { - return checkSizeAdvanced(buildProperties, builderLogger) + if b.buildProperties.ContainsKey("recipe.advanced_size.pattern") { + return b.checkSizeAdvanced() } - return checkSize(buildProperties, builderLogger) + return b.checkSize() } -func checkSizeAdvanced(buildProperties *properties.Map, builderLogger *logger.BuilderLogger) (ExecutablesFileSections, error) { - command, err := utils.PrepareCommandForRecipe(buildProperties, "recipe.advanced_size.pattern", false) +func (b *Builder) checkSizeAdvanced() (ExecutablesFileSections, error) { + command, err := utils.PrepareCommandForRecipe(b.buildProperties, "recipe.advanced_size.pattern", false) if err != nil { return nil, errors.New(tr("Error while determining sketch size: %s", err)) } - verboseInfo, out, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */) - if builderLogger.Verbose() { - builderLogger.Info(string(verboseInfo)) + verboseInfo, out, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */) + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } if err != nil { return nil, errors.New(tr("Error while determining sketch size: %s", err)) @@ -107,21 +99,21 @@ func checkSizeAdvanced(buildProperties *properties.Map, builderLogger *logger.Bu executableSectionsSize := resp.Sections switch resp.Severity { case "error": - builderLogger.Warn(resp.Output) + b.logger.Warn(resp.Output) return executableSectionsSize, errors.New(resp.ErrorMessage) case "warning": - builderLogger.Warn(resp.Output) + b.logger.Warn(resp.Output) case "info": - builderLogger.Info(resp.Output) + b.logger.Info(resp.Output) default: return executableSectionsSize, fmt.Errorf("invalid '%s' severity from sketch sizer: it must be 'error', 'warning' or 'info'", resp.Severity) } return executableSectionsSize, nil } -func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLogger) (ExecutablesFileSections, error) { - properties := buildProperties.Clone() - properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+builderLogger.WarningsLevel())) +func (b *Builder) checkSize() (ExecutablesFileSections, error) { + properties := b.buildProperties.Clone() + properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+b.logger.WarningsLevel())) maxTextSizeString := properties.Get("upload.maximum_size") maxDataSizeString := properties.Get("upload.maximum_data_size") @@ -143,25 +135,25 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog } } - textSize, dataSize, _, err := execSizeRecipe(properties, builderLogger) + textSize, dataSize, _, err := b.execSizeRecipe(properties) if err != nil { - builderLogger.Warn(tr("Couldn't determine program size")) + b.logger.Warn(tr("Couldn't determine program size")) return nil, nil } - builderLogger.Info(tr("Sketch uses %[1]s bytes (%[3]s%%) of program storage space. Maximum is %[2]s bytes.", + b.logger.Info(tr("Sketch uses %[1]s bytes (%[3]s%%) of program storage space. Maximum is %[2]s bytes.", strconv.Itoa(textSize), strconv.Itoa(maxTextSize), strconv.Itoa(textSize*100/maxTextSize))) if dataSize >= 0 { if maxDataSize > 0 { - builderLogger.Info(tr("Global variables use %[1]s bytes (%[3]s%%) of dynamic memory, leaving %[4]s bytes for local variables. Maximum is %[2]s bytes.", + b.logger.Info(tr("Global variables use %[1]s bytes (%[3]s%%) of dynamic memory, leaving %[4]s bytes for local variables. Maximum is %[2]s bytes.", strconv.Itoa(dataSize), strconv.Itoa(maxDataSize), strconv.Itoa(dataSize*100/maxDataSize), strconv.Itoa(maxDataSize-dataSize))) } else { - builderLogger.Info(tr("Global variables use %[1]s bytes of dynamic memory.", strconv.Itoa(dataSize))) + b.logger.Info(tr("Global variables use %[1]s bytes of dynamic memory.", strconv.Itoa(dataSize))) } } @@ -181,12 +173,12 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog } if textSize > maxTextSize { - builderLogger.Warn(tr("Sketch too big; see %[1]s for tips on reducing it.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) + b.logger.Warn(tr("Sketch too big; see %[1]s for tips on reducing it.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) return executableSectionsSize, errors.New(tr("text section exceeds available space in board")) } if maxDataSize > 0 && dataSize > maxDataSize { - builderLogger.Warn(tr("Not enough memory; see %[1]s for tips on reducing your footprint.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) + b.logger.Warn(tr("Not enough memory; see %[1]s for tips on reducing your footprint.", "https://support.arduino.cc/hc/en-us/articles/360013825179")) return executableSectionsSize, errors.New(tr("data section exceeds available space in board")) } @@ -196,23 +188,23 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog return executableSectionsSize, err } if maxDataSize > 0 && dataSize > maxDataSize*warnDataPercentage/100 { - builderLogger.Warn(tr("Low memory available, stability problems may occur.")) + b.logger.Warn(tr("Low memory available, stability problems may occur.")) } } return executableSectionsSize, nil } -func execSizeRecipe(properties *properties.Map, builderLogger *logger.BuilderLogger) (textSize int, dataSize int, eepromSize int, resErr error) { +func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, dataSize int, eepromSize int, resErr error) { command, err := utils.PrepareCommandForRecipe(properties, "recipe.size.pattern", false) if err != nil { resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err) return } - verboseInfo, out, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */) - if builderLogger.Verbose() { - builderLogger.Info(string(verboseInfo)) + verboseInfo, out, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */) + if b.logger.Verbose() { + b.logger.Info(string(verboseInfo)) } if err != nil { resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err) diff --git a/arduino/builder/sizer/sizer_test.go b/arduino/builder/sizer_test.go similarity index 99% rename from arduino/builder/sizer/sizer_test.go rename to arduino/builder/sizer_test.go index 42c3b323d42..79514ae6862 100644 --- a/arduino/builder/sizer/sizer_test.go +++ b/arduino/builder/sizer_test.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package sizer +package builder import ( "testing" diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 688a7913b28..83f67c68d99 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -19,7 +19,6 @@ import ( "reflect" "time" - "github.com/arduino/arduino-cli/arduino/builder/sizer" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/pkg/errors" @@ -214,10 +213,8 @@ func (s *Builder) Run(ctx *types.Context) error { }), types.BareCommand(func(ctx *types.Context) error { - executableSectionsSize, err := sizer.Size( + executableSectionsSize, err := ctx.Builder.Size( ctx.OnlyUpdateCompilationDatabase, mainErr != nil, - ctx.Builder.GetBuildProperties(), - ctx.BuilderLogger, ) ctx.ExecutableSectionsSize = executableSectionsSize return err diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index c241d02997a..b57a3076723 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -21,7 +21,6 @@ import ( "github.com/arduino/arduino-cli/arduino/builder/detector" "github.com/arduino/arduino-cli/arduino/builder/logger" "github.com/arduino/arduino-cli/arduino/builder/progress" - "github.com/arduino/arduino-cli/arduino/builder/sizer" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -58,7 +57,7 @@ type Context struct { ProgressCB rpc.TaskProgressCB // Sizer results - ExecutableSectionsSize sizer.ExecutablesFileSections + ExecutableSectionsSize builder.ExecutablesFileSections // Compilation Database to build/update CompilationDatabase *compilation.Database