Skip to content

Commit

Permalink
feat: add version header (#40)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
  • Loading branch information
wdconinc and veprbl authored Aug 15, 2023
1 parent 63bb29c commit c12fbf7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
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
#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

0 comments on commit c12fbf7

Please sign in to comment.