Tests/FindPkgConfig: correctly handle broken pkg-config

Older versions of pkg-config (e.g. 0.21 and 0.22) do not
handle correctly spaces in paths specified in .pc files.
It breaks RunCMake.FindPkgConfig test, if CMake is built
inside path containing spaces. In this case, we check if
we're inside such path, and if pkg-config is broken; and
if both are true, test is to be skipped.
This commit is contained in:
makise-homura
2022-07-01 19:22:59 +03:00
parent f41b2b25a6
commit bcdac84961
@@ -24,6 +24,27 @@ endif()
# We need a real pkg-config to run the test for get_variable.
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
string(FIND "${CMAKE_CURRENT_BINARY_DIR}" " " IS_SPACES_IN_PATH)
if(IS_SPACES_IN_PATH GREATER -1)
string(REPLACE " " "\\ " ESCAPED_ROOT "${CMAKE_CURRENT_BINARY_DIR}")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/test_spaces.pc" "
libdir=${ESCAPED_ROOT}
Name: test_spaces.pc
Version: 0.0
Description: test spaces
Libs: -L\${libdir}
")
set(PKG_CONFIG_PATH_SAVED "$ENV{PKG_CONFIG_PATH}")
set(ENV{PKG_CONFIG_PATH} "${CMAKE_CURRENT_BINARY_DIR}")
execute_process(COMMAND "${PKG_CONFIG_EXECUTABLE}" --libs test_spaces
ERROR_QUIET COMMAND_ERROR_IS_FATAL ANY
OUTPUT_VARIABLE test_spaces_LIBS)
set(ENV{PKG_CONFIG_PATH} "${PKG_CONFIG_PATH_SAVED}")
string(STRIP "${test_spaces_LIBS}" test_spaces_LIBS_STRIPPED)
if(NOT "${test_spaces_LIBS_STRIPPED}" STREQUAL "-L${ESCAPED_ROOT}")
set(PKG_CONFIG_DONT_SUPPORT_SPACES_IN_PATH TRUE)
endif()
endif()
run_cmake(FindPkgConfig_GET_VARIABLE)
run_cmake(FindPkgConfig_GET_VARIABLE_PREFIX_PATH)
run_cmake(FindPkgConfig_GET_VARIABLE_PKGCONFIG_PATH)
@@ -32,5 +53,7 @@ if (PKG_CONFIG_FOUND)
run_cmake(FindPkgConfig_VERSION_OPERATORS)
run_cmake(FindPkgConfig_GET_MATCHING_MODULE_NAME)
run_cmake(FindPkgConfig_empty_target)
run_cmake(FindPkgConfig_LIBRARY_PATH)
if(NOT PKG_CONFIG_DONT_SUPPORT_SPACES_IN_PATH)
run_cmake(FindPkgConfig_LIBRARY_PATH)
endif()
endif ()