# 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 _property_name) get_property(prop_set TARGET ${_target_name} PROPERTY ${_property_name} SET) if (prop_set) get_property(vals TARGET ${_target_name} PROPERTY ${_property_name}) else() if (CMAKE_BUILD_TYPE) list(APPEND configs ${CMAKE_BUILD_TYPE}) elseif(CMAKE_CONFIGURATION_TYPES) list(APPEND configs ${CMAKE_CONFIGURATION_TYPES}) endif() foreach(cfg ${configs}) string(TOUPPER "${cfg}" UPPERCFG) get_property(mapped_configs TARGET ${_target_name} PROPERTY "MAP_IMPORTED_CONFIG_${UPPERCFG}") if (mapped_configs) list(GET "${mapped_configs}" 0 target_cfg) else() set(target_cfg "${UPPERCFG}") endif() get_property(prop_set TARGET ${_target_name} PROPERTY ${_property_name}_${target_cfg} SET) if (prop_set) get_property(val_for_cfg TARGET ${_target_name} PROPERTY ${_property_name}_${target_cfg}) list(APPEND vals "$<$:${val_for_cfg}>") break() endif() endforeach() if (NOT prop_set) get_property(imported_cfgs TARGET ${_target_name} PROPERTY IMPORTED_CONFIGURATIONS) # CMake docs say we can use any of the imported configs list(GET imported_cfgs 0 imported_config) get_property(vals TARGET ${_target_name} PROPERTY ${_property_name}_${imported_config}) # remove config generator expression. Only in this case! Notice we use such expression # ourselves in the loop above string(REPLACE "$<$:" "$<1:" vals "${vals}") endif() endif() # HACK for static libraries cmake populates link dependencies as $. # pkg-config does not support special handling for static libraries and as such we will remove # that generator expression string(REPLACE "$" "" _interface_compile_options "${_interface_compile_options}") endif() # 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" ${_target_arg}) 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 "$" ${_target_arg}) 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()