Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / CMake / CurlCheckCSourceRuns.cmake
blob19681bd85d60d9ec9be5bb27d632afe9e77dac60
1 # - Check if the source code provided in the SOURCE argument compiles and runs.
2 # CURL_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 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(CURL_CHECK_C_SOURCE_RUNS SOURCE VAR)
16   IF("${VAR}" MATCHES "^${VAR}$" OR "${VAR}" MATCHES "UNKNOWN")
17     SET(message "${VAR}")
18     # If the number of arguments is greater than 2 (SOURCE VAR)
19     IF(${ARGC} GREATER 2)
20       # then add the third argument as a message
21       SET(message "${ARGV2} (${VAR})")
22     ENDIF(${ARGC} GREATER 2)
23     SET(MACRO_CHECK_FUNCTION_DEFINITIONS
24       "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
25     IF(CMAKE_REQUIRED_LIBRARIES)
26       SET(CURL_CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
27         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
28     ELSE(CMAKE_REQUIRED_LIBRARIES)
29       SET(CURL_CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
30     ENDIF(CMAKE_REQUIRED_LIBRARIES)
31     IF(CMAKE_REQUIRED_INCLUDES)
32       SET(CURL_CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
33         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
34     ELSE(CMAKE_REQUIRED_INCLUDES)
35       SET(CURL_CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
36     ENDIF(CMAKE_REQUIRED_INCLUDES)
37     SET(src "")
38     FOREACH(def ${EXTRA_DEFINES})
39       SET(src "${src}#define ${def} 1\n")
40     ENDFOREACH(def)
41     FOREACH(inc ${HEADER_INCLUDES})
42       SET(src "${src}#include <${inc}>\n")
43     ENDFOREACH(inc)
45     SET(src "${src}\nint main() { ${SOURCE} ; return 0; }")
46     SET(CMAKE_CONFIGURABLE_FILE_CONTENT "${src}")
47     CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/CMake/CMakeConfigurableFile.in
48       "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
49       IMMEDIATE)
50     MESSAGE(STATUS "Performing Test ${message}")
51     TRY_RUN(${VAR} ${VAR}_COMPILED
52       ${CMAKE_BINARY_DIR}
53       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
54       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
55       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
56       "${CURL_CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}"
57       "${CURL_CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
58       OUTPUT_VARIABLE OUTPUT)
59     # if it did not compile make the return value fail code of 1
60     IF(NOT ${VAR}_COMPILED)
61       SET(${VAR} 1)
62     ENDIF(NOT ${VAR}_COMPILED)
63     # if the return value was 0 then it worked
64     SET(result_var ${${VAR}})
65     IF("${result_var}" EQUAL 0)
66       SET(${VAR} 1 CACHE INTERNAL "Test ${message}")
67       MESSAGE(STATUS "Performing Test ${message} - Success")
68       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
69         "Performing C SOURCE FILE Test ${message} succeded with the following output:\n"
70         "${OUTPUT}\n"
71         "Return value: ${${VAR}}\n"
72         "Source file was:\n${src}\n")
73     ELSE("${result_var}" EQUAL 0)
74       MESSAGE(STATUS "Performing Test ${message} - Failed")
75       SET(${VAR} "" CACHE INTERNAL "Test ${message}")
76       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
77         "Performing C SOURCE FILE Test ${message} failed with the following output:\n"
78         "${OUTPUT}\n"
79         "Return value: ${result_var}\n"
80         "Source file was:\n${src}\n")
81     ENDIF("${result_var}" EQUAL 0)
82   ENDIF("${VAR}" MATCHES "^${VAR}$" OR "${VAR}" MATCHES "UNKNOWN")
83 ENDMACRO(CURL_CHECK_C_SOURCE_RUNS)