Imported Upstream version 2.8.10.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 include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
28
29
30 macro(CHECK_C_SOURCE_COMPILES SOURCE VAR)
31   if("${VAR}" MATCHES "^${VAR}$")
32     set(_FAIL_REGEX)
33     set(_key)
34     foreach(arg ${ARGN})
35       if("${arg}" MATCHES "^(FAIL_REGEX)$")
36         set(_key "${arg}")
37       elseif(_key)
38         list(APPEND _${_key} "${arg}")
39       else()
40         message(FATAL_ERROR "Unknown argument:\n  ${arg}\n")
41       endif()
42     endforeach()
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}")
50     else()
51       set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
52     endif()
53     if(CMAKE_REQUIRED_INCLUDES)
54       set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
55         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
56     else()
57       set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
58     endif()
59     file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
60       "${SOURCE}\n")
61
62     message(STATUS "Performing Test ${VAR}")
63     try_compile(${VAR}
64       ${CMAKE_BINARY_DIR}
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)
71
72     foreach(_regex ${_FAIL_REGEX})
73       if("${OUTPUT}" MATCHES "${_regex}")
74         set(${VAR} 0)
75       endif()
76     endforeach()
77
78     if(${VAR})
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"
83         "${OUTPUT}\n"
84         "Source file was:\n${SOURCE}\n")
85     else()
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"
90         "${OUTPUT}\n"
91         "Source file was:\n${SOURCE}\n")
92     endif()
93   endif()
94 endmacro()
95