ENH: fix bug where sharedforward would not work if there was a space in the path...
[cmake.git] / Modules / CheckCSourceRuns.cmake
blobc5455ae10ba65b04da268d6fbf124425898e0968
1 # - Check if the source code provided in the SOURCE argument compiles and runs.
2 # CHECK_C_SOURCE_RUNS(SOURCE VAR)
3 # - macro which checks if the source code runs
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_C_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_C_SOURCE_COMPILES_ADD_LIBRARIES
21         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
22     ELSE(CMAKE_REQUIRED_LIBRARIES)
23       SET(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
24     ENDIF(CMAKE_REQUIRED_LIBRARIES)
25     IF(CMAKE_REQUIRED_INCLUDES)
26       SET(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
27         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
28     ELSE(CMAKE_REQUIRED_INCLUDES)
29       SET(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
30     ENDIF(CMAKE_REQUIRED_INCLUDES)
31     FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
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.c
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_C_SOURCE_COMPILES_ADD_LIBRARIES}"
42       "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
43       COMPILE_OUTPUT_VARIABLE OUTPUT)
44     # if it did not compile make the return value fail code of 1
45     IF(NOT ${VAR}_COMPILED)
46       SET(${VAR}_EXITCODE 1)
47     ENDIF(NOT ${VAR}_COMPILED)
48     # if the return value was 0 then it worked
49     IF("${${VAR}_EXITCODE}" EQUAL 0)
50       SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
51       MESSAGE(STATUS "Performing Test ${VAR} - Success")
52       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log 
53         "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
54         "${OUTPUT}\n"
55         "Return value: ${${VAR}}\n"
56         "Source file was:\n${SOURCE}\n")
57     ELSE("${${VAR}_EXITCODE}" EQUAL 0)
58       IF(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
59         SET(${VAR} "${${VAR}_EXITCODE}")
60       ELSE(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
61         SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
62       ENDIF(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
64       MESSAGE(STATUS "Performing Test ${VAR} - Failed")
65       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 
66         "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
67         "${OUTPUT}\n"
68         "Return value: ${${VAR}_EXITCODE}\n"
69         "Source file was:\n${SOURCE}\n")
71     ENDIF("${${VAR}_EXITCODE}" EQUAL 0)
72   ENDIF("${VAR}" MATCHES "^${VAR}$")
73 ENDMACRO(CHECK_C_SOURCE_RUNS)