CMakeLists.txt 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. PROJECT(TryCompile)
  2. MACRO(TEST_ASSERT value msg)
  3. IF (NOT ${value})
  4. MESSAGE (SEND_ERROR "Assertion failure:" ${msg} )
  5. ENDIF (NOT ${value})
  6. ENDMACRO(TEST_ASSERT)
  7. MACRO(TEST_FAIL value msg)
  8. IF (${value})
  9. MESSAGE (SEND_ERROR "Failing test succeeded:" ${msg} )
  10. ENDIF (${value})
  11. ENDMACRO(TEST_FAIL)
  12. MACRO(TEST_EXPECT_EXACT command expected)
  13. IF(NOT "x${result}" STREQUAL "x${expected}")
  14. MESSAGE(SEND_ERROR "${CMAKE_CURRENT_LIST_LINE}: TEST \"${command}\" failed: \"${result}\" expected: \"${expected}\"")
  15. ENDIF(NOT "x${result}" STREQUAL "x${expected}")
  16. ENDMACRO(TEST_EXPECT_EXACT command expected)
  17. MACRO(TEST_EXPECT_CONTAINS command expected)
  18. IF(NOT "${result}" MATCHES "${expected}")
  19. MESSAGE(SEND_ERROR "${CMAKE_CURRENT_LIST_LINE}: TEST \"${command}\" failed: \"${result}\" expected: \"${expected}\"")
  20. ENDIF(NOT "${result}" MATCHES "${expected}")
  21. ENDMACRO(TEST_EXPECT_CONTAINS command expected)
  22. # try to compile a file that should compile
  23. # also check that COPY_FILE works
  24. TRY_COMPILE(SHOULD_PASS
  25. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  26. ${TryCompile_SOURCE_DIR}/pass.c
  27. OUTPUT_VARIABLE TRY_OUT
  28. COPY_FILE ${TryCompile_BINARY_DIR}/CopyOfPass
  29. )
  30. IF(NOT SHOULD_PASS)
  31. MESSAGE(SEND_ERROR "should pass failed ${TRY_OUT}")
  32. ENDIF(NOT SHOULD_PASS)
  33. IF(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
  34. MESSAGE(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfPass\" failed")
  35. ELSE(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
  36. FILE(REMOVE "${TryCompile_BINARY_DIR}/CopyOfPass")
  37. ENDIF(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
  38. # try to compile a file that should not compile
  39. TRY_COMPILE(SHOULD_FAIL
  40. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  41. ${TryCompile_SOURCE_DIR}/fail.c
  42. OUTPUT_VARIABLE TRY_OUT)
  43. IF(SHOULD_FAIL)
  44. MESSAGE(SEND_ERROR "Should fail passed ${TRY_OUT}")
  45. ENDIF(SHOULD_FAIL)
  46. # try to compile a file that should compile
  47. TRY_COMPILE(SHOULD_PASS
  48. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  49. ${TryCompile_SOURCE_DIR}/pass.c
  50. OUTPUT_VARIABLE TRY_OUT)
  51. IF(NOT SHOULD_PASS)
  52. MESSAGE(SEND_ERROR "should pass failed ${TRY_OUT}")
  53. ENDIF(NOT SHOULD_PASS)
  54. # try to compile a file that should not compile
  55. TRY_COMPILE(SHOULD_FAIL
  56. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  57. ${TryCompile_SOURCE_DIR}/fail.c
  58. OUTPUT_VARIABLE TRY_OUT)
  59. IF(SHOULD_FAIL)
  60. MESSAGE(SEND_ERROR "Should fail passed ${TRY_OUT}")
  61. ENDIF(SHOULD_FAIL)
  62. IF(NOT SHOULD_FAIL)
  63. IF(SHOULD_PASS)
  64. MESSAGE("All Tests passed, ignore all previous output.")
  65. ELSE(SHOULD_PASS)
  66. MESSAGE("Test failed")
  67. ENDIF(SHOULD_PASS)
  68. ELSE(NOT SHOULD_FAIL)
  69. MESSAGE("Test failed")
  70. ENDIF(NOT SHOULD_FAIL)
  71. TRY_COMPILE(CMAKE_ANSI_FOR_SCOPE
  72. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  73. ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
  74. IF (CMAKE_ANSI_FOR_SCOPE)
  75. MESSAGE("Compiler supports ansi for")
  76. ELSE(CMAKE_ANSI_FOR_SCOPE)
  77. MESSAGE("Compiler does not support ansi for scope")
  78. ENDIF(CMAKE_ANSI_FOR_SCOPE)
  79. TRY_COMPILE(CMAKE_ANSI_FOR_SCOPE
  80. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  81. ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
  82. IF (CMAKE_ANSI_FOR_SCOPE)
  83. MESSAGE("Compiler supports ansi for")
  84. ELSE(CMAKE_ANSI_FOR_SCOPE)
  85. MESSAGE("Compiler does not support ansi for scope")
  86. ENDIF(CMAKE_ANSI_FOR_SCOPE)
  87. MESSAGE("use the module now")
  88. INCLUDE(${CMAKE_ROOT}/Modules/TestForANSIForScope.cmake)
  89. IF (CMAKE_ANSI_FOR_SCOPE)
  90. MESSAGE("Compiler supports ansi for")
  91. ELSE(CMAKE_ANSI_FOR_SCOPE)
  92. MESSAGE("Compiler does not support ansi for scope")
  93. ENDIF(CMAKE_ANSI_FOR_SCOPE)
  94. ADD_EXECUTABLE(TryCompile pass.c)
  95. ######################################
  96. # now two tests for TRY_RUN
  97. # try to run a file that should compile and run without error
  98. # also check that OUTPUT_VARIABLE contains both the compile output
  99. # and the run output
  100. TRY_RUN(SHOULD_RUN SHOULD_COMPILE
  101. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  102. ${TryCompile_SOURCE_DIR}/exit_success.c
  103. OUTPUT_VARIABLE TRY_OUT)
  104. IF(NOT SHOULD_COMPILE)
  105. MESSAGE(SEND_ERROR "exit_success failed compiling: ${TRY_OUT}")
  106. ENDIF(NOT SHOULD_COMPILE)
  107. IF(NOT "${SHOULD_RUN}" STREQUAL "0")
  108. MESSAGE(SEND_ERROR "exit_success failed running with exit code ${SHOULD_RUN}")
  109. ENDIF(NOT "${SHOULD_RUN}" STREQUAL "0")
  110. # check the compile output for the filename
  111. IF(NOT "${TRY_OUT}" MATCHES "exit_success")
  112. MESSAGE(SEND_ERROR " TRY_OUT didn't contain \"exit_success\": \"${TRY_OUT}\"")
  113. ENDIF(NOT "${TRY_OUT}" MATCHES "exit_success")
  114. # check the run output
  115. IF(NOT "${TRY_OUT}" MATCHES "hello world")
  116. MESSAGE(SEND_ERROR " TRY_OUT didn't contain \"hello world\": \"${TRY_OUT}\"")
  117. ENDIF(NOT "${TRY_OUT}" MATCHES "hello world")
  118. # try to run a file that should compile and run, but return an error
  119. TRY_RUN(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
  120. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  121. ${TryCompile_SOURCE_DIR}/exit_with_error.c
  122. COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
  123. RUN_OUTPUT_VARIABLE RUN_OUTPUT)
  124. IF(NOT SHOULD_COMPILE)
  125. MESSAGE(STATUS " exit_with_error failed compiling: ${COMPILE_OUTPUT}")
  126. ENDIF(NOT SHOULD_COMPILE)
  127. IF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
  128. MESSAGE(SEND_ERROR " exit_with_error passed with exit code ${SHOULD_EXIT_WITH_ERROR}")
  129. ENDIF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
  130. # check the compile output, it should contain the filename
  131. IF(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
  132. MESSAGE(SEND_ERROR " COMPILE_OUT didn't contain \"exit_with_error\": \"${COMPILE_OUTPUT}\"")
  133. ENDIF(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
  134. #... but not the run time output
  135. IF("${COMPILE_OUTPUT}" MATCHES "hello world")
  136. MESSAGE(SEND_ERROR " COMPILE_OUT contains the run output: \"${COMPILE_OUTPUT}\"")
  137. ENDIF("${COMPILE_OUTPUT}" MATCHES "hello world")
  138. # check the run output, it should stdout
  139. IF(NOT "${RUN_OUTPUT}" MATCHES "hello world")
  140. MESSAGE(SEND_ERROR " RUN_OUTPUT didn't contain \"hello world\": \"${RUN_OUTPUT}\"")
  141. ENDIF(NOT "${RUN_OUTPUT}" MATCHES "hello world")
  142. #######################################################################
  143. #
  144. # also test that the CHECK_C_SOURCE_COMPILES, CHECK_CXX_SOURCE_COMPILES
  145. # CHECK_C_SOURCE_RUNS and CHECK_CXX_SOURCE_RUNS macros work
  146. INCLUDE(CheckCSourceCompiles)
  147. INCLUDE(CheckCXXSourceCompiles)
  148. INCLUDE(CheckCSourceRuns)
  149. INCLUDE(CheckCXXSourceRuns)
  150. CHECK_C_SOURCE_COMPILES("I dont build" C_BUILD_SHOULD_FAIL)
  151. CHECK_C_SOURCE_COMPILES("int main() {return 0;}" C_BUILD_SHOULD_WORK)
  152. CHECK_C_SOURCE_RUNS("int main() {return 1;}" C_RUN_SHOULD_FAIL)
  153. CHECK_C_SOURCE_RUNS("int main() {return 0;}" C_RUN_SHOULD_WORK)
  154. TEST_FAIL(C_BUILD_SHOULD_FAIL "CHECK_C_SOURCE_COMPILES() succeeded, but should have failed")
  155. TEST_ASSERT(C_BUILD_SHOULD_WORK "CHECK_C_SOURCE_COMPILES() failed")
  156. TEST_FAIL(C_RUN_SHOULD_FAIL "CHECK_C_SOURCE_RUNS() succeeded, but should have failed")
  157. TEST_ASSERT(C_RUN_SHOULD_WORK "CHECK_C_SOURCE_RUNS() failed")
  158. CHECK_CXX_SOURCE_COMPILES("I dont build" CXX_BUILD_SHOULD_FAIL)
  159. CHECK_CXX_SOURCE_COMPILES("int main() {return 0;}" CXX_BUILD_SHOULD_WORK)
  160. CHECK_CXX_SOURCE_RUNS("int main() {return 2;}" CXX_RUN_SHOULD_FAIL)
  161. CHECK_CXX_SOURCE_RUNS("int main() {return 0;}" CXX_RUN_SHOULD_WORK)
  162. TEST_FAIL(CXX_BUILD_SHOULD_FAIL "CHECK_CXX_SOURCE_COMPILES() succeeded, but should have failed")
  163. TEST_ASSERT(CXX_BUILD_SHOULD_WORK "CHECK_CXX_SOURCE_COMPILES() failed")
  164. TEST_FAIL(CXX_RUN_SHOULD_FAIL "CHECK_CXX_SOURCE_RUNS() succeeded, but should have failed")
  165. TEST_ASSERT(CXX_RUN_SHOULD_WORK "CHECK_CXX_SOURCE_RUNS() failed")