CheckCXXSourceCompiles.cmake 2.51 KB
Newer Older
1 2 3 4
# - Check if the given C++ source code compiles.
# CHECK_CXX_SOURCE_COMPILES(<code> <var>)
#  <code>       - source code to try to compile
#  <var>        - variable to store whether the source code compiled
5 6 7 8 9 10 11
# The following variables may be set before calling this macro to
# modify the way the check is run:
#
#  CMAKE_REQUIRED_FLAGS = string of compile command line flags
#  CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
#  CMAKE_REQUIRED_INCLUDES = list of include directories
#  CMAKE_REQUIRED_LIBRARIES = list of libraries to link
Andy Cedilnik's avatar
Andy Cedilnik committed
12 13 14

MACRO(CHECK_CXX_SOURCE_COMPILES SOURCE VAR)
  IF("${VAR}" MATCHES "^${VAR}$")
15
    SET(MACRO_CHECK_FUNCTION_DEFINITIONS
Andy Cedilnik's avatar
Andy Cedilnik committed
16 17 18 19
      "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
    IF(CMAKE_REQUIRED_LIBRARIES)
      SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
        "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
20 21
    ELSE(CMAKE_REQUIRED_LIBRARIES)
      SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
Andy Cedilnik's avatar
Andy Cedilnik committed
22 23 24 25
    ENDIF(CMAKE_REQUIRED_LIBRARIES)
    IF(CMAKE_REQUIRED_INCLUDES)
      SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
        "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
26 27
    ELSE(CMAKE_REQUIRED_INCLUDES)
      SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
Andy Cedilnik's avatar
Andy Cedilnik committed
28
    ENDIF(CMAKE_REQUIRED_INCLUDES)
29
    FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
30
      "${SOURCE}\n")
Andy Cedilnik's avatar
Andy Cedilnik committed
31 32 33 34

    MESSAGE(STATUS "Performing Test ${VAR}")
    TRY_COMPILE(${VAR}
      ${CMAKE_BINARY_DIR}
35
      ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx
36
      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
37
      CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
Andy Cedilnik's avatar
Andy Cedilnik committed
38 39 40 41
      "${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}"
      "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
      OUTPUT_VARIABLE OUTPUT)
    IF(${VAR})
42
      SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
Andy Cedilnik's avatar
Andy Cedilnik committed
43
      MESSAGE(STATUS "Performing Test ${VAR} - Success")
44
      FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
Andy Cedilnik's avatar
Andy Cedilnik committed
45 46
        "Performing C++ SOURCE FILE Test ${VAR} succeded with the following output:\n"
        "${OUTPUT}\n"
Bill Hoffman's avatar
Bill Hoffman committed
47
        "Source file was:\n${SOURCE}\n")
Andy Cedilnik's avatar
Andy Cedilnik committed
48 49
    ELSE(${VAR})
      MESSAGE(STATUS "Performing Test ${VAR} - Failed")
50
      SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
51
      FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
Andy Cedilnik's avatar
Andy Cedilnik committed
52 53
        "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
        "${OUTPUT}\n"
Bill Hoffman's avatar
Bill Hoffman committed
54
        "Source file was:\n${SOURCE}\n")
Andy Cedilnik's avatar
Andy Cedilnik committed
55 56 57 58
    ENDIF(${VAR})
  ENDIF("${VAR}" MATCHES "^${VAR}$")
ENDMACRO(CHECK_CXX_SOURCE_COMPILES)