CMakeFindPackageMode.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #.rst:
  2. # CMakeFindPackageMode
  3. # --------------------
  4. #
  5. #
  6. #
  7. # This file is executed by cmake when invoked with --find-package. It
  8. # expects that the following variables are set using -D:
  9. #
  10. # ``NAME``
  11. # name of the package
  12. # ``COMPILER_ID``
  13. # the CMake compiler ID for which the result is,
  14. # i.e. GNU/Intel/Clang/MSVC, etc.
  15. # ``LANGUAGE``
  16. # language for which the result will be used,
  17. # i.e. C/CXX/Fortan/ASM
  18. # ``MODE``
  19. # ``EXIST``
  20. # only check for existence of the given package
  21. # ``COMPILE``
  22. # print the flags needed for compiling an object file which uses
  23. # the given package
  24. # ``LINK``
  25. # print the flags needed for linking when using the given package
  26. # ``QUIET``
  27. # if TRUE, don't print anything
  28. #=============================================================================
  29. # Copyright 2006-2011 Alexander Neundorf, <neundorf@kde.org>
  30. #
  31. # Distributed under the OSI-approved BSD License (the "License");
  32. # see accompanying file Copyright.txt for details.
  33. #
  34. # This software is distributed WITHOUT ANY WARRANTY; without even the
  35. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  36. # See the License for more information.
  37. #=============================================================================
  38. # (To distribute this file outside of CMake, substitute the full
  39. # License text for the above reference.)
  40. if(NOT NAME)
  41. message(FATAL_ERROR "Name of the package to be searched not specified. Set the CMake variable NAME, e.g. -DNAME=JPEG .")
  42. endif()
  43. if(NOT COMPILER_ID)
  44. message(FATAL_ERROR "COMPILER_ID argument not specified. In doubt, use GNU.")
  45. endif()
  46. if(NOT LANGUAGE)
  47. message(FATAL_ERROR "LANGUAGE argument not specified. Use C, CXX or Fortran.")
  48. endif()
  49. if(NOT MODE)
  50. message(FATAL_ERROR "MODE argument not specified. Use either EXIST, COMPILE or LINK.")
  51. endif()
  52. # require the current version. If we don't do this, Platforms/CYGWIN.cmake complains because
  53. # it doesn't know whether it should set WIN32 or not:
  54. cmake_minimum_required(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION} )
  55. macro(ENABLE_LANGUAGE)
  56. # disable the enable_language() command, otherwise --find-package breaks on Windows.
  57. # On Windows, enable_language(RC) is called in the platform files unconditionally.
  58. # But in --find-package mode, we don't want (and can't) enable any language.
  59. endmacro()
  60. set(CMAKE_PLATFORM_INFO_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY})
  61. include(CMakeDetermineSystem)
  62. # short-cut some tests on Darwin, see Darwin-GNU.cmake:
  63. if("${CMAKE_SYSTEM_NAME}" MATCHES Darwin AND "${COMPILER_ID}" MATCHES GNU)
  64. set(CMAKE_${LANGUAGE}_SYSROOT_FLAG "")
  65. set(CMAKE_${LANGUAGE}_OSX_DEPLOYMENT_TARGET_FLAG "")
  66. endif()
  67. # Also load the system specific file, which sets up e.g. the search paths.
  68. # This makes the FIND_XXX() calls work much better
  69. include(CMakeSystemSpecificInformation)
  70. if(UNIX)
  71. # try to guess whether we have a 64bit system, if it has not been set
  72. # from the outside
  73. if(NOT CMAKE_SIZEOF_VOID_P)
  74. set(CMAKE_SIZEOF_VOID_P 4)
  75. if(EXISTS /usr/lib64)
  76. set(CMAKE_SIZEOF_VOID_P 8)
  77. else()
  78. # use the file utility to check whether itself is 64 bit:
  79. find_program(FILE_EXECUTABLE file)
  80. if(FILE_EXECUTABLE)
  81. get_filename_component(FILE_ABSPATH "${FILE_EXECUTABLE}" ABSOLUTE)
  82. execute_process(COMMAND "${FILE_ABSPATH}" "${FILE_ABSPATH}" OUTPUT_VARIABLE fileOutput ERROR_QUIET)
  83. if("${fileOutput}" MATCHES "64-bit")
  84. set(CMAKE_SIZEOF_VOID_P 8)
  85. endif()
  86. endif()
  87. endif()
  88. endif()
  89. # guess Debian multiarch if it has not been set:
  90. if(EXISTS /etc/debian_version)
  91. if(NOT CMAKE_${LANGUAGE}_LIBRARY_ARCHITECTURE )
  92. file(GLOB filesInLib RELATIVE /lib /lib/*-linux-gnu* )
  93. foreach(file ${filesInLib})
  94. if("${file}" MATCHES "${CMAKE_LIBRARY_ARCHITECTURE_REGEX}")
  95. set(CMAKE_${LANGUAGE}_LIBRARY_ARCHITECTURE ${file})
  96. break()
  97. endif()
  98. endforeach()
  99. endif()
  100. if(NOT CMAKE_LIBRARY_ARCHITECTURE)
  101. set(CMAKE_LIBRARY_ARCHITECTURE ${CMAKE_${LANGUAGE}_LIBRARY_ARCHITECTURE})
  102. endif()
  103. endif()
  104. endif()
  105. set(CMAKE_${LANGUAGE}_COMPILER "dummy")
  106. set(CMAKE_${LANGUAGE}_COMPILER_ID "${COMPILER_ID}")
  107. include(CMake${LANGUAGE}Information)
  108. function(set_compile_flags_var _packageName)
  109. string(TOUPPER "${_packageName}" PACKAGE_NAME)
  110. # Check the following variables:
  111. # FOO_INCLUDE_DIRS
  112. # Foo_INCLUDE_DIRS
  113. # FOO_INCLUDES
  114. # Foo_INCLUDES
  115. # FOO_INCLUDE_DIR
  116. # Foo_INCLUDE_DIR
  117. set(includes)
  118. if(DEFINED ${_packageName}_INCLUDE_DIRS)
  119. set(includes ${_packageName}_INCLUDE_DIRS)
  120. elseif(DEFINED ${PACKAGE_NAME}_INCLUDE_DIRS)
  121. set(includes ${PACKAGE_NAME}_INCLUDE_DIRS)
  122. elseif(DEFINED ${_packageName}_INCLUDES)
  123. set(includes ${_packageName}_INCLUDES)
  124. elseif(DEFINED ${PACKAGE_NAME}_INCLUDES)
  125. set(includes ${PACKAGE_NAME}_INCLUDES)
  126. elseif(DEFINED ${_packageName}_INCLUDE_DIR)
  127. set(includes ${_packageName}_INCLUDE_DIR)
  128. elseif(DEFINED ${PACKAGE_NAME}_INCLUDE_DIR)
  129. set(includes ${PACKAGE_NAME}_INCLUDE_DIR)
  130. endif()
  131. set(PACKAGE_INCLUDE_DIRS "${${includes}}" PARENT_SCOPE)
  132. # Check the following variables:
  133. # FOO_DEFINITIONS
  134. # Foo_DEFINITIONS
  135. set(definitions)
  136. if(DEFINED ${_packageName}_DEFINITIONS)
  137. set(definitions ${_packageName}_DEFINITIONS)
  138. elseif(DEFINED ${PACKAGE_NAME}_DEFINITIONS)
  139. set(definitions ${PACKAGE_NAME}_DEFINITIONS)
  140. endif()
  141. set(PACKAGE_DEFINITIONS "${${definitions}}" )
  142. endfunction()
  143. function(set_link_flags_var _packageName)
  144. string(TOUPPER "${_packageName}" PACKAGE_NAME)
  145. # Check the following variables:
  146. # FOO_LIBRARIES
  147. # Foo_LIBRARIES
  148. # FOO_LIBS
  149. # Foo_LIBS
  150. set(libs)
  151. if(DEFINED ${_packageName}_LIBRARIES)
  152. set(libs ${_packageName}_LIBRARIES)
  153. elseif(DEFINED ${PACKAGE_NAME}_LIBRARIES)
  154. set(libs ${PACKAGE_NAME}_LIBRARIES)
  155. elseif(DEFINED ${_packageName}_LIBS)
  156. set(libs ${_packageName}_LIBS)
  157. elseif(DEFINED ${PACKAGE_NAME}_LIBS)
  158. set(libs ${PACKAGE_NAME}_LIBS)
  159. endif()
  160. set(PACKAGE_LIBRARIES "${${libs}}" PARENT_SCOPE )
  161. endfunction()
  162. find_package("${NAME}" QUIET)
  163. set(PACKAGE_FOUND FALSE)
  164. string(TOUPPER "${NAME}" UPPERCASE_NAME)
  165. if(${NAME}_FOUND OR ${UPPERCASE_NAME}_FOUND)
  166. set(PACKAGE_FOUND TRUE)
  167. if("${MODE}" STREQUAL "EXIST")
  168. # do nothing
  169. elseif("${MODE}" STREQUAL "COMPILE")
  170. set_compile_flags_var(${NAME})
  171. elseif("${MODE}" STREQUAL "LINK")
  172. set_link_flags_var(${NAME})
  173. else()
  174. message(FATAL_ERROR "Invalid mode argument ${MODE} given.")
  175. endif()
  176. endif()
  177. set(PACKAGE_QUIET ${SILENT} )