diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index cfe1e8cfb..4e223d8f7 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -68,6 +68,7 @@ jobs: OPTIX_VERSION=${{ matrix.optix_version }} GEANT4_VERSION=${{ matrix.geant4_version }} CMAKE_VERSION=${{ matrix.cmake_version }} + PROJECT_VERSION=${{ github.ref_name }} cache-from: type=local,src=/home/runner/.buildx-cache cache-to: type=local,dest=/home/runner/.buildx-cache-new,mode=max diff --git a/CMakeLists.txt b/CMakeLists.txt index ca8269b51..d7b3a9893 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,15 @@ if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type") endif() -project(eic-opticks VERSION 0.1.0 LANGUAGES CXX CUDA) +project(eic-opticks VERSION 0.3.0 LANGUAGES CXX CUDA) + +include(GitVersion) + +# Allow CI/Docker to override the derived version string via -DPROJECT_VERSION_OVERRIDE=... +if(DEFINED PROJECT_VERSION_OVERRIDE AND NOT "${PROJECT_VERSION_OVERRIDE}" STREQUAL "") + set(PROJECT_VERSION_STRING "${PROJECT_VERSION_OVERRIDE}") + message(STATUS "Version overridden to: ${PROJECT_VERSION_STRING}") +endif() set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/Dockerfile b/Dockerfile index afb8a4993..7931c004e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -81,17 +81,23 @@ RUN uv sync FROM base AS release +ARG PROJECT_VERSION + COPY . $OPTICKS_HOME RUN cmake -S $OPTICKS_HOME -B $OPTICKS_BUILD -DCMAKE_INSTALL_PREFIX=$OPTICKS_PREFIX -DCMAKE_BUILD_TYPE=Release \ + ${PROJECT_VERSION:+-DPROJECT_VERSION_OVERRIDE=$PROJECT_VERSION} \ && cmake --build $OPTICKS_BUILD --parallel --target install FROM base AS develop +ARG PROJECT_VERSION + RUN apt update && apt install -y x11-apps mesa-utils vim COPY . $OPTICKS_HOME RUN cmake -S $OPTICKS_HOME -B $OPTICKS_BUILD -DCMAKE_INSTALL_PREFIX=$OPTICKS_PREFIX -DCMAKE_BUILD_TYPE=Debug \ + ${PROJECT_VERSION:+-DPROJECT_VERSION_OVERRIDE=$PROJECT_VERSION} \ && cmake --build $OPTICKS_BUILD --parallel --target install diff --git a/cmake/GitVersion.cmake b/cmake/GitVersion.cmake new file mode 100644 index 000000000..ec24a14dd --- /dev/null +++ b/cmake/GitVersion.cmake @@ -0,0 +1,45 @@ +# GitVersion.cmake +# +# Derives PROJECT_VERSION_STRING and PROJECT_GIT_REVISION from git when available. +# Falls back to PROJECT_VERSION when git is not present (e.g. Docker builds). +# +# Sets in the caller's scope: +# PROJECT_VERSION_STRING - Version string: exact tag ("0.3.0") or +# tag+distance+sha ("0.3.0-2-gabcdef-dirty"). +# Falls back to PROJECT_VERSION. +# PROJECT_GIT_REVISION - Short commit hash, or empty string if git is unavailable. + +find_package(Git QUIET) + +if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --match "[0-9]*" --dirty + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE _git_describe + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + RESULT_VARIABLE _git_result + ) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE PROJECT_GIT_REVISION + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + RESULT_VARIABLE _git_rev_result + ) + if(_git_rev_result) + set(PROJECT_GIT_REVISION "") + endif() +else() + set(_git_result 1) + set(PROJECT_GIT_REVISION "") +endif() + +if(_git_result EQUAL 0 AND _git_describe) + set(PROJECT_VERSION_STRING "${_git_describe}") + message(STATUS "Version from git: ${PROJECT_VERSION_STRING}") +else() + set(PROJECT_VERSION_STRING "${PROJECT_VERSION}") + message(STATUS "Version from project(): ${PROJECT_VERSION_STRING} (git not available)") +endif() diff --git a/pyproject.toml b/pyproject.toml index 7ef5bc28e..c45694a17 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "optiphy" -version = "0.0.0" +version = "0.3.0" description = "" authors = [ { name = "Dmitri Smirnov", email = "dmixsmi@gmail.com" } ] requires-python = ">=3.10" diff --git a/src/config.cpp b/src/config.cpp index 844244017..7b2651ba2 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -18,6 +18,16 @@ namespace gphox { using namespace std; +const char *Config::Version() +{ + return PROJECT_VERSION; +} + +const char *Config::GitRevision() +{ + return PROJECT_GIT_REVISION; +} + constexpr const char *GPHOX_PTX_PATH_ENV = "CSGOptiX__optixpath"; bool FileExists(const std::string &path) diff --git a/src/config.h b/src/config.h index 1fc5c838d..4ee5ad4fd 100644 --- a/src/config.h +++ b/src/config.h @@ -21,6 +21,12 @@ class Config static std::string PtxPath(const std::string &ptx_name = "CSGOptiX7.ptx"); + /// Returns the project version (e.g. "0.3.0" or "0.3.0-2-gabcdef-dirty") + static const char *Version(); + + /// Returns the short git commit hash, or empty string if unavailable + static const char *GitRevision(); + /// A unique name associated with this Config std::string name; diff --git a/src/config_path.h.in b/src/config_path.h.in index 19bda3687..054c9f9c6 100644 --- a/src/config_path.h.in +++ b/src/config_path.h.in @@ -2,3 +2,9 @@ #define GPHOX_CONFIG_SEARCH_PATHS ".:config:@GPHOX_INSTALL_FULL_DATADIR@/config" #define GPHOX_PTX_DIR "@CMAKE_INSTALL_FULL_LIBDIR@" + +#define PROJECT_VERSION "@PROJECT_VERSION_STRING@" +#define PROJECT_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ +#define PROJECT_VERSION_MINOR @PROJECT_VERSION_MINOR@ +#define PROJECT_VERSION_PATCH @PROJECT_VERSION_PATCH@ +#define PROJECT_GIT_REVISION "@PROJECT_GIT_REVISION@"