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 compiled version info #2313

Merged
merged 7 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 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,26 @@ 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;
andiwand marked this conversation as resolved.
Show resolved Hide resolved

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