CheckCXXSourceRuns.cmake 3.8 KB
Newer Older
1 2 3 4 5 6
#.rst:
# CheckCXXSourceRuns
# ------------------
#
# Check if the given C++ source code compiles and runs.
#
7
# CHECK_CXX_SOURCE_RUNS(<code> <var>)
8 9 10 11 12 13
#
# ::
#
#   <code>   - source code to try to compile
#   <var>    - variable to store the result
#              (1 for success, empty for failure)
14
#              Will be created as an internal cache variable.
15 16 17 18 19 20 21 22 23 24
#
# The following variables may be set before calling this macro to modify
# the way the check is run:
#
# ::
#
#   CMAKE_REQUIRED_FLAGS = string of compile command line flags
#   CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
#   CMAKE_REQUIRED_INCLUDES = list of include directories
#   CMAKE_REQUIRED_LIBRARIES = list of libraries to link
25
#   CMAKE_REQUIRED_QUIET = execute quietly without messages
26

27 28 29 30 31 32 33 34 35 36
#=============================================================================
# Copyright 2006-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
37
# (To distribute this file outside of CMake, substitute the full
38 39
#  License text for the above reference.)

40 41


42
macro(CHECK_CXX_SOURCE_RUNS SOURCE VAR)
43
  if(NOT DEFINED "${VAR}")
44
    set(MACRO_CHECK_FUNCTION_DEFINITIONS
45
      "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
46 47
    if(CMAKE_REQUIRED_LIBRARIES)
      set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES
48
        LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
49
    else()
50
      set(CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES)
51
    endif()
52 53
    if(CMAKE_REQUIRED_INCLUDES)
      set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES
54
        "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
55
    else()
56
      set(CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES)
57
    endif()
58
    file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx"
59 60
      "${SOURCE}\n")

61 62 63
    if(NOT CMAKE_REQUIRED_QUIET)
      message(STATUS "Performing Test ${VAR}")
    endif()
64
    try_run(${VAR}_EXITCODE ${VAR}_COMPILED
65 66 67
      ${CMAKE_BINARY_DIR}
      ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx
      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
68
      ${CHECK_CXX_SOURCE_COMPILES_ADD_LIBRARIES}
69
      CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
70
      -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
71
      "${CHECK_CXX_SOURCE_COMPILES_ADD_INCLUDES}"
72 73
      COMPILE_OUTPUT_VARIABLE OUTPUT)

74
    # if it did not compile make the return value fail code of 1
75 76
    if(NOT ${VAR}_COMPILED)
      set(${VAR}_EXITCODE 1)
77
    endif()
78
    # if the return value was 0 then it worked
79 80
    if("${${VAR}_EXITCODE}" EQUAL 0)
      set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
81 82 83
      if(NOT CMAKE_REQUIRED_QUIET)
        message(STATUS "Performing Test ${VAR} - Success")
      endif()
84
      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
85
        "Performing C++ SOURCE FILE Test ${VAR} succeeded with the following output:\n"
86
        "${OUTPUT}\n"
87 88
        "Return value: ${${VAR}}\n"
        "Source file was:\n${SOURCE}\n")
89
    else()
90 91
      if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES  "FAILED_TO_RUN")
        set(${VAR} "${${VAR}_EXITCODE}")
92
      else()
93
        set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
94
      endif()
95

96 97 98
      if(NOT CMAKE_REQUIRED_QUIET)
        message(STATUS "Performing Test ${VAR} - Failed")
      endif()
99
      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
100
        "Performing C++ SOURCE FILE Test ${VAR} failed with the following output:\n"
101
        "${OUTPUT}\n"
102
        "Return value: ${${VAR}_EXITCODE}\n"
103
        "Source file was:\n${SOURCE}\n")
104 105 106
    endif()
  endif()
endmacro()
107