From 5cfbc763716c1e15ee20a24c503b9b7371a2da28 Mon Sep 17 00:00:00 2001 From: Alex Birch <51103-Birchlabs@users.noreply.gitlab.kitware.com> Date: Fri, 31 Dec 2021 16:48:56 +0000 Subject: FindPkgConfig: Fix parsing of backslash-escaped spaces in pkg-config output Treat backslash-escaped spaces as "space within argument" rather than "space delimiting arguments". Update our `FindPkgConfig_LIBRARY_PATH` test case to escape spaces in the path, and run it unconditionally. --- Modules/FindPkgConfig.cmake | 4 +++- .../FindPkgConfig_LIBRARY_PATH-stdout.txt | 4 ++-- .../FindPkgConfig/FindPkgConfig_LIBRARY_PATH.cmake | 19 ++++++++++++------- Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake | 5 +---- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Modules/FindPkgConfig.cmake b/Modules/FindPkgConfig.cmake index 547bc52..ffcd8b7 100644 --- a/Modules/FindPkgConfig.cmake +++ b/Modules/FindPkgConfig.cmake @@ -143,7 +143,9 @@ macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp) string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}") endif() - separate_arguments(_pkgconfig_invoke_result) + # pkg-config can represent "spaces within an argument" by backslash-escaping the space. + # UNIX_COMMAND mode treats backslash-escaped spaces as "not a space that delimits arguments". + separate_arguments(_pkgconfig_invoke_result UNIX_COMMAND "${_pkgconfig_invoke_result}") #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}") set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result}) diff --git a/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH-stdout.txt b/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH-stdout.txt index 6615d80..539e5ef 100644 --- a/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH-stdout.txt +++ b/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH-stdout.txt @@ -1,3 +1,3 @@ -- ZOT_LIBRARIES='zot' --- ZOT_LINK_LIBRARIES='[^']*/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH-build/zot/lib/prefix-zot-suffix' --- ZOT_LDFLAGS='-L[^']*/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH-build/zot/lib;-lzot' +-- ZOT_LINK_LIBRARIES='[^']*/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH-build/root/lib/prefix-zot-suffix' +-- ZOT_LDFLAGS='-L[^']*/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH-build/root/lib;-lzot' diff --git a/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH.cmake b/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH.cmake index 9f654b5..1278c49 100644 --- a/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH.cmake +++ b/Tests/RunCMake/FindPkgConfig/FindPkgConfig_LIBRARY_PATH.cmake @@ -1,11 +1,16 @@ find_package(PkgConfig REQUIRED) -file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/zot/lib/pkgconfig/zot.pc" " -prefix=${CMAKE_CURRENT_BINARY_DIR}/zot +set(ROOT "${CMAKE_CURRENT_BINARY_DIR}/root") +string(REPLACE " " "\\ " ESCAPED_ROOT "${ROOT}") +set(LIB_DIR "${ROOT}/lib") +set(PKGCONFIG_DIR "${LIB_DIR}/pkgconfig") + +file(WRITE "${PKGCONFIG_DIR}/zot.pc" " +prefix=${ESCAPED_ROOT} libdir=\${prefix}/lib Name: Zot -Description: Dummy packaget to test LIBRARY_DIR support +Description: Dummy package to test LIBRARY_DIR support Version: 1.0 Libs: -L\${libdir} -lzot ") @@ -13,15 +18,15 @@ Libs: -L\${libdir} -lzot # Create a "library" file to find in libdir. set(CMAKE_FIND_LIBRARY_PREFIXES "prefix-") set(CMAKE_FIND_LIBRARY_SUFFIXES "-suffix") -file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/zot/lib/prefix-zot-suffix") +file(WRITE "${LIB_DIR}/prefix-zot-suffix") # 'pkg-config --libs' drops -L flags in PKG_CONFIG_SYSTEM_LIBRARY_PATH by default. -set(ENV{PKG_CONFIG_SYSTEM_LIBRARY_PATH} "${CMAKE_CURRENT_BINARY_DIR}/zot/lib") +set(ENV{PKG_CONFIG_SYSTEM_LIBRARY_PATH} "${LIB_DIR}") # 'pkgconf --libs' also drops -L flags in LIBRARY_PATH by default. -set(ENV{LIBRARY_PATH} "${CMAKE_CURRENT_BINARY_DIR}/zot/lib") +set(ENV{LIBRARY_PATH} "${LIB_DIR}") -set(ENV{PKG_CONFIG_PATH} "${CMAKE_CURRENT_BINARY_DIR}/zot/lib/pkgconfig") +set(ENV{PKG_CONFIG_PATH} "${PKGCONFIG_DIR}") pkg_check_modules(ZOT REQUIRED zot) message(STATUS "ZOT_LIBRARIES='${ZOT_LIBRARIES}'") diff --git a/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake b/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake index f36d1eb..f479dcf 100644 --- a/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake +++ b/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake @@ -32,8 +32,5 @@ if (PKG_CONFIG_FOUND) run_cmake(FindPkgConfig_VERSION_OPERATORS) run_cmake(FindPkgConfig_GET_MATCHING_MODULE_NAME) run_cmake(FindPkgConfig_empty_target) - - if(NOT RunCMake_BINARY_DIR MATCHES " ") - run_cmake(FindPkgConfig_LIBRARY_PATH) - endif() + run_cmake(FindPkgConfig_LIBRARY_PATH) endif () -- cgit v0.12