CMakeLists.txt 7.5 KB

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