Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version header #40

Merged
merged 11 commits into from
Aug 15, 2023
17 changes: 15 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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
)

Expand Down
91 changes: 91 additions & 0 deletions EDM4eicVersion.h.in
Original file line number Diff line number Diff line change
@@ -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
wdconinc marked this conversation as resolved.
Show resolved Hide resolved
#define EDM4EIC_VERSION_H

#include <cstdint>
#include <tuple>
#include <ostream>
#if __cplusplus >= 202002L
#include <compare>
#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