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

Feature: vcpkg compatibility #296

Merged
merged 7 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ jobs:
run: |
set -e
sudo apt-get update || true
sudo apt-get install -y ninja-build
sudo python3 -m pip install --upgrade pip
sudo apt-get install -y ninja-build python3-pip
sudo pip3 install scikit-build
sudo pip3 install cmake requests gitpython gcovr pyyaml
- name: "cmake"
Expand Down
40 changes: 33 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
cmake_minimum_required(VERSION 3.12)

if (PACKAGE_MANAGER)
if(PACKAGE_MANAGER NOT MATCHES "^(hunter|vcpkg)$")
message(FATAL_ERROR "PACKAGE_MANAGER must be set to 'hunter', 'vcpkg' or isn't set")
endif ()
else ()
set(PACKAGE_MANAGER "hunter")
if (CMAKE_TOOLCHAIN_FILE)
get_filename_component(ACTUAL_NAME ${CMAKE_TOOLCHAIN_FILE} NAME)
if(ACTUAL_NAME STREQUAL "vcpkg.cmake")
message(STATUS "vcpkg will be used because vcpkg.cmake has found")
set(PACKAGE_MANAGER "vcpkg")
endif ()
endif ()
endif ()
message(STATUS "Selected package manager: ${PACKAGE_MANAGER}")

if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.27")
cmake_policy(SET CMP0144 NEW)
endif()
Expand All @@ -20,7 +36,9 @@ set(CMAKE_TOOLCHAIN_FILE
cmake_policy(SET CMP0048 NEW)
cmake_policy(SET CMP0135 NEW)

include("cmake/Hunter/init.cmake")
if (PACKAGE_MANAGER STREQUAL "hunter")
include("cmake/Hunter/init.cmake")
endif ()

project(libp2p VERSION 0.1.17 LANGUAGES C CXX)

Expand All @@ -39,6 +57,7 @@ option(TSAN "Enable thread sanitizer" OFF)
option(UBSAN "Enable UB sanitizer" OFF)
option(EXPOSE_MOCKS "Make mocks header files visible for child projects" ON)
option(METRICS_ENABLED "Enable libp2p metrics" OFF)
option(SQLITE_ENABLED "Enable sqlite based libp2p storage" OFF)

include(cmake/print.cmake)
print("C flags: ${CMAKE_C_FLAGS}")
Expand All @@ -59,6 +78,10 @@ if (METRICS_ENABLED)
add_compile_definitions("LIBP2P_METRICS_ENABLED")
endif ()

if(SQLITE_ENABLED)
set(SQLITE_FIND_DEP "find_dependency(SQLiteModernCpp CONFIG REQUIRED)")
endif()

## setup compilation flags
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(AppleClang|Clang|GNU)$")
# enable those flags
Expand Down Expand Up @@ -114,6 +137,9 @@ if(EXAMPLES)
add_subdirectory(example)
endif()
if(TESTING OR COVERAGE)
if (PACKAGE_MANAGER STREQUAL "vcpkg")
list(APPEND VCPKG_MANIFEST_FEATURES libp2p-tests)
endif()
enable_testing()
add_subdirectory(test)
endif()
Expand All @@ -126,11 +152,11 @@ include(CMakePackageConfigHelpers)

set(CONFIG_INCLUDE_DIRS ${CMAKE_INSTALL_FULL_INCLUDEDIR}/libp2p)
configure_package_config_file(${CMAKE_CURRENT_LIST_DIR}/cmake/libp2pConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/libp2pConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libp2p
)
${CMAKE_CURRENT_BINARY_DIR}/libp2pConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libp2p
)

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/libp2pConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libp2p
)
${CMAKE_CURRENT_BINARY_DIR}/libp2pConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libp2p
)
24 changes: 24 additions & 0 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,27 @@
# SHA1 1234567890abcdef1234567890abcdef12345678
# CMAKE_ARGS "CMAKE_VARIABLE=value"
# )

hunter_config(
soralog
URL https://github.com/xDimon/soralog/archive/4dfffd3d949b1c16a04db2e5756555a4031732f7.tar.gz
SHA1 60e3dcaab2d8e43f0ed4fd22087677663c618716
# VERSION 0.2.4
KEEP_PACKAGE_SOURCES
)

hunter_config(
ZLIB
VERSION 1.3.0-p0
URL https://github.com/cpp-pm/zlib/archive/refs/tags/v1.3.0-p0.tar.gz
SHA1 311ca59e20cbbfe9d9e05196c12c6ae109093987
)

hunter_config(
qtils
URL https://github.com/qdrvm/qtils/archive/1e492cf09a3640570cae59a951502614320c0797.tar.gz
SHA1 033dd907e2566c95ce2ccf1fa6dd9766bc896894
CMAKE_ARGS
FORMAT_ERROR_WITH_FULLTYPE=ON
KEEP_PACKAGE_SOURCES
)
24 changes: 18 additions & 6 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@
# SPDX-License-Identifier: Apache-2.0
#

if (NOT PACKAGE_MANAGER STREQUAL "hunter")
function(hunter_add_package)
endfunction()
endif ()

if (TESTING)
# https://docs.hunter.sh/en/latest/packages/pkg/GTest.html
hunter_add_package(GTest)
find_package(GTest CONFIG REQUIRED)
endif()

# https://docs.hunter.sh/en/latest/packages/pkg/Boost.html
hunter_add_package(Boost COMPONENTS random filesystem program_options)
find_package(Boost CONFIG REQUIRED random filesystem program_options)
if (PACKAGE_MANAGER STREQUAL "hunter")
hunter_add_package(Boost COMPONENTS random filesystem program_options)
find_package(Boost CONFIG REQUIRED filesystem random program_options)
else ()
find_package(Boost CONFIG REQUIRED filesystem random beast program_options)
endif ()

# https://www.openssl.org/
hunter_add_package(BoringSSL)
Expand Down Expand Up @@ -53,6 +61,10 @@ find_package(tsl_hat_trie CONFIG REQUIRED)
hunter_add_package(Boost.DI)
find_package(Boost.DI CONFIG REQUIRED)

# https://github.com/qdrvm/libp2p-sqlite-modern-cpp/tree/hunter
hunter_add_package(SQLiteModernCpp)
find_package(SQLiteModernCpp CONFIG REQUIRED)
if (SQLITE_ENABLED)
# https://github.com/qdrvm/libp2p-sqlite-modern-cpp/tree/hunter
hunter_add_package(SQLiteModernCpp)
find_package(SQLiteModernCpp CONFIG REQUIRED)
endif ()

find_package(ZLIB REQUIRED)
2 changes: 1 addition & 1 deletion cmake/libp2pConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ find_dependency(yaml-cpp CONFIG REQUIRED)
find_dependency(soralog CONFIG REQUIRED)
find_dependency(tsl_hat_trie CONFIG REQUIRED)
find_dependency(Boost.DI CONFIG REQUIRED)
find_dependency(SQLiteModernCpp CONFIG REQUIRED)
@SQLITE_FIND_DEP@

include("${CMAKE_CURRENT_LIST_DIR}/libp2pTargets.cmake")

Expand Down
1 change: 1 addition & 0 deletions include/libp2p/multi/multiaddress_protocol_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <functional>
#include <map>
#include <string_view>
#include <unistd.h>

namespace libp2p::multi {

Expand Down
15 changes: 13 additions & 2 deletions src/crypto/rsa_provider/rsa_provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,19 @@ namespace libp2p::crypto::rsa {
const Signature &signature,
const PublicKey &public_key) const {
OUTCOME_TRY(x509_key, RsaProviderImpl::getPublicKeyFromBytes(public_key));
EVP_PKEY *key = X509_PUBKEY_get0(x509_key.get());
std::unique_ptr<RSA, void (*)(RSA *)> rsa{EVP_PKEY_get1_RSA(key), RSA_free};

EVP_PKEY *key = X509_PUBKEY_get(x509_key.get());
if (!key) {
return CryptoProviderError::SIGNATURE_VERIFICATION_FAILED;
}
std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> key_ptr{key,
EVP_PKEY_free};
std::unique_ptr<RSA, decltype(&RSA_free)> rsa{
EVP_PKEY_get1_RSA(key_ptr.get()), RSA_free};
if (!rsa) {
return CryptoProviderError::SIGNATURE_VERIFICATION_FAILED;
}

OUTCOME_TRY(digest, sha256(message));
int result = RSA_verify(NID_sha256,
digest.data(),
Expand Down
2 changes: 1 addition & 1 deletion src/protocol_muxer/multiselect/multiselect_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ namespace libp2p::protocol_muxer::multiselect {

SL_DEBUG(log(),
"Failed to negotiate protocols: {}",
fmt::join(protocols_.begin(), protocols_.end(), ", "));
fmt::join(protocols_, ", "));
return MaybeResult(ProtocolMuxer::Error::NEGOTIATION_FAILED);
}

Expand Down
12 changes: 7 additions & 5 deletions src/storage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
# SPDX-License-Identifier: Apache-2.0
#

libp2p_add_library(p2p_sqlite sqlite.cpp)
target_link_libraries(p2p_sqlite
SQLiteModernCpp::SQLiteModernCpp
p2p_logger
)
if (SQLITE_ENABLED)
libp2p_add_library(p2p_sqlite sqlite.cpp)
target_link_libraries(p2p_sqlite
SQLiteModernCpp::SQLiteModernCpp
p2p_logger
)
endif ()
1 change: 1 addition & 0 deletions src/transport/quic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ libp2p_add_library(p2p_quic
target_link_libraries(p2p_quic
lsquic::lsquic
p2p_tls
ZLIB::ZLIB
)
2 changes: 1 addition & 1 deletion test/acceptance/p2p/host/basic_host_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ TEST_F(BasicHostTest, NewStream) {

bool executed = false;
host->newStream(pinfo, {protocol}, [&](auto &&result) {
auto stream = EXPECT_OK(result);
ASSERT_OUTCOME_SUCCESS(stream, result);
(void)stream;
executed = true;
});
Expand Down
8 changes: 4 additions & 4 deletions test/acceptance/p2p/host/peer/test_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Peer::Peer(Peer::Duration timeout, bool secure)
basic::Scheduler::Config{})},
secure_{secure} {
auto keys =
EXPECT_OK(crypto_provider_->generateKeys(crypto::Key::Type::Ed25519));
crypto_provider_->generateKeys(crypto::Key::Type::Ed25519).value();
host_ = makeHost(keys);

auto handler = [this](StreamAndProtocol stream) { echo_->handle(stream); };
Expand All @@ -64,7 +64,7 @@ Peer::Peer(Peer::Duration timeout, bool secure)
void Peer::startServer(const multi::Multiaddress &address,
std::shared_ptr<std::promise<peer::PeerInfo>> promise) {
post(*context_, [this, address, p = std::move(promise)] {
EXPECT_OK(host_->listen(address));
ASSERT_OUTCOME_SUCCESS(host_->listen(address));
host_->start();
p->set_value(host_->getPeerInfo());
});
Expand All @@ -89,7 +89,7 @@ void Peer::startClient(const peer::PeerInfo &pinfo,
counter = std::move(counter)](
StreamAndProtocolOrError rstream) mutable {
// get stream
auto stream = EXPECT_OK(rstream);
ASSERT_OUTCOME_SUCCESS(stream, rstream);
// make client session
auto client = std::make_shared<protocol::ClientTestSession>(
stream.stream, ping_times);
Expand All @@ -102,7 +102,7 @@ void Peer::startClient(const peer::PeerInfo &pinfo,
// count message exchange
counter->tick();
// ensure message returned
auto vec = EXPECT_OK(res);
ASSERT_OUTCOME_SUCCESS(vec, res);
// ensure message is correct
ASSERT_EQ(vec.size(), client->bufferSize()); // NOLINT
});
Expand Down
26 changes: 13 additions & 13 deletions test/acceptance/p2p/muxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct UpgraderSemiMock : public Upgrader {

void upgradeToMuxed(SecSPtr conn, OnMuxedCallbackFunc cb) override {
mux->muxConnection(std::move(conn), [cb = std::move(cb)](auto &&conn_res) {
auto conn = EXPECT_OK(conn_res);
ASSERT_OUTCOME_SUCCESS(conn, conn_res);
cb(std::move(conn));
});
}
Expand All @@ -111,7 +111,7 @@ struct Server : public std::enable_shared_from_this<Server> {

conn->onStream(
[this, conn](outcome::result<std::shared_ptr<Stream>> rstream) {
auto stream = EXPECT_OK(rstream);
ASSERT_OUTCOME_SUCCESS(stream, rstream);
this->println("new stream created");
this->streamsCreated++;
auto buf = std::make_shared<std::vector<uint8_t>>();
Expand All @@ -136,7 +136,7 @@ struct Server : public std::enable_shared_from_this<Server> {
this->println(fmt::format("readSome error: {}", rread.error()));
}

auto read = EXPECT_OK(rread);
ASSERT_OUTCOME_SUCCESS(read, rread);

this->println("readSome ", read, " bytes");
if (read == 0) {
Expand All @@ -150,7 +150,7 @@ struct Server : public std::enable_shared_from_this<Server> {
stream,
*buf,
[buf, read, stream, this](outcome::result<size_t> rwrite) {
auto write = EXPECT_OK(rwrite);
ASSERT_OUTCOME_SUCCESS(write, rwrite);
this->println("write ", write, " bytes");
this->streamWrites++;
ASSERT_EQ(write, read);
Expand All @@ -163,12 +163,12 @@ struct Server : public std::enable_shared_from_this<Server> {
void listen(const Multiaddress &ma) {
listener_ = transport_->createListener(
[this](outcome::result<std::shared_ptr<CapableConnection>> rconn) {
auto conn = EXPECT_OK(rconn);
ASSERT_OUTCOME_SUCCESS(conn, rconn);
this->println("new connection received");
this->onConnection(conn);
});

EXPECT_OK(this->listener_->listen(ma));
ASSERT_OUTCOME_SUCCESS(this->listener_->listen(ma));
}

size_t clientsConnected = 0;
Expand Down Expand Up @@ -210,7 +210,7 @@ struct Client : public std::enable_shared_from_this<Client> {
p,
server,
[this](outcome::result<std::shared_ptr<CapableConnection>> rconn) {
auto conn = EXPECT_OK(rconn);
ASSERT_OUTCOME_SUCCESS(conn, rconn);
conn->start();
this->println("connected");
this->onConnection(conn);
Expand All @@ -222,7 +222,7 @@ struct Client : public std::enable_shared_from_this<Client> {
boost::asio::post(*context_, [i, conn, this]() {
conn->newStream(
[i, conn, this](outcome::result<std::shared_ptr<Stream>> rstream) {
auto stream = EXPECT_OK(rstream);
ASSERT_OUTCOME_SUCCESS(stream, rstream);
this->println("new stream number ", i, " created");
this->onStream(i, this->rounds_, stream);
});
Expand All @@ -248,7 +248,7 @@ struct Client : public std::enable_shared_from_this<Client> {
stream,
*buf,
[round, streamId, buf, stream, this](outcome::result<size_t> rwrite) {
auto write = EXPECT_OK(rwrite);
ASSERT_OUTCOME_SUCCESS(write, rwrite);
this->println(streamId, " write ", write, " bytes");
this->streamWrites++;

Expand All @@ -259,7 +259,7 @@ struct Client : public std::enable_shared_from_this<Client> {
readbuf->size(),
[round, streamId, write, buf, readbuf, stream, this](
outcome::result<size_t> rread) {
auto read = EXPECT_OK(rread);
ASSERT_OUTCOME_SUCCESS(read, rread);
this->println(streamId, " readSome ", read, " bytes");
this->streamReads++;

Expand Down Expand Up @@ -437,9 +437,9 @@ TEST_P(MuxerAcceptanceTest, ParallelEcho) {
auto client = std::make_shared<Client>(
transport, localSeed, context, streams, rounds);

auto marshalled_key =
EXPECT_OK(key_marshaller->marshal(serverKeyPair.publicKey));
auto p = EXPECT_OK(PeerId::fromPublicKey(marshalled_key));
ASSERT_OUTCOME_SUCCESS(
marshalled_key, key_marshaller->marshal(serverKeyPair.publicKey));
ASSERT_OUTCOME_SUCCESS(p, PeerId::fromPublicKey(marshalled_key));
client->connect(p, serverAddr);

context->run_for(10000ms);
Expand Down
Loading