Browse Source

Add use_package to 3rdParty

sendfile
Martchus 3 years ago
parent
commit
e120a897c5
  1. 3
      CMakeLists.txt
  2. 38
      cmake/modules/3rdParty.cmake
  3. 10
      cmake/modules/LibraryTarget.cmake
  4. 18
      cmake/templates/Config.cmake.in

3
CMakeLists.txt

@ -140,8 +140,7 @@ if (USE_NATIVE_FILE_BUFFER)
endforeach ()
else ()
message(STATUS "Using boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_sink> for NativeFileStream")
find_package(Boost REQUIRED COMPONENTS iostreams)
use_target(TARGET_NAME Boost::iostreams)
use_package(TARGET_NAME Boost::iostreams PACKAGE_NAME Boost PACKAGE_ARGS "REQUIRED;COMPONENTS;iostreams")
foreach (NATIVE_FILE_STREAM_IMPL_FILE ${NATIVE_FILE_STREAM_IMPL_FILES})
set_property(SOURCE ${NATIVE_FILE_STREAM_IMPL_FILE}
APPEND

38
cmake/modules/3rdParty.cmake

@ -44,8 +44,8 @@ endfunction ()
function (parse_arguments_for_use_functions)
# parse arguments
set(OPTIONAL_ARGS OPTIONAL)
set(ONE_VALUE_ARGS VISIBILITY LIBRARIES_VARIABLE PACKAGES_VARIABLE PKG_CONFIG_MODULES_VARIABLE TARGET_NAME)
set(MULTI_VALUE_ARGS PKG_CONFIG_MODULES)
set(ONE_VALUE_ARGS VISIBILITY LIBRARIES_VARIABLE PACKAGES_VARIABLE PKG_CONFIG_MODULES_VARIABLE TARGET_NAME PACKAGE_NAME)
set(MULTI_VALUE_ARGS PKG_CONFIG_MODULES PACKAGE_ARGS)
cmake_parse_arguments(ARGS "${OPTIONAL_ARGS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN})
# validate values
@ -79,6 +79,8 @@ function (parse_arguments_for_use_functions)
set(ARGS_PACKAGES_VARIABLE "${ARGS_PACKAGES_VARIABLE}" PARENT_SCOPE)
set(ARGS_PKG_CONFIG_MODULES_VARIABLE "${ARGS_PKG_CONFIG_MODULES_VARIABLE}" PARENT_SCOPE)
set(ARGS_TARGET_NAME "${ARGS_TARGET_NAME}" PARENT_SCOPE)
set(ARGS_PACKAGE_NAME "${ARGS_PACKAGE_NAME}" PARENT_SCOPE)
set(ARGS_PACKAGE_ARGS "${ARGS_PACKAGE_ARGS}" PARENT_SCOPE)
set(ARGS_PKG_CONFIG_MODULES "${ARGS_PKG_CONFIG_MODULES}" PARENT_SCOPE)
set(ARGS_OPTIONAL "${ARGS_OPTIONAL}" PARENT_SCOPE)
if (NOT ARGS_OPTIONAL)
@ -126,6 +128,7 @@ function (use_openssl)
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};OpenSSL::SSL;OpenSSL::Crypto" PARENT_SCOPE)
set("${ARGS_PACKAGES_VARIABLE}" "${${ARGS_PACKAGES_VARIABLE}};OpenSSL" PARENT_SCOPE)
if (WIN32 AND OPENSSL_USE_STATIC_LIBS)
# FIXME: preferably use pkg-config to cover this case without hardcoding OpenSSL's dependencies under Windows
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};-lws2_32;-lgdi32;-lcrypt32" PARENT_SCOPE)
endif ()
set("PKG_CONFIG_OpenSSL_SSL" "libssl" PARENT_SCOPE)
@ -170,8 +173,39 @@ function (use_target)
message(FATAL_ERROR "Target \"${ARGS_TARGET_NAME}\" does not exist.")
endif ()
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};${ARGS_TARGET_NAME}" PARENT_SCOPE)
if (ARGS_PACKAGE_NAME)
set("${ARGS_PACKAGES_VARIABLE}" "${${ARGS_PACKAGES_VARIABLE}};${ARGS_PACKAGE_NAME}" PARENT_SCOPE)
if (ARGS_PACKAGE_ARGS)
set("PACKAGE_ARGS_${ARGS_PACKAGE_NAME}" "${ARGS_PACKAGE_ARGS}" PARENT_SCOPE)
endif ()
endif ()
endfunction ()
function (use_package)
parse_arguments_for_use_functions(${ARGN})
if (NOT ARGS_PACKAGE_NAME)
message(FATAL_ERROR "No PACKAGE_NAME specified.")
endif ()
if (NOT ARGS_TARGET_NAME)
message(FATAL_ERROR "No TARGET_NAME specified.")
endif ()
if (NOT ARGS_PACKAGE_ARGS)
set(ARGS_PACKAGE_ARGS ${ARGS_FIND_PACKAGE})
endif ()
find_package("${ARGS_PACKAGE_NAME}" ${ARGS_PACKAGE_ARGS})
if (NOT TARGET "${ARGS_TARGET_NAME}")
if (ARGS_OPTIONAL)
return()
endif ()
message(FATAL_ERROR "Found package \"${ARGS_PACKAGE_NAME}\" but target \"${ARGS_TARGET_NAME}\" does not exist.")
endif ()
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};${ARGS_TARGET_NAME}" PARENT_SCOPE)
set("${ARGS_PACKAGES_VARIABLE}" "${${ARGS_PACKAGES_VARIABLE}};${ARGS_PACKAGE_NAME}" PARENT_SCOPE)
set("PACKAGE_ARGS_${ARGS_PACKAGE_NAME}" "${ARGS_PACKAGE_ARGS}" PARENT_SCOPE)
endfunction()
function (use_pkg_config_module)
# parse and validate arguments
parse_arguments_for_use_functions(${ARGN})

10
cmake/modules/LibraryTarget.cmake

@ -201,6 +201,16 @@ if (META_HEADER_ONLY_LIB)
"${AUTOGEN_DEPS}")
endif ()
# generate CMake code to configure additional arguments for required CMake-packages
set(ADDITIONAL_ARGUMENTS_FOR_REQUIRED_CMAKE_PACKAGES)
foreach (INTERFACE_REQUIRED_PACKAGE ${INTERFACE_REQUIRED_PACKAGES})
if (PACKAGE_ARGS_${INTERFACE_REQUIRED_PACKAGE})
set(ADDITIONAL_ARGUMENTS_FOR_REQUIRED_CMAKE_PACKAGES
"${ADDITIONAL_ARGUMENTS_FOR_REQUIRED_CMAKE_PACKAGES}set(${META_PROJECT_VARNAME_UPPER}_FIND_PACKAGE_ARGS_${INTERFACE_REQUIRED_PACKAGE} \"${PACKAGE_ARGS_${INTERFACE_REQUIRED_PACKAGE}}\")\n"
)
endif ()
endforeach ()
# generate CMake code to configure CMake-target to pkg-config module mapping
set(TARGET_TO_PKG_CONFIG_MODULE_NAME_MAPPING
"set(PKG_CONFIG_${META_TARGET_NAME} \"${META_PROJECT_NAME}${META_CONFIG_SUFFIX}\")")

18
cmake/templates/Config.cmake.in

@ -33,6 +33,8 @@ set(@META_PROJECT_VARNAME_UPPER@_QT_RESOURCES @QT_RESOURCES@)
# define mapping from CMake targets to pkg-config module names
@TARGET_TO_PKG_CONFIG_MODULE_NAME_MAPPING@
# define additional find_package arguments for required CMake-packages
@ADDITIONAL_ARGUMENTS_FOR_REQUIRED_CMAKE_PACKAGES@
# define library config, add imported target
set(@META_PROJECT_VARNAME_UPPER@_PACKAGE "@META_PROJECT_NAME@@META_CONFIG_SUFFIX@")
set(@META_PROJECT_VARNAME_UPPER@_LIB "@META_TARGET_NAME@")
@ -62,9 +64,21 @@ if(NOT TARGET "${@META_PROJECT_VARNAME_UPPER@_LIB}")
foreach (_REQUIRED_PACKAGE ${@META_PROJECT_VARNAME_UPPER@_REQUIRED_PACKAGES})
string(REGEX MATCH _REQUIRED_PACKAGE_MATCH "(.*)-([^-]*)" "${_REQUIRED_PACKAGE}")
if (_REQUIRED_PACKAGE_MATCH)
find_package("${CMAKE_MATCH_1}" "${CMAKE_MATCH_2}" REQUIRED)
set(_REQUIRED_PACKAGE "${CMAKE_MATCH_1}")
set(_REQUIRED_VERSION "${CMAKE_MATCH_2}")
else ()
find_package("${_REQUIRED_PACKAGE}" REQUIRED)
unset(_REQUIRED_VERSION)
endif ()
string(REPLACE "::" "_" _REQUIRED_PACKAGE_VARNAME "${_REQUIRED_PACKAGE}")
if (@META_PROJECT_VARNAME_UPPER@_FIND_PACKAGE_ARGS_${_REQUIRED_PACKAGE_VARNAME})
set(_FIND_PACKAGE_ARGS ${@META_PROJECT_VARNAME_UPPER@_FIND_PACKAGE_ARGS_${_REQUIRED_PACKAGE_VARNAME}})
else ()
set(_FIND_PACKAGE_ARGS REQUIRED)
endif ()
if (_REQUIRED_VERSION)
find_package("${_REQUIRED_PACKAGE}" "${_REQUIRED_VERSION}" ${_FIND_PACKAGE_ARGS})
else ()
find_package("${_REQUIRED_PACKAGE}" ${_FIND_PACKAGE_ARGS})
endif()
endforeach()

Loading…
Cancel
Save