cmake_minimum_required ( VERSION 3.12 )
if ( ${ CMAKE_VERSION } VERSION_GREATER_EQUAL "3.27" )
cmake_policy ( SET CMP0144 NEW )
endif ( )
find_program ( CCACHE_FOUND ccache )
if ( CCACHE_FOUND )
set_property ( GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache )
set_property ( GLOBAL PROPERTY RULE_LAUNCH_LINK ccache )
endif ( CCACHE_FOUND )
set ( CMAKE_TOOLCHAIN_FILE
" $ { C M A K E _ S O U R C E _ D I R } / c m a k e / t o o l c h a i n / c x x 2 0 . c m a k e "
C A C H E
F I L E P A T H
" D e f a u l t t o o l c h a i n "
)
cmake_policy ( SET CMP0048 NEW )
cmake_policy ( SET CMP0135 NEW )
include ( "cmake/Hunter/init.cmake" )
project ( libp2p VERSION 0.1.17 LANGUAGES C CXX )
set ( CMAKE_EXPORT_COMPILE_COMMANDS ON )
option ( TESTING "Build tests" ON )
option ( EXAMPLES "Build examples" ON )
option ( CLANG_FORMAT "Enable clang-format target" ON )
option ( CLANG_TIDY "Enable clang-tidy checks during compilation" OFF )
option ( COVERAGE "Enable generation of coverage info" OFF )
# sanitizers will be enabled only for libp2p, and will be disabled for dependencies
option ( ASAN "Enable address sanitizer" OFF )
option ( LSAN "Enable leak sanitizer" OFF )
option ( MSAN "Enable memory sanitizer" OFF )
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 )
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) {
| ~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
4 years ago
include ( cmake/print.cmake )
print ( "C flags: ${CMAKE_C_FLAGS}" )
print ( "CXX flags: ${CMAKE_CXX_FLAGS}" )
print ( "Using CMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}" )
# the property is out of "if TESTING" scope due to addtest func is out too
set_property ( GLOBAL PROPERTY TEST_TARGETS )
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) {
| ~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
4 years ago
include ( CheckCXXCompilerFlag )
include ( cmake/install.cmake )
include ( cmake/libp2p_add_library.cmake )
include ( cmake/dependencies.cmake )
include ( cmake/functions.cmake )
include ( cmake/san.cmake )
if ( METRICS_ENABLED )
add_compile_definitions ( "LIBP2P_METRICS_ENABLED" )
endif ( )
## setup compilation flags
if ( "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(AppleClang|Clang|GNU)$" )
# enable those flags
add_flag ( -Wall )
add_flag ( -Wextra )
add_flag ( -Woverloaded-virtual ) # warn if you overload (not override) a virtual function
add_flag ( -Wformat=2 ) # warn on security issues around functions that format output (ie printf)
add_flag ( -Wmisleading-indentation ) # (only in GCC >= 6.0) warn if indentation implies blocks where blocks do not exist
add_flag ( -Wduplicated-cond ) # (only in GCC >= 6.0) warn if if / else chain has duplicated conditions
add_flag ( -Wduplicated-branches ) # (only in GCC >= 7.0) warn if if / else branches have duplicated code
add_flag ( -Wnull-dereference ) # (only in GCC >= 6.0) warn if a null dereference is detected
add_flag ( -Wdouble-promotion ) # (GCC >= 4.6, Clang >= 3.8) warn if float is implicit promoted to double
add_flag ( -Wsign-compare )
add_flag ( -Wtype-limits ) # size_t - size_t >= 0 -> always true
# suppress warnings if a certain compiler version doesn't know some of the warnings above
add_flag ( -Wno-unknown-warning-option )
# disable those flags
add_flag ( -Wno-unused-command-line-argument ) # clang: warning: argument unused during compilation: '--coverage' [-Wunused-command-line-argument]
add_flag ( -Wno-unused-parameter ) # prints too many useless warnings
add_flag ( -Wno-format-nonliteral ) # prints way too many warnings from spdlog
add_flag ( -Wno-gnu-zero-variadic-macro-arguments ) # https://stackoverflow.com/questions/21266380/is-the-gnu-zero-variadic-macro-arguments-safe-to-ignore
# promote to errors
add_flag ( -Werror-unused-lambda-capture ) # error if lambda capture is unused
add_flag ( -Werror-return-type ) # warning: control reaches end of non-void function [-Wreturn-type]
add_flag ( -Werror-non-virtual-dtor ) # warn the user if a class with virtual functions has a non-virtual destructor. This helps catch hard to track down memory errors
add_flag ( -Werror-sign-compare ) # warn the user if they compare a signed and unsigned numbers
add_flag ( -Werror-reorder ) # field '$1' will be initialized after field '$2'
elseif ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" )
# using Visual Studio C++
# TODO(warchant): add flags https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md#msvc
endif ( )
if ( CLANG_TIDY )
include ( cmake/clang-tidy.cmake )
endif ( )
if ( CLANG_FORMAT )
set ( RECOMMENDED_CLANG_FORMAT_VERSION 15 )
include ( cmake/clang-format.cmake )
endif ( )
include_directories (
S Y S T E M
# project includes
$ { P R O J E C T _ S O U R C E _ D I R } / i n c l u d e
)
add_subdirectory ( src )
if ( EXAMPLES )
add_subdirectory ( example )
endif ( )
if ( TESTING OR COVERAGE )
enable_testing ( )
add_subdirectory ( test )
endif ( )
if ( COVERAGE )
include ( cmake/coverage.cmake )
endif ( )
include ( CMakePackageConfigHelpers )
set ( CONFIG_INCLUDE_DIRS ${ CMAKE_INSTALL_FULL_INCLUDEDIR } /libp2p )
configure_package_config_file ( ${ CMAKE_CURRENT_LIST_DIR } /cmake/libp2pConfig.cmake.in
$ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / l i b p 2 p C o n f i g . c m a k e
I N S T A L L _ D E S T I N A T I O N $ { C M A K E _ I N S T A L L _ L I B D I R } / c m a k e / l i b p 2 p
)
install ( FILES
$ { C M A K E _ C U R R E N T _ B I N A R Y _ D I R } / l i b p 2 p C o n f i g . c m a k e
D E S T I N A T I O N $ { C M A K E _ I N S T A L L _ L I B D I R } / c m a k e / l i b p 2 p
)