diff --git a/Core/ActsVersion.hpp.in b/Core/ActsVersion.hpp.in index 29b24edd7e5..4dc1ba7acb7 100644 --- a/Core/ActsVersion.hpp.in +++ b/Core/ActsVersion.hpp.in @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2016-2020 CERN for the benefit of the Acts project +// Copyright (C) 2016-2023 CERN for the benefit of the Acts project // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -13,6 +13,8 @@ // will cause a recompile every time a new Acts version is // used. +#include + namespace Acts { // clang-format does not like the CMake @...@ replacement variables @@ -26,4 +28,28 @@ constexpr unsigned int Version = constexpr const char* const CommitHash = "@_acts_commit_hash@"; constexpr const char* const CommitHashShort = "@_acts_commit_hash_short@"; +struct VersionInfo { + unsigned int versionMajor; + unsigned int versionMinor; + unsigned int versionPatch; + const char* const commitHash; + + VersionInfo() = delete; + + static VersionInfo fromHeader() { + return VersionInfo(VersionMajor, VersionMinor, VersionPatch, CommitHash); + } + + static VersionInfo fromLibrary(); + + bool operator==(const VersionInfo& other) const; + bool operator!=(const VersionInfo& other) const { return !(*this == other); } + + friend std::ostream& operator<<(std::ostream& os, const VersionInfo& vi); + + private: + VersionInfo(unsigned int majorIn, unsigned int minorIn, unsigned int patchIn, + const char* const commitHashIn); +}; + } // namespace Acts diff --git a/Core/CMakeLists.txt b/Core/CMakeLists.txt index 3513a2a8993..ff7797d381b 100644 --- a/Core/CMakeLists.txt +++ b/Core/CMakeLists.txt @@ -6,6 +6,11 @@ configure_file( add_library( ActsCore SHARED "") +target_sources( + ActsCore + PRIVATE + src/ActsVersion.cpp) + target_compile_features( ActsCore PUBLIC ${ACTS_CXX_STANDARD_FEATURE}) diff --git a/Core/src/ActsVersion.cpp b/Core/src/ActsVersion.cpp new file mode 100644 index 00000000000..dffaef6d0cd --- /dev/null +++ b/Core/src/ActsVersion.cpp @@ -0,0 +1,41 @@ +// This file is part of the Acts project. +// +// Copyright (C) 2023 CERN for the benefit of the Acts project +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "Acts/ActsVersion.hpp" + +#include +#include + +namespace Acts { + +VersionInfo::VersionInfo(unsigned int majorIn, unsigned int minorIn, + unsigned int patchIn, const char* const commitHashIn) + : versionMajor(majorIn), + versionMinor(minorIn), + versionPatch(patchIn), + commitHash(commitHashIn) {} + +VersionInfo VersionInfo::fromLibrary() { + // this is filled by the Core shared library + // while the constants below depend on the include + return VersionInfo{VersionMajor, VersionMinor, VersionPatch, CommitHash}; +} + +bool VersionInfo::operator==(const VersionInfo& other) const { + return versionMajor == other.versionMajor && + versionMinor == other.versionMinor && + versionPatch == other.versionPatch && + std::string_view{commitHash} == std::string_view{other.commitHash}; +} + +std::ostream& operator<<(std::ostream& os, const VersionInfo& vi) { + os << vi.versionMajor << "." << vi.versionMinor << "." << vi.versionPatch + << " (commit " << vi.commitHash << ")"; + return os; +} +} // namespace Acts diff --git a/Tests/DownstreamProject/ShowActsVersion.cpp b/Tests/DownstreamProject/ShowActsVersion.cpp index 9799ae967bb..144c3c8a18b 100644 --- a/Tests/DownstreamProject/ShowActsVersion.cpp +++ b/Tests/DownstreamProject/ShowActsVersion.cpp @@ -8,11 +8,19 @@ #include -#include -#include +#include int main(void) { - printf("Using Acts version %u.%u.%u commit %s\n", Acts::VersionMajor, - Acts::VersionMinor, Acts::VersionPatch, Acts::CommitHash); + std::cout << "Using Acts version " << Acts::VersionMajor << "." + << Acts::VersionMinor << "." << Acts::VersionPatch << " commit " + << Acts::CommitHash << std::endl; + + if (Acts::VersionInfo::fromHeader() != Acts::VersionInfo::fromLibrary()) { + std::cout << "WARNING: The version information is inconsistent!" + << std::endl; + std::cout << "Header: " << Acts::VersionInfo::fromHeader() << std::endl; + std::cout << "Library: " << Acts::VersionInfo::fromLibrary() << std::endl; + return EXIT_FAILURE; + } return EXIT_SUCCESS; }