16
16
package phases
17
17
18
18
import (
19
+ "encoding/json"
19
20
"fmt"
20
21
"regexp"
21
22
"strconv"
22
23
23
24
"github.com/arduino/arduino-cli/legacy/builder/builder_utils"
24
- "github.com/arduino/arduino-cli/legacy/builder/constants"
25
25
"github.com/arduino/arduino-cli/legacy/builder/types"
26
26
"github.com/arduino/arduino-cli/legacy/builder/utils"
27
27
"github.com/arduino/go-properties-orderedmap"
@@ -42,20 +42,64 @@ func (s *Sizer) Run(ctx *types.Context) error {
42
42
43
43
buildProperties := ctx .BuildProperties
44
44
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 ())
46
54
if err != nil {
47
- return errors .WithStack ( err )
55
+ return errors .New ( tr ( "Error while determining sketch size: %s" , err ) )
48
56
}
49
57
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
+ }
50
94
return nil
51
95
}
52
96
53
97
func checkSize (ctx * types.Context , buildProperties * properties.Map ) error {
54
98
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 ))
56
100
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" )
59
103
60
104
if maxTextSizeString == "" {
61
105
return nil
@@ -121,8 +165,8 @@ func checkSize(ctx *types.Context, buildProperties *properties.Map) error {
121
165
return errors .New (tr ("data section exceeds available space in board" ))
122
166
}
123
167
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 )
126
170
if err != nil {
127
171
return err
128
172
}
@@ -135,7 +179,7 @@ func checkSize(ctx *types.Context, buildProperties *properties.Map) error {
135
179
}
136
180
137
181
func 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 ())
139
183
if err != nil {
140
184
resErr = fmt .Errorf (tr ("Error while determining sketch size: %s" ), err )
141
185
return
@@ -150,7 +194,7 @@ func execSizeRecipe(ctx *types.Context, properties *properties.Map) (textSize in
150
194
// force multiline match prepending "(?m)" to the actual regexp
151
195
// return an error if RECIPE_SIZE_REGEXP doesn't exist
152
196
153
- textSize , err = computeSize (properties .Get (constants . RECIPE_SIZE_REGEXP ), out )
197
+ textSize , err = computeSize (properties .Get ("recipe.size.regex" ), out )
154
198
if err != nil {
155
199
resErr = fmt .Errorf (tr ("Invalid size regexp: %s" ), err )
156
200
return
@@ -160,13 +204,13 @@ func execSizeRecipe(ctx *types.Context, properties *properties.Map) (textSize in
160
204
return
161
205
}
162
206
163
- dataSize , err = computeSize (properties .Get (constants . RECIPE_SIZE_REGEXP_DATA ), out )
207
+ dataSize , err = computeSize (properties .Get ("recipe.size.regex.data" ), out )
164
208
if err != nil {
165
209
resErr = fmt .Errorf (tr ("Invalid data size regexp: %s" ), err )
166
210
return
167
211
}
168
212
169
- eepromSize , err = computeSize (properties .Get (constants . RECIPE_SIZE_REGEXP_EEPROM ), out )
213
+ eepromSize , err = computeSize (properties .Get ("recipe.size.regex.eeprom" ), out )
170
214
if err != nil {
171
215
resErr = fmt .Errorf (tr ("Invalid eeprom size regexp: %s" ), err )
172
216
return
0 commit comments