CMakeLists.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 ()
  7. endmacro()
  8. macro(TEST_FAIL value msg)
  9. if (${value})
  10. message (SEND_ERROR "Failing test succeeded:" ${msg} )
  11. endif ()
  12. endmacro()
  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()
  17. endmacro()
  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()
  22. endmacro()
  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()
  34. if(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
  35. message(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfPass\" failed")
  36. else()
  37. file(REMOVE "${TryCompile_BINARY_DIR}/CopyOfPass")
  38. endif()
  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()
  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()
  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()
  63. if(NOT SHOULD_FAIL)
  64. if(SHOULD_PASS)
  65. message("All Tests passed, ignore all previous output.")
  66. else()
  67. message("Test failed")
  68. endif()
  69. else()
  70. message("Test failed")
  71. endif()
  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()
  78. message("Compiler does not support ansi for scope")
  79. endif()
  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()
  86. message("Compiler does not support ansi for scope")
  87. endif()
  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()
  93. message("Compiler does not support ansi for scope")
  94. endif()
  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()
  115. if(NOT "${SHOULD_RUN}" STREQUAL "0")
  116. message(SEND_ERROR "exit_success failed running with exit code ${SHOULD_RUN}")
  117. endif()
  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()
  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()
  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()
  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()
  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()
  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()
  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()
  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()
  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()
  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")
  198. if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
  199. unset(C_STRICT_PROTOTYPES CACHE)
  200. CHECK_C_COMPILER_FLAG("-Werror;-Wstrict-prototypes" C_STRICT_PROTOTYPES)
  201. TEST_ASSERT(C_STRICT_PROTOTYPES "CHECK_C_COMPILER_FLAG failed -Werror -Wstrict-prototypes")
  202. endif()
  203. #######################################################################
  204. #
  205. # also test that the check_prototype_definition macro works
  206. include(CheckPrototypeDefinition)
  207. check_prototype_definition(remove
  208. "int remove(const char *pathname)"
  209. "0"
  210. "stdio.h"
  211. TEST_REMOVE_PROTO)
  212. test_assert(TEST_REMOVE_PROTO "check_prototype_definition for remove() failed")