Skip to content

Commit d2fb077

Browse files
pdgendtcarlescufi
authored andcommitted
cmake: Create git module
Create a CMake git module for a git_describe function. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent 02afc35 commit d2fb077

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

cmake/gen_version_h.cmake

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
cmake_minimum_required(VERSION 3.20.0)
44

5+
set(ZEPHYR_BASE $ENV{ZEPHYR_BASE} CACHE PATH "Zephyr base")
6+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ZEPHYR_BASE}/cmake/modules)
7+
8+
include(git)
9+
510
if(VERSION_TYPE STREQUAL KERNEL)
611
set(BUILD_VERSION_NAME BUILD_VERSION)
712
else()
@@ -10,23 +15,7 @@ endif()
1015

1116
if(NOT DEFINED ${BUILD_VERSION_NAME})
1217
cmake_path(GET VERSION_FILE PARENT_PATH work_dir)
13-
find_package(Git QUIET)
14-
if(GIT_FOUND)
15-
execute_process(
16-
COMMAND ${GIT_EXECUTABLE} describe --abbrev=12 --always
17-
WORKING_DIRECTORY ${work_dir}
18-
OUTPUT_VARIABLE ${BUILD_VERSION_NAME}
19-
OUTPUT_STRIP_TRAILING_WHITESPACE
20-
ERROR_STRIP_TRAILING_WHITESPACE
21-
ERROR_VARIABLE stderr
22-
RESULT_VARIABLE return_code
23-
)
24-
if(return_code)
25-
message(STATUS "git describe failed: ${stderr}")
26-
elseif(NOT "${stderr}" STREQUAL "")
27-
message(STATUS "git describe warned: ${stderr}")
28-
endif()
29-
endif()
18+
git_describe(${work_dir} ${BUILD_VERSION_NAME})
3019
endif()
3120

3221
include(${ZEPHYR_BASE}/cmake/modules/version.cmake)

cmake/modules/git.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
include_guard(GLOBAL)
4+
5+
find_package(Git QUIET)
6+
7+
# Usage:
8+
# git_describe(<dir> <output>)
9+
#
10+
# Helper function to get a short GIT desciption associated with a directory.
11+
# OUTPUT is set to the output of `git describe --abbrev=12 --always` as run
12+
# from DIR.
13+
#
14+
function(git_describe DIR OUTPUT)
15+
if(GIT_FOUND)
16+
execute_process(
17+
COMMAND ${GIT_EXECUTABLE} describe --abbrev=12 --always
18+
WORKING_DIRECTORY ${DIR}
19+
OUTPUT_VARIABLE DESCRIPTION
20+
OUTPUT_STRIP_TRAILING_WHITESPACE
21+
ERROR_STRIP_TRAILING_WHITESPACE
22+
ERROR_VARIABLE stderr
23+
RESULT_VARIABLE return_code
24+
)
25+
if(return_code)
26+
message(STATUS "git describe failed: ${stderr}")
27+
elseif(NOT "${stderr}" STREQUAL "")
28+
message(STATUS "git describe warned: ${stderr}")
29+
else()
30+
# Save output
31+
set(${OUTPUT} ${DESCRIPTION} PARENT_SCOPE)
32+
endif()
33+
endif()
34+
endfunction()

0 commit comments

Comments
 (0)