Browse Source

Export a target for using only headers of a library

Some backend libraries of Syncthing Tray and Reflective RapidJSON only use
certain headers of qtutilities/c++utilities. The current solution did not
really work because it did not distinguish between the build and install
interface and also did not take compile definitions and options into
account.
charconv v5.10.0
Martchus 1 year ago
parent
commit
9fb3bbe179
  1. 5
      cmake/modules/3rdParty.cmake
  2. 15
      cmake/modules/LibraryTarget.cmake
  3. 11
      cmake/templates/Config.cmake.in

5
cmake/modules/3rdParty.cmake

@ -45,7 +45,7 @@ endfunction ()
function (parse_arguments_for_use_functions)
# parse arguments
set(OPTIONAL_ARGS OPTIONAL)
set(OPTIONAL_ARGS OPTIONAL ONLY_HEADERS)
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})
@ -103,6 +103,9 @@ function (parse_arguments_for_use_functions)
set(ARGS_OPTIONAL
"${ARGS_OPTIONAL}"
PARENT_SCOPE)
set(ARGS_ONLY_HEADERS
"${ARGS_ONLY_HEADERS}"
PARENT_SCOPE)
if (NOT ARGS_OPTIONAL)
set(ARGS_FIND_PACKAGE
"REQUIRED"

15
cmake/modules/LibraryTarget.cmake

@ -172,6 +172,18 @@ else ()
if (META_TARGET_NAME MATCHES "lib.*")
set_target_properties(${META_TARGET_NAME} PROPERTIES PREFIX "")
endif ()
# add target for pulling only headers because some libraries contain header-only parts which are useful on their own
if (NOT META_PLUGIN_CATEGORY)
add_library(${META_TARGET_NAME}-headers INTERFACE)
target_include_directories(
${META_TARGET_NAME}-headers INTERFACE $<BUILD_INTERFACE:${TARGET_INCLUDE_DIRECTORY_BUILD_INTERFACE}>
$<INSTALL_INTERFACE:${HEADER_INSTALL_DESTINATION}> ${PUBLIC_INCLUDE_DIRS})
target_compile_definitions(${META_TARGET_NAME}-headers INTERFACE "${META_PUBLIC_COMPILE_DEFINITIONS}"
"${META_PRIVATE_COMPILE_DEFINITIONS}")
target_compile_options(${META_TARGET_NAME}-headers INTERFACE "${META_PUBLIC_COMPILE_OPTIONS}"
"${META_PRIVATE_COMPILE_OPTIONS}")
endif ()
endif ()
# populate META_PUBLIC_LIB_DEPENDS
@ -454,6 +466,9 @@ if (NOT META_NO_INSTALL_TARGETS AND ENABLE_INSTALL_TARGETS)
list(APPEND TARGETS_TO_EXPORT ${BUNDLED_TARGET})
endif ()
endforeach ()
if (NOT META_HEADER_ONLY_LIB AND NOT META_PLUGIN_CATEGORY)
list(APPEND TARGETS_TO_EXPORT "${META_TARGET_NAME}-headers")
endif ()
install(
TARGETS ${TARGETS_TO_EXPORT}
EXPORT "${META_PROJECT_NAME}${META_CONFIG_SUFFIX}Targets"

11
cmake/templates/Config.cmake.in

@ -118,9 +118,18 @@ function(use_@META_PROJECT_VARNAME@)
# link against library (also "link" against header-only libs to get compile defs and interface link libs)
link_directories(${@META_PROJECT_VARNAME_UPPER@_LIB_DIR})
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};${@META_PROJECT_VARNAME_UPPER@_LIB}" PARENT_SCOPE)
if (ARGS_ONLY_HEADERS)
set(TARGET_NAME "${@META_PROJECT_VARNAME_UPPER@_LIB}-headers")
else ()
set(TARGET_NAME "${@META_PROJECT_VARNAME_UPPER@_LIB}")
endif ()
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};${TARGET_NAME}" PARENT_SCOPE)
set("${ARGS_PACKAGES_VARIABLE}" "${${ARGS_PACKAGES_VARIABLE}};${@META_PROJECT_VARNAME_UPPER@_PACKAGE}" PARENT_SCOPE)
if (ARGS_ONLY_HEADERS)
return ()
endif ()
# add required Qt and KF modules
set(IMPORTED_QT_MODULES
${IMPORTED_QT_MODULES}

Loading…
Cancel
Save