From d2b34abea4754265fb89a60acbb2586fd15203f7 Mon Sep 17 00:00:00 2001 From: Manuel Huber Date: Fri, 20 Dec 2024 15:39:15 +0100 Subject: [PATCH] cmake: get version from git, or git archive --- .gitattributes | 1 + .github/workflows/main.yml | 8 +++++++- CMakeLists.txt | 5 ++++- cmake/RMGGitVersion.cmake | 24 ++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 cmake/RMGGitVersion.cmake diff --git a/.gitattributes b/.gitattributes index 58e8cc74..7ed0f901 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ docs/rmg-commands.md linguist-generated=true +cmake/RMGGitVersion.cmake export-subst diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b7bcc43c..617e0f87 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,10 +28,16 @@ jobs: - G4.11.2 - slim - container: docker://gipert/remage-base:${{ matrix.container_version }} + container: + image: docker://gipert/remage-base:${{ matrix.container_version }} + # make git work inside container. reference: + # https://github.com/actions/runner/issues/2033#issuecomment-1598547465 + options: "--user 1001" steps: - uses: actions/checkout@v4 + with: + fetch-tags: true - name: Build project run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index e28c0b06..4ad3135e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,10 @@ cmake_minimum_required(VERSION 3.12 FATAL_ERROR) + +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/RMGGitVersion.cmake) + project( remage - VERSION 0.5.0 + VERSION ${RMG_GIT_VERSION} DESCRIPTION "Simulation framework for HPGe-based experiments" LANGUAGES C CXX) # C is needed for Geant4's HDF5 support diff --git a/cmake/RMGGitVersion.cmake b/cmake/RMGGitVersion.cmake new file mode 100644 index 00000000..c3a93d6a --- /dev/null +++ b/cmake/RMGGitVersion.cmake @@ -0,0 +1,24 @@ +# this is a special value replaced by git archive. +set(RMG_GIT_VERSION "$Format:%(describe:tags=1)$") + +# try to get the tag ourselves from git describe, if we did not have it set above +# (i.e. we are not in a git-created archive). +if(RMG_GIT_VERSION MATCHES "Format:") + set(RMG_GIT_VERSION "v0.0.0") + find_package(Git) + if(Git_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --match "v*" + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_OUTPUT + RESULT_VARIABLE GIT_STATUS + OUTPUT_STRIP_TRAILING_WHITESPACE) + if(GIT_STATUS EQUAL 0) + set(RMG_GIT_VERSION ${GIT_OUTPUT}) + endif() + endif() +endif() + +# cleanup git describe output to match cmake's expectations. +string(REGEX REPLACE "^v" "" RMG_GIT_VERSION ${RMG_GIT_VERSION}) +string(REGEX REPLACE "-.*$" "" RMG_GIT_VERSION ${RMG_GIT_VERSION})