Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CheckCSourceRuns.cmake
1 # - Check if the given C source code compiles and runs.
2 # CHECK_C_SOURCE_RUNS(<code> <var>)
3 #  <code>   - source code to try to compile
4 #  <var>    - variable to store the result
5 #             (1 for success, empty for failure)
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 2006-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_RUNS SOURCE VAR)
31   if("${VAR}" MATCHES "^${VAR}$")
32     set(MACRO_CHECK_FUNCTION_DEFINITIONS
33       "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
34     if(CMAKE_REQUIRED_LIBRARIES)
35       # this one translates potentially used imported library targets to their files on disk
36       CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
37       set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
38         "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
39     else()
40       set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
41     endif()
42     if(CMAKE_REQUIRED_INCLUDES)
43       set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
44         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
45     else()
46       set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
47     endif()
48     file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
49       "${SOURCE}\n")
50
51     message(STATUS "Performing Test ${VAR}")
52     try_run(${VAR}_EXITCODE ${VAR}_COMPILED
53       ${CMAKE_BINARY_DIR}
54       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
55       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
56       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
57       -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
58       "${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}"
59       "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
60       COMPILE_OUTPUT_VARIABLE OUTPUT)
61     # if it did not compile make the return value fail code of 1
62     if(NOT ${VAR}_COMPILED)
63       set(${VAR}_EXITCODE 1)
64     endif()
65     # if the return value was 0 then it worked
66     if("${${VAR}_EXITCODE}" EQUAL 0)
67       set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
68       message(STATUS "Performing Test ${VAR} - Success")
69       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
70         "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
71         "${OUTPUT}\n"
72         "Return value: ${${VAR}}\n"
73         "Source file was:\n${SOURCE}\n")
74     else()
75       if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
76         set(${VAR} "${${VAR}_EXITCODE}")
77       else()
78         set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
79       endif()
80
81       message(STATUS "Performing Test ${VAR} - Failed")
82       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
83         "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
84         "${OUTPUT}\n"
85         "Return value: ${${VAR}_EXITCODE}\n"
86         "Source file was:\n${SOURCE}\n")
87
88     endif()
89   endif()
90 endmacro()
91