CurlCheckCSourceCompiles.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # - Check if the source code provided in the SOURCE argument compiles.
  2. # CURL_CHECK_C_SOURCE_COMPILES(SOURCE VAR)
  3. # - macro which checks if the source code compiles
  4. # SOURCE - source code to try to compile
  5. # VAR - variable to store whether the source code compiled
  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_COMPILES 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_COMPILE(${VAR}
  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(${VAR})
  58. SET(${VAR} 1 CACHE INTERNAL "Test ${message}")
  59. MESSAGE(STATUS "Performing Test ${message} - Success")
  60. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  61. "Performing C SOURCE FILE Test ${message} succeded with the following output:\n"
  62. "${OUTPUT}\n"
  63. "Source file was:\n${src}\n")
  64. ELSE(${VAR})
  65. MESSAGE(STATUS "Performing Test ${message} - Failed")
  66. SET(${VAR} "" CACHE INTERNAL "Test ${message}")
  67. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  68. "Performing C SOURCE FILE Test ${message} failed with the following output:\n"
  69. "${OUTPUT}\n"
  70. "Source file was:\n${src}\n")
  71. ENDIF(${VAR})
  72. ENDIF("${VAR}" MATCHES "^${VAR}$" OR "${VAR}" MATCHES "UNKNOWN")
  73. ENDMACRO(CURL_CHECK_C_SOURCE_COMPILES)