Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constexpr helpers to identify core version #5269

Merged
merged 10 commits into from
Nov 29, 2018
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern "C" {
#include "esp8266_peri.h"
#include "twi.h"
#include "core_esp8266_features.h"
#include "core_esp8266_version.h"

#define HIGH 0x1
#define LOW 0x0
Expand Down
11 changes: 3 additions & 8 deletions cores/esp8266/Esp-version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#define STR(x) STRHELPER(x) // stringifier

static const char arduino_esp8266_git_ver [] PROGMEM = STR(ARDUINO_ESP8266_GIT_DESC);
#if LWIP_VERSION_MAJOR != 1
static const char lwip2_version [] PROGMEM = "/lwIP:" STR(LWIP_VERSION_MAJOR) "." STR(LWIP_VERSION_MINOR) "." STR(LWIP_VERSION_REVISION);
#endif
static const char bearssl_version [] PROGMEM = "/BearSSL:" STR(BEARSSL_GIT);

String EspClass::getFullVersion()
Expand All @@ -40,17 +37,15 @@ String EspClass::getFullVersion()
+ F("/Core:") + FPSTR(arduino_esp8266_git_ver)
#if LWIP_VERSION_MAJOR == 1
+ F("/lwIP:") + String(LWIP_VERSION_MAJOR) + "." + String(LWIP_VERSION_MINOR) + "." + String(LWIP_VERSION_REVISION)
#else
+ FPSTR(lwip2_version)
#endif
#if LWIP_VERSION_IS_DEVELOPMENT
+ F("-dev")
#endif
#if LWIP_VERSION_IS_RC
+ F("rc") + String(LWIP_VERSION_RC)
#endif
#ifdef LWIP_HASH_STR
+ "(" + F(LWIP_HASH_STR) + ")"
#else // LWIP_VERSION_MAJOR != 1
+ F("/lwIP:")
+ F(LWIP_HASH_STR)
#endif
+ FPSTR(bearssl_version)
;
Expand Down
157 changes: 157 additions & 0 deletions cores/esp8266/core_esp8266_version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@

/*
core_esp8266_version.h - parse "git describe" at compile time
Copyright (c) 2018 david gauchard. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __CORE_ESP8266_VERSION_H
#define __CORE_ESP8266_VERSION_H

#include <core_version.h>

#define STRHELPER(x) #x
#define STR(x) STRHELPER(x)

#ifdef __cplusplus
extern "C++"
{

// Following constexpr functions are compiled and executed
// *after* pre-processing and *during* compilation
//
// Their result is treated like a numeric constant in final binary code.
// git tags must be in the form:
// - <major>.<minor>.<revision> (2.4.2) (2.5.0)
// - <major>.<minor>.<revision>-rc<rc> (2.5.0-rc1) (2.5.0-rc2)
//
// "git describe" = ARDUINO_ESP8266_GIT_DESC will thus be in the form:
// - <tag> (2.4.2) (2.5.0)
// - <tag>-<numberOfCommits>-g<git-hash> (2.4.2-91-gcb05b86d) (2.5.0-rc3-1-gcb05b86d)
//
// Examples:
// case 2.4.2 (fresh version/tag)
// esp8266CoreVersionSubRevision() is 0 Numeric is: 20402000
// case 2.4.2-91-gcb05b86d:
// esp8266CoreVersionSubRevision() is -91 Numeric is: 20402091
// case 2.5.0-rc3-1-gcb05b86d:
// esp8266CoreVersionSubRevision() is 3 Numeric is: 20499903
// case 2.5.0:
// esp8266CoreVersionSubRevision() is 0 Numeric is: 20500000

constexpr
bool constexpr_isDecimal (const char c)
{
return c >= '0' && c <= '9';
}

template<unsigned N> constexpr
bool constexpr_isMinus (const char (&arr) [N], unsigned i)
{
return arr[i] == '-' && constexpr_isDecimal(arr[i+1]);
}

template<unsigned N> constexpr
int constexpr_atoi (const char (&arr) [N], unsigned i)
{
return ({ // <= c++11 requires a "return statement"
int ret = 0;
int sign = 1;
if (arr[i] == '-')
{
sign = -1;
i++;
}
while (constexpr_isDecimal(arr[i]))
ret = 10*ret + arr[i++] - '0';
ret * sign;
});
}

template<unsigned N> constexpr
int constexpr_extract_int (const char (&arr) [N], unsigned f)
{
return ({ // <= c++11 requires a "return statement"
unsigned i = 0;
while (f && arr[i])
{
if (constexpr_isMinus(arr, i))
i++;
for (; constexpr_isDecimal(arr[i]); i++);
f--;
for (; arr[i] && !constexpr_isMinus(arr, i) && !constexpr_isDecimal(arr[i]); i++);
}
constexpr_atoi(arr, i);
});
}

/*
* version major
*/
constexpr
int esp8266CoreVersionMajor ()
{
return constexpr_extract_int(STR(ARDUINO_ESP8266_GIT_DESC), 0);
}

/*
* version minor
*/
constexpr
int esp8266CoreVersionMinor ()
{
return constexpr_extract_int(STR(ARDUINO_ESP8266_GIT_DESC), 1);
}

/*
* version revision
*/
constexpr
int esp8266CoreVersionRevision ()
{
return constexpr_extract_int(STR(ARDUINO_ESP8266_GIT_DESC), 2);
}

/*
* git commit number since last tag (negative)
* or RC-number (positive)
*/
constexpr
int esp8266CoreVersionSubRevision ()
{
return constexpr_extract_int(STR(ARDUINO_ESP8266_GIT_DESC), 3);
}

/*
* unique revision indentifier (never decreases)
*/
constexpr
int esp8266CoreVersionNumeric ()
{
return esp8266CoreVersionMajor() * 10000000
+ esp8266CoreVersionMinor() * 100000
+ esp8266CoreVersionRevision() * 1000
+ (esp8266CoreVersionSubRevision() < 0 ?
-esp8266CoreVersionSubRevision() :
esp8266CoreVersionSubRevision() ?
esp8266CoreVersionSubRevision() - 100 :
0);
}

} // extern "C++"
#endif // __cplusplus
#endif // __CORE_ESP8266_ESP8266_VERSION_H
9 changes: 8 additions & 1 deletion libraries/esp8266/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ WAKE_RFCAL LITERAL1
WAKE_NO_RFCAL LITERAL1
WAKE_RF_DISABLED LITERAL1
ADC_VCC LITERAL1
ADC_TOUT LITERAL1
ADC_TOUT LITERAL1

# constexpr
esp8266CoreVersionMajor LITERAL1
esp8266CoreVersionMinor LITERAL1
esp8266CoreVersionRevision LITERAL1
esp8266CoreVersionSubRevision LITERAL1
esp8266CoreVersionNumeric LITERAL1
2 changes: 2 additions & 0 deletions package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Here is an overview of the release process. See the section below for detailed i

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

* Update `version` to the release in platform.txt and commit. E.g. `2.5.0`.

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):

```
Expand Down