cmake: Create git module

Create a CMake git module for a git_describe function.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2024-09-06 14:05:08 +02:00 committed by Carles Cufí
parent 02afc352e3
commit d2fb07785d
2 changed files with 40 additions and 17 deletions

View File

@ -2,6 +2,11 @@
cmake_minimum_required(VERSION 3.20.0)
set(ZEPHYR_BASE $ENV{ZEPHYR_BASE} CACHE PATH "Zephyr base")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ZEPHYR_BASE}/cmake/modules)
include(git)
if(VERSION_TYPE STREQUAL KERNEL)
set(BUILD_VERSION_NAME BUILD_VERSION)
else()
@ -10,23 +15,7 @@ endif()
if(NOT DEFINED ${BUILD_VERSION_NAME})
cmake_path(GET VERSION_FILE PARENT_PATH work_dir)
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --abbrev=12 --always
WORKING_DIRECTORY ${work_dir}
OUTPUT_VARIABLE ${BUILD_VERSION_NAME}
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE stderr
RESULT_VARIABLE return_code
)
if(return_code)
message(STATUS "git describe failed: ${stderr}")
elseif(NOT "${stderr}" STREQUAL "")
message(STATUS "git describe warned: ${stderr}")
endif()
endif()
git_describe(${work_dir} ${BUILD_VERSION_NAME})
endif()
include(${ZEPHYR_BASE}/cmake/modules/version.cmake)

34
cmake/modules/git.cmake Normal file
View File

@ -0,0 +1,34 @@
# SPDX-License-Identifier: Apache-2.0
include_guard(GLOBAL)
find_package(Git QUIET)
# Usage:
# git_describe(<dir> <output>)
#
# Helper function to get a short GIT desciption associated with a directory.
# OUTPUT is set to the output of `git describe --abbrev=12 --always` as run
# from DIR.
#
function(git_describe DIR OUTPUT)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --abbrev=12 --always
WORKING_DIRECTORY ${DIR}
OUTPUT_VARIABLE DESCRIPTION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE stderr
RESULT_VARIABLE return_code
)
if(return_code)
message(STATUS "git describe failed: ${stderr}")
elseif(NOT "${stderr}" STREQUAL "")
message(STATUS "git describe warned: ${stderr}")
else()
# Save output
set(${OUTPUT} ${DESCRIPTION} PARENT_SCOPE)
endif()
endif()
endfunction()