From e4bbb1d11c1daca72be740ea6b2b8d6fb5392350 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 5 Dec 2023 12:02:03 +0100 Subject: [PATCH] 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++. --- .gitignore | 1 + platformio.ini | 1 + src/build_timestamp.cpp | 6 ++++++ src/build_timestamp.h | 1 + src/main.cpp | 2 +- src/main.h | 1 + update_ts.py | 6 ++++++ 7 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/build_timestamp.cpp create mode 100644 src/build_timestamp.h create mode 100644 update_ts.py diff --git a/.gitignore b/.gitignore index b7a08be..febe05b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ CMakeLists.txt CMakeListsPrivate.txt platformio_override.ini node_modules +src/build_timestamp_value.h diff --git a/platformio.ini b/platformio.ini index 2fd2a2e..b2990e2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/build_timestamp.cpp b/src/build_timestamp.cpp new file mode 100644 index 0000000..0ce9be8 --- /dev/null +++ b/src/build_timestamp.cpp @@ -0,0 +1,6 @@ +#include "build_timestamp.h" +#include "build_timestamp_value.h" + +const char *getBuildTimestamp() { + return BUILD_TIMESTAMP; +} diff --git a/src/build_timestamp.h b/src/build_timestamp.h new file mode 100644 index 0000000..16ebb3f --- /dev/null +++ b/src/build_timestamp.h @@ -0,0 +1 @@ +const char *getBuildTimestamp(); diff --git a/src/main.cpp b/src/main.cpp index 3436700..e3c9c4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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()) diff --git a/src/main.h b/src/main.h index 90a4f9d..fa00c10 100644 --- a/src/main.h +++ b/src/main.h @@ -27,6 +27,7 @@ #include "globals.h" #include "mqtt.h" #include "string_utils.h" +#include "build_timestamp.h" #ifdef M5STICK #include #endif diff --git a/update_ts.py b/update_ts.py new file mode 100644 index 0000000..caf45e3 --- /dev/null +++ b/update_ts.py @@ -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}"')