cmake: strip directories and library prefixes for pkg-config

Replace full library names with -L commands and -llib-name pairs. This
way it resembles what autotools produce and additionally -l can not be
used with GNU linker and full paths (as it prepends "lib" to such
arguments).

Fixes #3424.
This commit is contained in:
Eugene Shalygin 2018-11-11 21:23:19 +01:00 committed by Arvid Norberg
parent a2a10e9bee
commit fccf25e839
3 changed files with 32 additions and 4 deletions

View File

@ -147,9 +147,6 @@ function(generate_and_install_pkg_config_file _target)
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}")

View File

@ -12,7 +12,37 @@ function (cmake_list_to_pkg_config _result _list _prefix)
set(${_result} "${_tmp_list}" PARENT_SCOPE)
endfunction()
cmake_list_to_pkg_config(_interface_link_libraries "${_TARGET_INTERFACE_LINK_LIBRARIES}" "-l")
# Helper function for splitting full library paths into [dir, name] and merging repetitive dir entries
function(split_library_dirs _libraries _base_library_dir _library_dirs_var _library_names_var)
set(libdirs "${_base_library_dir}")
set(libs "")
foreach (l IN LISTS _libraries)
get_filename_component(lDir "${l}" DIRECTORY)
if (lDir)
get_filename_component(lDir "${lDir}" REALPATH)
endif()
get_filename_component(lFile "${l}" NAME_WE)
string(REGEX REPLACE "${_SHARED_LIBRARY_PREFIX}" "" lFile "${lFile}")
list(APPEND libdirs "${lDir}")
list(APPEND libs "${lFile}")
endforeach()
list(REMOVE_DUPLICATES libdirs)
list(REMOVE_AT libdirs 0) # as it is the base libdir and will be handled separately
set(${_library_dirs_var} "${libdirs}" PARENT_SCOPE)
set(${_library_names_var} "${libs}" PARENT_SCOPE)
endfunction()
split_library_dirs("${_TARGET_INTERFACE_LINK_LIBRARIES}" "${CMAKE_INSTALL_PREFIX}/${_INSTALL_LIBDIR}" _lib_dirs _library_names)
cmake_list_to_pkg_config(_libs "${_library_names}" "-l")
list(LENGTH _lib_dirs _additional_libdirs_count)
if (_additional_libdirs_count GREATER 0)
cmake_list_to_pkg_config(_additional_libdirs "${_lib_dirs}" "-L")
set(_interface_link_libraries "${_additional_libdirs} ${_libs}")
else()
set(_interface_link_libraries "${_libs}")
endif()
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}")

View File

@ -6,6 +6,7 @@ set(_TARGET_OUTPUT_NAME "@_output_name@")
set(_INSTALL_LIBDIR "@CMAKE_INSTALL_LIBDIR@")
set(_INSTALL_INCLUDEDIR "@CMAKE_INSTALL_INCLUDEDIR@")
set(_SHARED_LIBRARY_PREFIX "@CMAKE_SHARED_LIBRARY_PREFIX@")
set(_PROJECT_NAME "@PROJECT_NAME@")
set(_PROJECT_DESCRIPTION "@PROJECT_DESCRIPTION@")