CheckCXXSourceCompiles.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #.rst:
  2. # CheckCXXSourceCompiles
  3. # ----------------------
  4. #
  5. # Check if given C++ source compiles and links into an executable
  6. #
  7. # CHECK_CXX_SOURCE_COMPILES(<code> <var> [FAIL_REGEX <fail-regex>])
  8. #
  9. # ::
  10. #
  11. # <code> - source code to try to compile, must define 'main'
  12. # <var> - variable to store whether the source code compiled
  13. # <fail-regex> - fail if test output matches this regex
  14. #
  15. # The following variables may be set before calling this macro to modify
  16. # the way the check is run:
  17. #
  18. # ::
  19. #
  20. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  21. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  22. # CMAKE_REQUIRED_INCLUDES = list of include directories
  23. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  24. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  25. #=============================================================================
  26. # Copyright 2005-2009 Kitware, Inc.
  27. #
  28. # Distributed under the OSI-approved BSD License (the "License");
  29. # see accompanying file Copyright.txt for details.
  30. #
  31. # This software is distributed WITHOUT ANY WARRANTY; without even the
  32. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  33. # See the License for more information.
  34. #=============================================================================
  35. # (To distribute this file outside of CMake, substitute the full
  36. # License text for the above reference.)
  37. macro(CHECK_CXX_SOURCE_COMPILES SOURCE VAR)
  38. if(NOT DEFINED "${VAR}")
  39. set(_FAIL_REGEX)
  40. set(_key)
  41. foreach(arg ${ARGN})
  42. if("${arg}" MATCHES "^(FAIL_REGEX)$")
  43. set(_key "${arg}")
  44. elseif(_key)
  45. list(APPEND _${_key} "${arg}")
  46. else()
  47. message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
  48. endif()
  49. endforeach()
  50. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  51. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  52. if(CMAKE_REQUIRED_LIBRARIES)
  53. set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
  54. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  55. else()
  56. set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
  57. endif()
  58. if(CMAKE_REQUIRED_INCLUDES)
  59. set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
  60. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  61. else()
  62. set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
  63. endif()
  64. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
  65. "${SOURCE}\n")
  66. if(NOT CMAKE_REQUIRED_QUIET)
  67. message(STATUS "Performing Test ${VAR}")
  68. endif()
  69. try_compile(${VAR}
  70. ${CMAKE_BINARY_DIR}
  71. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx
  72. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  73. ${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}
  74. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  75. "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
  76. OUTPUT_VARIABLE OUTPUT)
  77. foreach(_regex ${_FAIL_REGEX})
  78. if("${OUTPUT}" MATCHES "${_regex}")
  79. set(${VAR} 0)
  80. endif()
  81. endforeach()
  82. if(${VAR})
  83. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  84. if(NOT CMAKE_REQUIRED_QUIET)
  85. message(STATUS "Performing Test ${VAR} - Success")
  86. endif()
  87. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  88. "Performing C++ SOURCE FILE Test ${VAR} succeded with the following output:\n"
  89. "${OUTPUT}\n"
  90. "Source file was:\n${SOURCE}\n")
  91. else()
  92. if(NOT CMAKE_REQUIRED_QUIET)
  93. message(STATUS "Performing Test ${VAR} - Failed")
  94. endif()
  95. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  96. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  97. "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
  98. "${OUTPUT}\n"
  99. "Source file was:\n${SOURCE}\n")
  100. endif()
  101. endif()
  102. endmacro()