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:
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 2006-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.)
29 macro(CHECK_C_SOURCE_RUNS SOURCE VAR)
30 if("${VAR}" MATCHES "^${VAR}$")
31 set(MACRO_CHECK_FUNCTION_DEFINITIONS
32 "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
33 if(CMAKE_REQUIRED_LIBRARIES)
34 set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
35 LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
37 set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
39 if(CMAKE_REQUIRED_INCLUDES)
40 set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
41 "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
43 set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
45 file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
48 message(STATUS "Performing Test ${VAR}")
49 try_run(${VAR}_EXITCODE ${VAR}_COMPILED
51 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
52 COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
53 ${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}
54 CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
55 -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
56 "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
57 COMPILE_OUTPUT_VARIABLE OUTPUT)
58 # if it did not compile make the return value fail code of 1
59 if(NOT ${VAR}_COMPILED)
60 set(${VAR}_EXITCODE 1)
62 # if the return value was 0 then it worked
63 if("${${VAR}_EXITCODE}" EQUAL 0)
64 set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
65 message(STATUS "Performing Test ${VAR} - Success")
66 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
67 "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
69 "Return value: ${${VAR}}\n"
70 "Source file was:\n${SOURCE}\n")
72 if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
73 set(${VAR} "${${VAR}_EXITCODE}")
75 set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
78 message(STATUS "Performing Test ${VAR} - Failed")
79 file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
80 "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
82 "Return value: ${${VAR}_EXITCODE}\n"
83 "Source file was:\n${SOURCE}\n")