From 97800434c4a7ad42fbca3915292ff3ab841a10c5 Mon Sep 17 00:00:00 2001 From: Stefan Oberhumer Date: Thu, 18 Apr 2024 09:02:57 +0200 Subject: [PATCH] Prevent compiling the whole project on each commit. Putting the git information into a generated sourcefile prevents recompiling the whole project after each commit. --- include/__compiled_constants.h | 8 ++++ pio-scripts/auto_firmware_version.py | 60 +++++++++++++++++++++++++--- src/MqttHandleHass.cpp | 5 ++- src/NetworkSettings.cpp | 3 +- src/WebApi_prometheus.cpp | 3 +- src/WebApi_sysstatus.cpp | 7 +--- 6 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 include/__compiled_constants.h diff --git a/include/__compiled_constants.h b/include/__compiled_constants.h new file mode 100644 index 0000000..ac8991e --- /dev/null +++ b/include/__compiled_constants.h @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +// The referenced values are generated by pio-scripts/auto_firmware_version.py + + +extern const char *__COMPILED_GIT_HASH__; +// extern const char *__COMPILED_DATE_TIME_UTC_STR__; diff --git a/pio-scripts/auto_firmware_version.py b/pio-scripts/auto_firmware_version.py index 2f15cc9..f169f61 100644 --- a/pio-scripts/auto_firmware_version.py +++ b/pio-scripts/auto_firmware_version.py @@ -2,6 +2,7 @@ # # Copyright (C) 2022 Thomas Basler and others # +import os import pkg_resources Import("env") @@ -15,15 +16,64 @@ if missing_pkgs: from dulwich import porcelain -def get_firmware_specifier_build_flag(): + +def updateFileIfChanged(filename, content): + mustUpdate = True + try: + fp = open(filename, "rb") + if fp.read() == content: + mustUpdate = False + fp.close() + except: + pass + if mustUpdate: + fp = open(filename, "wb") + fp.write(content) + fp.close() + return mustUpdate + + +def get_build_version(): try: build_version = porcelain.describe('.') # '.' refers to the repository root dir except: build_version = "g0000000" - build_flag = "-D AUTO_GIT_HASH=\\\"" + build_version + "\\\"" print ("Firmware Revision: " + build_version) + return build_version + + +def get_firmware_specifier_build_flag(): + build_version = get_build_version() + build_flag = "-D AUTO_GIT_HASH=\\\"" + build_version + "\\\"" return (build_flag) -env.Append( - BUILD_FLAGS=[get_firmware_specifier_build_flag()] -) \ No newline at end of file + +def do_main(): + if 0: + # this results in a full recompilation of the whole project after each commit + env.Append( + BUILD_FLAGS=[get_firmware_specifier_build_flag()] + ) + else: + # we just create a .c file containing the needed datas + targetfile = os.path.join(env.subst("$BUILD_DIR"), "__compiled_constants.c") + lines = "" + lines += "/* Generated file within build process - Do NOT edit */\n" + + if 0: + # Add the current date and time as string in UTC timezone + from datetime import datetime, timezone + now = datetime.now(tz=timezone.utc) + COMPILED_DATE_TIME_UTC_STR = now.strftime("%Y/%m/%d %H:%M:%S") + lines += 'const char *__COMPILED_DATE_TIME_UTC_STR__ = "%s";\n' % (COMPILED_DATE_TIME_UTC_STR) + + if 1: + # Add the description of the current git revision + lines += 'const char *__COMPILED_GIT_HASH__ = "%s";\n' % (get_build_version()) + + updateFileIfChanged(targetfile, bytes(lines, "utf-8")) + + # Add the created file to the buildfiles - platformio knows how to handle *.c files + env.AppendUnique(PIOBUILDFILES=[targetfile]) + +do_main() diff --git a/src/MqttHandleHass.cpp b/src/MqttHandleHass.cpp index a2d998d..b286eaa 100644 --- a/src/MqttHandleHass.cpp +++ b/src/MqttHandleHass.cpp @@ -8,6 +8,7 @@ #include "NetworkSettings.h" #include "Utils.h" #include "defaults.h" +#include "__compiled_constants.h" MqttHandleHassClass MqttHandleHass; @@ -380,7 +381,7 @@ void MqttHandleHassClass::createInverterInfo(JsonDocument& root, std::shared_ptr getDtuUrl(), "OpenDTU", inv->typeName(), - AUTO_GIT_HASH, + __COMPILED_GIT_HASH__, getDtuUniqueId()); } @@ -393,7 +394,7 @@ void MqttHandleHassClass::createDtuInfo(JsonDocument& root) getDtuUrl(), "OpenDTU", "OpenDTU", - AUTO_GIT_HASH); + __COMPILED_GIT_HASH__); } void MqttHandleHassClass::createDeviceInfo( diff --git a/src/NetworkSettings.cpp b/src/NetworkSettings.cpp index 59ce8f7..dac3ecb 100644 --- a/src/NetworkSettings.cpp +++ b/src/NetworkSettings.cpp @@ -10,6 +10,7 @@ #include "defaults.h" #include #include +#include "__compiled_constants.h" NetworkSettingsClass::NetworkSettingsClass() : _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&NetworkSettingsClass::loop, this)) @@ -136,7 +137,7 @@ void NetworkSettingsClass::handleMDNS() MDNS.addService("http", "tcp", 80); MDNS.addService("opendtu", "tcp", 80); - MDNS.addServiceTxt("opendtu", "tcp", "git_hash", AUTO_GIT_HASH); + MDNS.addServiceTxt("opendtu", "tcp", "git_hash", __COMPILED_GIT_HASH__); MessageOutput.println("done"); } else { diff --git a/src/WebApi_prometheus.cpp b/src/WebApi_prometheus.cpp index 9b3039c..f0a0d7b 100644 --- a/src/WebApi_prometheus.cpp +++ b/src/WebApi_prometheus.cpp @@ -9,6 +9,7 @@ #include "NetworkSettings.h" #include "WebApi.h" #include +#include "__compiled_constants.h" void WebApiPrometheusClass::init(AsyncWebServer& server, Scheduler& scheduler) { @@ -29,7 +30,7 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques stream->print("# HELP opendtu_build Build info\n"); stream->print("# TYPE opendtu_build gauge\n"); stream->printf("opendtu_build{name=\"%s\",id=\"%s\",version=\"%d.%d.%d\"} 1\n", - NetworkSettings.getHostname().c_str(), AUTO_GIT_HASH, CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff); + NetworkSettings.getHostname().c_str(), __COMPILED_GIT_HASH__, CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff); stream->print("# HELP opendtu_platform Platform info\n"); stream->print("# TYPE opendtu_platform gauge\n"); diff --git a/src/WebApi_sysstatus.cpp b/src/WebApi_sysstatus.cpp index a2893c8..9b317f9 100644 --- a/src/WebApi_sysstatus.cpp +++ b/src/WebApi_sysstatus.cpp @@ -11,10 +11,7 @@ #include #include #include - -#ifndef AUTO_GIT_HASH -#define AUTO_GIT_HASH "" -#endif +#include "__compiled_constants.h" void WebApiSysstatusClass::init(AsyncWebServer& server, Scheduler& scheduler) { @@ -64,7 +61,7 @@ void WebApiSysstatusClass::onSystemStatus(AsyncWebServerRequest* request) char version[16]; snprintf(version, sizeof(version), "%d.%d.%d", CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff); root["config_version"] = version; - root["git_hash"] = AUTO_GIT_HASH; + root["git_hash"] = __COMPILED_GIT_HASH__; root["pioenv"] = PIOENV; root["uptime"] = esp_timer_get_time() / 1000000;