CMakeFindPackageMode.cmake 6.2 KB

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