Browse Source

Merge topic 'pkg-get-variable-prefix-path'

f401a57160 pkg_get_variable now sets PKG_CONFIG_PATH like pkg_check_modules
4d76941924 FindPkgConfig: hoist PKG_CONFIG_PATH ops out into _pkg_set_path_internal
78f23de70c More tests for pkg_get_variable

Acked-by: Kitware Robot <[email protected]>
Merge-request: !3131
Brad King 6 years ago
parent
commit
28ef639d2b

+ 128 - 109
Modules/FindPkgConfig.cmake

@@ -84,26 +84,8 @@ macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
   endif()
 endmacro()
 
-#[========================================[.rst:
-.. command:: pkg_get_variable
-
-  Retrieves the value of a pkg-config variable ``varName`` and stores it in the
-  result variable ``resultVar`` in the calling scope.
-
-  .. code-block:: cmake
-
-    pkg_get_variable(<resultVar> <moduleName> <varName>)
-
-  If ``pkg-config`` returns multiple values for the specified variable,
-  ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
-
-  For example:
-
-  .. code-block:: cmake
-
-    pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
-#]========================================]
-function (pkg_get_variable result pkg variable)
+# Internal version of pkg_get_variable; expects PKG_CONFIG_PATH to already be set
+function (_pkg_get_variable result pkg variable)
   _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
   set("${result}"
     "${prefix_result}"
@@ -278,6 +260,102 @@ macro(_pkg_recalculate _prefix _no_cmake_path _no_cmake_environment_path _imp_ta
   endif()
 endmacro()
 
+###
+macro(_pkg_set_path_internal)
+  set(_extra_paths)
+
+  if(NOT _no_cmake_path)
+    _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
+    _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
+    _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
+  endif()
+
+  if(NOT _no_cmake_environment_path)
+    _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
+    _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
+    _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
+  endif()
+
+  if(NOT _extra_paths STREQUAL "")
+    # Save the PKG_CONFIG_PATH environment variable, and add paths
+    # from the CMAKE_PREFIX_PATH variables
+    set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
+    set(_pkgconfig_path "${_pkgconfig_path_old}")
+    if(NOT _pkgconfig_path STREQUAL "")
+      file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
+    endif()
+
+    # Create a list of the possible pkgconfig subfolder (depending on
+    # the system
+    set(_lib_dirs)
+    if(NOT DEFINED CMAKE_SYSTEM_NAME
+        OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
+            AND NOT CMAKE_CROSSCOMPILING))
+      if(EXISTS "/etc/debian_version") # is this a debian system ?
+        if(CMAKE_LIBRARY_ARCHITECTURE)
+          list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
+        endif()
+      else()
+        # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
+        get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
+        if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
+          list(APPEND _lib_dirs "lib32/pkgconfig")
+        endif()
+        get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
+        if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
+          list(APPEND _lib_dirs "lib64/pkgconfig")
+        endif()
+        get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
+        if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
+          list(APPEND _lib_dirs "libx32/pkgconfig")
+        endif()
+      endif()
+    endif()
+    if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
+      list(APPEND _lib_dirs "libdata/pkgconfig")
+    endif()
+    list(APPEND _lib_dirs "lib/pkgconfig")
+    list(APPEND _lib_dirs "share/pkgconfig")
+
+    # Check if directories exist and eventually append them to the
+    # pkgconfig path list
+    foreach(_prefix_dir ${_extra_paths})
+      foreach(_lib_dir ${_lib_dirs})
+        if(EXISTS "${_prefix_dir}/${_lib_dir}")
+          list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
+          list(REMOVE_DUPLICATES _pkgconfig_path)
+        endif()
+      endforeach()
+    endforeach()
+
+    # Prepare and set the environment variable
+    if(NOT _pkgconfig_path STREQUAL "")
+      # remove empty values from the list
+      list(REMOVE_ITEM _pkgconfig_path "")
+      file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
+      if(UNIX)
+        string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
+        string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
+      endif()
+      set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
+    endif()
+
+    # Unset variables
+    unset(_lib_dirs)
+    unset(_pkgconfig_path)
+  endif()
+endmacro()
+
+macro(_pkg_restore_path_internal)
+  if(NOT _extra_paths STREQUAL "")
+    # Restore the environment variable
+    set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
+  endif()
+
+  unset(_extra_paths)
+  unset(_pkgconfig_path_old)
+endmacro()
+
 ###
 macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global _prefix)
   _pkgconfig_unset(${_prefix}_FOUND)
@@ -318,88 +396,7 @@ macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cma
     set(_pkg_check_modules_packages)
     set(_pkg_check_modules_failed)
 
-    set(_extra_paths)
-
-    if(NOT _no_cmake_path)
-      _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
-      _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
-      _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
-    endif()
-
-    if(NOT _no_cmake_environment_path)
-      _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
-      _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
-      _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
-    endif()
-
-    if(NOT "${_extra_paths}" STREQUAL "")
-      # Save the PKG_CONFIG_PATH environment variable, and add paths
-      # from the CMAKE_PREFIX_PATH variables
-      set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
-      set(_pkgconfig_path "${_pkgconfig_path_old}")
-      if(NOT "${_pkgconfig_path}" STREQUAL "")
-        file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
-      endif()
-
-      # Create a list of the possible pkgconfig subfolder (depending on
-      # the system
-      set(_lib_dirs)
-      if(NOT DEFINED CMAKE_SYSTEM_NAME
-          OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
-              AND NOT CMAKE_CROSSCOMPILING))
-        if(EXISTS "/etc/debian_version") # is this a debian system ?
-          if(CMAKE_LIBRARY_ARCHITECTURE)
-            list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
-          endif()
-        else()
-          # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
-          get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
-          if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
-            list(APPEND _lib_dirs "lib32/pkgconfig")
-          endif()
-          get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
-          if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
-            list(APPEND _lib_dirs "lib64/pkgconfig")
-          endif()
-          get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
-          if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
-            list(APPEND _lib_dirs "libx32/pkgconfig")
-          endif()
-        endif()
-      endif()
-      if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
-        list(APPEND _lib_dirs "libdata/pkgconfig")
-      endif()
-      list(APPEND _lib_dirs "lib/pkgconfig")
-      list(APPEND _lib_dirs "share/pkgconfig")
-
-      # Check if directories exist and eventually append them to the
-      # pkgconfig path list
-      foreach(_prefix_dir ${_extra_paths})
-        foreach(_lib_dir ${_lib_dirs})
-          if(EXISTS "${_prefix_dir}/${_lib_dir}")
-            list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
-            list(REMOVE_DUPLICATES _pkgconfig_path)
-          endif()
-        endforeach()
-      endforeach()
-
-      # Prepare and set the environment variable
-      if(NOT "${_pkgconfig_path}" STREQUAL "")
-        # remove empty values from the list
-        list(REMOVE_ITEM _pkgconfig_path "")
-        file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
-        if(UNIX)
-          string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
-          string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
-        endif()
-        set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
-      endif()
-
-      # Unset variables
-      unset(_lib_dirs)
-      unset(_pkgconfig_path)
-    endif()
+    _pkg_set_path_internal()
 
     # iterate through module list and check whether they exist and match the required version
     foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
@@ -502,13 +499,7 @@ macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cma
       _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
     endif()
 
-    if(NOT "${_extra_paths}" STREQUAL "")
-      # Restore the environment variable
-      set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
-    endif()
-
-    unset(_extra_paths)
-    unset(_pkgconfig_path_old)
+    _pkg_restore_path_internal()
   else()
     if (${_is_required})
       message(SEND_ERROR "pkg-config tool not found")
@@ -712,6 +703,34 @@ macro(pkg_search_module _prefix _module0)
   endif()
 endmacro()
 
+#[========================================[.rst:
+.. command:: pkg_get_variable
+
+  Retrieves the value of a pkg-config variable ``varName`` and stores it in the
+  result variable ``resultVar`` in the calling scope.
+
+  .. code-block:: cmake
+
+    pkg_get_variable(<resultVar> <moduleName> <varName>)
+
+  If ``pkg-config`` returns multiple values for the specified variable,
+  ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
+
+  For example:
+
+  .. code-block:: cmake
+
+    pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
+#]========================================]
+function (pkg_get_variable result pkg variable)
+  _pkg_set_path_internal()
+  _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
+  set("${result}"
+    "${prefix_result}"
+    PARENT_SCOPE)
+  _pkg_restore_path_internal()
+endfunction ()
+
 
 #[========================================[.rst:
 Variables Affecting Behavior

+ 21 - 0
Tests/RunCMake/FindPkgConfig/FindPkgConfig_GET_VARIABLE_PKGCONFIG_PATH.cmake

@@ -0,0 +1,21 @@
+# Prepare environment and variables
+set(PKG_CONFIG_USE_CMAKE_PREFIX_PATH FALSE)
+if(WIN32)
+    set(ENV{CMAKE_PREFIX_PATH} "C:\\baz")
+    set(ENV{PKG_CONFIG_PATH} "${CMAKE_CURRENT_SOURCE_DIR}\\pc-bletch\\lib\\pkgconfig;X:\\this\\directory\\should\\not\\exist\\in\\the\\filesystem")
+else()
+    set(ENV{CMAKE_PREFIX_PATH} "/baz")
+    set(ENV{PKG_CONFIG_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/pc-bletch/lib/pkgconfig:/this/directory/should/not/exist/in/the/filesystem")
+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 PKG_CONFIG_PATH")
+endif ()
+
+pkg_get_variable(bletchvar bletch jackpot)
+if (NOT bletchvar STREQUAL "bletch-lives")
+  message(FATAL_ERROR "Failed to fetch variable jackpot from embedded package bletch via PKG_CONFIG_PATH")
+endif ()

+ 21 - 0
Tests/RunCMake/FindPkgConfig/FindPkgConfig_GET_VARIABLE_PREFIX_PATH.cmake

@@ -0,0 +1,21 @@
+# 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;X:\\this\\directory\\should\\not\\exist\\in\\the\\filesystem")
+    set(ENV{PKG_CONFIG_PATH} "C:\\baz")
+else()
+    set(ENV{CMAKE_PREFIX_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/pc-bletch:/this/directory/should/not/exist/in/the/filesystem")
+    set(ENV{PKG_CONFIG_PATH} "/baz")
+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 jackpot)
+if (NOT bletchvar STREQUAL "bletch-lives")
+  message(FATAL_ERROR "Failed to fetch variable jackpot from embedded package bletch via CMAKE_PREFIX_PATH")
+endif ()

+ 2 - 0
Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake

@@ -14,6 +14,8 @@ endif()
 find_package(PkgConfig)
 if (PKG_CONFIG_FOUND)
   run_cmake(FindPkgConfig_GET_VARIABLE)
+  run_cmake(FindPkgConfig_GET_VARIABLE_PREFIX_PATH)
+  run_cmake(FindPkgConfig_GET_VARIABLE_PKGCONFIG_PATH)
   run_cmake(FindPkgConfig_cache_variables)
   run_cmake(FindPkgConfig_IMPORTED_TARGET)
   run_cmake(FindPkgConfig_VERSION_OPERATORS)

+ 12 - 0
Tests/RunCMake/FindPkgConfig/pc-bletch/lib/pkgconfig/bletch.pc

@@ -0,0 +1,12 @@
+prefix=/opt/bletch
+exec_prefix=${prefix}
+libdir=${exec_prefix}/lib
+includedir=${prefix}/include
+
+jackpot=bletch-lives
+
+Name: Bletch
+Description: Dummy packaget to test variable support
+Version: 1.0
+Libs: -L${libdir} -lbletch
+Cflags: -Dbletch_version=1