ENH: fix advanced bug
[cmake.git] / Modules / CheckCXXSourceCompiles.cmake
blobe5c3eccb9f2291b70f4c3d1a0872a472262d0b62
1 # - Check if the source code provided in the SOURCE argument compiles.
2 # CHECK_CXX_SOURCE_COMPILES(SOURCE VAR)
3 # - macro which checks if the source code compiles
4 #  SOURCE - source code to try to compile
5 #  VAR    - variable to store whether the source code compiled
7 # The following variables may be set before calling this macro to
8 # modify the way the check is run:
10 #  CMAKE_REQUIRED_FLAGS = string of compile command line flags
11 #  CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
12 #  CMAKE_REQUIRED_INCLUDES = list of include directories
13 #  CMAKE_REQUIRED_LIBRARIES = list of libraries to link
15 MACRO(CHECK_CXX_SOURCE_COMPILES SOURCE VAR)
16   IF("${VAR}" MATCHES "^${VAR}$")
17     SET(MACRO_CHECK_FUNCTION_DEFINITIONS
18       "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
19     IF(CMAKE_REQUIRED_LIBRARIES)
20       SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
21         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
22     ELSE(CMAKE_REQUIRED_LIBRARIES)
23       SET(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
24     ENDIF(CMAKE_REQUIRED_LIBRARIES)
25     IF(CMAKE_REQUIRED_INCLUDES)
26       SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
27         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
28     ELSE(CMAKE_REQUIRED_INCLUDES)
29       SET(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
30     ENDIF(CMAKE_REQUIRED_INCLUDES)
31     FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
32       "${SOURCE}\n")
34     MESSAGE(STATUS "Performing Test ${VAR}")
35     TRY_COMPILE(${VAR}
36       ${CMAKE_BINARY_DIR}
37       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx
38       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
39       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
40       "${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}"
41       "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
42       OUTPUT_VARIABLE OUTPUT)
43     IF(${VAR})
44       SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
45       MESSAGE(STATUS "Performing Test ${VAR} - Success")
46       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
47         "Performing C++ SOURCE FILE Test ${VAR} succeded with the following output:\n"
48         "${OUTPUT}\n"
49         "Source file was:\n${SOURCE}\n")
50     ELSE(${VAR})
51       MESSAGE(STATUS "Performing Test ${VAR} - Failed")
52       SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
53       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
54         "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
55         "${OUTPUT}\n"
56         "Source file was:\n${SOURCE}\n")
57     ENDIF(${VAR})
58   ENDIF("${VAR}" MATCHES "^${VAR}$")
59 ENDMACRO(CHECK_CXX_SOURCE_COMPILES)