From b59f36aad8576ca0ab7a7ecac345d6483d818d3e Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 16 Mar 2020 12:25:38 -0400 Subject: FindPkgConfig: Unset results when pkg-config is broken Inspired-by: FUJI Goro --- Modules/FindPkgConfig.cmake | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Modules/FindPkgConfig.cmake b/Modules/FindPkgConfig.cmake index c79f20f..b7e6442 100644 --- a/Modules/FindPkgConfig.cmake +++ b/Modules/FindPkgConfig.cmake @@ -34,16 +34,31 @@ endif() find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable") mark_as_advanced(PKG_CONFIG_EXECUTABLE) +set(_PKG_CONFIG_FAILURE_MESSAGE "") if (PKG_CONFIG_EXECUTABLE) execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --version - OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING - ERROR_QUIET - OUTPUT_STRIP_TRAILING_WHITESPACE) + OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_VARIABLE _PKG_CONFIG_VERSION_ERROR ERROR_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE _PKG_CONFIG_VERSION_RESULT + ) + + if (NOT _PKG_CONFIG_VERSION_RESULT EQUAL 0) + string(REPLACE "\n" "\n " _PKG_CONFIG_VERSION_ERROR " ${_PKG_CONFIG_VERSION_ERROR}") + string(APPEND _PKG_CONFIG_FAILURE_MESSAGE + "The command\n" + " \"${PKG_CONFIG_EXECUTABLE}\" --version\n" + " failed with output\n${_PKG_CONFIG_VERSION_ERROR}" + ) + set(PKG_CONFIG_EXECUTABLE "") + unset(PKG_CONFIG_VERSION_STRING) + endif () + unset(_PKG_CONFIG_VERSION_RESULT) endif () include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) find_package_handle_standard_args(PkgConfig REQUIRED_VARS PKG_CONFIG_EXECUTABLE + REASON_FAILURE_MESSAGE "${_PKG_CONFIG_FAILURE_MESSAGE}" VERSION_VAR PKG_CONFIG_VERSION_STRING) # This is needed because the module name is "PkgConfig" but the name of -- cgit v0.12 From 1c99f5df288dbc8a79a38b20db9ff319eb456fe8 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 17 Mar 2020 08:02:15 -0400 Subject: FindPkgConfig: Add test for specified pkg-config tool missing --- Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist-stdout.txt | 6 ++++++ Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist.cmake | 4 ++++ Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake | 2 ++ 3 files changed, 12 insertions(+) create mode 100644 Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist-stdout.txt create mode 100644 Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist.cmake diff --git a/Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist-stdout.txt b/Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist-stdout.txt new file mode 100644 index 0000000..ef5f7f6 --- /dev/null +++ b/Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist-stdout.txt @@ -0,0 +1,6 @@ +-- Could NOT find PkgConfig \(missing: PKG_CONFIG_EXECUTABLE\) * + Reason given by package: The command + "pkg-config-does-not-exist" --version + failed with output.* +-- PKG_CONFIG_FOUND='FALSE' +-- PKG_CONFIG_EXECUTABLE='' diff --git a/Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist.cmake b/Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist.cmake new file mode 100644 index 0000000..a4fabde --- /dev/null +++ b/Tests/RunCMake/FindPkgConfig/PkgConfigDoesNotExist.cmake @@ -0,0 +1,4 @@ +set(PKG_CONFIG_EXECUTABLE "pkg-config-does-not-exist" CACHE FILEPATH "") +find_package(PkgConfig) +message(STATUS "PKG_CONFIG_FOUND='${PKG_CONFIG_FOUND}'") +message(STATUS "PKG_CONFIG_EXECUTABLE='${PKG_CONFIG_EXECUTABLE}'") diff --git a/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake b/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake index b77bb54..9df1d5b 100644 --- a/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake +++ b/Tests/RunCMake/FindPkgConfig/RunCMakeTest.cmake @@ -1,5 +1,7 @@ include(RunCMake) +run_cmake(PkgConfigDoesNotExist) + run_cmake(FindPkgConfig_NO_PKGCONFIG_PATH) run_cmake(FindPkgConfig_PKGCONFIG_PATH) run_cmake(FindPkgConfig_PKGCONFIG_PATH_NO_CMAKE_PATH) -- cgit v0.12