Skip to content

Commit 211d793

Browse files
committed
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 <jamie.mccrae@nordicsemi.no>
1 parent 6fdf630 commit 211d793

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

app/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,27 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
1010
project(app LANGUAGES C)
1111

1212
target_sources(app PRIVATE src/main.c)
13+
14+
# The code below locates the git index file for this repository and adds it as a dependency for
15+
# the application VERSION file so that if the repo has a new commit added, even if no files in
16+
# the build have changed, the application version file will be regenerated with the new git commit
17+
find_package(Git QUIET)
18+
if(GIT_FOUND)
19+
execute_process(
20+
COMMAND ${GIT_EXECUTABLE} rev-parse --absolute-git-dir
21+
WORKING_DIRECTORY .
22+
OUTPUT_VARIABLE application_git_dir
23+
OUTPUT_STRIP_TRAILING_WHITESPACE
24+
ERROR_STRIP_TRAILING_WHITESPACE
25+
ERROR_VARIABLE stderr
26+
RESULT_VARIABLE return_code)
27+
# If there is an error e.g. it is not a git repo, we just silently ignore it and continue
28+
# without a dependency, this will be the case with freestanding applications.
29+
if(NOT return_code)
30+
if(NOT "${stderr}" STREQUAL "")
31+
message(WARNING "APPLICATION_BUILD_VERSION: git rev-parse warned: ${stderr}")
32+
endif()
33+
34+
set_property(TARGET app_version_h PROPERTY APP_VERSION_DEPENDS ${application_git_dir}/index)
35+
endif()
36+
endif()

0 commit comments

Comments
 (0)