From 7bb1abe56a05ccc50ff2c56e2df60f305af17ca4 Mon Sep 17 00:00:00 2001 From: Alex Neundorf Date: Wed, 20 Feb 2013 19:35:11 +0100 Subject: [PATCH 1/3] FPHSA: Add FOUND_VAR option to specify _FOUND variable name In the new mode FPHSA now accepts a FOUND_VAR option, which can be set either to ExactCase_FOUND or UPPERCASE_FOUND, no other values are accepted. Also add tests for that, including failure. Alex --- Modules/FindPackageHandleStandardArgs.cmake | 47 +++++++++++++------ Tests/CMakeLists.txt | 3 ++ Tests/FPHSA_InvalidFOUND_VAR/CMakeLists.txt | 5 ++ .../FindBadFoundVar.cmake | 6 +++ Tests/FindPackageTest/CMakeLists.txt | 10 ++++ Tests/FindPackageTest/FindSomePackage.cmake | 6 +++ .../FindUpperCasePackage.cmake | 6 +++ 7 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 Tests/FPHSA_InvalidFOUND_VAR/CMakeLists.txt create mode 100644 Tests/FPHSA_InvalidFOUND_VAR/FindBadFoundVar.cmake create mode 100644 Tests/FindPackageTest/FindSomePackage.cmake create mode 100644 Tests/FindPackageTest/FindUpperCasePackage.cmake diff --git a/Modules/FindPackageHandleStandardArgs.cmake b/Modules/FindPackageHandleStandardArgs.cmake index e89e9a97d..ce89c059f 100644 --- a/Modules/FindPackageHandleStandardArgs.cmake +++ b/Modules/FindPackageHandleStandardArgs.cmake @@ -2,7 +2,7 @@ # # This function is intended to be used in FindXXX.cmake modules files. # It handles the REQUIRED, QUIET and version-related arguments to find_package(). -# It also sets the _FOUND variable. +# It also sets the _FOUND variable. # The package is considered found if all variables ... listed contain # valid results, e.g. valid filepaths. # @@ -18,14 +18,21 @@ # for the failure case. This is not recommended. # # The second mode is more powerful and also supports version checking: -# FIND_PACKAGE_HANDLE_STANDARD_ARGS(NAME [REQUIRED_VARS ...] +# FIND_PACKAGE_HANDLE_STANDARD_ARGS(NAME [FOUND_VAR ] +# [REQUIRED_VARS ...] # [VERSION_VAR ] # [HANDLE_COMPONENTS] # [CONFIG_MODE] # [FAIL_MESSAGE "Custom failure message"] ) # -# As above, if through are all valid, _FOUND -# will be set to TRUE. +# In this mode, the name of the result-variable can be set either to either +# _FOUND or _FOUND using the FOUND_VAR option. +# Other names for the result-variable are not allowed. +# So for a Find-module named FindFooBar.cmake, the two possible names are +# FooBar_FOUND and FOOBAR_FOUND. It is recommended to use the original case version. +# +# As in the simple mode, if through are all valid, +# _FOUND will be set to TRUE. # After REQUIRED_VARS the variables which are required for this package are listed. # Following VERSION_VAR the name of the variable can be specified which holds # the version of the package which has been found. If this is done, this version @@ -35,7 +42,7 @@ # which has been actually found, both if the version is ok or not. # If the package supports components, use the HANDLE_COMPONENTS option to enable # handling them. In this case, find_package_handle_standard_args() will report -# which components have been found and which are missing, and the _FOUND +# which components have been found and which are missing, and the _FOUND # variable will be set to FALSE if any of the required components (i.e. not the # ones listed after OPTIONAL_COMPONENTS) are missing. # Use the option CONFIG_MODE if your FindXXX.cmake module is a wrapper for @@ -137,8 +144,8 @@ function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG) # set up the arguments for CMAKE_PARSE_ARGUMENTS and check whether we are in # new extended or in the "old" mode: - set(options CONFIG_MODE HANDLE_COMPONENTS) - set(oneValueArgs FAIL_MESSAGE VERSION_VAR) + set(options CONFIG_MODE HANDLE_COMPONENTS) + set(oneValueArgs FAIL_MESSAGE VERSION_VAR FOUND_VAR) set(multiValueArgs REQUIRED_VARS) set(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} ) list(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX) @@ -183,22 +190,32 @@ function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG) string(TOUPPER ${_NAME} _NAME_UPPER) string(TOLOWER ${_NAME} _NAME_LOWER) + if(FPHSA_FOUND_VAR) + if("${FPHSA_FOUND_VAR}" MATCHES "^${_NAME}_FOUND$" OR "${FPHSA_FOUND_VAR}" MATCHES "^${_NAME_UPPER}_FOUND$") + set(_FOUND_VAR ${FPHSA_FOUND_VAR}) + else() + message(FATAL_ERROR "The argument for FOUND_VAR is \"${FPHSA_FOUND_VAR}\", but only \"${_NAME}_FOUND\" and \"${_NAME_UPPER}_FOUND\" are valid names.") + endif() + else() + set(_FOUND_VAR ${_NAME_UPPER}_FOUND) + endif() + # collect all variables which were not found, so they can be printed, so the # user knows better what went wrong (#6375) set(MISSING_VARS "") set(DETAILS "") # check if all passed variables are valid - unset(${_NAME_UPPER}_FOUND) + unset(${_FOUND_VAR}) foreach(_CURRENT_VAR ${FPHSA_REQUIRED_VARS}) if(NOT ${_CURRENT_VAR}) - set(${_NAME_UPPER}_FOUND FALSE) + set(${_FOUND_VAR} FALSE) set(MISSING_VARS "${MISSING_VARS} ${_CURRENT_VAR}") else() set(DETAILS "${DETAILS}[${${_CURRENT_VAR}}]") endif() endforeach() - if(NOT "${${_NAME_UPPER}_FOUND}" STREQUAL "FALSE") - set(${_NAME_UPPER}_FOUND TRUE) + if(NOT "${${_FOUND_VAR}}" STREQUAL "FALSE") + set(${_FOUND_VAR} TRUE) endif() # component handling @@ -222,7 +239,7 @@ function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG) set(MISSING_COMPONENTS_MSG "${MISSING_COMPONENTS_MSG} ${comp}") if(${_NAME}_FIND_REQUIRED_${comp}) - set(${_NAME_UPPER}_FOUND FALSE) + set(${_FOUND_VAR} FALSE) set(MISSING_VARS "${MISSING_VARS} ${comp}") endif() @@ -276,12 +293,12 @@ function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG) if(VERSION_OK) set(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]") else() - set(${_NAME_UPPER}_FOUND FALSE) + set(${_FOUND_VAR} FALSE) endif() # print the result: - if (${_NAME_UPPER}_FOUND) + if (${_FOUND_VAR}) FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}") else () @@ -297,6 +314,6 @@ function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG) endif () - set(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE) + set(${_FOUND_VAR} ${${_FOUND_VAR}} PARENT_SCOPE) endfunction() diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index f8e4afd62..27252024b 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -147,6 +147,9 @@ if(BUILD_TESTING) # add a bunch of standard build-and-test style tests ADD_TEST_MACRO(CommandLineTest CommandLineTest) ADD_TEST_MACRO(FindPackageTest FindPackageTest) + ADD_TEST_MACRO(FPHSA_InvalidFOUND_VAR FPHSA_InvalidFOUND_VAR) + set_tests_properties(FPHSA_InvalidFOUND_VAR PROPERTIES + PASS_REGULAR_EXPRESSION "The argument for FOUND_VAR is .badfoundvar_FOUND., but only") ADD_TEST_MACRO(FindModulesExecuteAll FindModulesExecuteAll) ADD_TEST_MACRO(StringFileTest StringFileTest) ADD_TEST_MACRO(TryCompile TryCompile) diff --git a/Tests/FPHSA_InvalidFOUND_VAR/CMakeLists.txt b/Tests/FPHSA_InvalidFOUND_VAR/CMakeLists.txt new file mode 100644 index 000000000..6a88cf5ab --- /dev/null +++ b/Tests/FPHSA_InvalidFOUND_VAR/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 2.8) + +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") + +find_package(BadFoundVar REQUIRED) diff --git a/Tests/FPHSA_InvalidFOUND_VAR/FindBadFoundVar.cmake b/Tests/FPHSA_InvalidFOUND_VAR/FindBadFoundVar.cmake new file mode 100644 index 000000000..152df5cd8 --- /dev/null +++ b/Tests/FPHSA_InvalidFOUND_VAR/FindBadFoundVar.cmake @@ -0,0 +1,6 @@ +set(BFV_FOO TRUE) + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(BadFoundVar REQUIRED_VARS BFV_FOO + FOUND_VAR badfoundvar_FOUND ) diff --git a/Tests/FindPackageTest/CMakeLists.txt b/Tests/FindPackageTest/CMakeLists.txt index bca149bea..a77713f27 100644 --- a/Tests/FindPackageTest/CMakeLists.txt +++ b/Tests/FindPackageTest/CMakeLists.txt @@ -43,6 +43,16 @@ if(NOT LOTSOFCOMPONENTS_FOUND) message(SEND_ERROR "LotsOfComponents not found !") endif() +find_package(SomePackage) +if(NOT SomePackage_FOUND) + message(SEND_ERROR "SomePackage with FOUND_VAR SomePackage_FOUND not found !") +endif() + +find_package(UpperCasePackage) +if(NOT UPPERCASEPACKAGE_FOUND) + message(SEND_ERROR "UpperCasePackage with FOUND_VAR UPPERCASEPACKAGE_FOUND not found !") +endif() + #----------------------------------------------------------------------------- # Test system package registry if possible. set(CMakeTestSystemPackage "") diff --git a/Tests/FindPackageTest/FindSomePackage.cmake b/Tests/FindPackageTest/FindSomePackage.cmake new file mode 100644 index 000000000..83d1d0e4d --- /dev/null +++ b/Tests/FindPackageTest/FindSomePackage.cmake @@ -0,0 +1,6 @@ +set(SOP_FOO TRUE) + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(SomePackage REQUIRED_VARS SOP_FOO + FOUND_VAR SomePackage_FOUND ) diff --git a/Tests/FindPackageTest/FindUpperCasePackage.cmake b/Tests/FindPackageTest/FindUpperCasePackage.cmake new file mode 100644 index 000000000..66c2fea5b --- /dev/null +++ b/Tests/FindPackageTest/FindUpperCasePackage.cmake @@ -0,0 +1,6 @@ +set(UCP_FOO TRUE) + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(UpperCasePackage REQUIRED_VARS UCP_FOO + FOUND_VAR UPPERCASEPACKAGE_FOUND ) From c1f5780e2d25015ba22dd0a7c8d43c925a62a7f6 Mon Sep 17 00:00:00 2001 From: Alex Neundorf Date: Thu, 21 Feb 2013 21:49:29 +0100 Subject: [PATCH 2/3] FPHSA: improve documentation Alex --- Modules/FindPackageHandleStandardArgs.cmake | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Modules/FindPackageHandleStandardArgs.cmake b/Modules/FindPackageHandleStandardArgs.cmake index ce89c059f..5f92a8642 100644 --- a/Modules/FindPackageHandleStandardArgs.cmake +++ b/Modules/FindPackageHandleStandardArgs.cmake @@ -30,6 +30,7 @@ # Other names for the result-variable are not allowed. # So for a Find-module named FindFooBar.cmake, the two possible names are # FooBar_FOUND and FOOBAR_FOUND. It is recommended to use the original case version. +# If the FOUND_VAR option is not used, the default is _FOUND. # # As in the simple mode, if through are all valid, # _FOUND will be set to TRUE. @@ -54,7 +55,7 @@ # # Example for mode 1: # -# FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2 DEFAULT_MSG LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR) +# find_package_handle_standard_args(LibXml2 DEFAULT_MSG LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR) # # LibXml2 is considered to be found, if both LIBXML2_LIBRARY and # LIBXML2_INCLUDE_DIR are valid. Then also LIBXML2_FOUND is set to TRUE. @@ -65,20 +66,23 @@ # # Example for mode 2: # -# FIND_PACKAGE_HANDLE_STANDARD_ARGS(BISON REQUIRED_VARS BISON_EXECUTABLE -# VERSION_VAR BISON_VERSION) -# In this case, BISON is considered to be found if the variable(s) listed -# after REQUIRED_VAR are all valid, i.e. BISON_EXECUTABLE in this case. -# Also the version of BISON will be checked by using the version contained -# in BISON_VERSION. +# find_package_handle_standard_args(LibXslt FOUND_VAR LibXslt_FOUND +# REQUIRED_VARS LibXslt_LIBRARIES LibXslt_INCLUDE_DIRS +# VERSION_VAR LibXslt_VERSION_STRING) +# In this case, LibXslt is considered to be found if the variable(s) listed +# after REQUIRED_VAR are all valid, i.e. LibXslt_LIBRARIES and LibXslt_INCLUDE_DIRS +# in this case. The result will then be stored in LibXslt_FOUND . +# Also the version of LibXslt will be checked by using the version contained +# in LibXslt_VERSION_STRING. # Since no FAIL_MESSAGE is given, the default messages will be printed. # # Another example for mode 2: # # find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4) -# FIND_PACKAGE_HANDLE_STANDARD_ARGS(Automoc4 CONFIG_MODE) +# find_package_handle_standard_args(Automoc4 CONFIG_MODE) # In this case, FindAutmoc4.cmake wraps a call to find_package(Automoc4 NO_MODULE) # and adds an additional search directory for automoc4. +# Here the result will be stored in AUTOMOC4_FOUND. # The following FIND_PACKAGE_HANDLE_STANDARD_ARGS() call produces a proper # success/error message. From f17711c546304a5e62a06796e0124c64284bfb9a Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 22 Feb 2013 08:44:13 -0500 Subject: [PATCH 3/3] FPHSA: Convert FOUND_VAR failure test to RunCMake Move the Tests/FPHSA_InvalidFOUND_VAR test case over to a new RunCMake.FPHSA test with a BadFoundVar case. The RunCMake tests are built to cover failure cases easily and robustly. --- Tests/CMakeLists.txt | 3 --- Tests/RunCMake/CMakeLists.txt | 1 + Tests/RunCMake/FPHSA/BadFoundVar-result.txt | 1 + Tests/RunCMake/FPHSA/BadFoundVar-stderr.txt | 7 +++++++ .../CMakeLists.txt => RunCMake/FPHSA/BadFoundVar.cmake} | 2 -- Tests/RunCMake/FPHSA/CMakeLists.txt | 3 +++ .../FPHSA}/FindBadFoundVar.cmake | 0 Tests/RunCMake/FPHSA/RunCMakeTest.cmake | 3 +++ 8 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 Tests/RunCMake/FPHSA/BadFoundVar-result.txt create mode 100644 Tests/RunCMake/FPHSA/BadFoundVar-stderr.txt rename Tests/{FPHSA_InvalidFOUND_VAR/CMakeLists.txt => RunCMake/FPHSA/BadFoundVar.cmake} (70%) create mode 100644 Tests/RunCMake/FPHSA/CMakeLists.txt rename Tests/{FPHSA_InvalidFOUND_VAR => RunCMake/FPHSA}/FindBadFoundVar.cmake (100%) create mode 100644 Tests/RunCMake/FPHSA/RunCMakeTest.cmake diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 27252024b..f8e4afd62 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -147,9 +147,6 @@ if(BUILD_TESTING) # add a bunch of standard build-and-test style tests ADD_TEST_MACRO(CommandLineTest CommandLineTest) ADD_TEST_MACRO(FindPackageTest FindPackageTest) - ADD_TEST_MACRO(FPHSA_InvalidFOUND_VAR FPHSA_InvalidFOUND_VAR) - set_tests_properties(FPHSA_InvalidFOUND_VAR PROPERTIES - PASS_REGULAR_EXPRESSION "The argument for FOUND_VAR is .badfoundvar_FOUND., but only") ADD_TEST_MACRO(FindModulesExecuteAll FindModulesExecuteAll) ADD_TEST_MACRO(StringFileTest StringFileTest) ADD_TEST_MACRO(TryCompile TryCompile) diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index c55bb3aad..dba772d88 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -56,6 +56,7 @@ if(UNIX AND "${CMAKE_TEST_GENERATOR}" MATCHES "Unix Makefiles") add_RunCMake_test(CompilerChange) endif() add_RunCMake_test(ExternalData) +add_RunCMake_test(FPHSA) add_RunCMake_test(GeneratorExpression) add_RunCMake_test(GeneratorToolset) add_RunCMake_test(TargetPropertyGeneratorExpressions) diff --git a/Tests/RunCMake/FPHSA/BadFoundVar-result.txt b/Tests/RunCMake/FPHSA/BadFoundVar-result.txt new file mode 100644 index 000000000..d00491fd7 --- /dev/null +++ b/Tests/RunCMake/FPHSA/BadFoundVar-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/FPHSA/BadFoundVar-stderr.txt b/Tests/RunCMake/FPHSA/BadFoundVar-stderr.txt new file mode 100644 index 000000000..4c739d89b --- /dev/null +++ b/Tests/RunCMake/FPHSA/BadFoundVar-stderr.txt @@ -0,0 +1,7 @@ +CMake Error at .*/Modules/FindPackageHandleStandardArgs.cmake:[0-9]+ \(message\): + The argument for FOUND_VAR is "badfoundvar_FOUND", but only + "BadFoundVar_FOUND" and "BADFOUNDVAR_FOUND" are valid names. +Call Stack \(most recent call first\): + FindBadFoundVar.cmake:5 \(find_package_handle_standard_args\) + BadFoundVar.cmake:3 \(find_package\) + CMakeLists.txt:3 \(include\) diff --git a/Tests/FPHSA_InvalidFOUND_VAR/CMakeLists.txt b/Tests/RunCMake/FPHSA/BadFoundVar.cmake similarity index 70% rename from Tests/FPHSA_InvalidFOUND_VAR/CMakeLists.txt rename to Tests/RunCMake/FPHSA/BadFoundVar.cmake index 6a88cf5ab..07d4322e4 100644 --- a/Tests/FPHSA_InvalidFOUND_VAR/CMakeLists.txt +++ b/Tests/RunCMake/FPHSA/BadFoundVar.cmake @@ -1,5 +1,3 @@ -cmake_minimum_required(VERSION 2.8) - set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}") find_package(BadFoundVar REQUIRED) diff --git a/Tests/RunCMake/FPHSA/CMakeLists.txt b/Tests/RunCMake/FPHSA/CMakeLists.txt new file mode 100644 index 000000000..e8db6b05b --- /dev/null +++ b/Tests/RunCMake/FPHSA/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 2.8) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/FPHSA_InvalidFOUND_VAR/FindBadFoundVar.cmake b/Tests/RunCMake/FPHSA/FindBadFoundVar.cmake similarity index 100% rename from Tests/FPHSA_InvalidFOUND_VAR/FindBadFoundVar.cmake rename to Tests/RunCMake/FPHSA/FindBadFoundVar.cmake diff --git a/Tests/RunCMake/FPHSA/RunCMakeTest.cmake b/Tests/RunCMake/FPHSA/RunCMakeTest.cmake new file mode 100644 index 000000000..0d48fa973 --- /dev/null +++ b/Tests/RunCMake/FPHSA/RunCMakeTest.cmake @@ -0,0 +1,3 @@ +include(RunCMake) + +run_cmake(BadFoundVar)