How to do it

Saving the Git information to the version.hpp header file at build time will require the following operations:

  1. We will move most of the code from the CMakeLists.txt of the previous recipe to a separate file, and will call the file git-hash.cmake:
# in case Git is not available, we default to "unknown"set(GIT_HASH "unknown")# find Git and if available set GIT_HASH variablefind_package(Git QUIET)if(GIT_FOUND)  execute_process(    COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%h    OUTPUT_VARIABLE GIT_HASH    OUTPUT_STRIP_TRAILING_WHITESPACE    ERROR_QUIET    )endif()message(STATUS "Git hash is ${GIT_HASH}")# generate file version.hpp based on version.hpp.inconfigure_file(  ${CMAKE_CURRENT_LIST_DIR}/version.hpp.in ${TARGET_DIR}/generated/version.hpp ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.