CMakeFindPackageMode.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 existance 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. include(CMakeDetermineSystem)
  43. # short-cut some tests on Darwin, see Darwin-GNU.cmake:
  44. if("${CMAKE_SYSTEM_NAME}" MATCHES Darwin AND "${COMPILER_ID}" MATCHES GNU)
  45. set(${CMAKE_${LANGUAGE}_HAS_ISYSROOT} 0 )
  46. set(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG "")
  47. endif()
  48. # Also load the system specific file, which sets up e.g. the search paths.
  49. # This makes the FIND_XXX() calls work much better
  50. include(CMakeSystemSpecificInformation)
  51. if(UNIX)
  52. # try to guess whether we have a 64bit system, if it has not been set
  53. # from the outside
  54. if(NOT CMAKE_SIZEOF_VOID_P)
  55. set(CMAKE_SIZEOF_VOID_P 4)
  56. if(EXISTS /usr/lib64)
  57. set(CMAKE_SIZEOF_VOID_P 8)
  58. else()
  59. # use the file utility to check whether itself is 64 bit:
  60. find_program(FILE_EXECUTABLE file)
  61. if(FILE_EXECUTABLE)
  62. get_filename_component(FILE_ABSPATH "${FILE_EXECUTABLE}" ABSOLUTE)
  63. execute_process(COMMAND "${FILE_ABSPATH}" "${FILE_ABSPATH}" OUTPUT_VARIABLE fileOutput ERROR_QUIET)
  64. if("${fileOutput}" MATCHES "64-bit")
  65. set(CMAKE_SIZEOF_VOID_P 8)
  66. endif()
  67. endif()
  68. endif()
  69. endif()
  70. # guess Debian multiarch if it has not been set:
  71. if(EXISTS /etc/debian_version)
  72. if(NOT CMAKE_${LANGUAGE}_LANGUAGE_ARCHITECTURE )
  73. file(GLOB filesInLib RELATIVE /lib /lib/*-linux-gnu* )
  74. foreach(file ${filesInLib})
  75. if("${file}" MATCHES "${CMAKE_LIBRARY_ARCHITECTURE_REGEX}")
  76. set(CMAKE_${LANGUAGE}_LANGUAGE_ARCHITECTURE ${file})
  77. break()
  78. endif()
  79. endforeach()
  80. endif()
  81. endif()
  82. endif()
  83. set(CMAKE_${LANGUAGE}_COMPILER "dummy")
  84. set(CMAKE_${LANGUAGE}_COMPILER_ID "${COMPILER_ID}")
  85. include(CMake${LANGUAGE}Information)
  86. function(set_compile_flags_var _packageName)
  87. string(TOUPPER "${_packageName}" PACKAGE_NAME)
  88. # Check the following variables:
  89. # FOO_INCLUDE_DIRS
  90. # Foo_INCLUDE_DIRS
  91. # FOO_INCLUDES
  92. # Foo_INCLUDES
  93. # FOO_INCLUDE_DIR
  94. # Foo_INCLUDE_DIR
  95. set(includes)
  96. if(DEFINED ${_packageName}_INCLUDE_DIRS)
  97. set(includes ${_packageName}_INCLUDE_DIRS)
  98. elseif(DEFINED ${PACKAGE_NAME}_INCLUDE_DIRS)
  99. set(includes ${PACKAGE_NAME}_INCLUDE_DIRS)
  100. elseif(DEFINED ${_packageName}_INCLUDES)
  101. set(includes ${_packageName}_INCLUDES)
  102. elseif(DEFINED ${PACKAGE_NAME}_INCLUDES)
  103. set(includes ${PACKAGE_NAME}_INCLUDES)
  104. elseif(DEFINED ${_packageName}_INCLUDE_DIR)
  105. set(includes ${_packageName}_INCLUDE_DIR)
  106. elseif(DEFINED ${PACKAGE_NAME}_INCLUDE_DIR)
  107. set(includes ${PACKAGE_NAME}_INCLUDE_DIR)
  108. endif()
  109. set(PACKAGE_INCLUDE_DIRS "${${includes}}" PARENT_SCOPE)
  110. # Check the following variables:
  111. # FOO_DEFINITIONS
  112. # Foo_DEFINITIONS
  113. set(definitions)
  114. if(DEFINED ${_packageName}_DEFINITIONS)
  115. set(definitions ${_packageName}_DEFINITIONS)
  116. elseif(DEFINED ${PACKAGE_NAME}_DEFINITIONS)
  117. set(definitions ${PACKAGE_NAME}_DEFINITIONS)
  118. endif()
  119. set(PACKAGE_DEFINITIONS "${${definitions}}" )
  120. endfunction()
  121. function(set_link_flags_var _packageName)
  122. string(TOUPPER "${_packageName}" PACKAGE_NAME)
  123. # Check the following variables:
  124. # FOO_LIBRARIES
  125. # Foo_LIBRARIES
  126. # FOO_LIBS
  127. # Foo_LIBS
  128. set(libs)
  129. if(DEFINED ${_packageName}_LIBRARIES)
  130. set(libs ${_packageName}_LIBRARIES)
  131. elseif(DEFINED ${PACKAGE_NAME}_LIBRARIES)
  132. set(libs ${PACKAGE_NAME}_LIBRARIES)
  133. elseif(DEFINED ${_packageName}_LIBS)
  134. set(libs ${_packageName}_LIBS)
  135. elseif(DEFINED ${PACKAGE_NAME}_LIBS)
  136. set(libs ${PACKAGE_NAME}_LIBS)
  137. endif()
  138. set(PACKAGE_LIBRARIES "${${libs}}" PARENT_SCOPE )
  139. endfunction()
  140. find_package("${NAME}" QUIET)
  141. set(PACKAGE_FOUND FALSE)
  142. string(TOUPPER "${NAME}" UPPERCASE_NAME)
  143. if(${NAME}_FOUND OR ${UPPERCASE_NAME}_FOUND)
  144. set(PACKAGE_FOUND TRUE)
  145. if("${MODE}" STREQUAL "EXIST")
  146. # do nothing
  147. elseif("${MODE}" STREQUAL "COMPILE")
  148. set_compile_flags_var(${NAME})
  149. elseif("${MODE}" STREQUAL "LINK")
  150. set_link_flags_var(${NAME})
  151. else()
  152. message(FATAL_ERROR "Invalid mode argument ${MODE} given.")
  153. endif()
  154. endif()
  155. set(PACKAGE_QUIET ${SILENT} )