Skip to content

Commit

Permalink
feat: Add compiled version info (#2313)
Browse files Browse the repository at this point in the history
This should help us detect when we're running with a mismatched header/shared lib combo, like can happen in the hack-build in Athena.
  • Loading branch information
paulgessinger authored Jul 25, 2023
1 parent dbcba62 commit 21cb496
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
28 changes: 27 additions & 1 deletion Core/ActsVersion.hpp.in
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +13,8 @@
// will cause a recompile every time a new Acts version is
// used.

#include <iosfwd>

namespace Acts {

// clang-format does not like the CMake @...@ replacement variables
Expand All @@ -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
5 changes: 5 additions & 0 deletions Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
41 changes: 41 additions & 0 deletions Core/src/ActsVersion.cpp
Original file line number Diff line number Diff line change
@@ -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 <ostream>
#include <string_view>

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
16 changes: 12 additions & 4 deletions Tests/DownstreamProject/ShowActsVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@

#include <Acts/ActsVersion.hpp>

#include <cstdio>
#include <cstdlib>
#include <iostream>

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;
}

0 comments on commit 21cb496

Please sign in to comment.