1 # - Check if given C source compiles and links into an executable
2 # CHECK_C_SOURCE_COMPILES(<code> <var> [FAIL_REGEX <fail-regex>])
3 # <code> - source code to try to compile, must define 'main'
4 # <var> - variable to store whether the source code compiled
5 # <fail-regex> - fail if test output matches this regex
6 # The following variables may be set before calling this macro to
7 # modify the way the check is run:
9 # CMAKE_REQUIRED_FLAGS = string of compile command line flags
10 # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
11 # CMAKE_REQUIRED_INCLUDES = list of include directories
12 # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
14 #=============================================================================
15 # Copyright 2005-2009 Kitware, Inc.
17 # Distributed under the OSI-approved BSD License (the "License");
18 # see accompanying file Copyright.txt for details.
20 # This software is distributed WITHOUT ANY WARRANTY; without even the
21 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 # See the License for more information.
23 #=============================================================================
24 # (To distribute this file outside of CMake, substitute the full
25 # License text for the above reference.)
29 macro(CHECK_C_SOURCE_COMPILES SOURCE VAR)
30 if("${VAR}" MATCHES "^${VAR}$")
34 if("${arg}" MATCHES "^(FAIL_REGEX)$")
37 list(APPEND _${_key} "${arg}")
39 message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
42 set(MACRO_CHECK_FUNCTION_DEFINITIONS
43 "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
44 if(CMAKE_REQUIRED_LIBRARIES)
45 set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
46 LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
48 set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
50 if(CMAKE_REQUIRED_INCLUDES)
51 set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
52 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
54 set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
56 file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
59 message(STATUS "Performing Test ${VAR}")
62 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
63 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
64 ${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}
65 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
66 "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
67 OUTPUT_VARIABLE OUTPUT)
69 foreach(_regex ${_FAIL_REGEX})
70 if("${OUTPUT}" MATCHES "${_regex}")
76 set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
77 message(STATUS "Performing Test ${VAR} - Success")
78 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
79 "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
81 "Source file was:\n${SOURCE}\n")
83 message(STATUS "Performing Test ${VAR} - Failed")
84 set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
85 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
86 "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
88 "Source file was:\n${SOURCE}\n")