1# SPDX-License-Identifier: Apache-2.0
2
3include_guard(GLOBAL)
4
5find_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#
14function(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()
34endfunction()
35