CheckCSourceRuns.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #.rst:
  2. # CheckCSourceRuns
  3. # ----------------
  4. #
  5. # Check if the given C source code compiles and runs.
  6. #
  7. # CHECK_C_SOURCE_RUNS(<code> <var>)
  8. #
  9. # ::
  10. #
  11. # <code> - source code to try to compile
  12. # <var> - variable to store the result
  13. # (1 for success, empty for failure)
  14. #
  15. # The following variables may be set before calling this macro to modify
  16. # the way the check is run:
  17. #
  18. # ::
  19. #
  20. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  21. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  22. # CMAKE_REQUIRED_INCLUDES = list of include directories
  23. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  24. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  25. #=============================================================================
  26. # Copyright 2006-2009 Kitware, Inc.
  27. #
  28. # Distributed under the OSI-approved BSD License (the "License");
  29. # see accompanying file Copyright.txt for details.
  30. #
  31. # This software is distributed WITHOUT ANY WARRANTY; without even the
  32. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  33. # See the License for more information.
  34. #=============================================================================
  35. # (To distribute this file outside of CMake, substitute the full
  36. # License text for the above reference.)
  37. macro(CHECK_C_SOURCE_RUNS SOURCE VAR)
  38. if(NOT DEFINED "${VAR}")
  39. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  40. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  41. if(CMAKE_REQUIRED_LIBRARIES)
  42. set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
  43. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  44. else()
  45. set(CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
  46. endif()
  47. if(CMAKE_REQUIRED_INCLUDES)
  48. set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
  49. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  50. else()
  51. set(CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
  52. endif()
  53. file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
  54. "${SOURCE}\n")
  55. if(NOT CMAKE_REQUIRED_QUIET)
  56. message(STATUS "Performing Test ${VAR}")
  57. endif()
  58. try_run(${VAR}_EXITCODE ${VAR}_COMPILED
  59. ${CMAKE_BINARY_DIR}
  60. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
  61. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  62. ${CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}
  63. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  64. -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
  65. "${CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
  66. COMPILE_OUTPUT_VARIABLE OUTPUT)
  67. # if it did not compile make the return value fail code of 1
  68. if(NOT ${VAR}_COMPILED)
  69. set(${VAR}_EXITCODE 1)
  70. endif()
  71. # if the return value was 0 then it worked
  72. if("${${VAR}_EXITCODE}" EQUAL 0)
  73. set(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  74. if(NOT CMAKE_REQUIRED_QUIET)
  75. message(STATUS "Performing Test ${VAR} - Success")
  76. endif()
  77. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  78. "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
  79. "${OUTPUT}\n"
  80. "Return value: ${${VAR}}\n"
  81. "Source file was:\n${SOURCE}\n")
  82. else()
  83. if(CMAKE_CROSSCOMPILING AND "${${VAR}_EXITCODE}" MATCHES "FAILED_TO_RUN")
  84. set(${VAR} "${${VAR}_EXITCODE}")
  85. else()
  86. set(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  87. endif()
  88. if(NOT CMAKE_REQUIRED_QUIET)
  89. message(STATUS "Performing Test ${VAR} - Failed")
  90. endif()
  91. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  92. "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
  93. "${OUTPUT}\n"
  94. "Return value: ${${VAR}_EXITCODE}\n"
  95. "Source file was:\n${SOURCE}\n")
  96. endif()
  97. endif()
  98. endmacro()