FindPackageHandleStandardArgs_SLIC3R.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. # Modified from the CMake github master,
  2. # required by the bundled FindCURL.cmake
  3. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  4. # file Copyright.txt or https://cmake.org/licensing for details.
  5. #[=======================================================================[.rst:
  6. FindPackageHandleStandardArgs
  7. -----------------------------
  8. This module provides a function intended to be used in :ref:`Find Modules`
  9. implementing :command:`find_package(<PackageName>)` calls. It handles the
  10. ``REQUIRED``, ``QUIET`` and version-related arguments of ``find_package``.
  11. It also sets the ``<PackageName>_FOUND`` variable. The package is
  12. considered found if all variables listed contain valid results, e.g.
  13. valid filepaths.
  14. .. command:: find_package_handle_standard_args
  15. There are two signatures::
  16. find_package_handle_standard_args(<PackageName>
  17. (DEFAULT_MSG|<custom-failure-message>)
  18. <required-var>...
  19. )
  20. find_package_handle_standard_args(<PackageName>
  21. [FOUND_VAR <result-var>]
  22. [REQUIRED_VARS <required-var>...]
  23. [VERSION_VAR <version-var>]
  24. [HANDLE_COMPONENTS]
  25. [CONFIG_MODE]
  26. [FAIL_MESSAGE <custom-failure-message>]
  27. )
  28. The ``<PackageName>_FOUND`` variable will be set to ``TRUE`` if all
  29. the variables ``<required-var>...`` are valid and any optional
  30. constraints are satisfied, and ``FALSE`` otherwise. A success or
  31. failure message may be displayed based on the results and on
  32. whether the ``REQUIRED`` and/or ``QUIET`` option was given to
  33. the :command:`find_package` call.
  34. The options are:
  35. ``(DEFAULT_MSG|<custom-failure-message>)``
  36. In the simple signature this specifies the failure message.
  37. Use ``DEFAULT_MSG`` to ask for a default message to be computed
  38. (recommended). Not valid in the full signature.
  39. ``FOUND_VAR <result-var>``
  40. Obsolete. Specifies either ``<PackageName>_FOUND`` or
  41. ``<PACKAGENAME>_FOUND`` as the result variable. This exists only
  42. for compatibility with older versions of CMake and is now ignored.
  43. Result variables of both names are always set for compatibility.
  44. ``REQUIRED_VARS <required-var>...``
  45. Specify the variables which are required for this package.
  46. These may be named in the generated failure message asking the
  47. user to set the missing variable values. Therefore these should
  48. typically be cache entries such as ``FOO_LIBRARY`` and not output
  49. variables like ``FOO_LIBRARIES``.
  50. ``VERSION_VAR <version-var>``
  51. Specify the name of a variable that holds the version of the package
  52. that has been found. This version will be checked against the
  53. (potentially) specified required version given to the
  54. :command:`find_package` call, including its ``EXACT`` option.
  55. The default messages include information about the required
  56. version and the version which has been actually found, both
  57. if the version is ok or not.
  58. ``HANDLE_COMPONENTS``
  59. Enable handling of package components. In this case, the command
  60. will report which components have been found and which are missing,
  61. and the ``<PackageName>_FOUND`` variable will be set to ``FALSE``
  62. if any of the required components (i.e. not the ones listed after
  63. the ``OPTIONAL_COMPONENTS`` option of :command:`find_package`) are
  64. missing.
  65. ``CONFIG_MODE``
  66. Specify that the calling find module is a wrapper around a
  67. call to ``find_package(<PackageName> NO_MODULE)``. This implies
  68. a ``VERSION_VAR`` value of ``<PackageName>_VERSION``. The command
  69. will automatically check whether the package configuration file
  70. was found.
  71. ``FAIL_MESSAGE <custom-failure-message>``
  72. Specify a custom failure message instead of using the default
  73. generated message. Not recommended.
  74. Example for the simple signature:
  75. .. code-block:: cmake
  76. find_package_handle_standard_args(LibXml2 DEFAULT_MSG
  77. LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
  78. The ``LibXml2`` package is considered to be found if both
  79. ``LIBXML2_LIBRARY`` and ``LIBXML2_INCLUDE_DIR`` are valid.
  80. Then also ``LibXml2_FOUND`` is set to ``TRUE``. If it is not found
  81. and ``REQUIRED`` was used, it fails with a
  82. :command:`message(FATAL_ERROR)`, independent whether ``QUIET`` was
  83. used or not. If it is found, success will be reported, including
  84. the content of the first ``<required-var>``. On repeated CMake runs,
  85. the same message will not be printed again.
  86. Example for the full signature:
  87. .. code-block:: cmake
  88. find_package_handle_standard_args(LibArchive
  89. REQUIRED_VARS LibArchive_LIBRARY LibArchive_INCLUDE_DIR
  90. VERSION_VAR LibArchive_VERSION)
  91. In this case, the ``LibArchive`` package is considered to be found if
  92. both ``LibArchive_LIBRARY`` and ``LibArchive_INCLUDE_DIR`` are valid.
  93. Also the version of ``LibArchive`` will be checked by using the version
  94. contained in ``LibArchive_VERSION``. Since no ``FAIL_MESSAGE`` is given,
  95. the default messages will be printed.
  96. Another example for the full signature:
  97. .. code-block:: cmake
  98. find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4)
  99. find_package_handle_standard_args(Automoc4 CONFIG_MODE)
  100. In this case, a ``FindAutmoc4.cmake`` module wraps a call to
  101. ``find_package(Automoc4 NO_MODULE)`` and adds an additional search
  102. directory for ``automoc4``. Then the call to
  103. ``find_package_handle_standard_args`` produces a proper success/failure
  104. message.
  105. #]=======================================================================]
  106. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageMessage_SLIC3R.cmake)
  107. # internal helper macro
  108. macro(_FPHSA_FAILURE_MESSAGE _msg)
  109. if (${_NAME}_FIND_REQUIRED)
  110. message(FATAL_ERROR "${_msg}")
  111. else ()
  112. if (NOT ${_NAME}_FIND_QUIETLY)
  113. message(STATUS "${_msg}")
  114. endif ()
  115. endif ()
  116. endmacro()
  117. # internal helper macro to generate the failure message when used in CONFIG_MODE:
  118. macro(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
  119. # <PackageName>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
  120. if(${_NAME}_CONFIG)
  121. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing:${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
  122. else()
  123. # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
  124. # List them all in the error message:
  125. if(${_NAME}_CONSIDERED_CONFIGS)
  126. set(configsText "")
  127. list(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
  128. math(EXPR configsCount "${configsCount} - 1")
  129. foreach(currentConfigIndex RANGE ${configsCount})
  130. list(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
  131. list(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
  132. string(APPEND configsText " ${filename} (version ${version})\n")
  133. endforeach()
  134. if (${_NAME}_NOT_FOUND_MESSAGE)
  135. string(APPEND configsText " Reason given by package: ${${_NAME}_NOT_FOUND_MESSAGE}\n")
  136. endif()
  137. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:\n${configsText}")
  138. else()
  139. # Simple case: No Config-file was found at all:
  140. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
  141. endif()
  142. endif()
  143. endmacro()
  144. function(FIND_PACKAGE_HANDLE_STANDARD_ARGS_SLIC3R _NAME _FIRST_ARG)
  145. # Set up the arguments for `cmake_parse_arguments`.
  146. set(options CONFIG_MODE HANDLE_COMPONENTS)
  147. set(oneValueArgs FAIL_MESSAGE VERSION_VAR FOUND_VAR)
  148. set(multiValueArgs REQUIRED_VARS)
  149. # Check whether we are in 'simple' or 'extended' mode:
  150. set(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} )
  151. list(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
  152. if(${INDEX} EQUAL -1)
  153. set(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
  154. set(FPHSA_REQUIRED_VARS ${ARGN})
  155. set(FPHSA_VERSION_VAR)
  156. else()
  157. cmake_parse_arguments(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
  158. if(FPHSA_UNPARSED_ARGUMENTS)
  159. message(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
  160. endif()
  161. if(NOT FPHSA_FAIL_MESSAGE)
  162. set(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
  163. endif()
  164. # In config-mode, we rely on the variable <PackageName>_CONFIG, which is set by find_package()
  165. # when it successfully found the config-file, including version checking:
  166. if(FPHSA_CONFIG_MODE)
  167. list(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
  168. list(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
  169. set(FPHSA_VERSION_VAR ${_NAME}_VERSION)
  170. endif()
  171. if(NOT FPHSA_REQUIRED_VARS)
  172. message(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
  173. endif()
  174. endif()
  175. # now that we collected all arguments, process them
  176. if("x${FPHSA_FAIL_MESSAGE}" STREQUAL "xDEFAULT_MSG")
  177. set(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
  178. endif()
  179. list(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
  180. string(TOUPPER ${_NAME} _NAME_UPPER)
  181. string(TOLOWER ${_NAME} _NAME_LOWER)
  182. if(FPHSA_FOUND_VAR)
  183. if(FPHSA_FOUND_VAR MATCHES "^${_NAME}_FOUND$" OR FPHSA_FOUND_VAR MATCHES "^${_NAME_UPPER}_FOUND$")
  184. set(_FOUND_VAR ${FPHSA_FOUND_VAR})
  185. else()
  186. message(FATAL_ERROR "The argument for FOUND_VAR is \"${FPHSA_FOUND_VAR}\", but only \"${_NAME}_FOUND\" and \"${_NAME_UPPER}_FOUND\" are valid names.")
  187. endif()
  188. else()
  189. set(_FOUND_VAR ${_NAME_UPPER}_FOUND)
  190. endif()
  191. # collect all variables which were not found, so they can be printed, so the
  192. # user knows better what went wrong (#6375)
  193. set(MISSING_VARS "")
  194. set(DETAILS "")
  195. # check if all passed variables are valid
  196. set(FPHSA_FOUND_${_NAME} TRUE)
  197. foreach(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
  198. if(NOT ${_CURRENT_VAR})
  199. set(FPHSA_FOUND_${_NAME} FALSE)
  200. string(APPEND MISSING_VARS " ${_CURRENT_VAR}")
  201. else()
  202. string(APPEND DETAILS "[${${_CURRENT_VAR}}]")
  203. endif()
  204. endforeach()
  205. if(FPHSA_FOUND_${_NAME})
  206. set(${_NAME}_FOUND TRUE)
  207. set(${_NAME_UPPER}_FOUND TRUE)
  208. else()
  209. set(${_NAME}_FOUND FALSE)
  210. set(${_NAME_UPPER}_FOUND FALSE)
  211. endif()
  212. # component handling
  213. unset(FOUND_COMPONENTS_MSG)
  214. unset(MISSING_COMPONENTS_MSG)
  215. if(FPHSA_HANDLE_COMPONENTS)
  216. foreach(comp ${${_NAME}_FIND_COMPONENTS})
  217. if(${_NAME}_${comp}_FOUND)
  218. if(NOT DEFINED FOUND_COMPONENTS_MSG)
  219. set(FOUND_COMPONENTS_MSG "found components: ")
  220. endif()
  221. string(APPEND FOUND_COMPONENTS_MSG " ${comp}")
  222. else()
  223. if(NOT DEFINED MISSING_COMPONENTS_MSG)
  224. set(MISSING_COMPONENTS_MSG "missing components: ")
  225. endif()
  226. string(APPEND MISSING_COMPONENTS_MSG " ${comp}")
  227. if(${_NAME}_FIND_REQUIRED_${comp})
  228. set(${_NAME}_FOUND FALSE)
  229. string(APPEND MISSING_VARS " ${comp}")
  230. endif()
  231. endif()
  232. endforeach()
  233. set(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
  234. string(APPEND DETAILS "[c${COMPONENT_MSG}]")
  235. endif()
  236. # version handling:
  237. set(VERSION_MSG "")
  238. set(VERSION_OK TRUE)
  239. # check with DEFINED here as the requested or found version may be "0"
  240. if (DEFINED ${_NAME}_FIND_VERSION)
  241. if(DEFINED ${FPHSA_VERSION_VAR})
  242. set(_FOUND_VERSION ${${FPHSA_VERSION_VAR}})
  243. if(${_NAME}_FIND_VERSION_EXACT) # exact version required
  244. # count the dots in the version string
  245. string(REGEX REPLACE "[^.]" "" _VERSION_DOTS "${_FOUND_VERSION}")
  246. # add one dot because there is one dot more than there are components
  247. string(LENGTH "${_VERSION_DOTS}." _VERSION_DOTS)
  248. if (_VERSION_DOTS GREATER ${_NAME}_FIND_VERSION_COUNT)
  249. # Because of the C++ implementation of find_package() ${_NAME}_FIND_VERSION_COUNT
  250. # is at most 4 here. Therefore a simple lookup table is used.
  251. if (${_NAME}_FIND_VERSION_COUNT EQUAL 1)
  252. set(_VERSION_REGEX "[^.]*")
  253. elseif (${_NAME}_FIND_VERSION_COUNT EQUAL 2)
  254. set(_VERSION_REGEX "[^.]*\\.[^.]*")
  255. elseif (${_NAME}_FIND_VERSION_COUNT EQUAL 3)
  256. set(_VERSION_REGEX "[^.]*\\.[^.]*\\.[^.]*")
  257. else ()
  258. set(_VERSION_REGEX "[^.]*\\.[^.]*\\.[^.]*\\.[^.]*")
  259. endif ()
  260. string(REGEX REPLACE "^(${_VERSION_REGEX})\\..*" "\\1" _VERSION_HEAD "${_FOUND_VERSION}")
  261. unset(_VERSION_REGEX)
  262. if (NOT ${_NAME}_FIND_VERSION VERSION_EQUAL _VERSION_HEAD)
  263. set(VERSION_MSG "Found unsuitable version \"${_FOUND_VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
  264. set(VERSION_OK FALSE)
  265. else ()
  266. set(VERSION_MSG "(found suitable exact version \"${_FOUND_VERSION}\")")
  267. endif ()
  268. unset(_VERSION_HEAD)
  269. else ()
  270. if (NOT ${_NAME}_FIND_VERSION VERSION_EQUAL _FOUND_VERSION)
  271. set(VERSION_MSG "Found unsuitable version \"${_FOUND_VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
  272. set(VERSION_OK FALSE)
  273. else ()
  274. set(VERSION_MSG "(found suitable exact version \"${_FOUND_VERSION}\")")
  275. endif ()
  276. endif ()
  277. unset(_VERSION_DOTS)
  278. else() # minimum version specified:
  279. if (${_NAME}_FIND_VERSION VERSION_GREATER _FOUND_VERSION)
  280. set(VERSION_MSG "Found unsuitable version \"${_FOUND_VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
  281. set(VERSION_OK FALSE)
  282. else ()
  283. set(VERSION_MSG "(found suitable version \"${_FOUND_VERSION}\", minimum required is \"${${_NAME}_FIND_VERSION}\")")
  284. endif ()
  285. endif()
  286. else()
  287. # if the package was not found, but a version was given, add that to the output:
  288. if(${_NAME}_FIND_VERSION_EXACT)
  289. set(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
  290. else()
  291. set(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
  292. endif()
  293. endif()
  294. else ()
  295. # Check with DEFINED as the found version may be 0.
  296. if(DEFINED ${FPHSA_VERSION_VAR})
  297. set(VERSION_MSG "(found version \"${${FPHSA_VERSION_VAR}}\")")
  298. endif()
  299. endif ()
  300. if(VERSION_OK)
  301. string(APPEND DETAILS "[v${${FPHSA_VERSION_VAR}}(${${_NAME}_FIND_VERSION})]")
  302. else()
  303. set(${_NAME}_FOUND FALSE)
  304. endif()
  305. # print the result:
  306. if (${_NAME}_FOUND)
  307. FIND_PACKAGE_MESSAGE_SLIC3R(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}")
  308. else ()
  309. if(FPHSA_CONFIG_MODE)
  310. _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
  311. else()
  312. if(NOT VERSION_OK)
  313. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
  314. else()
  315. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing:${MISSING_VARS}) ${VERSION_MSG}")
  316. endif()
  317. endif()
  318. endif ()
  319. set(${_NAME}_FOUND ${${_NAME}_FOUND} PARENT_SCOPE)
  320. set(${_NAME_UPPER}_FOUND ${${_NAME}_FOUND} PARENT_SCOPE)
  321. endfunction()