FindPkgConfig: Fix parsing of quoted lists with pkgconf <1.5.1

Unquote pkg-config output if they are printed within quotes. pkgconf
<1.5.1 and classic pkg-config <0.29.1 prints quoted variables without
unquoting them, this breaks returning variables with multiple values
as a list behavior of CMake.

Add a new test case for pkg_get_variable with multiple values to test
list behavior and backslash escaped spaces within variable values.

Fixes: #25904
This commit is contained in:
Kerem Aksu
2024-04-20 20:31:50 +03:00
parent 70413d2c35
commit 9ddef32ba0
4 changed files with 46 additions and 1 deletions

View File

@@ -158,6 +158,17 @@ macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
endif()
# pkg-config <0.29.1 and pkgconf <1.5.1 prints quoted variables without unquoting
# unquote only if quotes are first and last characters
if((PKG_CONFIG_VERSION_STRING VERSION_LESS 0.29.1) OR
(PKG_CONFIG_VERSION_STRING VERSION_GREATER_EQUAL 1.0 AND PKG_CONFIG_VERSION_STRING VERSION_LESS 1.5.1))
if (_pkgconfig_invoke_result MATCHES "^\"(.*)\"$")
set(_pkgconfig_invoke_result "${CMAKE_MATCH_1}")
elseif(_pkgconfig_invoke_result MATCHES "^'(.*)'$")
set(_pkgconfig_invoke_result "${CMAKE_MATCH_1}")
endif()
endif()
# 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}")

View File

@@ -0,0 +1,31 @@
# Prepare environment and variables
set(PKG_CONFIG_USE_CMAKE_PREFIX_PATH TRUE)
if(WIN32)
set(ENV{CMAKE_PREFIX_PATH} "${CMAKE_CURRENT_SOURCE_DIR}\\pc-bletch")
else()
set(ENV{CMAKE_PREFIX_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/pc-bletch")
endif()
find_package(PkgConfig REQUIRED)
pkg_check_modules(BLETCH QUIET bletch)
if (NOT BLETCH_FOUND)
message(FATAL_ERROR "Failed to find embedded package bletch via CMAKE_PREFIX_PATH")
endif ()
set(expected_value "item1;item2;item3;item with spaces")
pkg_get_variable(bletchvar1 bletch multiple_values1)
pkg_get_variable(bletchvar2 bletch multiple_values2)
string(FIND "${bletchvar1}" ";" IS_VARIABLE_A_LIST1)
string(FIND "${bletchvar2}" ";" IS_VARIABLE_A_LIST2)
if (IS_VARIABLE_A_LIST1 EQUAL -1 OR IS_VARIABLE_A_LIST2 EQUAL -1)
message(FATAL_ERROR "Failed to fetch variable multiple_values from embedded package bletch as a list")
endif()
if (NOT (bletchvar1 STREQUAL expected_value AND bletchvar2 STREQUAL expected_value))
message(NOTICE "multiple_values1=${bletchvar1} and expected_value=${expected_value}")
message(NOTICE "multiple_values2=${bletchvar2} and expected_value=${expected_value}")
message(FATAL_ERROR "Failed to fetch variable multiple_values from embedded package bletch with escaped spaces")
endif()

View File

@@ -54,6 +54,7 @@ Libs: -L\${libdir}
run_cmake(FindPkgConfig_GET_VARIABLE_DEFINE_VARIABLES)
run_cmake(FindPkgConfig_GET_VARIABLE_PREFIX_PATH)
run_cmake(FindPkgConfig_GET_VARIABLE_PKGCONFIG_PATH)
run_cmake(FindPkgConfig_GET_VARIABLE_MULTIPLE_VALUES)
run_cmake(FindPkgConfig_cache_variables)
run_cmake(FindPkgConfig_IMPORTED_TARGET)
run_cmake(FindPkgConfig_VERSION_OPERATORS)

View File

@@ -4,9 +4,11 @@ libdir=${exec_prefix}/lib
includedir=${prefix}/include
jackpot=bletch-lives
multiple_values1="item1 item2 item3 item\ with\ spaces"
multiple_values2='item1 item2 item3 item\ with\ spaces'
Name: Bletch
Description: Dummy packaget to test variable support
Description: Dummy package to test variable support
Version: 1.0
Libs: -L${libdir} -lbletch
Cflags: -Dbletch_version=1