CheckCXXSourceCompiles.cmake 3.8 KB

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