diff options
author | Christopher Degawa <ccom@randomderp.com> | 2021-06-11 20:17:39 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2021-06-18 15:10:12 (GMT) |
commit | 35d3e00e4e9b138dec1d1a646766fc342d04b8f5 (patch) | |
tree | a27895e2748c4ab00ce77d30f2dbb6191ac86027 /Modules/FindPkgConfig.cmake | |
parent | 11695f5985e9cbd7a5206908fe6d2ea7e77865e9 (diff) | |
download | CMake-35d3e00e4e9b138dec1d1a646766fc342d04b8f5.zip CMake-35d3e00e4e9b138dec1d1a646766fc342d04b8f5.tar.gz CMake-35d3e00e4e9b138dec1d1a646766fc342d04b8f5.tar.bz2 |
FindPkgConfig: split args if loaded from environment
It's common for some people to use the PKG_CONFIG environment variable
to not only load a custom pkg-config/pkgconf but also to load some
default arguments such as `--static` or `--keep-system-libs` which often
worked since shell scripts would call `$PKG_CONFIG --libs pkg` without
quotes, but this breaks FindPkgConfig since it uses the full string as
`argv[0]` and might try looking for a binary called `pkgconf --static`,
instead of looking for `pkgconf` and adding `--static` as the `argv[1]`
Additionally adds RunCMake.FindPkgConfig ARGN test case
Fixes: #22305
Signed-off-by: Christopher Degawa <ccom@randomderp.com>
Diffstat (limited to 'Modules/FindPkgConfig.cmake')
-rw-r--r-- | Modules/FindPkgConfig.cmake | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/Modules/FindPkgConfig.cmake b/Modules/FindPkgConfig.cmake index 3bc9dba..bd54fd9 100644 --- a/Modules/FindPkgConfig.cmake +++ b/Modules/FindPkgConfig.cmake @@ -15,6 +15,8 @@ following variables will also be set: if pkg-config executable was found ``PKG_CONFIG_EXECUTABLE`` pathname of the pkg-config program +``PKG_CONFIG_ARGN`` + list of arguments to pass to pkg-config ``PKG_CONFIG_VERSION_STRING`` version of pkg-config (since CMake 2.8.8) @@ -29,7 +31,15 @@ set(PKG_CONFIG_VERSION 1) # find pkg-config, use PKG_CONFIG if set if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL "")) - set(PKG_CONFIG_EXECUTABLE "$ENV{PKG_CONFIG}" CACHE FILEPATH "pkg-config executable") + separate_arguments(PKG_CONFIG_FROM_ENV_SPLIT NATIVE_COMMAND PROGRAM SEPARATE_ARGS "$ENV{PKG_CONFIG}") + list(LENGTH PKG_CONFIG_FROM_ENV_SPLIT PKG_CONFIG_FROM_ENV_SPLIT_ARGC) + if(PKG_CONFIG_FROM_ENV_SPLIT_ARGC GREATER 0) + list(GET PKG_CONFIG_FROM_ENV_SPLIT 0 PKG_CONFIG_FROM_ENV_ARGV0) + if(PKG_CONFIG_FROM_ENV_SPLIT_ARGC GREATER 1) + list(SUBLIST PKG_CONFIG_FROM_ENV_SPLIT 1 -1 PKG_CONFIG_ARGN) + endif() + set(PKG_CONFIG_EXECUTABLE "${PKG_CONFIG_FROM_ENV_ARGV0}" CACHE FILEPATH "pkg-config executable") + endif() endif() set(PKG_CONFIG_NAMES "pkg-config") @@ -43,9 +53,12 @@ find_program(PKG_CONFIG_EXECUTABLE DOC "pkg-config executable") mark_as_advanced(PKG_CONFIG_EXECUTABLE) +set(PKG_CONFIG_ARGN "${PKG_CONFIG_ARGN}" CACHE STRING "Arguments to supply to pkg-config") +mark_as_advanced(PKG_CONFIG_ARGN) + set(_PKG_CONFIG_FAILURE_MESSAGE "") if (PKG_CONFIG_EXECUTABLE) - execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --version + execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} --version OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_VARIABLE _PKG_CONFIG_VERSION_ERROR ERROR_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE _PKG_CONFIG_VERSION_RESULT @@ -53,14 +66,18 @@ if (PKG_CONFIG_EXECUTABLE) if (NOT _PKG_CONFIG_VERSION_RESULT EQUAL 0) string(REPLACE "\n" "\n " _PKG_CONFIG_VERSION_ERROR " ${_PKG_CONFIG_VERSION_ERROR}") + if(PKG_CONFIG_ARGN) + string(REPLACE ";" " " PKG_CONFIG_ARGN " ${PKG_CONFIG_ARGN}") + endif() string(APPEND _PKG_CONFIG_FAILURE_MESSAGE "The command\n" - " \"${PKG_CONFIG_EXECUTABLE}\" --version\n" + " \"${PKG_CONFIG_EXECUTABLE}\"${PKG_CONFIG_ARGN} --version\n" " failed with output:\n${PKG_CONFIG_VERSION_STRING}\n" " stderr: \n${_PKG_CONFIG_VERSION_ERROR}\n" " result: \n${_PKG_CONFIG_VERSION_RESULT}" ) set(PKG_CONFIG_EXECUTABLE "") + set(PKG_CONFIG_ARGN "") unset(PKG_CONFIG_VERSION_STRING) endif () unset(_PKG_CONFIG_VERSION_RESULT) @@ -91,7 +108,7 @@ macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp) set(_pkgconfig_invoke_result) execute_process( - COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist} + COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${ARGN} ${_pkglist} OUTPUT_VARIABLE _pkgconfig_invoke_result RESULT_VARIABLE _pkgconfig_failed OUTPUT_STRIP_TRAILING_WHITESPACE) @@ -533,7 +550,7 @@ macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cma # execute the query execute_process( - COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query} + COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${_pkg_check_modules_exist_query} RESULT_VARIABLE _pkgconfig_retval ERROR_VARIABLE _pkgconfig_error ERROR_STRIP_TRAILING_WHITESPACE) @@ -892,6 +909,18 @@ Variables Affecting Behavior .. versionadded:: 3.1 The ``PKG_CONFIG`` environment variable can be used as a hint. +.. variable:: PKG_CONFIG_ARGN + + .. versionadded:: 3.22 + + This can be set to a list of arguments to additionally pass to pkg-config + if needed. If not provided, it will be an empty string, however, if the + environment variable ``PKG_CONFIG`` is provided, this will be set to the + result of splitting the variable. + + The ``PKG_CONFIG`` environment variable can be used to provide both + ``PKG_CONFIG_EXECUTABLE`` and ``PKG_CONFIG_ARGN`` + .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH .. versionadded:: 3.1 |