Skip to content

Commit 0ba4a26

Browse files
authored
Cross compiling fixes and improvements (#59)
* CMake: Expose CMake options to includes This change shuffles the order of several CMake steps to expose CMake options to included files. By defining options before inclusion, included files are able to access the input provided by the build system. * CMake: Exclude test dependencies when building tests * CMake: Allow build system to provide protobuf compiler When building without Hunter, it is desirable to pass the path to protoc and the protobuf include directory from the build system. Allow these variables to be overridden. Fixes the error: | CMake Error at cmake/functions.cmake:52 (message): | Protobuf_PROTOC_EXECUTABLE is empty | Call Stack (most recent call first): | cmake/functions.cmake:96 (compile_proto_to_cpp) | src/crypto/protobuf/CMakeLists.txt:6 (add_proto_library) * Fix build error due to missing include Error was: | literals.cpp: In function 'libp2p::common::Hash256 libp2p::common::operator""_hash256(const char*, size_t)': | literals.cpp:17:36: error: no matching function for call to 'min(size_t&, long unsigned int)' | 17 | std::copy_n(c, std::min(s, 32ul), hash.rbegin()); | | ^ * Fix build error due to mismatched types Error was: | literals.cpp: In function 'libp2p::common::Hash256 libp2p::common::operator""_hash256(const char*, size_t)': | literals.cpp:19:36: error: no matching function for call to 'min(size_t&, long unsigned int)' | 19 | std::copy_n(c, std::min(s, 32ul), hash.rbegin()); | | ^ * Fix compiler warning due to sign comparison When compiling with -Werror, this causes the build to fail. Warning was: | yamux_frame.cpp:103:28: error: comparison of integers of different signs: 'gsl::span::index_type' (aka 'int') and 'const uint32_t' (aka 'const unsigned int') [-Werror,-Wsign-compare] | if (frame_bytes.size() < YamuxFrame::kHeaderLength) { | ~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
1 parent aebc95f commit 0ba4a26

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

CMakeLists.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@ include("cmake/Hunter/init.cmake")
1717
cmake_policy(SET CMP0048 NEW)
1818
project(libp2p VERSION 0.0.1 LANGUAGES C CXX)
1919

20-
include(cmake/print.cmake)
21-
print("C flags: ${CMAKE_C_FLAGS}")
22-
print("CXX flags: ${CMAKE_CXX_FLAGS}")
23-
print("Using CMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}")
24-
25-
include(CheckCXXCompilerFlag)
26-
include(cmake/install.cmake)
27-
include(cmake/libp2p_add_library.cmake)
28-
include(cmake/dependencies.cmake)
29-
include(cmake/functions.cmake)
30-
include(cmake/san.cmake)
31-
3220
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3321

3422
option(TESTING "Build tests" ON)
@@ -44,6 +32,18 @@ option(TSAN "Enable thread sanitizer" OFF)
4432
option(UBSAN "Enable UB sanitizer" OFF)
4533
option(EXPOSE_MOCKS "Make mocks header files visible for child projects" OFF)
4634

35+
include(cmake/print.cmake)
36+
print("C flags: ${CMAKE_C_FLAGS}")
37+
print("CXX flags: ${CMAKE_CXX_FLAGS}")
38+
print("Using CMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}")
39+
40+
include(CheckCXXCompilerFlag)
41+
include(cmake/install.cmake)
42+
include(cmake/libp2p_add_library.cmake)
43+
include(cmake/dependencies.cmake)
44+
include(cmake/functions.cmake)
45+
include(cmake/san.cmake)
46+
4747

4848
## setup compilation flags
4949
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(AppleClang|Clang|GNU)$")

cmake/dependencies.cmake

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
# https://docs.hunter.sh/en/latest/packages/pkg/GTest.html
2-
hunter_add_package(GTest)
3-
find_package(GTest CONFIG REQUIRED)
4-
find_package(GMock CONFIG REQUIRED)
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
if (TESTING)
7+
# https://docs.hunter.sh/en/latest/packages/pkg/GTest.html
8+
hunter_add_package(GTest)
9+
find_package(GTest CONFIG REQUIRED)
10+
find_package(GMock CONFIG REQUIRED)
11+
endif()
512

613
# https://docs.hunter.sh/en/latest/packages/pkg/Boost.html
714
hunter_add_package(Boost COMPONENTS random filesystem program_options)

cmake/functions.cmake

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ function(add_flag flag)
4545
endfunction()
4646

4747
function(compile_proto_to_cpp PROTO_LIBRARY_NAME PB_H PB_CC PROTO)
48-
get_target_property(Protobuf_INCLUDE_DIR protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES)
49-
get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc IMPORTED_LOCATION_RELEASE)
48+
if (NOT Protobuf_INCLUDE_DIR)
49+
get_target_property(Protobuf_INCLUDE_DIR protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES)
50+
endif()
51+
if (NOT Protobuf_PROTOC_EXECUTABLE)
52+
get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc IMPORTED_LOCATION_RELEASE)
53+
set(PROTOBUF_DEPENDS protobuf::protoc)
54+
endif()
5055

5156
if (NOT Protobuf_PROTOC_EXECUTABLE)
5257
message(FATAL_ERROR "Protobuf_PROTOC_EXECUTABLE is empty")
@@ -78,7 +83,7 @@ function(compile_proto_to_cpp PROTO_LIBRARY_NAME PB_H PB_CC PROTO)
7883
COMMAND ${GEN_COMMAND}
7984
ARGS -I${PROJECT_SOURCE_DIR}/src -I${GEN_ARGS} --cpp_out=${SCHEMA_OUT_DIR} ${PROTO_ABS}
8085
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
81-
DEPENDS protobuf::protoc
86+
DEPENDS ${PROTOBUF_DEPENDS}
8287
VERBATIM
8388
)
8489

src/common/literals.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
#include <libp2p/multi/multihash.hpp>
1212
#include <libp2p/peer/peer_id.hpp>
1313

14+
#include <algorithm>
15+
1416
namespace libp2p::common {
1517
libp2p::common::Hash256 operator""_hash256(const char *c, size_t s) {
1618
libp2p::common::Hash256 hash{};
17-
std::copy_n(c, std::min(s, 32ul), hash.rbegin());
19+
std::copy_n(c, std::min(s, static_cast<size_t>(32u)), hash.rbegin());
1820
return hash;
1921
}
2022

2123
libp2p::common::Hash512 operator""_hash512(const char *c, size_t s) {
2224
libp2p::common::Hash512 hash{};
23-
std::copy_n(c, std::min(s, 64ul), hash.rbegin());
25+
std::copy_n(c, std::min(s, static_cast<size_t>(64u)), hash.rbegin());
2426
return hash;
2527
}
2628

src/muxer/yamux/yamux_frame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace libp2p::connection {
100100
}
101101

102102
boost::optional<YamuxFrame> parseFrame(gsl::span<const uint8_t> frame_bytes) {
103-
if (frame_bytes.size() < YamuxFrame::kHeaderLength) {
103+
if (frame_bytes.size() < static_cast<int>(YamuxFrame::kHeaderLength)) {
104104
return {};
105105
}
106106

0 commit comments

Comments
 (0)