CMakeFindPackageMode.cmake 6.1 KB

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