CurlCheckCSourceRuns.cmake 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # - Check if the source code provided in the SOURCE argument compiles and runs.
  2. # CURL_CHECK_C_SOURCE_RUNS(SOURCE VAR)
  3. # - macro which checks if the source code runs
  4. # SOURCE - source code to try to compile
  5. # VAR - variable to store size if the type exists.
  6. #
  7. # The following variables may be set before calling this macro to
  8. # modify the way the check is run:
  9. #
  10. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  11. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  12. # CMAKE_REQUIRED_INCLUDES = list of include directories
  13. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  14. macro(CURL_CHECK_C_SOURCE_RUNS SOURCE VAR)
  15. if("${VAR}" MATCHES "^${VAR}$" OR "${VAR}" MATCHES "UNKNOWN")
  16. set(message "${VAR}")
  17. # If the number of arguments is greater than 2 (SOURCE VAR)
  18. if(${ARGC} GREATER 2)
  19. # then add the third argument as a message
  20. set(message "${ARGV2} (${VAR})")
  21. endif(${ARGC} GREATER 2)
  22. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  23. "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
  24. if(CMAKE_REQUIRED_LIBRARIES)
  25. set(CURL_CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES
  26. "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  27. else(CMAKE_REQUIRED_LIBRARIES)
  28. set(CURL_CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES)
  29. endif(CMAKE_REQUIRED_LIBRARIES)
  30. if(CMAKE_REQUIRED_INCLUDES)
  31. set(CURL_CHECK_C_SOURCE_COMPILES_ADD_INCLUDES
  32. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  33. else(CMAKE_REQUIRED_INCLUDES)
  34. set(CURL_CHECK_C_SOURCE_COMPILES_ADD_INCLUDES)
  35. endif(CMAKE_REQUIRED_INCLUDES)
  36. set(src "")
  37. foreach(def ${EXTRA_DEFINES})
  38. set(src "${src}#define ${def} 1\n")
  39. endforeach(def)
  40. foreach(inc ${HEADER_INCLUDES})
  41. set(src "${src}#include <${inc}>\n")
  42. endforeach(inc)
  43. set(src "${src}\nint main() { ${SOURCE} ; return 0; }")
  44. set(CMAKE_CONFIGURABLE_FILE_CONTENT "${src}")
  45. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CMake/CMakeConfigurableFile.in
  46. "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
  47. IMMEDIATE)
  48. message(STATUS "Performing Test ${message}")
  49. try_run(${VAR} ${VAR}_COMPILED
  50. ${CMAKE_BINARY_DIR}
  51. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
  52. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  53. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  54. "${CURL_CHECK_C_SOURCE_COMPILES_ADD_LIBRARIES}"
  55. "${CURL_CHECK_C_SOURCE_COMPILES_ADD_INCLUDES}"
  56. OUTPUT_VARIABLE OUTPUT)
  57. # if it did not compile make the return value fail code of 1
  58. if(NOT ${VAR}_COMPILED)
  59. set(${VAR} 1)
  60. endif(NOT ${VAR}_COMPILED)
  61. # if the return value was 0 then it worked
  62. set(result_var ${${VAR}})
  63. if("${result_var}" EQUAL 0)
  64. set(${VAR} 1 CACHE INTERNAL "Test ${message}")
  65. message(STATUS "Performing Test ${message} - Success")
  66. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  67. "Performing C SOURCE FILE Test ${message} succeded with the following output:\n"
  68. "${OUTPUT}\n"
  69. "Return value: ${${VAR}}\n"
  70. "Source file was:\n${src}\n")
  71. else("${result_var}" EQUAL 0)
  72. message(STATUS "Performing Test ${message} - Failed")
  73. set(${VAR} "" CACHE INTERNAL "Test ${message}")
  74. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  75. "Performing C SOURCE FILE Test ${message} failed with the following output:\n"
  76. "${OUTPUT}\n"
  77. "Return value: ${result_var}\n"
  78. "Source file was:\n${src}\n")
  79. endif("${result_var}" EQUAL 0)
  80. endif("${VAR}" MATCHES "^${VAR}$" OR "${VAR}" MATCHES "UNKNOWN")
  81. endmacro(CURL_CHECK_C_SOURCE_RUNS)