Enforce levelization in libxrpl with CMake (#5111)

Adds two CMake functions:

* add_module(library subdirectory): Declares an OBJECT "library" (a CMake abstraction for a collection of object files) with sources from the given subdirectory of the given library, representing a module. Isolates the module's headers by creating a subdirectory in the build directory, e.g. .build/tmp123, that contains just a symlink, e.g. .build/tmp123/basics, to the module's header directory, e.g. include/xrpl/basics, in the source directory, and putting .build/tmp123 (but not include/xrpl) on the include path of the module sources. This prevents the module sources from including headers not explicitly linked to the module in CMake with target_link_libraries.
* target_link_modules(library scope modules...): Links the library target to each of the module targets, and removes their sources from its source list (so they are not compiled and linked twice).

Uses these functions to separate and explicitly link modules in libxrpl:

    Level 01: beast
    Level 02: basics
    Level 03: json, crypto
    Level 04: protocol
    Level 05: resource, server
This commit is contained in:
John Freeman
2024-12-06 16:54:40 -06:00
committed by GitHub
parent 6d58065909
commit ea1fffeebf
61 changed files with 351 additions and 203 deletions

View File

@@ -20,8 +20,6 @@
#ifndef RIPPLE_BASICS_NUMBER_H_INCLUDED
#define RIPPLE_BASICS_NUMBER_H_INCLUDED
#include <xrpl/basics/MPTAmount.h>
#include <xrpl/basics/XRPAmount.h>
#include <cstdint>
#include <limits>
#include <ostream>
@@ -60,9 +58,6 @@ public:
explicit Number(rep mantissa, int exponent);
explicit constexpr Number(rep mantissa, int exponent, unchecked) noexcept;
Number(XRPAmount const& x);
Number(MPTAmount const& x);
constexpr rep
mantissa() const noexcept;
constexpr int
@@ -104,10 +99,6 @@ public:
* "mixed mode" more convenient, e.g. MPTAmount + Number.
*/
explicit
operator XRPAmount() const; // round to nearest, even on tie
explicit
operator MPTAmount() const; // round to nearest, even on tie
explicit
operator rep() const; // round to nearest, even on tie
friend constexpr bool
@@ -217,14 +208,6 @@ inline Number::Number(rep mantissa) : Number{mantissa, 0}
{
}
inline Number::Number(XRPAmount const& x) : Number{x.drops()}
{
}
inline Number::Number(MPTAmount const& x) : Number{x.value()}
{
}
inline constexpr Number::rep
Number::mantissa() const noexcept
{

View File

@@ -21,6 +21,7 @@
#define RIPPLE_BASICS_SHAMAP_HASH_H_INCLUDED
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/partitioned_unordered_map.h>
#include <ostream>
@@ -108,6 +109,13 @@ operator!=(SHAMapHash const& x, SHAMapHash const& y)
return !(x == y);
}
template <>
inline std::size_t
extract(SHAMapHash const& key)
{
return *reinterpret_cast<std::size_t const*>(key.as_uint256().data());
}
} // namespace ripple
#endif // RIPPLE_BASICS_SHAMAP_HASH_H_INCLUDED

View File

@@ -29,6 +29,7 @@
#include <xrpl/basics/Slice.h>
#include <xrpl/basics/contract.h>
#include <xrpl/basics/hardened_hash.h>
#include <xrpl/basics/partitioned_unordered_map.h>
#include <xrpl/basics/strHex.h>
#include <xrpl/beast/utility/Zero.h>
#include <xrpl/beast/utility/instrumentation.h>
@@ -637,6 +638,17 @@ operator<<(std::ostream& out, base_uint<Bits, Tag> const& u)
return out << to_string(u);
}
template <>
inline std::size_t
extract(uint256 const& key)
{
std::size_t result;
// Use memcpy to avoid unaligned UB
// (will optimize to equivalent code)
std::memcpy(&result, key.data(), sizeof(std::size_t));
return result;
}
#ifndef __INTELLISENSE__
static_assert(sizeof(uint128) == 128 / 8, "There should be no padding bytes");
static_assert(sizeof(uint160) == 160 / 8, "There should be no padding bytes");

View File

@@ -20,9 +20,11 @@
#ifndef RIPPLE_BASICS_PARTITIONED_UNORDERED_MAP_H
#define RIPPLE_BASICS_PARTITIONED_UNORDERED_MAP_H
#include <xrpl/beast/hash/uhash.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <functional>
#include <optional>
#include <string>
#include <thread>
#include <unordered_map>
#include <utility>
@@ -31,8 +33,18 @@
namespace ripple {
template <typename Key>
std::size_t
partitioner(Key const& key, std::size_t const numPartitions);
static std::size_t
extract(Key const& key)
{
return key;
}
template <>
inline std::size_t
extract(std::string const& key)
{
return ::beast::uhash<>{}(key);
}
template <
typename Key,
@@ -211,7 +223,7 @@ private:
std::size_t
partitioner(Key const& key) const
{
return ripple::partitioner(key, partitions_);
return extract(key) % partitions_;
}
template <class T>

View File

@@ -20,9 +20,9 @@
#ifndef RIPPLE_PROTOCOL_AMOUNTCONVERSION_H_INCLUDED
#define RIPPLE_PROTOCOL_AMOUNTCONVERSION_H_INCLUDED
#include <xrpl/basics/IOUAmount.h>
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/protocol/IOUAmount.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/XRPAmount.h>
#include <type_traits>

View File

@@ -19,14 +19,18 @@
#ifndef BASICS_FEES_H_INCLUDED
#define BASICS_FEES_H_INCLUDED
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/beast/utility/Zero.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/json/json_value.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/operators.hpp>
#include <cmath>
#include <ios>
#include <iosfwd>
#include <limits>
#include <optional>
#include <sstream>
#include <string>
#include <utility>
@@ -35,6 +39,9 @@ namespace ripple {
namespace feeunit {
/** "drops" are the smallest divisible amount of XRP. This is what most
of the code uses. */
struct dropTag;
/** "fee units" calculations are a not-really-unitless value that is used
to express the cost of a given transaction vs. a reference transaction.
They are primarily used by the Transactor classes. */

View File

@@ -20,7 +20,7 @@
#ifndef RIPPLE_PROTOCOL_FEES_H_INCLUDED
#define RIPPLE_PROTOCOL_FEES_H_INCLUDED
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/protocol/XRPAmount.h>
namespace ripple {

View File

@@ -21,11 +21,11 @@
#define RIPPLE_PROTOCOL_LEDGERHEADER_H_INCLUDED
#include <xrpl/basics/Slice.h>
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/chrono.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/protocol/XRPAmount.h>
namespace ripple {

View File

@@ -17,9 +17,10 @@
*/
//==============================================================================
#ifndef RIPPLE_BASICS_MPTAMOUNT_H_INCLUDED
#define RIPPLE_BASICS_MPTAMOUNT_H_INCLUDED
#ifndef RIPPLE_PROTOCOL_MPTAMOUNT_H_INCLUDED
#define RIPPLE_PROTOCOL_MPTAMOUNT_H_INCLUDED
#include <xrpl/basics/Number.h>
#include <xrpl/basics/contract.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/beast/utility/Zero.h>
@@ -52,6 +53,11 @@ public:
constexpr MPTAmount&
operator=(MPTAmount const& other) = default;
// Round to nearest, even on tie.
explicit MPTAmount(Number const& x) : MPTAmount(static_cast<value_type>(x))
{
}
constexpr explicit MPTAmount(value_type value);
constexpr MPTAmount& operator=(beast::Zero);
@@ -78,6 +84,11 @@ public:
explicit constexpr
operator bool() const noexcept;
operator Number() const noexcept
{
return value();
}
/** Return the sign of the amount */
constexpr int
signum() const noexcept;

View File

@@ -20,10 +20,10 @@
#ifndef RIPPLE_PROTOCOL_PAYCHAN_H_INCLUDED
#define RIPPLE_PROTOCOL_PAYCHAN_H_INCLUDED
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/HashPrefix.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/protocol/XRPAmount.h>
namespace ripple {

View File

@@ -22,6 +22,7 @@
#include <xrpl/basics/ByteUtilities.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/partitioned_unordered_map.h>
#include <cstdint>
namespace ripple {

View File

@@ -20,10 +20,10 @@
#ifndef RIPPLE_PROTOCOL_QUALITY_H_INCLUDED
#define RIPPLE_PROTOCOL_QUALITY_H_INCLUDED
#include <xrpl/basics/IOUAmount.h>
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/protocol/AmountConversions.h>
#include <xrpl/protocol/IOUAmount.h>
#include <xrpl/protocol/STAmount.h>
#include <xrpl/protocol/XRPAmount.h>
#include <algorithm>
#include <cstdint>

View File

@@ -21,16 +21,17 @@
#define RIPPLE_PROTOCOL_STAMOUNT_H_INCLUDED
#include <xrpl/basics/CountedObject.h>
#include <xrpl/basics/IOUAmount.h>
#include <xrpl/basics/LocalValue.h>
#include <xrpl/basics/MPTAmount.h>
#include <xrpl/basics/Number.h>
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/IOUAmount.h>
#include <xrpl/protocol/Issue.h>
#include <xrpl/protocol/MPTAmount.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STBase.h>
#include <xrpl/protocol/Serializer.h>
#include <xrpl/protocol/XRPAmount.h>
#include <xrpl/protocol/json_get_or_throw.h>
namespace ripple {

View File

@@ -21,11 +21,11 @@
#define RIPPLE_PROTOCOL_STOBJECT_H_INCLUDED
#include <xrpl/basics/CountedObject.h>
#include <xrpl/basics/FeeUnits.h>
#include <xrpl/basics/Slice.h>
#include <xrpl/basics/chrono.h>
#include <xrpl/basics/contract.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/protocol/FeeUnits.h>
#include <xrpl/protocol/HashPrefix.h>
#include <xrpl/protocol/SOTemplate.h>
#include <xrpl/protocol/STAmount.h>

View File

@@ -20,9 +20,9 @@
#ifndef RIPPLE_PROTOCOL_STVALIDATION_H_INCLUDED
#define RIPPLE_PROTOCOL_STVALIDATION_H_INCLUDED
#include <xrpl/basics/FeeUnits.h>
#include <xrpl/basics/Log.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/protocol/FeeUnits.h>
#include <xrpl/protocol/PublicKey.h>
#include <xrpl/protocol/STObject.h>
#include <xrpl/protocol/SecretKey.h>

View File

@@ -20,8 +20,8 @@
#ifndef RIPPLE_PROTOCOL_SYSTEMPARAMETERS_H_INCLUDED
#define RIPPLE_PROTOCOL_SYSTEMPARAMETERS_H_INCLUDED
#include <xrpl/basics/XRPAmount.h>
#include <xrpl/basics/chrono.h>
#include <xrpl/protocol/XRPAmount.h>
#include <cstdint>
#include <string>

View File

@@ -17,13 +17,14 @@
*/
//==============================================================================
#ifndef RIPPLE_BASICS_XRPAMOUNT_H_INCLUDED
#define RIPPLE_BASICS_XRPAMOUNT_H_INCLUDED
#ifndef RIPPLE_PROTOCOL_XRPAMOUNT_H_INCLUDED
#define RIPPLE_PROTOCOL_XRPAMOUNT_H_INCLUDED
#include <xrpl/basics/Number.h>
#include <xrpl/basics/contract.h>
#include <xrpl/basics/safe_cast.h>
#include <xrpl/beast/utility/Zero.h>
#include <xrpl/json/json_value.h>
#include <xrpl/protocol/FeeUnits.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/operators.hpp>
@@ -35,14 +36,6 @@
namespace ripple {
namespace feeunit {
/** "drops" are the smallest divisible amount of XRP. This is what most
of the code uses. */
struct dropTag;
} // namespace feeunit
class XRPAmount : private boost::totally_ordered<XRPAmount>,
private boost::additive<XRPAmount>,
private boost::equality_comparable<XRPAmount, std::int64_t>,
@@ -61,6 +54,11 @@ public:
constexpr XRPAmount&
operator=(XRPAmount const& other) = default;
// Round to nearest, even on tie.
explicit XRPAmount(Number const& x) : XRPAmount(static_cast<value_type>(x))
{
}
constexpr XRPAmount(beast::Zero) : drops_(0)
{
}
@@ -162,6 +160,11 @@ public:
return drops_ != 0;
}
operator Number() const noexcept
{
return drops();
}
/** Return the sign of the amount */
constexpr int
signum() const noexcept