# SPDX-License-Identifier: MIT # Run at build time (cmake -P): writes ${OUTPUT} with the release version (from project()) and the # build description from `git describe`, rewriting the file only when it changes. # # Inputs: SOURCE_DIR, OUTPUT, VERSION (x.y.z), GIT_EXECUTABLE (may be empty) # # Description examples: # 0.2.0 built exactly from tag v0.2.0, clean tree # 0.2.0+3.g1a2b3c4 3 commits after v0.2.0 # 0.2.0+3.g1a2b3c4.dirty ... with uncommitted changes # 0.2.0 no git (release tarball, HaikuPorts recipe) set(description "${VERSION}") if(GIT_EXECUTABLE AND EXISTS "${SOURCE_DIR}/.git") execute_process( COMMAND "${GIT_EXECUTABLE}" describe --tags --long --dirty --match "v[0-9]*" WORKING_DIRECTORY "${SOURCE_DIR}" OUTPUT_VARIABLE describe RESULT_VARIABLE rc OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) # v0.2.0-3-g1a2b3c4[-dirty] if(rc EQUAL 0 AND describe MATCHES "^v([0-9]+\\.[0-9]+\\.[0-9]+)-([0-9]+)-(g[0-9a-f]+)(-dirty)?$") set(tag_version "${CMAKE_MATCH_1}") set(distance "${CMAKE_MATCH_2}") set(hash "${CMAKE_MATCH_3}") set(dirty "${CMAKE_MATCH_4}") if(NOT tag_version VERSION_EQUAL VERSION AND distance EQUAL 0) message(WARNING "tag v${tag_version} does not match project(VERSION ${VERSION})") endif() if(NOT distance EQUAL 0) string(APPEND description "+${distance}.${hash}") endif() if(dirty) if(distance EQUAL 0) string(APPEND description "+${hash}") endif() string(APPEND description ".dirty") endif() else() # No tag yet: use the commit hash only. execute_process( COMMAND "${GIT_EXECUTABLE}" rev-parse --short HEAD WORKING_DIRECTORY "${SOURCE_DIR}" OUTPUT_VARIABLE hash RESULT_VARIABLE rc OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) if(rc EQUAL 0) string(APPEND description "+g${hash}") endif() endif() endif() set(content "// Generated by cmake/GitVersion.cmake — do not edit. #include \"openpu/version.hpp\" namespace openpu { const char* version() noexcept { return \"${VERSION}\"; } const char* versionDescription() noexcept { return \"${description}\"; } } // namespace openpu ") if(EXISTS "${OUTPUT}") file(READ "${OUTPUT}" previous) endif() if(NOT previous STREQUAL content) file(WRITE "${OUTPUT}" "${content}") endif()