CMakeFindPackageMode.cmake 5.6 KB

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