1# - Check if the source code provided in the SOURCE argument compiles.
2# CURL_CHECK_C_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 whether the source code compiled
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_COMPILES 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_compile(${VAR}
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(${VAR})
60      set(${VAR} 1 CACHE INTERNAL "Test ${message}")
61      message(STATUS "Performing Test ${message} - Success")
62      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
63        "Performing C SOURCE FILE Test ${message} succeded with the following output:\n"
64        "${OUTPUT}\n"
65        "Source file was:\n${src}\n")
66    else(${VAR})
67      message(STATUS "Performing Test ${message} - Failed")
68      set(${VAR} "" CACHE INTERNAL "Test ${message}")
69      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
70        "Performing C SOURCE FILE Test ${message} failed with the following output:\n"
71        "${OUTPUT}\n"
72        "Source file was:\n${src}\n")
73    endif(${VAR})
74  endif("${VAR}" MATCHES "^${VAR}$" OR "${VAR}" MATCHES "UNKNOWN")
75endmacro(CURL_CHECK_C_SOURCE_COMPILES)
76