CMakeFindDependencyMacro.cmake 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #.rst:
  2. # CMakeFindDependencyMacro
  3. # -------------------------
  4. #
  5. # ::
  6. #
  7. # find_dependency(<dep> [<version>])
  8. #
  9. #
  10. # ``find_dependency()`` wraps a :command:`find_package` call for a package
  11. # dependency. It is designed to be used in a <package>Config.cmake file, and it
  12. # forwards the correct parameters for EXACT, QUIET and REQUIRED which were
  13. # passed to the original :command:`find_package` call. It also sets an
  14. # informative diagnostic message if the dependency could not be found.
  15. #
  16. #=============================================================================
  17. # Copyright 2013 Stephen Kelly <[email protected]>
  18. #
  19. # Distributed under the OSI-approved BSD License (the "License");
  20. # see accompanying file Copyright.txt for details.
  21. #
  22. # This software is distributed WITHOUT ANY WARRANTY; without even the
  23. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. # See the License for more information.
  25. #=============================================================================
  26. # (To distribute this file outside of CMake, substitute the full
  27. # License text for the above reference.)
  28. macro(find_dependency dep)
  29. if (NOT ${dep}_FOUND)
  30. set(version ${ARGV1})
  31. set(exact_arg)
  32. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_VERSION_EXACT)
  33. set(exact_arg EXACT)
  34. endif()
  35. set(quiet_arg)
  36. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
  37. set(quiet_arg QUIET)
  38. endif()
  39. set(required_arg)
  40. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
  41. set(required_arg REQUIRED)
  42. endif()
  43. get_property(alreadyTransitive GLOBAL PROPERTY
  44. _CMAKE_${dep}_TRANSITIVE_DEPENDENCY
  45. )
  46. find_package(${dep} ${version} ${exact_arg} ${quiet_arg} ${required_arg})
  47. if(NOT DEFINED alreadyTransitive OR alreadyTransitive)
  48. set_property(GLOBAL PROPERTY _CMAKE_${dep}_TRANSITIVE_DEPENDENCY TRUE)
  49. endif()
  50. if (NOT ${dep}_FOUND)
  51. set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency ${dep} could not be found.")
  52. set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
  53. return()
  54. endif()
  55. set(required_arg)
  56. set(quiet_arg)
  57. set(exact_arg)
  58. endif()
  59. endmacro()