Prevent compiling the whole project on each commit.

Putting the git information into a generated sourcefile prevents
recompiling the whole project after each commit.
This commit is contained in:
Stefan Oberhumer 2024-04-18 09:02:57 +02:00
parent d0981934b0
commit 97800434c4
6 changed files with 72 additions and 14 deletions

View File

@ -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__;

View File

@ -2,6 +2,7 @@
# #
# Copyright (C) 2022 Thomas Basler and others # Copyright (C) 2022 Thomas Basler and others
# #
import os
import pkg_resources import pkg_resources
Import("env") Import("env")
@ -15,15 +16,64 @@ if missing_pkgs:
from dulwich import porcelain 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: try:
build_version = porcelain.describe('.') # '.' refers to the repository root dir build_version = porcelain.describe('.') # '.' refers to the repository root dir
except: except:
build_version = "g0000000" build_version = "g0000000"
build_flag = "-D AUTO_GIT_HASH=\\\"" + build_version + "\\\""
print ("Firmware Revision: " + 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) return (build_flag)
env.Append(
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()] 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()

View File

@ -8,6 +8,7 @@
#include "NetworkSettings.h" #include "NetworkSettings.h"
#include "Utils.h" #include "Utils.h"
#include "defaults.h" #include "defaults.h"
#include "__compiled_constants.h"
MqttHandleHassClass MqttHandleHass; MqttHandleHassClass MqttHandleHass;
@ -380,7 +381,7 @@ void MqttHandleHassClass::createInverterInfo(JsonDocument& root, std::shared_ptr
getDtuUrl(), getDtuUrl(),
"OpenDTU", "OpenDTU",
inv->typeName(), inv->typeName(),
AUTO_GIT_HASH, __COMPILED_GIT_HASH__,
getDtuUniqueId()); getDtuUniqueId());
} }
@ -393,7 +394,7 @@ void MqttHandleHassClass::createDtuInfo(JsonDocument& root)
getDtuUrl(), getDtuUrl(),
"OpenDTU", "OpenDTU",
"OpenDTU", "OpenDTU",
AUTO_GIT_HASH); __COMPILED_GIT_HASH__);
} }
void MqttHandleHassClass::createDeviceInfo( void MqttHandleHassClass::createDeviceInfo(

View File

@ -10,6 +10,7 @@
#include "defaults.h" #include "defaults.h"
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include <ETH.h> #include <ETH.h>
#include "__compiled_constants.h"
NetworkSettingsClass::NetworkSettingsClass() NetworkSettingsClass::NetworkSettingsClass()
: _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&NetworkSettingsClass::loop, this)) : _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&NetworkSettingsClass::loop, this))
@ -136,7 +137,7 @@ void NetworkSettingsClass::handleMDNS()
MDNS.addService("http", "tcp", 80); MDNS.addService("http", "tcp", 80);
MDNS.addService("opendtu", "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"); MessageOutput.println("done");
} else { } else {

View File

@ -9,6 +9,7 @@
#include "NetworkSettings.h" #include "NetworkSettings.h"
#include "WebApi.h" #include "WebApi.h"
#include <Hoymiles.h> #include <Hoymiles.h>
#include "__compiled_constants.h"
void WebApiPrometheusClass::init(AsyncWebServer& server, Scheduler& scheduler) 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("# HELP opendtu_build Build info\n");
stream->print("# TYPE opendtu_build gauge\n"); stream->print("# TYPE opendtu_build gauge\n");
stream->printf("opendtu_build{name=\"%s\",id=\"%s\",version=\"%d.%d.%d\"} 1\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("# HELP opendtu_platform Platform info\n");
stream->print("# TYPE opendtu_platform gauge\n"); stream->print("# TYPE opendtu_platform gauge\n");

View File

@ -11,10 +11,7 @@
#include <Hoymiles.h> #include <Hoymiles.h>
#include <LittleFS.h> #include <LittleFS.h>
#include <ResetReason.h> #include <ResetReason.h>
#include "__compiled_constants.h"
#ifndef AUTO_GIT_HASH
#define AUTO_GIT_HASH ""
#endif
void WebApiSysstatusClass::init(AsyncWebServer& server, Scheduler& scheduler) void WebApiSysstatusClass::init(AsyncWebServer& server, Scheduler& scheduler)
{ {
@ -64,7 +61,7 @@ void WebApiSysstatusClass::onSystemStatus(AsyncWebServerRequest* request)
char version[16]; char version[16];
snprintf(version, sizeof(version), "%d.%d.%d", CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff); snprintf(version, sizeof(version), "%d.%d.%d", CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff);
root["config_version"] = version; root["config_version"] = version;
root["git_hash"] = AUTO_GIT_HASH; root["git_hash"] = __COMPILED_GIT_HASH__;
root["pioenv"] = PIOENV; root["pioenv"] = PIOENV;
root["uptime"] = esp_timer_get_time() / 1000000; root["uptime"] = esp_timer_get_time() / 1000000;