From e0d00b9218bc458ae6cdd5e7ce514c0ba7b4dec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20No=C3=ABl?= Date: Sun, 17 Sep 2023 18:44:26 +0200 Subject: FindPkgConfig: Allow to override variables when calling pkg_get_variable This is specifically useful when building applications within containers as we sometimes need to redefine the prefix used in a variable. --- Help/release/dev/FindPkgConfig-override.rst | 5 +++++ Modules/FindPkgConfig.cmake | 25 ++++++++++++++++++++-- ...ndPkgConfig_GET_VARIABLE_DEFINE_VARIABLES.cmake | 19 ++++++++++++++++ Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake | 1 + 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 Help/release/dev/FindPkgConfig-override.rst create mode 100644 Tests/RunCMake/FindPkgConfig/FindPkgConfig_GET_VARIABLE_DEFINE_VARIABLES.cmake diff --git a/Help/release/dev/FindPkgConfig-override.rst b/Help/release/dev/FindPkgConfig-override.rst new file mode 100644 index 0000000..3f58065 --- /dev/null +++ b/Help/release/dev/FindPkgConfig-override.rst @@ -0,0 +1,5 @@ +FindPkgConfig-override +---------------------- + +* The :module:`FindPkgConfig` module :command:`pkg_get_variable` function + gained a ``DEFINE_VARIABLES`` option to pass variables to ``pkg-config``. diff --git a/Modules/FindPkgConfig.cmake b/Modules/FindPkgConfig.cmake index 6e8b784..5466e79 100644 --- a/Modules/FindPkgConfig.cmake +++ b/Modules/FindPkgConfig.cmake @@ -923,11 +923,20 @@ endmacro() .. code-block:: cmake - pkg_get_variable( ) + pkg_get_variable( + [DEFINE_VARIABLES =...]) If ``pkg-config`` returns multiple values for the specified variable, ``resultVar`` will contain a :ref:`;-list `. + Options: + + ``DEFINE_VARIABLES =...`` + .. versionadded:: 3.28 + + Specify key-value pairs to redefine variables affecting the variable + retrieved with ``pkg-config``. + For example: .. code-block:: cmake @@ -935,8 +944,20 @@ endmacro() pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir) #]========================================] function (pkg_get_variable result pkg variable) + set(_multiValueArgs DEFINE_VARIABLES) + + CMAKE_PARSE_ARGUMENTS(_parsedArguments "" "" "${_multiValueArgs}" ${ARGN}) + set(defined_variables ) + foreach(_def_var ${_parsedArguments_DEFINE_VARIABLES}) + if(NOT _def_var MATCHES "^.+=.*$") + message(FATAL_ERROR "DEFINE_VARIABLES should contain arguments in the form of key=value") + endif() + + list(APPEND defined_variables "--define-variable=${_def_var}") + endforeach() + _pkg_set_path_internal() - _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}") + _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}" ${defined_variables}) set("${result}" "${prefix_result}" PARENT_SCOPE) diff --git a/Tests/RunCMake/FindPkgConfig/FindPkgConfig_GET_VARIABLE_DEFINE_VARIABLES.cmake b/Tests/RunCMake/FindPkgConfig/FindPkgConfig_GET_VARIABLE_DEFINE_VARIABLES.cmake new file mode 100644 index 0000000..98f3cd6 --- /dev/null +++ b/Tests/RunCMake/FindPkgConfig/FindPkgConfig_GET_VARIABLE_DEFINE_VARIABLES.cmake @@ -0,0 +1,19 @@ +# 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 () + +pkg_get_variable(bletchvar bletch exec_prefix DEFINE_VARIABLES prefix=customprefix) +if (NOT bletchvar STREQUAL "customprefix") + message(FATAL_ERROR "Failed to fetch variable exec_prefix from embedded package bletch with prefix overridden to customprefix") +endif () diff --git a/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake b/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake index 2bfe028..7af425a 100644 --- a/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake +++ b/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake @@ -51,6 +51,7 @@ Libs: -L\${libdir} endif() endif() run_cmake(FindPkgConfig_GET_VARIABLE) + run_cmake(FindPkgConfig_GET_VARIABLE_DEFINE_VARIABLES) run_cmake(FindPkgConfig_GET_VARIABLE_PREFIX_PATH) run_cmake(FindPkgConfig_GET_VARIABLE_PKGCONFIG_PATH) run_cmake(FindPkgConfig_cache_variables) -- cgit v0.12