Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CheckCXXSourceRuns.cmake
1 # - Check if the given C++ source code compiles and runs.
2 # CHECK_CXX_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_CXX_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_CXX_SOURCE_COMPILES_ADD_LIBRARIES
38         "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
39     else()
40       set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
41     endif()
42     if(CMAKE_REQUIRED_INCLUDES)
43       set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
44         "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
45     else()
46       set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
47     endif()
48     file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
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.cxx
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_CXX_SOURCE_COMPILES_ADD_LIBRARIES}"
59       "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
60       COMPILE_OUTPUT_VARIABLE OUTPUT)
61
62     # if it did not compile make the return value fail code of 1
63     if(NOT ${VAR}_COMPILED)
64       set(${VAR}_EXITCODE 1)
65     endif()
66     # if the return value was 0 then it worked
67     if("${${VAR}_EXITCODE}" EQUAL 0)
68       set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
69       message(STATUS "Performing Test ${VAR} - Success")
70       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
71         "Performing C++ SOURCE FILE Test ${VAR} succeded with the following output:\n"
72         "${OUTPUT}\n"
73         "Return value: ${${VAR}}\n"
74         "Source file was:\n${SOURCE}\n")
75     else()
76       if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
77         set(${VAR} "${${VAR}_EXITCODE}")
78       else()
79         set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
80       endif()
81
82       message(STATUS "Performing Test ${VAR} - Failed")
83       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
84         "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
85         "${OUTPUT}\n"
86         "Return value: ${${VAR}_EXITCODE}\n"
87         "Source file was:\n${SOURCE}\n")
88     endif()
89   endif()
90 endmacro()
91