1 # - Check if the source code provided in the SOURCE argument compiles.
2 # 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
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_COMPILES 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"
34 MESSAGE(STATUS "Performing Test ${VAR}")
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 "${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}"
41 "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
42 OUTPUT_VARIABLE OUTPUT)
44 SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
45 MESSAGE(STATUS "Performing Test ${VAR} - Success")
46 FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
47 "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
49 "Source file was:\n${SOURCE}\n")
51 MESSAGE(STATUS "Performing Test ${VAR} - Failed")
52 SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
53 FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
54 "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
56 "Source file was:\n${SOURCE}\n")
58 ENDIF("${VAR}" MATCHES "^${VAR}$")
59 ENDMACRO(CHECK_C_SOURCE_COMPILES)