Add build timestamp to the version number (#1145)

I've found this rather useful when repeatedly deploying updated firmware
images to a large number of nodes.

The patch carefully avoids full rebuilds of any "real" source files by
putting the timestamp into a separate .cpp file. It also works around
PlatformIO's build cache that would happily permanently cache the timestamp
file if we were using __DATE__.

The Python script isn't entirely necessary because we could just simply
touch the timestamp file via extra_scripts to force a rebuild. But we'd
need _some_ kind of script anyway and this way we can format the timestamp
more easily than is possible with C++.
This commit is contained in:
Gunnar Beutner 2023-12-05 12:02:03 +01:00 committed by GitHub
parent 11e3e3dc63
commit e4bbb1d11c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 17 additions and 1 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ CMakeLists.txt
CMakeListsPrivate.txt
platformio_override.ini
node_modules
src/build_timestamp_value.h

View File

@ -61,6 +61,7 @@ board_build.partitions = partitions_singleapp.csv
monitor_speed = 115200
monitor_filters = esp32_exception_decoder, time
upload_speed = 1500000
extra_scripts = update_ts.py
[esp32]
extends = common

6
src/build_timestamp.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "build_timestamp.h"
#include "build_timestamp_value.h"
const char *getBuildTimestamp() {
return BUILD_TIMESTAMP;
}

1
src/build_timestamp.h Normal file
View File

@ -0,0 +1 @@
const char *getBuildTimestamp();

View File

@ -91,7 +91,7 @@ bool sendTelemetry(unsigned int totalSeen, unsigned int totalFpSeen, unsigned in
#ifdef VERSION
doc["ver"] = String(VERSION);
#else
doc["ver"] = ESP.getSketchMD5();
doc["ver"] = ESP.getSketchMD5() + "-" + getBuildTimestamp();
#endif
if (!BleFingerprintCollection::countIds.isEmpty())

View File

@ -27,6 +27,7 @@
#include "globals.h"
#include "mqtt.h"
#include "string_utils.h"
#include "build_timestamp.h"
#ifdef M5STICK
#include <AXP192.h>
#endif

6
update_ts.py Normal file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env python3
from datetime import datetime
with open("src/build_timestamp_value.h", "w") as fp:
timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
fp.write(f'#define BUILD_TIMESTAMP "{timestamp}"')