|
| 1 | +/* |
| 2 | + * This file is part of Arduino Builder. |
| 3 | + * |
| 4 | + * Arduino Builder is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As a special exception, you may use this file as part of a free software |
| 19 | + * library without restriction. Specifically, if other files instantiate |
| 20 | + * templates or use macros or inline functions from this file, or you compile |
| 21 | + * this file and link it with other files to produce an executable, this |
| 22 | + * file does not by itself cause the resulting executable to be covered by |
| 23 | + * the GNU General Public License. This exception does not however |
| 24 | + * invalidate any other reasons why the executable file might be covered by |
| 25 | + * the GNU General Public License. |
| 26 | + * |
| 27 | + * Copyright 2016 Arduino LLC (http://www.arduino.cc/) |
| 28 | + */ |
| 29 | + |
| 30 | +package phases |
| 31 | + |
| 32 | +import ( |
| 33 | + "errors" |
| 34 | + "regexp" |
| 35 | + "strconv" |
| 36 | + |
| 37 | + "arduino.cc/builder/builder_utils" |
| 38 | + "arduino.cc/builder/constants" |
| 39 | + "arduino.cc/builder/i18n" |
| 40 | + "arduino.cc/builder/types" |
| 41 | + "arduino.cc/properties" |
| 42 | +) |
| 43 | + |
| 44 | +type Sizer struct { |
| 45 | + SketchError bool |
| 46 | +} |
| 47 | + |
| 48 | +func (s *Sizer) Run(ctx *types.Context) error { |
| 49 | + |
| 50 | + if s.SketchError { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + buildProperties := ctx.BuildProperties |
| 55 | + verbose := ctx.Verbose |
| 56 | + warningsLevel := ctx.WarningsLevel |
| 57 | + logger := ctx.GetLogger() |
| 58 | + |
| 59 | + err := checkSize(buildProperties, verbose, warningsLevel, logger) |
| 60 | + if err != nil { |
| 61 | + return i18n.WrapError(err) |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func checkSize(buildProperties properties.Map, verbose bool, warningsLevel string, logger i18n.Logger) error { |
| 68 | + |
| 69 | + properties := buildProperties.Clone() |
| 70 | + properties[constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS] = properties[constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+warningsLevel] |
| 71 | + |
| 72 | + maxTextSizeString := properties[constants.PROPERTY_UPLOAD_MAX_SIZE] |
| 73 | + maxDataSizeString := properties[constants.PROPERTY_UPLOAD_MAX_DATA_SIZE] |
| 74 | + |
| 75 | + if maxTextSizeString == "" { |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + maxTextSize, err := strconv.Atoi(maxTextSizeString) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + maxDataSize := -1 |
| 85 | + if maxDataSizeString != "" { |
| 86 | + maxDataSize, err = strconv.Atoi(maxDataSizeString) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + textSize, dataSize, _, err := execSizeReceipe(properties, logger) |
| 93 | + if err != nil { |
| 94 | + logger.Println(constants.LOG_LEVEL_WARN, constants.MSG_SIZER_ERROR_NO_RULE) |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_SIZER_TEXT_FULL, strconv.Itoa(textSize), strconv.Itoa(maxTextSize), strconv.Itoa(textSize*100/maxTextSize)) |
| 99 | + if dataSize >= 0 { |
| 100 | + if maxDataSize > 0 { |
| 101 | + logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_SIZER_DATA_FULL, strconv.Itoa(dataSize), strconv.Itoa(maxDataSize), strconv.Itoa(dataSize*100/maxDataSize), strconv.Itoa(maxDataSize-dataSize)) |
| 102 | + } else { |
| 103 | + logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_SIZER_DATA, strconv.Itoa(dataSize)) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if textSize > maxTextSize { |
| 108 | + logger.Println(constants.LOG_LEVEL_ERROR, constants.MSG_SIZER_TEXT_TOO_BIG) |
| 109 | + return errors.New("") |
| 110 | + } |
| 111 | + |
| 112 | + if maxDataSize > 0 && dataSize > maxDataSize { |
| 113 | + logger.Println(constants.LOG_LEVEL_ERROR, constants.MSG_SIZER_DATA_TOO_BIG) |
| 114 | + return errors.New("") |
| 115 | + } |
| 116 | + |
| 117 | + if properties[constants.PROPERTY_WARN_DATA_PERCENT] != "" { |
| 118 | + warnDataPercentage, err := strconv.Atoi(properties[constants.PROPERTY_WARN_DATA_PERCENT]) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + if maxDataSize > 0 && dataSize > maxDataSize*warnDataPercentage/100 { |
| 123 | + logger.Println(constants.LOG_LEVEL_WARN, constants.MSG_SIZER_LOW_MEMORY) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func execSizeReceipe(properties properties.Map, logger i18n.Logger) (textSize int, dataSize int, eepromSize int, resErr error) { |
| 131 | + out, err := builder_utils.ExecRecipe(properties, constants.RECIPE_SIZE_PATTERN, false, false, false, logger) |
| 132 | + if err != nil { |
| 133 | + resErr = errors.New("Error while determining sketch size: " + err.Error()) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + // force multiline match prepending "(?m)" to the actual regexp |
| 138 | + // return an error if RECIPE_SIZE_REGEXP doesn't exist |
| 139 | + |
| 140 | + textSize, err = computeSize(properties[constants.RECIPE_SIZE_REGEXP], out) |
| 141 | + if err != nil { |
| 142 | + resErr = errors.New("Invalid size regexp: " + err.Error()) |
| 143 | + return |
| 144 | + } |
| 145 | + if textSize == -1 { |
| 146 | + resErr = errors.New("Missing size regexp") |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + dataSize, err = computeSize(properties[constants.RECIPE_SIZE_REGEXP_DATA], out) |
| 151 | + if err != nil { |
| 152 | + resErr = errors.New("Invalid data size regexp: " + err.Error()) |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + eepromSize, err = computeSize(properties[constants.RECIPE_SIZE_REGEXP_EEPROM], out) |
| 157 | + if err != nil { |
| 158 | + resErr = errors.New("Invalid eeprom size regexp: " + err.Error()) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + return |
| 163 | +} |
| 164 | + |
| 165 | +func computeSize(re string, output []byte) (int, error) { |
| 166 | + if re == "" { |
| 167 | + return -1, nil |
| 168 | + } |
| 169 | + r, err := regexp.Compile("(?m)" + re) |
| 170 | + if err != nil { |
| 171 | + return -1, err |
| 172 | + } |
| 173 | + result := r.FindAllSubmatch(output, -1) |
| 174 | + size := 0 |
| 175 | + for _, b := range result { |
| 176 | + for _, c := range b { |
| 177 | + if res, err := strconv.Atoi(string(c)); err == nil { |
| 178 | + size += res |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + return size, nil |
| 183 | +} |
0 commit comments