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.)
27 include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
30 macro(CHECK_C_SOURCE_COMPILES SOURCE VAR)
31 if("${VAR}" MATCHES "^${VAR}$")
35 if("${arg}" MATCHES "^(FAIL_REGEX)$")
38 list(APPEND _${_key} "${arg}")
40 message(FATAL_ERROR "Unknown argument:\n ${arg}\n")
43 set(MACRO_CHECK_FUNCTION_DEFINITIONS
44 "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
45 if(CMAKE_REQUIRED_LIBRARIES)
46 # this one translates potentially used imported library targets to their files on disk
47 CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
48 set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
49 "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
51 set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
53 if(CMAKE_REQUIRED_INCLUDES)
54 set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
55 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
57 set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
59 file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
62 message(STATUS "Performing Test ${VAR}")
65 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
66 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
67 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
68 "${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}"
69 "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
70 OUTPUT_VARIABLE OUTPUT)
72 foreach(_regex ${_FAIL_REGEX})
73 if("${OUTPUT}" MATCHES "${_regex}")
79 set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
80 message(STATUS "Performing Test ${VAR} - Success")
81 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
82 "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
84 "Source file was:\n${SOURCE}\n")
86 message(STATUS "Performing Test ${VAR} - Failed")
87 set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
88 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
89 "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
91 "Source file was:\n${SOURCE}\n")