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.
6#
7# The following variables may be set before calling this macro to
8# modify the way the check is run:
9#
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
14
15macro(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)
44
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")
83endmacro(CURL_CHECK_C_SOURCE_RUNS)
84