Skip to content

Commit cd0fd04

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 <[email protected]>
1 parent 314381c commit cd0fd04

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

app/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,28 @@ 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+
)
28+
# If there is an error e.g. it is not a git repo, we just silently ignore it and continue
29+
# without a dependency, this will be the case with freestanding applications.
30+
if(NOT return_code)
31+
if(NOT "${stderr}" STREQUAL "")
32+
message(WARNING "Application build version git rev-parse warned: ${stderr}")
33+
endif()
34+
35+
set_property(TARGET app_version_h PROPERTY APP_VERSION_DEPENDS ${application_git_dir}/index)
36+
endif()
37+
endif()

0 commit comments

Comments
 (0)