CheckCCompilerFlag.cmake 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # - Check if the C source code provided in the SOURCE argument compiles.
  2. # CHECK_C_SOURCE_COMPILES(SOURCE VAR)
  3. #
  4. # FLAG - compiler flag to check
  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(CHECK_C_COMPILER_FLAG FLAG VAR)
  15. IF("${VAR}" MATCHES "^${VAR}$")
  16. SET(MACRO_CHECK_FUNCTION_DEFINITIONS
  17. "${FLAG} ${CMAKE_REQUIRED_FLAGS}")
  18. IF(CMAKE_REQUIRED_LIBRARIES)
  19. SET(CHECK_C_COMPILER_FLAG_ADD_LIBRARIES
  20. "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  21. ELSE(CMAKE_REQUIRED_LIBRARIES)
  22. SET(CHECK_C_COMPILER_FLAG_ADD_LIBRARIES)
  23. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  24. IF(CMAKE_REQUIRED_INCLUDES)
  25. SET(CHECK_C_COMPILER_FLAG_ADD_INCLUDES
  26. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  27. ELSE(CMAKE_REQUIRED_INCLUDES)
  28. SET(CHECK_C_COMPILER_FLAG_ADD_INCLUDES)
  29. ENDIF(CMAKE_REQUIRED_INCLUDES)
  30. FILE(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c"
  31. "int main() {return 0;}\n")
  32. MESSAGE(STATUS "Checking if C compiler supports ${FLAG}")
  33. TRY_COMPILE(${VAR}
  34. ${CMAKE_BINARY_DIR}
  35. ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c
  36. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  37. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  38. "${CHECK_C_COMPILER_FLAG_ADD_LIBRARIES}"
  39. "${CHECK_C_COMPILER_FLAG_ADD_INCLUDES}"
  40. OUTPUT_VARIABLE OUTPUT)
  41. IF(${VAR})
  42. SET(${VAR} 1 CACHE INTERNAL "Test ${VAR}")
  43. MESSAGE(STATUS "Checking if C compiler supports ${FLAG} - Success")
  44. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  45. "Performing C SOURCE FILE Test ${VAR} succeded with the following output:\n"
  46. "${OUTPUT}\n"
  47. "Source file was:\n${SOURCE}\n")
  48. ELSE(${VAR})
  49. MESSAGE(STATUS "Checking if C compiler supports ${FLAG} - Failed")
  50. SET(${VAR} "" CACHE INTERNAL "Test ${VAR}")
  51. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  52. "Performing C SOURCE FILE Test ${VAR} failed with the following output:\n"
  53. "${OUTPUT}\n"
  54. "Source file was:\n${SOURCE}\n")
  55. ENDIF(${VAR})
  56. ENDIF("${VAR}" MATCHES "^${VAR}$")
  57. ENDMACRO(CHECK_C_COMPILER_FLAG)