From 93021adb732106776458caac8be99fa2c8609566 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 13 Apr 2011 14:28:00 -0400 Subject: [PATCH] find_package: Test system package registry when possible Teach the FindPackagTest to try creating the appropriate HKLM system package registry value. If it works then add a test to verify that find_package() reads it as expected. Then delete the value to cleanup. --- Tests/FindPackageTest/CMakeLists.txt | 41 +++++++++++++++++++ .../CMakeTestSystemPackageConfig.cmake | 1 + 2 files changed, 42 insertions(+) create mode 100644 Tests/FindPackageTest/SystemPackage/CMakeTestSystemPackageConfig.cmake diff --git a/Tests/FindPackageTest/CMakeLists.txt b/Tests/FindPackageTest/CMakeLists.txt index d4984d7aa..9ac9eee0d 100644 --- a/Tests/FindPackageTest/CMakeLists.txt +++ b/Tests/FindPackageTest/CMakeLists.txt @@ -37,6 +37,38 @@ FIND_PACKAGE(VersionTestB 1.2) FIND_PACKAGE(VersionTestC 1.2.3) FIND_PACKAGE(VersionTestD 1.2.3.4) +#----------------------------------------------------------------------------- +# Test system package registry if possible. +SET(CMakeTestSystemPackage "") +IF(WIN32 AND NOT CYGWIN) + # Try writing a value to the system package registry. + FILE(TO_NATIVE_PATH "${FindPackageTest_SOURCE_DIR}/SystemPackage" _data) + SET(_key "HKLM\\Software\\Kitware\\CMake\\Packages\\CMakeTestSystemPackage") + SET(_file "${FindPackageTest_BINARY_DIR}/CMakeTestSystemPackage.data") + FILE(WRITE ${_file} "${_data}\n") + EXECUTE_PROCESS( + COMMAND ${CMAKE_COMMAND} -E md5sum ${_file} + OUTPUT_VARIABLE _output ERROR_VARIABLE _error RESULT_VARIABLE _failed + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + STRING(REGEX REPLACE " .*" "" _value "${_output}") + IF(NOT _failed AND _value) + EXECUTE_PROCESS( + COMMAND reg add "${_key}" /v "${_value}" /t REG_SZ /d "${_data}" /f + OUTPUT_VARIABLE _output ERROR_VARIABLE _output RESULT_VARIABLE _failed + ) + ENDIF() + # If the above worked, add the rest of the test and a rule to + # cleanup the value. + IF(NOT _failed) + MESSAGE(STATUS "HKLM is writable: enabling CMakeTestSystemPackage") + SET(CMakeTestSystemPackage_CLEANUP reg delete "${_key}" /v "${_value}" /f) + SET(CMakeTestSystemPackage CMakeTestSystemPackage) + ELSE() + MESSAGE(STATUS "HKLM is readonly: disabling CMakeTestSystemPackage") + ENDIF() +ENDIF() + #----------------------------------------------------------------------------- #SET(CMAKE_FIND_DEBUG_MODE 1) @@ -49,6 +81,7 @@ SET(PACKAGES wibbleA wibbleB RecursiveA RecursiveB RecursiveC EnvA EnvB + ${CMakeTestSystemPackage} ) FOREACH(p ${PACKAGES}) SET(${p}_DIR "" CACHE FILEPATH "Wipe out find results for testing." FORCE) @@ -116,6 +149,13 @@ SET(ENV{EnvA_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/lib/zot-3.1") FIND_PACKAGE(EnvA 3.1 EXACT QUIET NAMES zot) # Should Work FIND_PACKAGE(EnvB 3.1 EXACT QUIET NAMES zot) # Should Fail +# Test system package registry if available. +IF(CMakeTestSystemPackage) + FIND_PACKAGE(CMakeTestSystemPackage) + EXECUTE_PROCESS(COMMAND ${CMakeTestSystemPackage_CLEANUP} + OUTPUT_VARIABLE _output ERROR_VARIABLE _error) +ENDIF() + # Expected locations at which packages should be found. SET(foo_EXPECTED "lib/foo-1.2/foo-config.cmake") SET(Foo_EXPECTED "lib/foo-1.2/CMake/FooConfig.cmake") @@ -145,6 +185,7 @@ SET(RecursiveB_EXPECTED "lib/zot-2.0/zot-config.cmake") SET(RecursiveC_EXPECTED "lib/zot-3.1/zot-config.cmake") SET(EnvA_EXPECTED "lib/zot-3.1/zot-config.cmake") SET(EnvB_MISSING "EnvB_DIR-NOTFOUND") +SET(CMakeTestSystemPackage_EXPECTED "SystemPackage/CMakeTestSystemPackageConfig.cmake") # Check the results. FOREACH(p ${PACKAGES}) diff --git a/Tests/FindPackageTest/SystemPackage/CMakeTestSystemPackageConfig.cmake b/Tests/FindPackageTest/SystemPackage/CMakeTestSystemPackageConfig.cmake new file mode 100644 index 000000000..deffa5729 --- /dev/null +++ b/Tests/FindPackageTest/SystemPackage/CMakeTestSystemPackageConfig.cmake @@ -0,0 +1 @@ +# Test config file.