From c12fbf72c938321d56f8a15d18faa82cf5660e0c Mon Sep 17 00:00:00 2001 From: Wouter Deconinck Date: Tue, 15 Aug 2023 08:41:36 -0500 Subject: [PATCH] feat: add version header (#40) ### Briefly, what does this PR introduce? This adds a version header to the installation of EDM4eic. This allows us to make downstream code behave differently based on the version. Note: with `.gitattributes` (in particular `export-subst`) we could automate the filling of this version information with the most recent tag and such. Maybe something to look into. --------- Co-authored-by: Dmitry Kalinkin --- CMakeLists.txt | 17 ++++++++- EDM4eicVersion.h.in | 91 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 EDM4eicVersion.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e75298..28bc4d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,15 @@ # Copyright (C) 2022 Whitney Armstrong, Sylvester Joosten, Wouter Deconinck cmake_minimum_required(VERSION 3.12) + project(EDM4EIC - VERSION 1.1.0 LANGUAGES CXX) +SET( ${PROJECT_NAME}_VERSION_MAJOR 2 ) +SET( ${PROJECT_NAME}_VERSION_MINOR 1 ) +SET( ${PROJECT_NAME}_VERSION_PATCH 0 ) +SET( ${PROJECT_NAME}_VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}" ) + # C++ standard set(CMAKE_CXX_STANDARD 17 CACHE STRING "Set the C++ standard to be used") if(NOT CMAKE_CXX_STANDARD MATCHES "17|20") @@ -97,6 +102,14 @@ else() message(STATUS "Doxygen not found; no documentation will be built.") endif() +# ------------------------- +# add version files + +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/EDM4eicVersion.h.in + ${CMAKE_CURRENT_BINARY_DIR}/EDM4eicVersion.h ) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/EDM4eicVersion.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/edm4eic ) + # ------------------------- # install library config include(CMakePackageConfigHelpers) @@ -119,7 +132,7 @@ configure_package_config_file( write_basic_package_version_file( ${PROJECT_NAME}ConfigVersion.cmake - VERSION ${VERSION} + VERSION ${${PROJECT_NAME}_VERSION} COMPATIBILITY SameMajorVersion ) diff --git a/EDM4eicVersion.h.in b/EDM4eicVersion.h.in new file mode 100644 index 0000000..afab469 --- /dev/null +++ b/EDM4eicVersion.h.in @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: Apache-2.0 +// Ref: https://github.com/key4hep/EDM4hep/blob/e0762272e0f718df2811fc1bf4590af976bac70d/EDM4hepVersion.h.in + +#ifndef EDM4EIC_VERSION_H +#define EDM4EIC_VERSION_H + +#include +#include +#include +#if __cplusplus >= 202002L +#include +#endif + +// Some preprocessor constants and macros for the use cases where they might be +// necessary + +/// Define a version to be used in edm4eic. +#define EDM4EIC_VERSION(major, minor, patch) ((UINT64_C(major) << 32) | (UINT64_C(minor) << 16) | (UINT64_C(patch))) +/// Get the major version from a preprocessor defined version +#define EDM4EIC_MAJOR_VERSION(v) (((v) & (-1UL >> 16)) >> 32) +/// Get the minor version from a preprocessor defined version +#define EDM4EIC_MINOR_VERSION(v) (((v) & (-1UL >> 32)) >> 16) +/// Get the patch version from a preprocessor defined version +#define EDM4EIC_PATCH_VERSION(v) ((v) & (-1UL >> 48)) + +// Some helper constants that are populated by the cmake configure step +#define EDM4EIC_VERSION_MAJOR @EDM4EIC_VERSION_MAJOR@ +#define EDM4EIC_VERSION_MINOR @EDM4EIC_VERSION_MINOR@ +#define EDM4EIC_VERSION_PATCH @EDM4EIC_VERSION_PATCH@ +#define edm4hep_VERSION EDM4EIC_VERSION(EDM4EIC_VERSION_MAJOR, EDM4EIC_VERSION_MINOR, EDM4EIC_VERSION_PATCH) + +/// The encoded version with which EDM4EIC has been built +#define EDM4EIC_BUILD_VERSION EDM4EIC_VERSION(EDM4EIC_VERSION_MAJOR, EDM4EIC_VERSION_MINOR, EDM4EIC_VERSION_PATCH) + +namespace edm4eic::version { + + /** + * Version class consisting of 3 16 bit unsigned integers to hold the major, + * minor and patch version. Provides constexpr comparison operators that allow + * to use this class in constexpr-if clauses. + */ + struct Version { + uint16_t major{0}; + uint16_t minor{0}; + uint16_t patch{0}; + +#if __cplusplus >= 202002L + auto operator<=>(const Version&) const = default; +#else +// No spaceship yet in c++17 +#define DEFINE_COMP_OPERATOR(OP) \ + constexpr bool operator OP(const Version& o) const noexcept { \ + return std::tie(major, minor, patch) OP std::tie(o.major, o.minor, o.patch); \ + } + + DEFINE_COMP_OPERATOR(<) + DEFINE_COMP_OPERATOR(<=) + DEFINE_COMP_OPERATOR(>) + DEFINE_COMP_OPERATOR(>=) + DEFINE_COMP_OPERATOR(==) + DEFINE_COMP_OPERATOR(!=) + +#undef DEFINE_COMP_OPERATOR +#endif + + friend std::ostream& operator<<(std::ostream&, const Version& v); + }; + + inline std::ostream& operator<<(std::ostream& os, const Version& v) { + return os << v.major << "." << v.minor << "." << v.patch; + } + + /** + * The current build version + */ + static constexpr Version build_version{EDM4EIC_VERSION_MAJOR, EDM4EIC_VERSION_MINOR, EDM4EIC_VERSION_PATCH}; + + /** + * Decode a version from a 64 bit unsigned + */ + static constexpr Version decode_version(uint64_t version) noexcept { + return Version{ + (uint16_t) EDM4EIC_MAJOR_VERSION(version), + (uint16_t) EDM4EIC_MINOR_VERSION(version), + (uint16_t) EDM4EIC_PATCH_VERSION(version) + }; + } +} + + +#endif