CMakeFindDependencyMacro.cmake 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #.rst:
  2. # CMakeFindDependencyMacro
  3. # -------------------------
  4. #
  5. # ::
  6. #
  7. # find_dependency(<dep> [<version> [EXACT]])
  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 <steveire@gmail.com>
  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(cmake_fd_version)
  31. if (${ARGC} GREATER 1)
  32. if ("${ARGV1}" STREQUAL "")
  33. message(FATAL_ERROR "Invalid arguments to find_dependency. VERSION is empty")
  34. endif()
  35. if ("${ARGV1}" STREQUAL EXACT)
  36. message(FATAL_ERROR "Invalid arguments to find_dependency. EXACT may only be specified if a VERSION is specified")
  37. endif()
  38. set(cmake_fd_version ${ARGV1})
  39. endif()
  40. set(cmake_fd_exact_arg)
  41. if(${ARGC} GREATER 2)
  42. if (NOT "${ARGV2}" STREQUAL EXACT)
  43. message(FATAL_ERROR "Invalid arguments to find_dependency")
  44. endif()
  45. set(cmake_fd_exact_arg EXACT)
  46. endif()
  47. if(${ARGC} GREATER 3)
  48. message(FATAL_ERROR "Invalid arguments to find_dependency")
  49. endif()
  50. set(cmake_fd_quiet_arg)
  51. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
  52. set(cmake_fd_quiet_arg QUIET)
  53. endif()
  54. set(cmake_fd_required_arg)
  55. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
  56. set(cmake_fd_required_arg REQUIRED)
  57. endif()
  58. get_property(cmake_fd_alreadyTransitive GLOBAL PROPERTY
  59. _CMAKE_${dep}_TRANSITIVE_DEPENDENCY
  60. )
  61. find_package(${dep} ${cmake_fd_version}
  62. ${cmake_fd_exact_arg}
  63. ${cmake_fd_quiet_arg}
  64. ${cmake_fd_required_arg}
  65. )
  66. if(NOT DEFINED cmake_fd_alreadyTransitive OR cmake_fd_alreadyTransitive)
  67. set_property(GLOBAL PROPERTY _CMAKE_${dep}_TRANSITIVE_DEPENDENCY TRUE)
  68. endif()
  69. if (NOT ${dep}_FOUND)
  70. set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency ${dep} could not be found.")
  71. set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
  72. return()
  73. endif()
  74. set(cmake_fd_version)
  75. set(cmake_fd_required_arg)
  76. set(cmake_fd_quiet_arg)
  77. set(cmake_fd_exact_arg)
  78. endif()
  79. endmacro()