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.
This commit is contained in:
Corentin Noël
2023-09-17 18:44:26 +02:00
parent aa6213848d
commit e0d00b9218
4 changed files with 48 additions and 2 deletions

View File

@@ -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``.

View File

@@ -923,11 +923,20 @@ endmacro()
.. code-block:: cmake
pkg_get_variable(<resultVar> <moduleName> <varName>)
pkg_get_variable(<resultVar> <moduleName> <varName>
[DEFINE_VARIABLES <key>=<value>...])
If ``pkg-config`` returns multiple values for the specified variable,
``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
Options:
``DEFINE_VARIABLES <key>=<value>...``
.. 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)

View File

@@ -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 ()

View File

@@ -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)