CMakeFindPackageMode.cmake 6.0 KB

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