Loading...
Searching...
No Matches
RgminAbi.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#pragma once
5
6// ABI contract with rgmin. The header gpridge compiles against must
7// carry the major gpridge is written for and a minor at or above the
8// lowest minor that declares every rgmin symbol gpridge calls. The
9// library found at run time must accept the stamp built from that header.
10
11#include <rgmin.h>
12
13#include <mutex>
14#include <sstream>
15#include <stdexcept>
16#include <string>
17
19#define GPR_RGMIN_ABI_MAJOR 1
22#define GPR_RGMIN_ABI_MIN_MINOR 43
23
24static_assert(RGMIN_ABI_VERSION_MAJOR == GPR_RGMIN_ABI_MAJOR,
25 "gpridge is written against rgmin C ABI major "
26 "GPR_RGMIN_ABI_MAJOR; the rgmin.h found declares another "
27 "major");
28static_assert(RGMIN_ABI_VERSION_MINOR >= GPR_RGMIN_ABI_MIN_MINOR,
29 "gpridge needs rgmin C ABI minor GPR_RGMIN_ABI_MIN_MINOR or "
30 "newer; the rgmin.h found is older (see subprojects/rgmin.wrap)");
31
32namespace gpr::rgmin_abi {
33
35struct Stamp {
36 unsigned major = 0;
37 unsigned minor = 0;
38 unsigned layout = 0;
39};
40
41[[nodiscard]] inline std::string describe(const Stamp& stamp)
42{
43 std::ostringstream out;
44 out << stamp.major << '.' << stamp.minor << " layout " << stamp.layout;
45 return out.str();
46}
47
52[[nodiscard]] inline std::string mismatch(const Stamp& header,
53 const Stamp& library,
54 bool library_accepts)
55{
56 if (library_accepts && header.major == library.major &&
57 header.layout == library.layout)
58 return {};
59 return "rgmin ABI mismatch: gpridge was built against rgmin.h " +
60 describe(header) + " and the loaded rgmin library reports " +
61 describe(library) +
62 (library_accepts ? "" : " and rejects the header's stamp");
63}
64
65[[nodiscard]] inline Stamp headerStamp() noexcept
66{
67 return {RGMIN_ABI_VERSION_MAJOR, RGMIN_ABI_VERSION_MINOR,
68 RGMIN_ABI_LAYOUT_REVISION};
69}
70
71[[nodiscard]] inline Stamp libraryStamp()
72{
73 const rgmin_abi_stamp_t stamp = rgmin_abi_stamp();
74 return {stamp.abi_major, stamp.abi_minor, stamp.layout_revision};
75}
76
78[[nodiscard]] inline std::string linkedLibraryMismatch()
79{
80 const Stamp header = headerStamp();
81 const rgmin_abi_stamp_t compiled{static_cast<uint16_t>(header.major),
82 static_cast<uint16_t>(header.minor),
83 static_cast<uint16_t>(header.layout)};
84 return mismatch(header, libraryStamp(),
85 rgmin_abi_compatible(&compiled) != 0);
86}
87
91{
92 static std::once_flag checked;
93 static std::string failure;
94 std::call_once(checked, [] { failure = linkedLibraryMismatch(); });
95 if (!failure.empty()) throw std::runtime_error(failure);
96}
97
98} // namespace gpr::rgmin_abi
#define GPR_RGMIN_ABI_MIN_MINOR
Definition RgminAbi.h:22
#define GPR_RGMIN_ABI_MAJOR
Major of the rgmin C ABI gpridge is written against.
Definition RgminAbi.h:19
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
std::string describe(const Stamp &stamp)
Definition RgminAbi.h:41
Stamp libraryStamp()
Definition RgminAbi.h:71
Stamp headerStamp() noexcept
Definition RgminAbi.h:65
void requireCompatibleLibrary()
Definition RgminAbi.h:90
std::string mismatch(const Stamp &header, const Stamp &library, bool library_accepts)
Definition RgminAbi.h:52
std::string linkedLibraryMismatch()
The mismatch message for the linked library; empty when compatible.
Definition RgminAbi.h:78
Header and library identities as plain numbers.
Definition RgminAbi.h:35