cmake: allow selecting C++ standard version during build

Also moves helper functions into a module file and replaces JOIN
generator expressions with list(TRANSFORM) commands in order to get
the correct sources list at the configure stage.
This commit is contained in:
Eugene Shalygin 2018-10-20 15:27:09 +02:00 committed by Arvid Norberg
parent a3eed212a1
commit 89a6e3cf7a
2 changed files with 103 additions and 60 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.11.0 FATAL_ERROR) cmake_minimum_required(VERSION 3.12.0 FATAL_ERROR)
project(libtorrent project(libtorrent
DESCRIPTION "Bittorrent library" DESCRIPTION "Bittorrent library"
@ -7,9 +7,9 @@ project(libtorrent
set (SOVERSION "10") set (SOVERSION "10")
list(APPEND CMAKE_MODULE_PATH ${libtorrent_SOURCE_DIR}/cmake/Modules) list(APPEND CMAKE_MODULE_PATH ${libtorrent_SOURCE_DIR}/cmake/Modules)
include(FeatureSummary)
include(GNUInstallDirs) include(GNUInstallDirs)
include(GeneratePkgConfig) include(GeneratePkgConfig)
include(LibtorrentMacros)
set(libtorrent_include_files set(libtorrent_include_files
add_torrent_params add_torrent_params
@ -428,46 +428,18 @@ set(ed25519_sources
verify verify
) )
function(target_optional_compile_definitions _target _scope) list(TRANSFORM sources PREPEND "src/")
set(options FEATURE) list(TRANSFORM kademlia_sources PREPEND "src/kademlia/")
set(oneValueArgs NAME DESCRIPTION DEFAULT) list(TRANSFORM ed25519_sources PREPEND "ed25519/src/")
set(multiValueArgs ENABLED DISABLED) list(TRANSFORM libtorrent_include_files PREPEND "include/libtorrent/")
cmake_parse_arguments(TOCD ${options} "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) list(TRANSFORM libtorrent_extensions_include_files PREPEND "include/libtorrent/extensions/")
option(${TOCD_NAME} "${TOCD_DESCRIPTION}" ${TOCD_DEFAULT}) list(TRANSFORM libtorrent_aux_include_files PREPEND "include/libtorrent/aux_/")
if (${${TOCD_NAME}}) list(TRANSFORM libtorrent_kademlia_include_files PREPEND "include/libtorrent/kademlia/")
target_compile_definitions(${_target} ${_scope} ${TOCD_ENABLED})
else()
target_compile_definitions(${_target} ${_scope} ${TOCD_DISABLED})
endif()
if(${TOCD_FEATURE})
add_feature_info(${TOCD_NAME} ${TOCD_NAME} "${TOCD_DESCRIPTION}")
endif()
endfunction()
macro(feature_option _name _description _default)
option(${_name} "${_description}" ${_default})
add_feature_info(${_name} ${_name} "${_description}")
endmacro()
# these options control target creation and thus have to be declared before the add_library() call # these options control target creation and thus have to be declared before the add_library() call
feature_option(BUILD_SHARED_LIBS "build libtorrent as a shared library" ON) feature_option(BUILD_SHARED_LIBS "build libtorrent as a shared library" ON)
feature_option(static_runtime "build libtorrent with static runtime" OFF) feature_option(static_runtime "build libtorrent with static runtime" OFF)
macro(find_public_dependency _name)
find_package(${_name} ${ARGN})
string(TOUPPER "${_name}" _name_uppercased)
if (${_name}_FOUND OR ${_name_uppercased}_FOUND)
# Dependencies to be used below for generating Config.cmake file
# We don't need the 'REQUIRED' argument there
set(_args "${_name}")
list(APPEND _args "${ARGN}")
list(REMOVE_ITEM _args "REQUIRED")
list(REMOVE_ITEM _args "") # just in case
string(REPLACE ";" " " _args "${_args}")
list(APPEND _package_dependencies "${_args}")
endif()
endmacro()
find_public_dependency(Threads REQUIRED) find_public_dependency(Threads REQUIRED)
if(CMAKE_CXX_COMPILER_ID MATCHES Clang) if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
@ -522,12 +494,14 @@ if (NOT BUILD_SHARED_LIBS)
endif() endif()
add_library(torrent-rasterbar add_library(torrent-rasterbar
src/$<JOIN:${sources}, src/> ${sources}
include/libtorrent/$<JOIN:${libtorrent_include_files}, include/libtorrent/> ${libtorrent_include_files}
include/libtorrent/extensions/$<JOIN:${libtorrent_extensions_include_files}, include/libtorrent/extensions/> ${libtorrent_extensions_include_files}
include/libtorrent/aux_/$<JOIN:${libtorrent_aux_include_files}, include/libtorrent/aux_/> ${libtorrent_aux_include_files}
) )
select_cxx_standard(torrent-rasterbar 11)
if (BUILD_SHARED_LIBS) if (BUILD_SHARED_LIBS)
target_compile_definitions(torrent-rasterbar target_compile_definitions(torrent-rasterbar
PRIVATE TORRENT_BUILDING_SHARED PRIVATE TORRENT_BUILDING_SHARED
@ -543,14 +517,6 @@ set_target_properties(torrent-rasterbar
SOVERSION ${SOVERSION} SOVERSION ${SOVERSION}
) )
if (cxx_std_14 IN_LIST CMAKE_CXX_COMPILE_FEATURES)
target_compile_features(torrent-rasterbar PUBLIC cxx_std_14)
message(STATUS "Building in C++14 mode")
else()
target_compile_features(torrent-rasterbar PUBLIC cxx_std_11)
message(STATUS "Building in C++11 mode")
endif()
target_include_directories(torrent-rasterbar PUBLIC target_include_directories(torrent-rasterbar PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
@ -580,8 +546,9 @@ if (WIN32)
debug dbghelp debug dbghelp
) )
add_definitions(-D_WIN32_WINNT=0x0600) # target Windows Vista or later
target_compile_definitions(torrent-rasterbar target_compile_definitions(torrent-rasterbar
PRIVATE _WIN32_WINNT=0x0600 # target Windows Vista or later
PUBLIC WIN32_LEAN_AND_MEAN # prevent winsock1 to be included PUBLIC WIN32_LEAN_AND_MEAN # prevent winsock1 to be included
) )
@ -696,9 +663,9 @@ endif()
if (dht) if (dht)
target_sources(torrent-rasterbar PRIVATE target_sources(torrent-rasterbar PRIVATE
src/kademlia/$<JOIN:${kademlia_sources}, src/kademlia/> ${kademlia_sources}
ed25519/src/$<JOIN:${ed25519_sources}, ed25519/src/> ${ed25519_sources}
include/libtorrent/kademlia/$<JOIN:${libtorrent_kademlia_include_files}, include/libtorrent/kademlia/> ${libtorrent_kademlia_include_files}
src/hasher512 src/hasher512
) )
target_include_directories(torrent-rasterbar PRIVATE ed25519/src) target_include_directories(torrent-rasterbar PRIVATE ed25519/src)
@ -792,16 +759,10 @@ set(ConfigPackageLocation ${CMAKE_INSTALL_LIBDIR}/cmake/LibtorrentRasterbar)
string(REGEX REPLACE "([^;]+)" "find_dependency(\\1)" _find_dependency_calls "${_package_dependencies}") string(REGEX REPLACE "([^;]+)" "find_dependency(\\1)" _find_dependency_calls "${_package_dependencies}")
string(REPLACE ";" "\n" _find_dependency_calls "${_find_dependency_calls}") string(REPLACE ";" "\n" _find_dependency_calls "${_find_dependency_calls}")
if(CMAKE_VERSION VERSION_LESS "3.11.0")
set(_compatibility ExactVersion)
else()
set(_compatibility SameMinorVersion)
endif()
write_basic_package_version_file( write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/LibtorrentRasterbar/LibtorrentRasterbarConfigVersion.cmake" "${CMAKE_CURRENT_BINARY_DIR}/LibtorrentRasterbar/LibtorrentRasterbarConfigVersion.cmake"
VERSION ${libtorrent_VERSION} VERSION ${libtorrent_VERSION}
COMPATIBILITY ${_compatibility} COMPATIBILITY SameMinorVersion
) )
export(EXPORT LibtorrentRasterbarTargets export(EXPORT LibtorrentRasterbarTargets

View File

@ -0,0 +1,82 @@
# Various helper function and macros for building libtorrent
include(FeatureSummary)
# macro for issuing option() and add_feature_info() in a single call.
# Synopsis:
# feature_option(<option_and_feature_name> <option_and_feature_description> <default_option_value>)
macro(feature_option _name _description _default)
option(${_name} "${_description}" ${_default})
add_feature_info(${_name} ${_name} "${_description}")
endmacro()
# function to add a simple build option which controls compile definition(s) for a target.
# Synopsis:
# target_optional_compile_definitions(<target> [FEATURE]
# NAME <name> DESCRIPTION <description> DEFAULT <default_value>
# [ENABLED [enabled_compile_definitions...]]
# [DISABLED [disabled_compile_defnitions...]]
# )
# NAME, DESCRIPTION and DEFAULT are passed to option() call
# if FEATURE is given, they are passed to add_feature_info()
# ENABLED lists compile definitions that will be set on <target> when option is enabled,
# DISABLED lists definitions that will be set otherwise
function(target_optional_compile_definitions _target _scope)
set(options FEATURE)
set(oneValueArgs NAME DESCRIPTION DEFAULT)
set(multiValueArgs ENABLED DISABLED)
cmake_parse_arguments(TOCD ${options} "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
option(${TOCD_NAME} "${TOCD_DESCRIPTION}" ${TOCD_DEFAULT})
if (${${TOCD_NAME}})
target_compile_definitions(${_target} ${_scope} ${TOCD_ENABLED})
else()
target_compile_definitions(${_target} ${_scope} ${TOCD_DISABLED})
endif()
if(${TOCD_FEATURE})
add_feature_info(${TOCD_NAME} ${TOCD_NAME} "${TOCD_DESCRIPTION}")
endif()
endfunction()
# a helper macro that calls find_package() and appends the package (if found) to the
# _package_dependencies list, which can be used later to generate package config file
macro(find_public_dependency _name)
find_package(${_name} ${ARGN})
string(TOUPPER "${_name}" _name_uppercased)
if (${_name}_FOUND OR ${_name_uppercased}_FOUND)
# Dependencies to be used below for generating Config.cmake file
# We don't need the 'REQUIRED' argument there
set(_args "${_name}")
list(APPEND _args "${ARGN}")
list(REMOVE_ITEM _args "REQUIRED")
list(REMOVE_ITEM _args "") # just in case
string(REPLACE ";" " " _args "${_args}")
list(APPEND _package_dependencies "${_args}")
endif()
endmacro()
function(select_cxx_standard _target _minimal_supported_version)
message(STATUS "Compiler default is C++${CMAKE_CXX_STANDARD_DEFAULT}")
# make the CXX_STANDARD property public to ensure it makes it into the pkg-config file
get_target_property(_std ${_target} CXX_STANDARD)
# if it is unset, select the default if it is sufficient or the ${_minimal_supported_version}
if (NOT ${_std})
if (${CMAKE_CXX_STANDARD_DEFAULT} GREATER_EQUAL ${_minimal_supported_version})
set(_std ${CMAKE_CXX_STANDARD_DEFAULT})
else()
set(_std ${_minimal_supported_version})
endif()
else()
if (${_std} LESS ${_minimal_supported_version})
message(FATAL_ERROR "Sorry, C++${_std} is not supported by libtorrent")
endif()
endif()
if (cxx_std_${_std} IN_LIST CMAKE_CXX_COMPILE_FEATURES)
# force this standard
target_compile_features(${_target} PUBLIC cxx_std_${_std})
else()
message(FATAL_ERROR "Requested C++ standard version (${_std}) is not supported by the compiler")
endif()
message(STATUS "Building in C++${_std} mode")
endfunction()