STYLE: Nightly Date Stamp
[cmake.git] / Source / kwsys / CheckCXXSourceRuns.cmake
blob7bcc873874f5dbec30d1aba1584a980f4a25b350
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 size if the type exists.
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_RUNS SOURCE VAR COMMENT)
16   IF("HAVE_${VAR}" MATCHES "^HAVE_${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     SET(CMAKE_EMPTY_INPUT_FILE_CONTENT "${SOURCE}")
32     CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/CMakeEmptyInputFile.in"
33       "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/src.cxx" IMMEDIATE)
35     MESSAGE(STATUS "Performing Test ${COMMENT}")
36     TRY_RUN(${VAR} HAVE_${VAR}
37       ${CMAKE_BINARY_DIR}
38       ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/src.cxx
39       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
40       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
41       "${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}"
42       "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
43       OUTPUT_VARIABLE OUTPUT)
44     IF(HAVE_${VAR})
45       SET(${VAR} 1 CACHE INTERNAL "Test ${COMMENT}")
46       MESSAGE(STATUS "Performing Test ${COMMENT} - Success")
47       FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeOutput.log 
48         "Performing C++ SOURCE FILE Test ${COMMENT} succeded with the following output:\n"
49         "${OUTPUT}\n"
50         "Source file was:\n${SOURCE}\n")
51     ELSE(HAVE_${VAR})
52       MESSAGE(STATUS "Performing Test ${COMMENT} - Failed")
53       SET(${VAR} "" CACHE INTERNAL "Test ${COMMENT}")
54       FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log 
55         "Performing C++ SOURCE FILE Test ${COMMENT} failed with the following output:\n"
56         "${OUTPUT}\n"
57         "Source file was:\n${SOURCE}\n")
58     ENDIF(HAVE_${VAR})
59   ENDIF("HAVE_${VAR}" MATCHES "^HAVE_${VAR}$")
60 ENDMACRO(CHECK_CXX_SOURCE_RUNS)