1616package phases
1717
1818import (
19+ "encoding/json"
1920 "fmt"
2021 "regexp"
2122 "strconv"
2223
2324 "github.com/arduino/arduino-cli/legacy/builder/builder_utils"
24- "github.com/arduino/arduino-cli/legacy/builder/constants"
2525 "github.com/arduino/arduino-cli/legacy/builder/types"
2626 "github.com/arduino/arduino-cli/legacy/builder/utils"
2727 "github.com/arduino/go-properties-orderedmap"
@@ -42,20 +42,64 @@ func (s *Sizer) Run(ctx *types.Context) error {
4242
4343 buildProperties := ctx .BuildProperties
4444
45- err := checkSize (ctx , buildProperties )
45+ if buildProperties .ContainsKey ("recipe.advanced_size.pattern" ) {
46+ return checkSizeAdvanced (ctx , buildProperties )
47+ }
48+
49+ return checkSize (ctx , buildProperties )
50+ }
51+
52+ func checkSizeAdvanced (ctx * types.Context , properties * properties.Map ) error {
53+ command , err := builder_utils .PrepareCommandForRecipe (properties , "recipe.advanced_size.pattern" , false , ctx .PackageManager .GetEnvVarsForSpawnedProcess ())
4654 if err != nil {
47- return errors .WithStack ( err )
55+ return errors .New ( tr ( "Error while determining sketch size: %s" , err ) )
4856 }
4957
58+ out , _ , err := utils .ExecCommand (ctx , command , utils .Capture /* stdout */ , utils .Show /* stderr */ )
59+ if err != nil {
60+ return errors .New (tr ("Error while determining sketch size: %s" , err ))
61+ }
62+
63+ type AdvancedSizerResponse struct {
64+ // Output are the messages displayed in console to the user
65+ Output string `json:"output"`
66+ // Severity may be one of "info", "warning" or "error". Warnings and errors will
67+ // likely be printed in red. Errors will stop build/upload.
68+ Severity string `json:"severity"`
69+ // Sections are the sections sizes for machine readable use
70+ Sections []types.ExecutableSectionSize `json:"sections"`
71+ // ErrorMessage is a one line error message like:
72+ // "text section exceeds available space in board"
73+ // it must be set when Severity is "error"
74+ ErrorMessage string `json:"error"`
75+ }
76+
77+ var resp AdvancedSizerResponse
78+ if err := json .Unmarshal (out , & resp ); err != nil {
79+ return errors .New (tr ("Error while determining sketch size: %s" , err ))
80+ }
81+
82+ ctx .ExecutableSectionsSize = resp .Sections
83+ switch resp .Severity {
84+ case "error" :
85+ ctx .Warn (resp .Output )
86+ return errors .New (resp .ErrorMessage )
87+ case "warning" :
88+ ctx .Warn (resp .Output )
89+ case "info" :
90+ ctx .Info (resp .Output )
91+ default :
92+ return fmt .Errorf ("invalid '%s' severity from sketch sizer: it must be 'error', 'warning' or 'info'" , resp .Severity )
93+ }
5094 return nil
5195}
5296
5397func checkSize (ctx * types.Context , buildProperties * properties.Map ) error {
5498 properties := buildProperties .Clone ()
55- properties .Set (constants . BUILD_PROPERTIES_COMPILER_WARNING_FLAGS , properties .Get (constants . BUILD_PROPERTIES_COMPILER_WARNING_FLAGS + " ."+ ctx .WarningsLevel ))
99+ properties .Set ("compiler.warning_flags" , properties .Get ("compiler.warning_flags ."+ ctx .WarningsLevel ))
56100
57- maxTextSizeString := properties .Get (constants . PROPERTY_UPLOAD_MAX_SIZE )
58- maxDataSizeString := properties .Get (constants . PROPERTY_UPLOAD_MAX_DATA_SIZE )
101+ maxTextSizeString := properties .Get ("upload.maximum_size" )
102+ maxDataSizeString := properties .Get ("upload.maximum_data_size" )
59103
60104 if maxTextSizeString == "" {
61105 return nil
@@ -121,8 +165,8 @@ func checkSize(ctx *types.Context, buildProperties *properties.Map) error {
121165 return errors .New (tr ("data section exceeds available space in board" ))
122166 }
123167
124- if properties .Get (constants . PROPERTY_WARN_DATA_PERCENT ) != "" {
125- warnDataPercentage , err := strconv .Atoi (properties . Get ( constants . PROPERTY_WARN_DATA_PERCENT ) )
168+ if w := properties .Get ("build.warn_data_percentage" ); w != "" {
169+ warnDataPercentage , err := strconv .Atoi (w )
126170 if err != nil {
127171 return err
128172 }
@@ -135,7 +179,7 @@ func checkSize(ctx *types.Context, buildProperties *properties.Map) error {
135179}
136180
137181func execSizeRecipe (ctx * types.Context , properties * properties.Map ) (textSize int , dataSize int , eepromSize int , resErr error ) {
138- command , err := builder_utils .PrepareCommandForRecipe (properties , constants . RECIPE_SIZE_PATTERN , false , ctx .PackageManager .GetEnvVarsForSpawnedProcess ())
182+ command , err := builder_utils .PrepareCommandForRecipe (properties , "recipe.size.pattern" , false , ctx .PackageManager .GetEnvVarsForSpawnedProcess ())
139183 if err != nil {
140184 resErr = fmt .Errorf (tr ("Error while determining sketch size: %s" ), err )
141185 return
@@ -150,7 +194,7 @@ func execSizeRecipe(ctx *types.Context, properties *properties.Map) (textSize in
150194 // force multiline match prepending "(?m)" to the actual regexp
151195 // return an error if RECIPE_SIZE_REGEXP doesn't exist
152196
153- textSize , err = computeSize (properties .Get (constants . RECIPE_SIZE_REGEXP ), out )
197+ textSize , err = computeSize (properties .Get ("recipe.size.regex" ), out )
154198 if err != nil {
155199 resErr = fmt .Errorf (tr ("Invalid size regexp: %s" ), err )
156200 return
@@ -160,13 +204,13 @@ func execSizeRecipe(ctx *types.Context, properties *properties.Map) (textSize in
160204 return
161205 }
162206
163- dataSize , err = computeSize (properties .Get (constants . RECIPE_SIZE_REGEXP_DATA ), out )
207+ dataSize , err = computeSize (properties .Get ("recipe.size.regex.data" ), out )
164208 if err != nil {
165209 resErr = fmt .Errorf (tr ("Invalid data size regexp: %s" ), err )
166210 return
167211 }
168212
169- eepromSize , err = computeSize (properties .Get (constants . RECIPE_SIZE_REGEXP_EEPROM ), out )
213+ eepromSize , err = computeSize (properties .Get ("recipe.size.regex.eeprom" ), out )
170214 if err != nil {
171215 resErr = fmt .Errorf (tr ("Invalid eeprom size regexp: %s" ), err )
172216 return
0 commit comments