Skip to content

Commit 4c85a16

Browse files
make Size a method recevier of arduino/builder
1 parent 02df09c commit 4c85a16

File tree

4 files changed

+31
-43
lines changed

4 files changed

+31
-43
lines changed

arduino/builder/sizer/sizer.go renamed to arduino/builder/sizer.go

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,20 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to license@arduino.cc.
1515

16-
package sizer
16+
package builder
1717

1818
import (
1919
"encoding/json"
2020
"fmt"
2121
"regexp"
2222
"strconv"
2323

24-
"github.com/arduino/arduino-cli/arduino/builder/logger"
2524
"github.com/arduino/arduino-cli/arduino/builder/utils"
26-
"github.com/arduino/arduino-cli/i18n"
2725
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2826
"github.com/arduino/go-properties-orderedmap"
2927
"github.com/pkg/errors"
3028
)
3129

32-
var tr = i18n.Tr
33-
3430
// ExecutableSectionSize represents a section of the executable output file
3531
type ExecutableSectionSize struct {
3632
Name string `json:"name"`
@@ -55,31 +51,27 @@ func (s ExecutablesFileSections) ToRPCExecutableSectionSizeArray() []*rpc.Execut
5551
}
5652

5753
// Size fixdoc
58-
func Size(
59-
onlyUpdateCompilationDatabase, sketchError bool,
60-
buildProperties *properties.Map,
61-
builderLogger *logger.BuilderLogger,
62-
) (ExecutablesFileSections, error) {
54+
func (b *Builder) Size(onlyUpdateCompilationDatabase, sketchError bool) (ExecutablesFileSections, error) {
6355
if onlyUpdateCompilationDatabase || sketchError {
6456
return nil, nil
6557
}
6658

67-
if buildProperties.ContainsKey("recipe.advanced_size.pattern") {
68-
return checkSizeAdvanced(buildProperties, builderLogger)
59+
if b.buildProperties.ContainsKey("recipe.advanced_size.pattern") {
60+
return b.checkSizeAdvanced()
6961
}
7062

71-
return checkSize(buildProperties, builderLogger)
63+
return b.checkSize()
7264
}
7365

74-
func checkSizeAdvanced(buildProperties *properties.Map, builderLogger *logger.BuilderLogger) (ExecutablesFileSections, error) {
75-
command, err := utils.PrepareCommandForRecipe(buildProperties, "recipe.advanced_size.pattern", false)
66+
func (b *Builder) checkSizeAdvanced() (ExecutablesFileSections, error) {
67+
command, err := utils.PrepareCommandForRecipe(b.buildProperties, "recipe.advanced_size.pattern", false)
7668
if err != nil {
7769
return nil, errors.New(tr("Error while determining sketch size: %s", err))
7870
}
7971

80-
verboseInfo, out, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */)
81-
if builderLogger.Verbose() {
82-
builderLogger.Info(string(verboseInfo))
72+
verboseInfo, out, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */)
73+
if b.logger.Verbose() {
74+
b.logger.Info(string(verboseInfo))
8375
}
8476
if err != nil {
8577
return nil, errors.New(tr("Error while determining sketch size: %s", err))
@@ -107,21 +99,21 @@ func checkSizeAdvanced(buildProperties *properties.Map, builderLogger *logger.Bu
10799
executableSectionsSize := resp.Sections
108100
switch resp.Severity {
109101
case "error":
110-
builderLogger.Warn(resp.Output)
102+
b.logger.Warn(resp.Output)
111103
return executableSectionsSize, errors.New(resp.ErrorMessage)
112104
case "warning":
113-
builderLogger.Warn(resp.Output)
105+
b.logger.Warn(resp.Output)
114106
case "info":
115-
builderLogger.Info(resp.Output)
107+
b.logger.Info(resp.Output)
116108
default:
117109
return executableSectionsSize, fmt.Errorf("invalid '%s' severity from sketch sizer: it must be 'error', 'warning' or 'info'", resp.Severity)
118110
}
119111
return executableSectionsSize, nil
120112
}
121113

122-
func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLogger) (ExecutablesFileSections, error) {
123-
properties := buildProperties.Clone()
124-
properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+builderLogger.WarningsLevel()))
114+
func (b *Builder) checkSize() (ExecutablesFileSections, error) {
115+
properties := b.buildProperties.Clone()
116+
properties.Set("compiler.warning_flags", properties.Get("compiler.warning_flags."+b.logger.WarningsLevel()))
125117

126118
maxTextSizeString := properties.Get("upload.maximum_size")
127119
maxDataSizeString := properties.Get("upload.maximum_data_size")
@@ -143,25 +135,25 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog
143135
}
144136
}
145137

146-
textSize, dataSize, _, err := execSizeRecipe(properties, builderLogger)
138+
textSize, dataSize, _, err := b.execSizeRecipe(properties)
147139
if err != nil {
148-
builderLogger.Warn(tr("Couldn't determine program size"))
140+
b.logger.Warn(tr("Couldn't determine program size"))
149141
return nil, nil
150142
}
151143

152-
builderLogger.Info(tr("Sketch uses %[1]s bytes (%[3]s%%) of program storage space. Maximum is %[2]s bytes.",
144+
b.logger.Info(tr("Sketch uses %[1]s bytes (%[3]s%%) of program storage space. Maximum is %[2]s bytes.",
153145
strconv.Itoa(textSize),
154146
strconv.Itoa(maxTextSize),
155147
strconv.Itoa(textSize*100/maxTextSize)))
156148
if dataSize >= 0 {
157149
if maxDataSize > 0 {
158-
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.",
150+
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.",
159151
strconv.Itoa(dataSize),
160152
strconv.Itoa(maxDataSize),
161153
strconv.Itoa(dataSize*100/maxDataSize),
162154
strconv.Itoa(maxDataSize-dataSize)))
163155
} else {
164-
builderLogger.Info(tr("Global variables use %[1]s bytes of dynamic memory.", strconv.Itoa(dataSize)))
156+
b.logger.Info(tr("Global variables use %[1]s bytes of dynamic memory.", strconv.Itoa(dataSize)))
165157
}
166158
}
167159

@@ -181,12 +173,12 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog
181173
}
182174

183175
if textSize > maxTextSize {
184-
builderLogger.Warn(tr("Sketch too big; see %[1]s for tips on reducing it.", "https://support.arduino.cc/hc/en-us/articles/360013825179"))
176+
b.logger.Warn(tr("Sketch too big; see %[1]s for tips on reducing it.", "https://support.arduino.cc/hc/en-us/articles/360013825179"))
185177
return executableSectionsSize, errors.New(tr("text section exceeds available space in board"))
186178
}
187179

188180
if maxDataSize > 0 && dataSize > maxDataSize {
189-
builderLogger.Warn(tr("Not enough memory; see %[1]s for tips on reducing your footprint.", "https://support.arduino.cc/hc/en-us/articles/360013825179"))
181+
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"))
190182
return executableSectionsSize, errors.New(tr("data section exceeds available space in board"))
191183
}
192184

@@ -196,23 +188,23 @@ func checkSize(buildProperties *properties.Map, builderLogger *logger.BuilderLog
196188
return executableSectionsSize, err
197189
}
198190
if maxDataSize > 0 && dataSize > maxDataSize*warnDataPercentage/100 {
199-
builderLogger.Warn(tr("Low memory available, stability problems may occur."))
191+
b.logger.Warn(tr("Low memory available, stability problems may occur."))
200192
}
201193
}
202194

203195
return executableSectionsSize, nil
204196
}
205197

206-
func execSizeRecipe(properties *properties.Map, builderLogger *logger.BuilderLogger) (textSize int, dataSize int, eepromSize int, resErr error) {
198+
func (b *Builder) execSizeRecipe(properties *properties.Map) (textSize int, dataSize int, eepromSize int, resErr error) {
207199
command, err := utils.PrepareCommandForRecipe(properties, "recipe.size.pattern", false)
208200
if err != nil {
209201
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)
210202
return
211203
}
212204

213-
verboseInfo, out, _, err := utils.ExecCommand(builderLogger.Verbose(), builderLogger.Stdout(), builderLogger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */)
214-
if builderLogger.Verbose() {
215-
builderLogger.Info(string(verboseInfo))
205+
verboseInfo, out, _, err := utils.ExecCommand(b.logger.Verbose(), b.logger.Stdout(), b.logger.Stderr(), command, utils.Capture /* stdout */, utils.Show /* stderr */)
206+
if b.logger.Verbose() {
207+
b.logger.Info(string(verboseInfo))
216208
}
217209
if err != nil {
218210
resErr = fmt.Errorf(tr("Error while determining sketch size: %s"), err)

arduino/builder/sizer/sizer_test.go renamed to arduino/builder/sizer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Arduino software without disclosing the source code of your own applications.
1414
// To purchase a commercial license, send an email to license@arduino.cc.
1515

16-
package sizer
16+
package builder
1717

1818
import (
1919
"testing"

legacy/builder/builder.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"reflect"
2020
"time"
2121

22-
"github.com/arduino/arduino-cli/arduino/builder/sizer"
2322
"github.com/arduino/arduino-cli/i18n"
2423
"github.com/arduino/arduino-cli/legacy/builder/types"
2524
"github.com/pkg/errors"
@@ -214,10 +213,8 @@ func (s *Builder) Run(ctx *types.Context) error {
214213
}),
215214

216215
types.BareCommand(func(ctx *types.Context) error {
217-
executableSectionsSize, err := sizer.Size(
216+
executableSectionsSize, err := ctx.Builder.Size(
218217
ctx.OnlyUpdateCompilationDatabase, mainErr != nil,
219-
ctx.Builder.GetBuildProperties(),
220-
ctx.BuilderLogger,
221218
)
222219
ctx.ExecutableSectionsSize = executableSectionsSize
223220
return err

legacy/builder/types/context.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/arduino/arduino-cli/arduino/builder/detector"
2222
"github.com/arduino/arduino-cli/arduino/builder/logger"
2323
"github.com/arduino/arduino-cli/arduino/builder/progress"
24-
"github.com/arduino/arduino-cli/arduino/builder/sizer"
2524
"github.com/arduino/arduino-cli/arduino/cores"
2625
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2726
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -58,7 +57,7 @@ type Context struct {
5857
ProgressCB rpc.TaskProgressCB
5958

6059
// Sizer results
61-
ExecutableSectionsSize sizer.ExecutablesFileSections
60+
ExecutableSectionsSize builder.ExecutablesFileSections
6261

6362
// Compilation Database to build/update
6463
CompilationDatabase *compilation.Database

0 commit comments

Comments
 (0)