CMakeLists.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. PROJECT(TryCompile)
  2. # try to compile a file that should compile
  3. TRY_COMPILE(SHOULD_PASS
  4. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  5. ${TryCompile_SOURCE_DIR}/pass.c
  6. OUTPUT_VARIABLE TRY_OUT)
  7. IF(NOT SHOULD_PASS)
  8. MESSAGE(SEND_ERROR "should pass failed ${TRY_OUT}")
  9. ENDIF(NOT SHOULD_PASS)
  10. # try to compile a file that should not compile
  11. TRY_COMPILE(SHOULD_FAIL
  12. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  13. ${TryCompile_SOURCE_DIR}/fail.c
  14. OUTPUT_VARIABLE TRY_OUT)
  15. IF(SHOULD_FAIL)
  16. MESSAGE(SEND_ERROR "Should fail passed ${TRY_OUT}")
  17. ENDIF(SHOULD_FAIL)
  18. # try to compile a file that should compile
  19. TRY_COMPILE(SHOULD_PASS
  20. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  21. ${TryCompile_SOURCE_DIR}/pass.c
  22. OUTPUT_VARIABLE TRY_OUT)
  23. IF(NOT SHOULD_PASS)
  24. MESSAGE(SEND_ERROR "should pass failed ${TRY_OUT}")
  25. ENDIF(NOT SHOULD_PASS)
  26. # try to compile a file that should not compile
  27. TRY_COMPILE(SHOULD_FAIL
  28. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  29. ${TryCompile_SOURCE_DIR}/fail.c
  30. OUTPUT_VARIABLE TRY_OUT)
  31. IF(SHOULD_FAIL)
  32. MESSAGE(SEND_ERROR "Should fail passed ${TRY_OUT}")
  33. ENDIF(SHOULD_FAIL)
  34. IF(NOT SHOULD_FAIL)
  35. IF(SHOULD_PASS)
  36. MESSAGE("All Tests passed, ignore all previous output.")
  37. ELSE(SHOULD_PASS)
  38. MESSAGE("Test failed")
  39. ENDIF(SHOULD_PASS)
  40. ELSE(NOT SHOULD_FAIL)
  41. MESSAGE("Test failed")
  42. ENDIF(NOT SHOULD_FAIL)
  43. TRY_COMPILE(CMAKE_ANSI_FOR_SCOPE
  44. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  45. ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
  46. IF (CMAKE_ANSI_FOR_SCOPE)
  47. MESSAGE("Compiler supports ansi for")
  48. ELSE(CMAKE_ANSI_FOR_SCOPE)
  49. MESSAGE("Compiler does not support ansi for scope")
  50. ENDIF(CMAKE_ANSI_FOR_SCOPE)
  51. TRY_COMPILE(CMAKE_ANSI_FOR_SCOPE
  52. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  53. ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
  54. IF (CMAKE_ANSI_FOR_SCOPE)
  55. MESSAGE("Compiler supports ansi for")
  56. ELSE(CMAKE_ANSI_FOR_SCOPE)
  57. MESSAGE("Compiler does not support ansi for scope")
  58. ENDIF(CMAKE_ANSI_FOR_SCOPE)
  59. MESSAGE("use the module now")
  60. INCLUDE(${CMAKE_ROOT}/Modules/TestForANSIForScope.cmake)
  61. IF (CMAKE_ANSI_FOR_SCOPE)
  62. MESSAGE("Compiler supports ansi for")
  63. ELSE(CMAKE_ANSI_FOR_SCOPE)
  64. MESSAGE("Compiler does not support ansi for scope")
  65. ENDIF(CMAKE_ANSI_FOR_SCOPE)
  66. ADD_EXECUTABLE(TryCompile pass.c)
  67. ######################################
  68. # now two tests for TRY_RUN
  69. # try to run a file that should compile and run without error
  70. TRY_RUN(SHOULD_RUN SHOULD_COMPILE
  71. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  72. ${TryCompile_SOURCE_DIR}/exit_success.c
  73. OUTPUT_VARIABLE TRY_OUT)
  74. IF(NOT SHOULD_COMPILE)
  75. MESSAGE(SEND_ERROR "exit_success failed compiling: ${TRY_OUT}")
  76. ENDIF(NOT SHOULD_COMPILE)
  77. IF(NOT "${SHOULD_RUN}" STREQUAL "0")
  78. MESSAGE(SEND_ERROR "exit_success failed running with exit code ${SHOULD_RUN}")
  79. ENDIF(NOT "${SHOULD_RUN}" STREQUAL "0")
  80. # try to run a file that should compile and run, but return an error
  81. TRY_RUN(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
  82. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  83. ${TryCompile_SOURCE_DIR}/exit_with_error.c
  84. OUTPUT_VARIABLE TRY_OUT)
  85. IF(NOT SHOULD_COMPILE)
  86. MESSAGE(STATUS " exit_with_error failed compiling: ${TRY_OUT}")
  87. ENDIF(NOT SHOULD_COMPILE)
  88. IF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
  89. MESSAGE(SEND_ERROR " exit_with_error passed with exit code ${SHOULD_EXIT_WITH_ERROR}")
  90. ENDIF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")