ENH: Support object lists longer than 128K on MSVC
[cmake.git] / Modules / CheckCXXSourceRuns.cmake
blobb0c9ba52f3de5a7193422f0ee020bb33d1ebddae
1 # - Check if the source code provided in the SOURCE argument compiles and runs.
2 # CHECK_CXX_SOURCE_RUNS(SOURCE VAR)
3 # - macro which checks if the source code compiles
4 #  SOURCE - source code to try to compile
5 #  VAR    - variable to store the result, 1 for success, empty for failure
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)
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_RUN(${VAR}_EXITCODE ${VAR}_COMPILED
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       -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
41       "${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}"
42       "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
43       COMPILE_OUTPUT_VARIABLE OUTPUT)
45     # if it did not compile make the return value fail code of 1
46     IF(NOT ${VAR}_COMPILED)
47       SET(${VAR}_EXITCODE 1)
48     ENDIF(NOT ${VAR}_COMPILED)
49     # if the return value was 0 then it worked
50     IF("${${VAR}_EXITCODE}" EQUAL 0)
51       SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
52       MESSAGE(STATUS "Performing Test ${VAR} - Success")
53       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log 
54         "Performing C++ SOURCE FILE Test ${VAR} succeded with the following output:\n"
55         "${OUTPUT}\n" 
56         "Return value: ${${VAR}}\n"
57         "Source file was:\n${SOURCE}\n")
58     ELSE("${${VAR}_EXITCODE}" EQUAL 0)
59       IF(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
60         SET(${VAR} "${${VAR}_EXITCODE}")
61       ELSE(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
62         SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
63       ENDIF(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
65       MESSAGE(STATUS "Performing Test ${VAR} - Failed")
66       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 
67         "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
68         "${OUTPUT}\n"  
69         "Return value: ${${VAR}_EXITCODE}\n"
70         "Source file was:\n${SOURCE}\n")
71     ENDIF("${${VAR}_EXITCODE}" EQUAL 0)
72   ENDIF("${VAR}" MATCHES "^${VAR}$")
73 ENDMACRO(CHECK_CXX_SOURCE_RUNS)