Generate proper pkg-config file in cmake build

This commit is contained in:
Eugene Shalygin 2018-04-12 22:35:42 +02:00 committed by Arvid Norberg
parent 049e918295
commit 3c6e0b2578
10 changed files with 272 additions and 86 deletions

View File

@ -39,11 +39,13 @@ addons:
apt:
sources:
- ubuntu-toolchain-r-test
- sourceline: 'ppa:dluxen/cmake-backports'
packages:
- libboost1.55-all-dev
- libboost1.55-tools-dev
- python2.7-dev
- g++-5
- cmake3
before_install:

View File

@ -1,10 +1,14 @@
cmake_minimum_required(VERSION 3.3.0 FATAL_ERROR)
cmake_minimum_required(VERSION 3.9.0 FATAL_ERROR)
project(libtorrent)
project(libtorrent
DESCRIPTION "Bittorrent library"
VERSION 1.2.0
)
set (SOVERSION "10")
set (VERSION "1.2.0")
list(APPEND CMAKE_MODULE_PATH ${libtorrent_SOURCE_DIR}/cmake/Modules)
include(GNUInstallDirs)
include(GeneratePkgConfig)
set(libtorrent_include_files
add_torrent_params
@ -539,10 +543,13 @@ if (shared)
PRIVATE TORRENT_BUILDING_SHARED
INTERFACE TORRENT_LINKING_SHARED
)
if(NOT MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden")
endif()
set_target_properties(torrent-rasterbar
PROPERTIES
CXX_VISIBILITY_PRESET "hidden"
VISIBILITY_INLINES_HIDDEN "true"
VERSION ${PROJECT_VERSION}
SOVERSION ${SOVERSION}
)
else()
if(static_runtime)
# fix /MT flag:
@ -568,23 +575,32 @@ else()
endif()
target_compile_features(torrent-rasterbar PUBLIC cxx_std_11)
target_compile_definitions(torrent-rasterbar PUBLIC $<$<CONFIG:Debug>:TORRENT_DEBUG>)
target_compile_definitions(torrent-rasterbar PRIVATE TORRENT_BUILDING_LIBRARY)
target_compile_definitions(torrent-rasterbar
PUBLIC
$<$<CONFIG:Debug>:TORRENT_DEBUG>
$<$<CONFIG:Debug>:TORRENT_USE_ASSERTS>
PRIVATE
TORRENT_BUILDING_LIBRARY
)
target_link_libraries(torrent-rasterbar
PUBLIC
Threads::Threads
)
if (libiconv)
find_package(Iconv REQUIRED)
target_compile_definitions(torrent-rasterbar PUBLIC TORRENT_USE_ICONV)
target_include_directories(torrent-rasterbar SYSTEM PUBLIC ${ICONV_INCLUDE_DIR})
target_include_directories(torrent-rasterbar PUBLIC ${ICONV_INCLUDE_DIR})
target_link_libraries(torrent-rasterbar PRIVATE ${ICONV_LIBRARIES})
endif (libiconv)
if (encryption)
if(NOT DEFINED OPENSSL_INCLUDE_DIR OR NOT DEFINED OPENSSL_LIBRARIES)
FIND_PACKAGE(OpenSSL REQUIRED)
if(NOT TARGET OpenSSL::SSL)
find_package(OpenSSL REQUIRED)
endif()
list(APPEND _package_dependencies "OpenSSL")
target_link_libraries(torrent-rasterbar PUBLIC OpenSSL::SSL)
target_compile_definitions(torrent-rasterbar PUBLIC TORRENT_USE_OPENSSL TORRENT_USE_LIBCRYPTO)
target_include_directories(torrent-rasterbar PUBLIC ${OPENSSL_INCLUDE_DIR})
else()
target_compile_definitions(torrent-rasterbar PUBLIC TORRENT_DISABLE_ENCRYPTION)
endif()
@ -598,45 +614,35 @@ if (NOT dht)
endif()
# Boost
if(NOT DEFINED Boost_INCLUDE_DIR OR NOT DEFINED Boost_LIBRARIES)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system)
endif()
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(torrent-rasterbar ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
find_package(Boost REQUIRED COMPONENTS system)
target_link_libraries(torrent-rasterbar PUBLIC Boost::system)
if (WIN32)
target_link_libraries(torrent-rasterbar wsock32 ws2_32 Iphlpapi)
# target Windows Vista or later
target_compile_definitions(torrent-rasterbar PUBLIC _WIN32_WINNT=0x0600)
target_link_libraries(torrent-rasterbar debug dbghelp)
# prevent winsock1 to be included
target_compile_definitions(torrent-rasterbar PUBLIC WIN32_LEAN_AND_MEAN)
target_link_libraries(torrent-rasterbar
PRIVATE
wsock32 ws2_32 Iphlpapi
debug dbghelp
)
target_compile_definitions(torrent-rasterbar
PUBLIC
_WIN32_WINNT=0x0600 # target Windows Vista or later
WIN32_LEAN_AND_MEAN # prevent winsock1 to be included
)
if (MSVC)
target_compile_definitions(torrent-rasterbar PUBLIC BOOST_ALL_NO_LIB)
# for multicore compilation
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
# increase the number of sections for obj files
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif()
endif()
if(ANDROID)
target_link_libraries(torrent-rasterbar dl)
target_link_libraries(torrent-rasterbar PRIVATE ${CMAKE_DL_LIBS})
endif()
if(APPLE)
# for ip_notifier
target_link_libraries(torrent-rasterbar "-framework CoreFoundation" "-framework SystemConfiguration")
target_link_libraries(torrent-rasterbar PRIVATE "-framework CoreFoundation" "-framework SystemConfiguration")
endif (APPLE)
if (encryption)
target_link_libraries(torrent-rasterbar ${OPENSSL_LIBRARIES})
endif()
if (libiconv)
target_link_libraries(torrent-rasterbar ${ICONV_LIBRARIES})
endif (libiconv)
if (NOT deprecated-functions)
target_compile_definitions(torrent-rasterbar PUBLIC TORRENT_NO_DEPRECATE)
endif()
@ -656,56 +662,44 @@ else()
endif()
if (MSVC)
# disable bogus deprecation warnings on msvc8
target_compile_definitions(torrent-rasterbar PUBLIC _SCL_SECURE_NO_DEPRECATE _CRT_SECURE_NO_DEPRECATE)
# these compiler settings just make the compiler standard conforming
target_compile_options(torrent-rasterbar PUBLIC /Zc:wchar_t /Zc:forScope)
# for multi-core compilation
target_compile_options(torrent-rasterbar PUBLIC /MP)
# increase the number of sections for obj files
target_compile_options(torrent-rasterbar PUBLIC /bigobj)
target_compile_definitions(torrent-rasterbar
PUBLIC
_SCL_SECURE_NO_DEPRECATE _CRT_SECURE_NO_DEPRECATE # disable bogus deprecation warnings on msvc8
)
target_compile_options(torrent-rasterbar
PRIVATE
/Zc:wchar_t /Zc:forScope # these compiler settings just make the compiler standard conforming
/MP # for multi-core compilation
/bigobj # increase the number of sections for obj files
)
endif()
target_compile_definitions(torrent-rasterbar PUBLIC _FILE_OFFSET_BITS=64)
target_compile_definitions(torrent-rasterbar PUBLIC BOOST_EXCEPTION_DISABLE)
target_compile_definitions(torrent-rasterbar PUBLIC BOOST_ASIO_ENABLE_CANCELIO)
target_compile_definitions(torrent-rasterbar PUBLIC BOOST_ASIO_HAS_STD_CHRONO)
target_compile_definitions(torrent-rasterbar
PUBLIC
_FILE_OFFSET_BITS=64
BOOST_EXCEPTION_DISABLE
BOOST_ASIO_ENABLE_CANCELIO
BOOST_ASIO_HAS_STD_CHRONO
)
set_target_properties(torrent-rasterbar PROPERTIES
SOVERSION ${SOVERSION})
get_property (COMPILETIME_OPTIONS_LIST
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIRECTORY}
PROPERTY COMPILE_DEFINITIONS
)
foreach (s ${COMPILETIME_OPTIONS_LIST})
set (COMPILETIME_OPTIONS "${COMPILETIME_OPTIONS} -D${s}")
endforeach (s)
configure_file(libtorrent-rasterbar-cmake.pc.in libtorrent-rasterbar.pc)
generate_and_install_pkg_config_file(torrent-rasterbar)
include(CheckCXXCompilerFlag)
add_subdirectory(bindings)
string (COMPARE EQUAL "${CMAKE_SIZEOF_VOID_P}" "8" IS64BITS)
if (IS64BITS AND RESPECTLIB64)
set (LIBDIR "lib64")
else()
set (LIBDIR "lib")
endif()
install(TARGETS torrent-rasterbar EXPORT torrent-rasterbarTargets
DESTINATION ${LIBDIR} INCLUDES DESTINATION include)
install(DIRECTORY include/libtorrent DESTINATION include)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libtorrent-rasterbar.pc DESTINATION ${LIBDIR}/pkgconfig)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(DIRECTORY include/libtorrent DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h*")
# === generate a CMake Config File ===
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/libtorrent-rasterbar/libtorrent-rasterbarConfigVersion.cmake"
VERSION ${VERSION}
VERSION ${libtorrent_VERSION}
COMPATIBILITY ExactVersion
)
@ -719,7 +713,7 @@ configure_file(libtorrent-rasterbarConfig.cmake
COPYONLY
)
set(ConfigPackageLocation lib/cmake/libtorrent-rasterbar)
set(ConfigPackageLocation ${CMAKE_INSTALL_LIBDIR}/cmake/libtorrent-rasterbar)
install(EXPORT torrent-rasterbarTargets
FILE
libtorrent-rasterbarTargets.cmake

View File

@ -83,7 +83,10 @@ set_target_properties(python-libtorrent
OUTPUT_NAME libtorrent
)
target_include_directories(python-libtorrent PRIVATE ${PYTHON_INCLUDE_DIRS})
target_include_directories(python-libtorrent
PRIVATE
${PYTHON_INCLUDE_DIRS}
)
target_link_libraries(python-libtorrent
PRIVATE

View File

@ -3,7 +3,7 @@ import platform
setup(
name='libtorrent',
version='@VERSION@',
version='@libtorrent_VERSION@',
author='Arvid Norberg',
author_email='arvid@libtorrent.org',
description='Python bindings for libtorrent-rasterbar',

View File

@ -0,0 +1,150 @@
# This module provides generate_and_install_pkg_config_file() function.
# The function takes target name and expects a fully configured project, i.e. with set version and
# description. The function extracts interface libraries, include dirs, definitions and options
# from the target and generates pkg-config file with install() command
# The function expands imported targets and generator expressions
# save the current file dir for later use in the generate_and_install_pkg_config_file() function
set(_GeneratePkGConfigDir "${CMAKE_CURRENT_LIST_DIR}/GeneratePkgConfig")
include(GNUInstallDirs)
function(_get_target_property_merging_configs _var_name _target_name _propert_name)
get_target_property(vals ${_target_name} ${_propert_name})
if (NOT vals)
set(vals "")
endif()
foreach(cfg ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER "${cfg}" UPPERCFG)
get_target_property(mapped_configs ${_target_name} "MAP_IMPORTED_CONFIG_${UPPERCFG}")
if (mapped_configs)
list(GET "${mapped_configs}" 0 target_cfg)
else()
set(target_cfg "${UPPERCFG}")
endif()
get_target_property(val_for_cfg ${_target_name} "${_propert_name}_${target_cfg}")
if (val_for_cfg)
list(APPEND vals "$<$<CONFIG:${cfg}>:${val_for_cfg}>")
endif()
endforeach()
set(${_var_name} "${vals}" PARENT_SCOPE)
endfunction()
# This helper function expands imported targets from the provided targets list, collecting their
# interface link libraries and imported locations, include directories, compile options and definitions
# into the specified variables
function(_expand_targets _targets _libraries_var _include_dirs_var _compile_options_var _compile_definitions_var)
set(_any_target_was_expanded True)
set(_libs "${${_libraries_var}}")
set(_includes "${${_include_dirs_var}}")
set(_defs "${${_compile_definitions_var}}")
set(_options "${${_compile_options_var}}")
list(APPEND _libs "${_targets}")
while(_any_target_was_expanded)
set(_any_target_was_expanded False)
set(_new_libs "")
foreach (_dep ${_libs})
if (TARGET ${_dep})
set(_any_target_was_expanded True)
get_target_property(_type ${_dep} TYPE)
if ("${_type}" STREQUAL "INTERFACE_LIBRARY")
# this library may not have IMPORTED_LOCATION property
set(_imported_location "")
else()
_get_target_property_merging_configs(_imported_location ${_dep} IMPORTED_LOCATION)
endif()
_get_target_property_merging_configs(_iface_link_libraries ${_dep} INTERFACE_LINK_LIBRARIES)
_get_target_property_merging_configs(_iface_include_dirs ${_dep} INTERFACE_INCLUDE_DIRECTORIES)
_get_target_property_merging_configs(_iface_compile_options ${_dep} INTERFACE_COMPILE_OPTIONS)
_get_target_property_merging_configs(_iface_definitions ${_dep} INTERFACE_COMPILE_DEFINITIONS)
if (_imported_location)
list(APPEND _new_libs "${_imported_location}")
endif()
if (_iface_link_libraries)
list(APPEND _new_libs "${_iface_link_libraries}")
endif()
if(_iface_include_dirs)
list(APPEND _includes "${_iface_include_dirs}")
endif()
if(_iface_compile_options)
list(APPEND _options "${_iface_compile_options}")
endif()
if(_iface_definitions)
list(APPEND _defs "${_iface_definitions}")
endif()
list(REMOVE_DUPLICATES _new_libs)
list(REMOVE_DUPLICATES _includes)
# Options order is important, thus leave duplicates as they are, skipping duplicates removal
list(REMOVE_DUPLICATES _defs)
else()
list(APPEND _new_libs "${_dep}")
endif()
endforeach()
set(_libs "${_new_libs}")
endwhile()
set(${_libraries_var} "${_libs}" PARENT_SCOPE)
set(${_include_dirs_var} "${_includes}" PARENT_SCOPE)
set(${_compile_options_var} "${_options}" PARENT_SCOPE)
set(${_compile_definitions_var} "${_defs}" PARENT_SCOPE)
endfunction()
# Generates and installs a pkg-config file for a given target
function(generate_and_install_pkg_config_file _target)
# collect target properties
_expand_targets(${_target}
_interface_link_libraries _interface_include_dirs
_interface_compile_options _interface_definitions)
get_target_property(_output_name ${_target} OUTPUT_NAME)
if (NOT _output_name)
set(_output_name "${_target}")
endif()
# TODO make it robust
set(_output_name "lib${_output_name}")
# remove standard include directories
foreach(d IN LISTS CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES)
list(REMOVE_ITEM _interface_include_dirs "${d}")
endforeach()
set(_generate_target_dir "${CMAKE_CURRENT_BINARY_DIR}/${_target}-pkgconfig")
set(_pkg_config_file_template_filename "${_GeneratePkGConfigDir}/pkg-config.cmake.in")
# put target and project properties into a file
configure_file("${_GeneratePkGConfigDir}/target-compile-settings.cmake.in"
"${_generate_target_dir}/compile-settings.cmake" @ONLY)
get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if (NOT _isMultiConfig)
set(_variables_file_name "${_generate_target_dir}/compile-settings-expanded.cmake")
file(GENERATE OUTPUT "${_variables_file_name}" INPUT "${_generate_target_dir}/compile-settings.cmake")
configure_file("${_GeneratePkGConfigDir}/generate-pkg-config.cmake.in"
"${_generate_target_dir}/generate-pkg-config.cmake" @ONLY)
install(SCRIPT "${_generate_target_dir}/generate-pkg-config.cmake")
else()
foreach(cfg IN LISTS CMAKE_CONFIGURATION_TYPES)
set(_variables_file_name "${_generate_target_dir}/${cfg}/compile-settings-expanded.cmake")
file(GENERATE OUTPUT "${_variables_file_name}" INPUT "${_generate_target_dir}/compile-settings.cmake" CONDITION "$<CONFIG:${cfg}>")
configure_file("${_GeneratePkGConfigDir}/generate-pkg-config.cmake.in"
"${_generate_target_dir}/${cfg}/generate-pkg-config.cmake" @ONLY)
install(SCRIPT "${_generate_target_dir}/${cfg}/generate-pkg-config.cmake")
endforeach()
endif()
endfunction()

View File

@ -0,0 +1,22 @@
include(@_variables_file_name@)
function (cmake_list_to_pkg_config _result _list _prefix)
set(_tmp_list "${_list}")
list(REMOVE_ITEM _tmp_list "")
# remove prefix from prefixed items
string(REGEX REPLACE "(^|;)(${_prefix})" "\\1" _tmp_list "${_tmp_list}")
# append 'prefix' to each element
string(REGEX REPLACE "([^;]+)" "${_prefix}\\1" _tmp_list "${_tmp_list}")
# transform cmake list into a space delimited list
string(REPLACE ";" " " _tmp_list "${_tmp_list}")
set(${_result} "${_tmp_list}" PARENT_SCOPE)
endfunction()
cmake_list_to_pkg_config(_interface_link_libraries "${_TARGET_INTERFACE_LINK_LIBRARIES}" "-l")
cmake_list_to_pkg_config(_interface_definitions "${_TARGET_INTERFACE_DEFINITIONS}" "-D")
cmake_list_to_pkg_config(_interface_include_dirs "${_TARGET_INTERFACE_INCLUDE_DIRS}" "-I")
set(_interface_compile_options "${_TARGET_INTERFACE_COMPILE_OPTIONS}")
string(REPLACE ";" " " _interface_compile_options "${_interface_compile_options}")
configure_file("@_pkg_config_file_template_filename@" "@_generate_target_dir@/@_output_name@.pc" @ONLY)
file(INSTALL "@_generate_target_dir@/@_output_name@.pc" DESTINATION "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/pkgconfig")

View File

@ -0,0 +1,10 @@
prefix=@CMAKE_INSTALL_PREFIX@
libdir=${prefix}/@_INSTALL_LIBDIR@
includedir=${prefix}/@_INSTALL_INCLUDEDIR@
Name: @_PROJECT_NAME@
Description: @_PROJECT_DESCRIPTION@
Version: @_PROJECT_VERSION@
Libs: -L${libdir} -l@_TARGET_OUTPUT_NAME@ @_interface_link_libraries@
Cflags: -I${includedir} @_interface_compile_options@ @_interface_include_dirs@ @_interface_definitions@

View File

@ -0,0 +1,12 @@
set(_TARGET_INTERFACE_LINK_LIBRARIES "@_interface_link_libraries@")
set(_TARGET_INTERFACE_COMPILE_OPTIONS "@_interface_compile_options@")
set(_TARGET_INTERFACE_INCLUDE_DIRS "@_interface_include_dirs@")
set(_TARGET_INTERFACE_DEFINITIONS "@_interface_definitions@")
set(_TARGET_OUTPUT_NAME "@_output_name@")
set(_INSTALL_LIBDIR "@CMAKE_INSTALL_LIBDIR@")
set(_INSTALL_INCLUDEDIR "@CMAKE_INSTALL_INCLUDEDIR@")
set(_PROJECT_NAME "@PROJECT_NAME@")
set(_PROJECT_DESCRIPTION "@PROJECT_DESCRIPTION@")
set(_PROJECT_VERSION "@PROJECT_VERSION@")

View File

@ -552,7 +552,6 @@ AC_CONFIG_FILES(
[bindings/python/compile_flags]
[bindings/python/compile_cmd]
[libtorrent-rasterbar.pc]
[libtorrent-rasterbar-cmake.pc]
)
AC_OUTPUT

View File

@ -1,6 +0,0 @@
Name: libtorrent-rasterbar
Description: Bittorrent library.
Version: @VERSION@
Libs: -L${CMAKE_INSTALL_PREFIX}/lib -ltorrent-rasterbar
Cflags: -I${CMAKE_INSTALL_PREFIX}/include -I${CMAKE_INSTALL_PREFIX}/include/libtorrent @COMPILETIME_OPTIONS@ @CXX_DEFINES@