FindPackageMessage.cmake 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindPackageMessage
  5. ------------------
  6. This module is intended to be used in
  7. :ref:`FindXXX.cmake modules <Find Modules>` and provides a function for printing
  8. find result messages.
  9. .. command:: find_package_message
  10. .. code-block:: cmake
  11. find_package_message(<PackageName> <message> <details>)
  12. Prints a ``<message>`` once for each unique find result to inform the user
  13. which package was found and where.
  14. ``<PackageName>``
  15. The name of the package (for example, as used in the
  16. ``Find<PackageName>.cmake`` module filename).
  17. ``<message>``
  18. The message string to display.
  19. ``<details>``
  20. A unique identifier for tracking message display. The ``<message>`` is
  21. printed only once per distinct ``<details>`` value. If ``<details>`` string
  22. changes in a subsequent configuration phase, the message will be displayed
  23. again.
  24. If :command:`find_package` was called with the ``QUIET`` option, the
  25. ``<message>`` is not printed.
  26. Examples
  27. ^^^^^^^^
  28. Printing result message in a find module:
  29. .. code-block:: cmake
  30. :caption: FindFoo.cmake
  31. find_library(Foo_LIBRARY foo)
  32. find_path(Foo_INCLUDE_DIR foo.h)
  33. # ...
  34. include(FindPackageMessage)
  35. if(Foo_FOUND)
  36. find_package_message(
  37. Foo
  38. "Found Foo: ${Foo_LIBRARY}"
  39. "[${Foo_LIBRARY}][${Foo_INCLUDE_DIR}]"
  40. )
  41. else()
  42. # ...
  43. endif()
  44. When writing standard :ref:`Find modules <Find Modules>`, use the
  45. :module:`find_package_handle_standard_args() <FindPackageHandleStandardArgs>`
  46. function, which automatically prints the find result message:
  47. .. code-block:: cmake
  48. :caption: FindFoo.cmake
  49. # ...
  50. include(FindPackageHandleStandardArgs)
  51. find_package_handle_standard_args(
  52. Foo
  53. REQUIRED_VARS Foo_LIBRARY Foo_INCLUDE_DIR
  54. )
  55. #]=======================================================================]
  56. function(find_package_message pkg msg details)
  57. # Avoid printing a message repeatedly for the same find result.
  58. if(NOT ${pkg}_FIND_QUIETLY)
  59. string(REPLACE "\n" "" details "${details}")
  60. set(DETAILS_VAR FIND_PACKAGE_MESSAGE_DETAILS_${pkg})
  61. if(NOT "${details}" STREQUAL "${${DETAILS_VAR}}")
  62. # The message has not yet been printed.
  63. string(STRIP "${msg}" msg)
  64. message(STATUS "${msg}")
  65. # Save the find details in the cache to avoid printing the same
  66. # message again.
  67. set("${DETAILS_VAR}" "${details}"
  68. CACHE INTERNAL "Details about finding ${pkg}")
  69. endif()
  70. endif()
  71. endfunction()