CMakeFindDependencyMacro.cmake 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CMakeFindDependencyMacro
  5. -------------------------
  6. .. command:: find_dependency
  7. The ``find_dependency()`` macro wraps a :command:`find_package` call for
  8. a package dependency::
  9. find_dependency(<dep> [...])
  10. It is designed to be used in a
  11. :ref:`Package Configuration File <Config File Packages>`
  12. (``<PackageName>Config.cmake``). ``find_dependency`` forwards the correct
  13. parameters for ``QUIET`` and ``REQUIRED`` which were passed to
  14. the original :command:`find_package` call. Any additional arguments
  15. specified are forwarded to :command:`find_package`.
  16. If the dependency could not be found it sets an informative diagnostic
  17. message and calls :command:`return` to end processing of the calling
  18. package configuration file and return to the :command:`find_package`
  19. command that loaded it.
  20. .. note::
  21. The call to :command:`return` makes this macro unsuitable to call
  22. from :ref:`Find Modules`.
  23. #]=======================================================================]
  24. macro(find_dependency dep)
  25. set(cmake_fd_quiet_arg)
  26. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
  27. set(cmake_fd_quiet_arg QUIET)
  28. endif()
  29. set(cmake_fd_required_arg)
  30. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
  31. set(cmake_fd_required_arg REQUIRED)
  32. endif()
  33. get_property(cmake_fd_alreadyTransitive GLOBAL PROPERTY
  34. _CMAKE_${dep}_TRANSITIVE_DEPENDENCY
  35. )
  36. find_package(${dep} ${ARGN}
  37. ${cmake_fd_quiet_arg}
  38. ${cmake_fd_required_arg}
  39. )
  40. if(NOT DEFINED cmake_fd_alreadyTransitive OR cmake_fd_alreadyTransitive)
  41. set_property(GLOBAL PROPERTY _CMAKE_${dep}_TRANSITIVE_DEPENDENCY TRUE)
  42. endif()
  43. if (NOT ${dep}_FOUND)
  44. set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency ${dep} could not be found.")
  45. set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
  46. return()
  47. endif()
  48. set(cmake_fd_required_arg)
  49. set(cmake_fd_quiet_arg)
  50. endmacro()