From 211d793b6f2010f6a6fc2b73e1626c0f0f92cdd9 Mon Sep 17 00:00:00 2001 From: Jamie McCrae Date: Fri, 31 Oct 2025 09:23:52 +0000 Subject: [PATCH] app: Add example of VERSION file tracking git index file Adds an example of how to add the git index file (if this is inside of a git repo) to the dependencies of the application version file, which allows the git commit of the application version to be updated as new commits are added Signed-off-by: Jamie McCrae --- app/CMakeLists.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index a59bf06b5..dc16ef9dd 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -10,3 +10,27 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(app LANGUAGES C) target_sources(app PRIVATE src/main.c) + +# The code below locates the git index file for this repository and adds it as a dependency for +# the application VERSION file so that if the repo has a new commit added, even if no files in +# the build have changed, the application version file will be regenerated with the new git commit +find_package(Git QUIET) +if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --absolute-git-dir + WORKING_DIRECTORY . + OUTPUT_VARIABLE application_git_dir + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_STRIP_TRAILING_WHITESPACE + ERROR_VARIABLE stderr + RESULT_VARIABLE return_code) + # If there is an error e.g. it is not a git repo, we just silently ignore it and continue + # without a dependency, this will be the case with freestanding applications. + if(NOT return_code) + if(NOT "${stderr}" STREQUAL "") + message(WARNING "APPLICATION_BUILD_VERSION: git rev-parse warned: ${stderr}") + endif() + + set_property(TARGET app_version_h PROPERTY APP_VERSION_DEPENDS ${application_git_dir}/index) + endif() +endif()