Skip to content

Commit

Permalink
make Size a method recevier of arduino/builder
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 12, 2023
1 parent 02df09c commit 4c85a16
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 43 deletions.
64 changes: 28 additions & 36 deletions arduino/builder/sizer/sizer.go → arduino/builder/sizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,20 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package sizer
package builder

import (
"encoding/json"
"fmt"
"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"`
Expand All @@ -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))
Expand Down Expand Up @@ -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")
Expand All @@ -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)))
}
}

Expand All @@ -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"))
}

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package sizer
package builder

import (
"testing"
Expand Down
5 changes: 1 addition & 4 deletions legacy/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions legacy/builder/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4c85a16

Please sign in to comment.