CMakeFindDependencyMacro.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # CMake - Cross Platform Makefile Generator
  20. # Copyright 2000-2014 Kitware, Inc.
  21. # Copyright 2000-2011 Insight Software Consortium
  22. # All rights reserved.
  23. #
  24. # Redistribution and use in source and binary forms, with or without
  25. # modification, are permitted provided that the following conditions
  26. # are met:
  27. #
  28. # * Redistributions of source code must retain the above copyright
  29. # notice, this list of conditions and the following disclaimer.
  30. #
  31. # * Redistributions in binary form must reproduce the above copyright
  32. # notice, this list of conditions and the following disclaimer in the
  33. # documentation and/or other materials provided with the distribution.
  34. #
  35. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  36. # nor the names of their contributors may be used to endorse or promote
  37. # products derived from this software without specific prior written
  38. # permission.
  39. #
  40. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  41. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  42. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  43. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  44. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  46. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  47. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  48. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  50. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. #
  52. #=============================================================================
  53. macro(find_dependency dep)
  54. if (NOT ${dep}_FOUND)
  55. set(cmake_fd_version)
  56. if (${ARGC} GREATER 1)
  57. if ("${ARGV1}" STREQUAL "")
  58. message(FATAL_ERROR "Invalid arguments to find_dependency. VERSION is empty")
  59. endif()
  60. if ("${ARGV1}" STREQUAL EXACT)
  61. message(FATAL_ERROR "Invalid arguments to find_dependency. EXACT may only be specified if a VERSION is specified")
  62. endif()
  63. set(cmake_fd_version ${ARGV1})
  64. endif()
  65. set(cmake_fd_exact_arg)
  66. if(${ARGC} GREATER 2)
  67. if (NOT "${ARGV2}" STREQUAL EXACT)
  68. message(FATAL_ERROR "Invalid arguments to find_dependency")
  69. endif()
  70. set(cmake_fd_exact_arg EXACT)
  71. endif()
  72. if(${ARGC} GREATER 3)
  73. message(FATAL_ERROR "Invalid arguments to find_dependency")
  74. endif()
  75. set(cmake_fd_quiet_arg)
  76. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
  77. set(cmake_fd_quiet_arg QUIET)
  78. endif()
  79. set(cmake_fd_required_arg)
  80. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
  81. set(cmake_fd_required_arg REQUIRED)
  82. endif()
  83. get_property(cmake_fd_alreadyTransitive GLOBAL PROPERTY
  84. _CMAKE_${dep}_TRANSITIVE_DEPENDENCY
  85. )
  86. find_package(${dep} ${cmake_fd_version}
  87. ${cmake_fd_exact_arg}
  88. ${cmake_fd_quiet_arg}
  89. ${cmake_fd_required_arg}
  90. )
  91. if(NOT DEFINED cmake_fd_alreadyTransitive OR cmake_fd_alreadyTransitive)
  92. set_property(GLOBAL PROPERTY _CMAKE_${dep}_TRANSITIVE_DEPENDENCY TRUE)
  93. endif()
  94. if (NOT ${dep}_FOUND)
  95. set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency ${dep} could not be found.")
  96. set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
  97. return()
  98. endif()
  99. set(cmake_fd_version)
  100. set(cmake_fd_required_arg)
  101. set(cmake_fd_quiet_arg)
  102. set(cmake_fd_exact_arg)
  103. endif()
  104. endmacro()