Browse Source

Merge topic 'FindPkgConfig-override'

e0d00b9218 FindPkgConfig: Allow to override variables when calling pkg_get_variable

Acked-by: Kitware Robot <[email protected]>
Merge-request: !8808
Brad King 2 years ago
parent
commit
59f6892ab7

+ 5 - 0
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``.

+ 23 - 2
Modules/FindPkgConfig.cmake

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

+ 19 - 0
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 ()

+ 1 - 0
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)