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)