CMakeLists.txt 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. MESSAGE("Testing try_compile project mode")
  96. TRY_COMPILE(TEST_INNER
  97. ${TryCompile_BINARY_DIR}/CMakeFiles/Inner
  98. ${TryCompile_SOURCE_DIR}/Inner
  99. TryCompileInner innerexe
  100. OUTPUT_VARIABLE output)
  101. TEST_ASSERT(TEST_INNER "try_compile project mode failed:\n${output}")
  102. ADD_EXECUTABLE(TryCompile pass.c)
  103. ######################################
  104. # now two tests for TRY_RUN
  105. # try to run a file that should compile and run without error
  106. # also check that OUTPUT_VARIABLE contains both the compile output
  107. # and the run output
  108. TRY_RUN(SHOULD_RUN SHOULD_COMPILE
  109. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  110. ${TryCompile_SOURCE_DIR}/exit_success.c
  111. OUTPUT_VARIABLE TRY_OUT)
  112. IF(NOT SHOULD_COMPILE)
  113. MESSAGE(SEND_ERROR "exit_success failed compiling: ${TRY_OUT}")
  114. ENDIF(NOT SHOULD_COMPILE)
  115. IF(NOT "${SHOULD_RUN}" STREQUAL "0")
  116. MESSAGE(SEND_ERROR "exit_success failed running with exit code ${SHOULD_RUN}")
  117. ENDIF(NOT "${SHOULD_RUN}" STREQUAL "0")
  118. # check the compile output for the filename
  119. IF(NOT "${TRY_OUT}" MATCHES "exit_success")
  120. MESSAGE(SEND_ERROR " TRY_OUT didn't contain \"exit_success\": \"${TRY_OUT}\"")
  121. ENDIF(NOT "${TRY_OUT}" MATCHES "exit_success")
  122. # check the run output
  123. IF(NOT "${TRY_OUT}" MATCHES "hello world")
  124. MESSAGE(SEND_ERROR " TRY_OUT didn't contain \"hello world\": \"${TRY_OUT}\"")
  125. ENDIF(NOT "${TRY_OUT}" MATCHES "hello world")
  126. TRY_RUN(ARG_TEST_RUN ARG_TEST_COMPILE
  127. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  128. ${TryCompile_SOURCE_DIR}/expect_arg.c
  129. OUTPUT_VARIABLE TRY_OUT
  130. ARGS arg1 arg2)
  131. IF(NOT ARG_TEST_COMPILE)
  132. MESSAGE(SEND_ERROR "expect_arg failed compiling: ${TRY_OUT}")
  133. ENDIF(NOT ARG_TEST_COMPILE)
  134. IF(NOT "${ARG_TEST_RUN}" STREQUAL "0")
  135. MESSAGE(SEND_ERROR "expect_arg failed running with exit code ${ARG_TEST_RUN} ${TRY_OUT}")
  136. ENDIF(NOT "${ARG_TEST_RUN}" STREQUAL "0")
  137. # try to run a file that should compile and run, but return an error
  138. TRY_RUN(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
  139. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  140. ${TryCompile_SOURCE_DIR}/exit_with_error.c
  141. COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
  142. RUN_OUTPUT_VARIABLE RUN_OUTPUT)
  143. IF(NOT SHOULD_COMPILE)
  144. MESSAGE(STATUS " exit_with_error failed compiling: ${COMPILE_OUTPUT}")
  145. ENDIF(NOT SHOULD_COMPILE)
  146. IF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
  147. MESSAGE(SEND_ERROR " exit_with_error passed with exit code ${SHOULD_EXIT_WITH_ERROR}")
  148. ENDIF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
  149. # check the compile output, it should contain the filename
  150. IF(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
  151. MESSAGE(SEND_ERROR " COMPILE_OUT didn't contain \"exit_with_error\": \"${COMPILE_OUTPUT}\"")
  152. ENDIF(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
  153. #... but not the run time output
  154. IF("${COMPILE_OUTPUT}" MATCHES "hello world")
  155. MESSAGE(SEND_ERROR " COMPILE_OUT contains the run output: \"${COMPILE_OUTPUT}\"")
  156. ENDIF("${COMPILE_OUTPUT}" MATCHES "hello world")
  157. # check the run output, it should stdout
  158. IF(NOT "${RUN_OUTPUT}" MATCHES "hello world")
  159. MESSAGE(SEND_ERROR " RUN_OUTPUT didn't contain \"hello world\": \"${RUN_OUTPUT}\"")
  160. ENDIF(NOT "${RUN_OUTPUT}" MATCHES "hello world")
  161. #######################################################################
  162. #
  163. # also test that the CHECK_C_SOURCE_COMPILES, CHECK_CXX_SOURCE_COMPILES
  164. # CHECK_C_SOURCE_RUNS and CHECK_CXX_SOURCE_RUNS macros work
  165. INCLUDE(CheckCSourceCompiles)
  166. INCLUDE(CheckCXXSourceCompiles)
  167. INCLUDE(CheckCSourceRuns)
  168. INCLUDE(CheckCXXSourceRuns)
  169. CHECK_C_SOURCE_COMPILES("I dont build" C_BUILD_SHOULD_FAIL)
  170. CHECK_C_SOURCE_COMPILES("int main() {return 0;}" C_BUILD_SHOULD_WORK)
  171. CHECK_C_SOURCE_RUNS("int main() {return 1;}" C_RUN_SHOULD_FAIL)
  172. CHECK_C_SOURCE_RUNS("int main() {return 0;}" C_RUN_SHOULD_WORK)
  173. TEST_FAIL(C_BUILD_SHOULD_FAIL "CHECK_C_SOURCE_COMPILES() succeeded, but should have failed")
  174. TEST_ASSERT(C_BUILD_SHOULD_WORK "CHECK_C_SOURCE_COMPILES() failed")
  175. TEST_FAIL(C_RUN_SHOULD_FAIL "CHECK_C_SOURCE_RUNS() succeeded, but should have failed")
  176. TEST_ASSERT(C_RUN_SHOULD_WORK "CHECK_C_SOURCE_RUNS() failed")
  177. CHECK_CXX_SOURCE_COMPILES("I dont build" CXX_BUILD_SHOULD_FAIL)
  178. CHECK_CXX_SOURCE_COMPILES("int main() {return 0;}" CXX_BUILD_SHOULD_WORK)
  179. CHECK_CXX_SOURCE_RUNS("int main() {return 2;}" CXX_RUN_SHOULD_FAIL)
  180. CHECK_CXX_SOURCE_RUNS("int main() {return 0;}" CXX_RUN_SHOULD_WORK)
  181. TEST_FAIL(CXX_BUILD_SHOULD_FAIL "CHECK_CXX_SOURCE_COMPILES() succeeded, but should have failed")
  182. TEST_ASSERT(CXX_BUILD_SHOULD_WORK "CHECK_CXX_SOURCE_COMPILES() failed")
  183. TEST_FAIL(CXX_RUN_SHOULD_FAIL "CHECK_CXX_SOURCE_RUNS() succeeded, but should have failed")
  184. TEST_ASSERT(CXX_RUN_SHOULD_WORK "CHECK_CXX_SOURCE_RUNS() failed")
  185. FOREACH(lang C CXX)
  186. IF(NOT "${CMAKE_${lang}_COMPILER_ID}" MATCHES "^(PathScale)$")
  187. SET(${lang}_DD --)
  188. ENDIF()
  189. ENDFOREACH()
  190. UNSET(C_BOGUS_FLAG CACHE)
  191. INCLUDE(CheckCCompilerFlag)
  192. CHECK_C_COMPILER_FLAG(${C_DD}-_this_is_not_a_flag_ C_BOGUS_FLAG)
  193. TEST_FAIL(C_BOGUS_FLAG "CHECK_C_COMPILER_FLAG() succeeded, but should have failed")
  194. UNSET(CXX_BOGUS_FLAG CACHE)
  195. INCLUDE(CheckCXXCompilerFlag)
  196. CHECK_CXX_COMPILER_FLAG(${CXX_DD}-_this_is_not_a_flag_ CXX_BOGUS_FLAG)
  197. TEST_FAIL(CXX_BOGUS_FLAG "CHECK_CXX_COMPILER_FLAG() succeeded, but should have failed")