Fix FW_COMMIT_HASH compiler warning

On my end, the default length for the abbreviated commit hash is 9 characters.
This won't fit into uint32_t (4 bytes).

Instead change FW_COMMIT_HASH into a string
and create preprocessor symbol for the string length
such that it's known at compile time.

If the string should be longer or shorter
then only FW_COMMIT_HASH_LENGTH needs to be configured on the CMake side
This commit is contained in:
Guðni Már Gilbert 2023-10-07 21:01:28 +00:00
parent fab47e63bf
commit 415c0c79d1
5 changed files with 28 additions and 8 deletions

View File

@ -367,7 +367,8 @@ function(add_base_binary variant_name)
target_compile_definitions(
${variant_name}
PRIVATE CMAKE_CONTROL FW_REPOSITORY="${PROJECT_REPOSITORY}"
FW_COMMIT_HASH=0x${FW_COMMIT_HASH}
FW_COMMIT_HASH="${FW_COMMIT_HASH}"
FW_COMMIT_HASH_LENGTH=${FW_COMMIT_HASH_LENGTH}
FW_MAJOR=${PROJECT_VERSION_MAJOR}
FW_MINOR=${PROJECT_VERSION_MINOR}
FW_REVISION=${PROJECT_VERSION_REV}

View File

@ -40,7 +40,8 @@ extern const char _sPrinterMmuName[] PROGMEM;
// The full version string and repository source are set via cmake
#ifndef CMAKE_CONTROL
#define FW_COMMIT_HASH 0
#define FW_COMMIT_HASH_LENGTH 1
#define FW_COMMIT_HASH "0"
#define FW_REPOSITORY "Unknown"
#define FW_VERSION_FULL FW_VERSION "-unknown"
#endif

View File

@ -17,7 +17,13 @@ const uint16_t FW_VERSION_NR[4] PROGMEM = {
FW_TWEAK,
};
const uint32_t FW_VERSION_HASH PROGMEM = FW_COMMIT_HASH;
const char FW_VERSION_HASH[] PROGMEM = FW_COMMIT_HASH;
static_assert(sizeof(FW_VERSION_HASH) == FW_COMMIT_HASH_LENGTH + 1);
const char* FW_VERSION_HASH_P()
{
return FW_VERSION_HASH;
}
const char* FW_VERSION_STR_P()
{

View File

@ -2,10 +2,10 @@
#include <stdint.h>
extern const uint16_t FW_VERSION_NR[4];
extern const char* FW_VERSION_STR_P();
const char* FW_VERSION_STR_P();
extern const uint32_t FW_VERSION_HASH PROGMEM;
static inline uint32_t FW_VERSION_HASH_P() { return (uint32_t)pgm_read_dword(&FW_VERSION_HASH); }
extern const char FW_VERSION_HASH[];
const char* FW_VERSION_HASH_P();
// Definition of a firmware flavor numerical values.
// To keep it short as possible

View File

@ -87,6 +87,12 @@ ENDIF()
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_REV}.${PROJECT_VERSION_TWEAK}")
# Define a constant length for the commit hash
set(FW_COMMIT_HASH_LENGTH 9)
# Create fallback value with constant length
string(REPEAT "0" ${FW_COMMIT_HASH_LENGTH} FW_COMMIT_HASH_UNKNOWN)
function(resolve_version_variables)
if(FW_COMMIT_DSC)
return()
@ -94,11 +100,17 @@ function(resolve_version_variables)
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
git_head_commit_data(FW_COMMIT_HASH "%h")
# Get the full commit hash
git_head_commit_data(FW_COMMIT_HASH "%H")
# Keep only the first 'FW_COMMIT_HASH_LENGTH' characters
string(SUBSTRING "${FW_COMMIT_HASH}" 0 ${FW_COMMIT_HASH_LENGTH} FW_COMMIT_HASH)
set(ERRORS "GIT-NOTFOUND" "HEAD-FORMAT-NOTFOUND")
if(FW_COMMIT_HASH IN_LIST ERRORS)
# git not available, set fallback values
set(FW_COMMIT_HASH "UNKNOWN")
set(FW_COMMIT_HASH ${FW_COMMIT_HASH_UNKNOWN})
set(FW_COMMIT_DSC "v${PROJECT_VERSION}-${FW_COMMIT_HASH}")
string(TIMESTAMP FW_COMMIT_DATE "%s")
else()