|
| 1 | + |
| 2 | +/* |
| 3 | + core_esp8266_version.h - parse "git describe" at compile time |
| 4 | + Copyright (c) 2018 david gauchard. All rights reserved. |
| 5 | + This file is part of the esp8266 core for Arduino environment. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | + |
| 22 | +#ifndef __CORE_ESP8266_VERSION_H |
| 23 | +#define __CORE_ESP8266_VERSION_H |
| 24 | + |
| 25 | +#include <core_version.h> |
| 26 | + |
| 27 | +#define STRHELPER(x) #x |
| 28 | +#define STR(x) STRHELPER(x) |
| 29 | + |
| 30 | +#ifdef __cplusplus |
| 31 | +extern "C++" |
| 32 | +{ |
| 33 | + |
| 34 | +// Following constexpr functions are compiled and executed |
| 35 | +// *after* pre-processing and *during* compilation |
| 36 | +// |
| 37 | +// Their result is treated like a numeric constant in final binary code. |
| 38 | +// git tags must be in the form: |
| 39 | +// - <major>.<minor>.<revision> (2.4.2) (2.5.0) |
| 40 | +// - <major>.<minor>.<revision>-rc<rc> (2.5.0-rc1) (2.5.0-rc2) |
| 41 | +// |
| 42 | +// "git describe" = ARDUINO_ESP8266_GIT_DESC will thus be in the form: |
| 43 | +// - <tag> (2.4.2) (2.5.0) |
| 44 | +// - <tag>-<numberOfCommits>-g<git-hash> (2.4.2-91-gcb05b86d) (2.5.0-rc3-1-gcb05b86d) |
| 45 | +// |
| 46 | +// Examples: |
| 47 | +// case 2.4.2 (fresh version/tag) |
| 48 | +// esp8266CoreVersionSubRevision() is 0 Numeric is: 20402000 |
| 49 | +// case 2.4.2-91-gcb05b86d: |
| 50 | +// esp8266CoreVersionSubRevision() is -91 Numeric is: 20402091 |
| 51 | +// case 2.5.0-rc3-1-gcb05b86d: |
| 52 | +// esp8266CoreVersionSubRevision() is 3 Numeric is: 20499903 |
| 53 | +// case 2.5.0: |
| 54 | +// esp8266CoreVersionSubRevision() is 0 Numeric is: 20500000 |
| 55 | + |
| 56 | +namespace conststr { |
| 57 | + |
| 58 | +constexpr |
| 59 | +bool isDecimal (const char c) |
| 60 | +{ |
| 61 | + return c >= '0' && c <= '9'; |
| 62 | +} |
| 63 | + |
| 64 | +template<unsigned N> constexpr |
| 65 | +bool isMinus (const char (&arr) [N], unsigned i) |
| 66 | +{ |
| 67 | + return arr[i] == '-' && isDecimal(arr[i+1]); |
| 68 | +} |
| 69 | + |
| 70 | +template<unsigned N> constexpr |
| 71 | +int atoi (const char (&arr) [N], unsigned i) |
| 72 | +{ |
| 73 | + return ({ // <= c++11 requires a "return statement" |
| 74 | + int ret = 0; |
| 75 | + int sign = 1; |
| 76 | + if (arr[i] == '-') |
| 77 | + { |
| 78 | + sign = -1; |
| 79 | + i++; |
| 80 | + } |
| 81 | + while (isDecimal(arr[i])) |
| 82 | + ret = 10*ret + arr[i++] - '0'; |
| 83 | + ret * sign; |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +template<unsigned N> constexpr |
| 88 | +int parseNthInteger (const char (&arr) [N], unsigned f) |
| 89 | +{ |
| 90 | + return ({ // <= c++11 requires a "return statement" |
| 91 | + unsigned i = 0; |
| 92 | + while (f && arr[i]) |
| 93 | + { |
| 94 | + if (isMinus(arr, i)) |
| 95 | + i++; |
| 96 | + for (; isDecimal(arr[i]); i++); |
| 97 | + f--; |
| 98 | + for (; arr[i] && !isMinus(arr, i) && !isDecimal(arr[i]); i++); |
| 99 | + } |
| 100 | + atoi(arr, i); |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +}; // namespace conststr |
| 105 | + |
| 106 | +namespace esp8266 { |
| 107 | + |
| 108 | +/* |
| 109 | + * version major |
| 110 | + */ |
| 111 | +constexpr |
| 112 | +int coreVersionMajor () |
| 113 | +{ |
| 114 | + return conststr::parseNthInteger(STR(ARDUINO_ESP8266_GIT_DESC), 0); |
| 115 | +} |
| 116 | + |
| 117 | +/* |
| 118 | + * version minor |
| 119 | + */ |
| 120 | +constexpr |
| 121 | +int coreVersionMinor () |
| 122 | +{ |
| 123 | + return conststr::parseNthInteger(STR(ARDUINO_ESP8266_GIT_DESC), 1); |
| 124 | +} |
| 125 | + |
| 126 | +/* |
| 127 | + * version revision |
| 128 | + */ |
| 129 | +constexpr |
| 130 | +int coreVersionRevision () |
| 131 | +{ |
| 132 | + return conststr::parseNthInteger(STR(ARDUINO_ESP8266_GIT_DESC), 2); |
| 133 | +} |
| 134 | + |
| 135 | +/* |
| 136 | + * git commit number since last tag (negative) |
| 137 | + * or RC-number (positive) |
| 138 | + */ |
| 139 | +constexpr |
| 140 | +int coreVersionSubRevision () |
| 141 | +{ |
| 142 | + return conststr::parseNthInteger(STR(ARDUINO_ESP8266_GIT_DESC), 3); |
| 143 | +} |
| 144 | + |
| 145 | +/* |
| 146 | + * unique revision indentifier (never decreases) |
| 147 | + */ |
| 148 | +constexpr |
| 149 | +int coreVersionNumeric () |
| 150 | +{ |
| 151 | + return coreVersionMajor() * 10000000 |
| 152 | + + coreVersionMinor() * 100000 |
| 153 | + + coreVersionRevision() * 1000 |
| 154 | + + (coreVersionSubRevision() < 0 ? |
| 155 | + -coreVersionSubRevision() : |
| 156 | + coreVersionSubRevision() ? |
| 157 | + coreVersionSubRevision() - 100 : |
| 158 | + 0); |
| 159 | +} |
| 160 | + |
| 161 | +}; // namespace esp8266 |
| 162 | + |
| 163 | +} // extern "C++" |
| 164 | +#endif // __cplusplus |
| 165 | +#endif // __CORE_ESP8266_ESP8266_VERSION_H |
0 commit comments