Imported Upstream version 2.8.11.2
[platform/upstream/cmake.git] / Modules / CheckCSourceCompiles.cmake
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:
8 #
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
13
14 #=============================================================================
15 # Copyright 2005-2009 Kitware, Inc.
16 #
17 # Distributed under the OSI-approved BSD License (the "License");
18 # see accompanying file Copyright.txt for details.
19 #
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.)
26
27
28
29 macro(CHECK_C_SOURCE_COMPILES SOURCE VAR)
30   if("${VAR}" MATCHES "^${VAR}$")
31     set(_FAIL_REGEX)
32     set(_key)
33     foreach(arg ${ARGN})
34       if("${arg}" MATCHES "^(FAIL_REGEX)$")
35         set(_key "${arg}")
36       elseif(_key)
37         list(APPEND _${_key} "${arg}")
38       else()
39         message(FATAL_ERROR "Unknown argument:\n  ${arg}\n")
40       endif()
41     endforeach()
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})
47     else()
48       set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
49     endif()
50     if(CMAKE_REQUIRED_INCLUDES)
51       set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
52         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
53     else()
54       set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
55     endif()
56     file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
57       "${SOURCE}\n")
58
59     message(STATUS "Performing Test ${VAR}")
60     try_compile(${VAR}
61       ${CMAKE_BINARY_DIR}
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)
68
69     foreach(_regex ${_FAIL_REGEX})
70       if("${OUTPUT}" MATCHES "${_regex}")
71         set(${VAR} 0)
72       endif()
73     endforeach()
74
75     if(${VAR})
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"
80         "${OUTPUT}\n"
81         "Source file was:\n${SOURCE}\n")
82     else()
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"
87         "${OUTPUT}\n"
88         "Source file was:\n${SOURCE}\n")
89     endif()
90   endif()
91 endmacro()
92