generate version

This commit is contained in:
2024-04-29 04:04:55 +02:00
parent 3164b6d8d9
commit 623ec1576a
4 changed files with 85 additions and 1 deletions

27
Revision/CMakeLists.txt Normal file
View File

@@ -0,0 +1,27 @@
# Add your source files
set(SOURCES
)
# Add your header files
set(HEADERS
version_info.h
)
# Generate the version_info.c file as a build step
add_custom_command(
OUTPUT version_info.c
COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/generate_version_info.py
DEPENDS generate_version_info.py
COMMENT "Generating version_info.c"
)
# Add the generated file to the list of sources
list(APPEND SOURCES ${CMAKE_CURRENT_BINARY_DIR}/version_info.c)
add_custom_target(generate_version_info DEPENDS version_info.c)
# Add the target
add_library(Revision STATIC ${SOURCES} ${HEADERS})
add_dependencies(Revision generate_version_info)
target_include_directories(Revision PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -0,0 +1,42 @@
import subprocess
def get_git_info():
try:
# Get the latest tag
latest_tag = subprocess.check_output(['git', 'describe', '--abbrev=0', '--tags']).decode().strip()
# Get the number of commits since the latest tag
commit_count = subprocess.check_output(['git', 'rev-list', f'{latest_tag}..HEAD', '--count']).decode().strip()
# Get the current branch
branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
# Get the short hash of the latest commit
commit_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode().strip()
# Parse the version string
version_parts = latest_tag.split('.')
major, minor, patch = map(int, version_parts)
return major, minor, patch, int(commit_count), branch, commit_hash
except Exception as e:
print(f"Error: {e}")
return None, None, None, None, None, None
def generate_c_file(filename, version_info):
version_info_string = f"""\
// This file is auto-generated by a Python script
// Do not modify manually
#include "version_info.h"
__attribute__((section(".version"))) const firmware_version_t VERSION_INFO = {{ {version_info[0]}, {version_info[1]}, {version_info[2]}, {version_info[3]}, "{version_info[4]}", "{version_info[5]}" }};
"""
with open(filename, 'w') as f:
f.write(version_info_string)
if __name__ == "__main__":
version_info = get_git_info()
if version_info:
generate_c_file("version_info.c", version_info)
print("Version information has been written to version_info.c")
else:
print("Failed to retrieve Git information.")

15
Revision/version_info.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include <stdint.h>
typedef struct firmware_version {
uint8_t major;
uint8_t minor;
uint8_t patch;
uint8_t count;
char branch[50];
char commit_hash[8];
} firmware_version_t;
extern const firmware_version_t VERSION_INFO;