Skip to content

Commit c677714

Browse files
authored
constexpr helpers to identify core version (#5269)
1 parent fcdffc5 commit c677714

File tree

5 files changed

+181
-3
lines changed

5 files changed

+181
-3
lines changed

cores/esp8266/Arduino.h

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C" {
3838
#include "esp8266_peri.h"
3939
#include "twi.h"
4040
#include "core_esp8266_features.h"
41+
#include "core_esp8266_version.h"
4142

4243
#define HIGH 0x1
4344
#define LOW 0x0

cores/esp8266/Esp-version.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ String EspClass::getFullVersion()
3535
{
3636
return String(F("SDK:")) + system_get_sdk_version()
3737
+ F("/Core:") + FPSTR(arduino_esp8266_git_ver)
38+
+ F("=") + String(esp8266::coreVersionNumeric())
3839
#if LWIP_VERSION_MAJOR == 1
3940
+ F("/lwIP:") + String(LWIP_VERSION_MAJOR) + "." + String(LWIP_VERSION_MINOR) + "." + String(LWIP_VERSION_REVISION)
4041
#if LWIP_VERSION_IS_DEVELOPMENT
@@ -47,9 +48,9 @@ String EspClass::getFullVersion()
4748
+ F("/lwIP:")
4849
#if LWIP_IPV6
4950
+ F("IPv6+")
50-
#endif
51+
#endif // LWIP_IPV6
5152
+ F(LWIP_HASH_STR)
52-
#endif
53+
#endif // LWIP_VERSION_MAJOR != 1
5354
+ FPSTR(bearssl_version)
5455
;
5556
}

cores/esp8266/core_esp8266_version.h

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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

libraries/esp8266/keywords.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ WAKE_RFCAL LITERAL1
7777
WAKE_NO_RFCAL LITERAL1
7878
WAKE_RF_DISABLED LITERAL1
7979
ADC_VCC LITERAL1
80-
ADC_TOUT LITERAL1
80+
ADC_TOUT LITERAL1
81+
82+
#######################################
83+
# namespace esp8266
84+
#######################################
85+
coreVersionMajor LITERAL1
86+
coreVersionMinor LITERAL1
87+
coreVersionRevision LITERAL1
88+
coreVersionSubRevision LITERAL1
89+
coreVersionNumeric LITERAL1

package/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Here is an overview of the release process. See the section below for detailed i
5757

5858
* When done, put release notes into a private Gist and send the link to other maintainers for review.
5959

60+
* Update `version` to the release in platform.txt and commit. E.g. `2.5.0`.
61+
6062
2. Tag the latest commit on the master branch. In this project, tags have form `X.Y.Z`, e.g. `2.4.0`, or `X.Y.Z-rcN` for release versions. Notice that there's no `v`at the beginning of the tag. Tags must be annotated, not lightweight tags. To create a tag, use git command (assuming that the master branch is checked out):
6163

6264
```

0 commit comments

Comments
 (0)