CheckSourceCompiles.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. CheckSourceCompiles
  5. ----------------------
  6. .. versionadded:: 3.19
  7. Check if given source compiles and links into an executable.
  8. .. command:: check_source_compiles
  9. .. code-block:: cmake
  10. check_source_compiles(<lang> <code> <resultVar>
  11. [FAIL_REGEX <regex1> [<regex2>...]]
  12. [SRC_EXT <extension>])
  13. Check that the source supplied in ``<code>`` can be compiled as a source
  14. file for the requested language and linked as an executable (so it must
  15. contain at least a ``main()`` function). The result will be stored in the
  16. internal cache variable specified by ``<resultVar>``, with a boolean true
  17. value for success and boolean false for failure. If ``FAIL_REGEX`` is
  18. provided, then failure is determined by checking if anything in the output
  19. matches any of the specified regular expressions.
  20. By default, the test source file will be given a file extension that matches
  21. the requested language. The ``SRC_EXT`` option can be used to override this
  22. with ``.<extension>`` instead.
  23. The underlying check is performed by the :command:`try_compile` command. The
  24. compile and link commands can be influenced by setting any of the following
  25. variables prior to calling ``check_source_compiles()``:
  26. ``CMAKE_REQUIRED_FLAGS``
  27. Additional flags to pass to the compiler. Note that the contents of
  28. :variable:`CMAKE_<LANG>_FLAGS <CMAKE_<LANG>_FLAGS>` and its associated
  29. configuration-specific variable are automatically added to the compiler
  30. command before the contents of ``CMAKE_REQUIRED_FLAGS``.
  31. ``CMAKE_REQUIRED_DEFINITIONS``
  32. A :ref:`;-list <CMake Language Lists>` of compiler definitions of the form
  33. ``-DFOO`` or ``-DFOO=bar``. A definition for the name specified by
  34. ``<resultVar>`` will also be added automatically.
  35. ``CMAKE_REQUIRED_INCLUDES``
  36. A :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  37. the compiler. These will be the only header search paths used by
  38. ``try_compile()``, i.e. the contents of the :prop_dir:`INCLUDE_DIRECTORIES`
  39. directory property will be ignored.
  40. ``CMAKE_REQUIRED_LINK_OPTIONS``
  41. A :ref:`;-list <CMake Language Lists>` of options to add to the link
  42. command (see :command:`try_compile` for further details).
  43. ``CMAKE_REQUIRED_LIBRARIES``
  44. A :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  45. command. These can be the name of system libraries or they can be
  46. :ref:`Imported Targets <Imported Targets>` (see :command:`try_compile` for
  47. further details).
  48. ``CMAKE_REQUIRED_QUIET``
  49. If this variable evaluates to a boolean true value, all status messages
  50. associated with the check will be suppressed.
  51. The check is only performed once, with the result cached in the variable
  52. named by ``<resultVar>``. Every subsequent CMake run will re-use this cached
  53. value rather than performing the check again, even if the ``<code>`` changes.
  54. In order to force the check to be re-evaluated, the variable named by
  55. ``<resultVar>`` must be manually removed from the cache.
  56. #]=======================================================================]
  57. include_guard(GLOBAL)
  58. cmake_policy(PUSH)
  59. cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
  60. cmake_policy(SET CMP0057 NEW) # if() supports IN_LIST
  61. function(CHECK_SOURCE_COMPILES _lang _source _var)
  62. if(NOT DEFINED "${_var}")
  63. if(_lang STREQUAL C)
  64. set(_lang_textual "C")
  65. set(_lang_ext "c")
  66. elseif(_lang STREQUAL CXX)
  67. set(_lang_textual "C++")
  68. set(_lang_ext "cxx")
  69. elseif(_lang STREQUAL CUDA)
  70. set(_lang_textual "CUDA")
  71. set(_lang_ext "cu")
  72. elseif(_lang STREQUAL Fortran)
  73. set(_lang_textual "Fortran")
  74. set(_lang_ext "F90")
  75. elseif(_lang STREQUAL ISPC)
  76. set(_lang_textual "ISPC")
  77. set(_lang_ext "ispc")
  78. elseif(_lang STREQUAL OBJC)
  79. set(_lang_textual "Objective-C")
  80. set(_lang_ext "m")
  81. elseif(_lang STREQUAL OBJCXX)
  82. set(_lang_textual "Objective-C++")
  83. set(_lang_ext "mm")
  84. else()
  85. message (SEND_ERROR "check_source_compiles: ${_lang}: unknown language.")
  86. return()
  87. endif()
  88. get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  89. if (NOT _lang IN_LIST _supported_languages)
  90. message (SEND_ERROR "check_source_compiles: ${_lang}: needs to be enabled before use.")
  91. return()
  92. endif()
  93. set(_FAIL_REGEX)
  94. set(_SRC_EXT)
  95. set(_key)
  96. foreach(arg ${ARGN})
  97. if("${arg}" MATCHES "^(FAIL_REGEX|SRC_EXT)$")
  98. set(_key "${arg}")
  99. elseif(_key STREQUAL "FAIL_REGEX")
  100. list(APPEND _FAIL_REGEX "${arg}")
  101. elseif(_key STREQUAL "SRC_EXT")
  102. set(_SRC_EXT "${arg}")
  103. set(_key "")
  104. else()
  105. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  106. endif()
  107. endforeach()
  108. if(NOT _SRC_EXT)
  109. set(_SRC_EXT ${_lang_ext})
  110. endif()
  111. if(CMAKE_REQUIRED_LINK_OPTIONS)
  112. set(CHECK_${LANG}_SOURCE_COMPILES_ADD_LINK_OPTIONS
  113. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  114. else()
  115. set(CHECK_${LANG}_SOURCE_COMPILES_ADD_LINK_OPTIONS)
  116. endif()
  117. if(CMAKE_REQUIRED_LIBRARIES)
  118. set(CHECK_${LANG}_SOURCE_COMPILES_ADD_LIBRARIES
  119. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  120. else()
  121. set(CHECK_${LANG}_SOURCE_COMPILES_ADD_LIBRARIES)
  122. endif()
  123. if(CMAKE_REQUIRED_INCLUDES)
  124. set(CHECK_${LANG}_SOURCE_COMPILES_ADD_INCLUDES
  125. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  126. else()
  127. set(CHECK_${LANG}_SOURCE_COMPILES_ADD_INCLUDES)
  128. endif()
  129. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}"
  130. "${_source}\n")
  131. if(NOT CMAKE_REQUIRED_QUIET)
  132. message(CHECK_START "Performing Test ${_var}")
  133. endif()
  134. try_compile(${_var}
  135. ${CMAKE_BINARY_DIR}
  136. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}
  137. COMPILE_DEFINITIONS -D${_var} ${CMAKE_REQUIRED_DEFINITIONS}
  138. ${CHECK_${LANG}_SOURCE_COMPILES_ADD_LINK_OPTIONS}
  139. ${CHECK_${LANG}_SOURCE_COMPILES_ADD_LIBRARIES}
  140. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}
  141. "${CHECK_${LANG}_SOURCE_COMPILES_ADD_INCLUDES}"
  142. OUTPUT_VARIABLE OUTPUT)
  143. foreach(_regex ${_FAIL_REGEX})
  144. if("${OUTPUT}" MATCHES "${_regex}")
  145. set(${_var} 0)
  146. endif()
  147. endforeach()
  148. if(${_var})
  149. set(${_var} 1 CACHE INTERNAL "Test ${_var}")
  150. if(NOT CMAKE_REQUIRED_QUIET)
  151. message(CHECK_PASS "Success")
  152. endif()
  153. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  154. "Performing ${_lang_textual} SOURCE FILE Test ${_var} succeeded with the following output:\n"
  155. "${OUTPUT}\n"
  156. "Source file was:\n${_source}\n")
  157. else()
  158. if(NOT CMAKE_REQUIRED_QUIET)
  159. message(CHECK_FAIL "Failed")
  160. endif()
  161. set(${_var} "" CACHE INTERNAL "Test ${_var}")
  162. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  163. "Performing ${_lang_textual} SOURCE FILE Test ${_var} failed with the following output:\n"
  164. "${OUTPUT}\n"
  165. "Source file was:\n${_source}\n")
  166. endif()
  167. endif()
  168. endfunction()
  169. cmake_policy(POP)