mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-09 07:11:05 -06:00
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:
5
Help/release/dev/FindPkgConfig-override.rst
Normal file
5
Help/release/dev/FindPkgConfig-override.rst
Normal 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``.
|
||||
@@ -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)
|
||||
|
||||
@@ -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 ()
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user