CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 compile
  40. # also check that COPY_FILE_ERROR works
  41. file(WRITE ${TryCompile_BINARY_DIR}/invalid "")
  42. try_compile(SHOULD_PASS
  43. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  44. ${TryCompile_SOURCE_DIR}/pass.c
  45. OUTPUT_VARIABLE TRY_OUT
  46. COPY_FILE ${TryCompile_BINARY_DIR}/invalid/path
  47. COPY_FILE_ERROR _captured
  48. )
  49. if(NOT SHOULD_PASS)
  50. message(SEND_ERROR "should pass failed ${TRY_OUT}")
  51. endif()
  52. if(NOT _captured MATCHES "Cannot copy output executable.*/invalid/path")
  53. message(SEND_ERROR "COPY_FILE_ERROR did not capture expected message")
  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. # try to compile a file that should compile
  64. try_compile(SHOULD_PASS
  65. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  66. ${TryCompile_SOURCE_DIR}/pass.c
  67. OUTPUT_VARIABLE TRY_OUT)
  68. if(NOT SHOULD_PASS)
  69. message(SEND_ERROR "should pass failed ${TRY_OUT}")
  70. endif()
  71. # try to compile a file that should not compile
  72. try_compile(SHOULD_FAIL
  73. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  74. ${TryCompile_SOURCE_DIR}/fail.c
  75. OUTPUT_VARIABLE TRY_OUT)
  76. if(SHOULD_FAIL)
  77. message(SEND_ERROR "Should fail passed ${TRY_OUT}")
  78. endif()
  79. # try to compile two files that should compile
  80. try_compile(SHOULD_PASS
  81. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  82. SOURCES ${TryCompile_SOURCE_DIR}/pass2a.c ${TryCompile_SOURCE_DIR}/pass2b.cxx
  83. OUTPUT_VARIABLE TRY_OUT)
  84. if(NOT SHOULD_PASS)
  85. message(SEND_ERROR "should pass failed ${TRY_OUT}")
  86. endif()
  87. # try to compile two files that should not compile
  88. try_compile(SHOULD_FAIL
  89. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  90. SOURCES ${TryCompile_SOURCE_DIR}/fail2a.c ${TryCompile_SOURCE_DIR}/fail2b.c
  91. OUTPUT_VARIABLE TRY_OUT)
  92. if(SHOULD_FAIL)
  93. message(SEND_ERROR "Should fail passed ${TRY_OUT}")
  94. endif()
  95. # try to compile a file that should compile
  96. set(_c_flags "${CMAKE_C_FLAGS}")
  97. if(WATCOM)
  98. string(APPEND CMAKE_C_FLAGS " -dTESTDEF")
  99. else()
  100. string(APPEND CMAKE_C_FLAGS " \"-DTESTDEF\"")
  101. endif()
  102. try_compile(SHOULD_PASS
  103. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  104. ${TryCompile_SOURCE_DIR}/testdef.c
  105. OUTPUT_VARIABLE TRY_OUT)
  106. if(NOT SHOULD_PASS)
  107. message(SEND_ERROR "should pass failed ${TRY_OUT}")
  108. endif()
  109. set(CMAKE_C_FLAGS "${_c_flags}")
  110. if(NOT SHOULD_FAIL)
  111. if(SHOULD_PASS)
  112. message("All Tests passed, ignore all previous output.")
  113. else()
  114. message("Test failed")
  115. endif()
  116. else()
  117. message("Test failed")
  118. endif()
  119. try_compile(CMAKE_ANSI_FOR_SCOPE
  120. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  121. ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
  122. if (CMAKE_ANSI_FOR_SCOPE)
  123. message("Compiler supports ansi for")
  124. else()
  125. message("Compiler does not support ansi for scope")
  126. endif()
  127. try_compile(CMAKE_ANSI_FOR_SCOPE
  128. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  129. ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
  130. if (CMAKE_ANSI_FOR_SCOPE)
  131. message("Compiler supports ansi for")
  132. else()
  133. message("Compiler does not support ansi for scope")
  134. endif()
  135. message("use the module now")
  136. include(${CMAKE_ROOT}/Modules/TestForANSIForScope.cmake)
  137. if (CMAKE_ANSI_FOR_SCOPE)
  138. message("Compiler supports ansi for")
  139. else()
  140. message("Compiler does not support ansi for scope")
  141. endif()
  142. message("Testing try_compile project mode")
  143. try_compile(TEST_INNER
  144. ${TryCompile_BINARY_DIR}/CMakeFiles/Inner
  145. ${TryCompile_SOURCE_DIR}/Inner
  146. TryCompileInner innerexe
  147. OUTPUT_VARIABLE output)
  148. TEST_ASSERT(TEST_INNER "try_compile project mode failed:\n${output}")
  149. add_executable(TryCompile pass.c)
  150. ######################################
  151. # now two tests for TRY_RUN
  152. # try to run a file that should compile and run without error
  153. # also check that OUTPUT_VARIABLE contains both the compile output
  154. # and the run output
  155. try_run(SHOULD_RUN SHOULD_COMPILE
  156. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  157. ${TryCompile_SOURCE_DIR}/exit_success.c
  158. OUTPUT_VARIABLE TRY_OUT)
  159. if(NOT SHOULD_COMPILE)
  160. message(SEND_ERROR "exit_success failed compiling: ${TRY_OUT}")
  161. endif()
  162. if(NOT "${SHOULD_RUN}" STREQUAL "0")
  163. message(SEND_ERROR "exit_success failed running with exit code ${SHOULD_RUN}")
  164. endif()
  165. # check the compile output for the filename
  166. if(NOT "${TRY_OUT}" MATCHES "exit_success")
  167. message(SEND_ERROR " TRY_OUT didn't contain \"exit_success\": \"${TRY_OUT}\"")
  168. endif()
  169. # check the run output
  170. if(NOT "${TRY_OUT}" MATCHES "hello world")
  171. message(SEND_ERROR " TRY_OUT didn't contain \"hello world\": \"${TRY_OUT}\"")
  172. endif()
  173. try_run(ARG_TEST_RUN ARG_TEST_COMPILE
  174. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  175. ${TryCompile_SOURCE_DIR}/expect_arg.c
  176. OUTPUT_VARIABLE TRY_OUT
  177. ARGS arg1 arg2)
  178. if(NOT ARG_TEST_COMPILE)
  179. message(SEND_ERROR "expect_arg failed compiling: ${TRY_OUT}")
  180. endif()
  181. if(NOT "${ARG_TEST_RUN}" STREQUAL "0")
  182. message(SEND_ERROR "expect_arg failed running with exit code ${ARG_TEST_RUN} ${TRY_OUT}")
  183. endif()
  184. # try to run a file that should compile and run, but return an error
  185. try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
  186. ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
  187. ${TryCompile_SOURCE_DIR}/exit_with_error.c
  188. COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
  189. RUN_OUTPUT_VARIABLE RUN_OUTPUT)
  190. if(NOT SHOULD_COMPILE)
  191. message(STATUS " exit_with_error failed compiling: ${COMPILE_OUTPUT}")
  192. endif()
  193. if("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
  194. message(SEND_ERROR " exit_with_error passed with exit code ${SHOULD_EXIT_WITH_ERROR}")
  195. endif()
  196. # check the compile output, it should contain the filename
  197. if(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
  198. message(SEND_ERROR " COMPILE_OUT didn't contain \"exit_with_error\": \"${COMPILE_OUTPUT}\"")
  199. endif()
  200. #... but not the run time output
  201. if("${COMPILE_OUTPUT}" MATCHES "hello world")
  202. message(SEND_ERROR " COMPILE_OUT contains the run output: \"${COMPILE_OUTPUT}\"")
  203. endif()
  204. # check the run output, it should stdout
  205. if(NOT "${RUN_OUTPUT}" MATCHES "hello world")
  206. message(SEND_ERROR " RUN_OUTPUT didn't contain \"hello world\": \"${RUN_OUTPUT}\"")
  207. endif()
  208. #######################################################################
  209. #
  210. # also test that the CHECK_C_SOURCE_COMPILES, CHECK_CXX_SOURCE_COMPILES
  211. # CHECK_C_SOURCE_RUNS and CHECK_CXX_SOURCE_RUNS macros work
  212. include(CheckCSourceCompiles)
  213. include(CheckCXXSourceCompiles)
  214. include(CheckCSourceRuns)
  215. include(CheckCXXSourceRuns)
  216. CHECK_C_SOURCE_COMPILES("I don't build" C_BUILD_SHOULD_FAIL)
  217. CHECK_C_SOURCE_COMPILES("int main() {return 0;}" C_BUILD_SHOULD_WORK)
  218. CHECK_C_SOURCE_RUNS("int main() {return 1;}" C_RUN_SHOULD_FAIL)
  219. CHECK_C_SOURCE_RUNS("int main() {return 0;}" C_RUN_SHOULD_WORK)
  220. TEST_FAIL(C_BUILD_SHOULD_FAIL "CHECK_C_SOURCE_COMPILES() succeeded, but should have failed")
  221. TEST_ASSERT(C_BUILD_SHOULD_WORK "CHECK_C_SOURCE_COMPILES() failed")
  222. TEST_FAIL(C_RUN_SHOULD_FAIL "CHECK_C_SOURCE_RUNS() succeeded, but should have failed")
  223. TEST_ASSERT(C_RUN_SHOULD_WORK "CHECK_C_SOURCE_RUNS() failed")
  224. CHECK_CXX_SOURCE_COMPILES("I don't build" CXX_BUILD_SHOULD_FAIL)
  225. CHECK_CXX_SOURCE_COMPILES("int main() {return 0;}" CXX_BUILD_SHOULD_WORK)
  226. CHECK_CXX_SOURCE_RUNS("int main() {return 2;}" CXX_RUN_SHOULD_FAIL)
  227. CHECK_CXX_SOURCE_RUNS("int main() {return 0;}" CXX_RUN_SHOULD_WORK)
  228. TEST_FAIL(CXX_BUILD_SHOULD_FAIL "CHECK_CXX_SOURCE_COMPILES() succeeded, but should have failed")
  229. TEST_ASSERT(CXX_BUILD_SHOULD_WORK "CHECK_CXX_SOURCE_COMPILES() failed")
  230. TEST_FAIL(CXX_RUN_SHOULD_FAIL "CHECK_CXX_SOURCE_RUNS() succeeded, but should have failed")
  231. TEST_ASSERT(CXX_RUN_SHOULD_WORK "CHECK_CXX_SOURCE_RUNS() failed")
  232. foreach(lang C CXX)
  233. if(NOT CMAKE_${lang}_COMPILER_ID STREQUAL "PathScale")
  234. set(${lang}_DD --)
  235. endif()
  236. endforeach()
  237. unset(C_BOGUS_FLAG CACHE)
  238. include(CheckCCompilerFlag)
  239. CHECK_C_COMPILER_FLAG(${C_DD}-_this_is_not_a_flag_ C_BOGUS_FLAG)
  240. TEST_FAIL(C_BOGUS_FLAG "CHECK_C_COMPILER_FLAG() succeeded, but should have failed")
  241. unset(CXX_BOGUS_FLAG CACHE)
  242. include(CheckCXXCompilerFlag)
  243. CHECK_CXX_COMPILER_FLAG(${CXX_DD}-_this_is_not_a_flag_ CXX_BOGUS_FLAG)
  244. TEST_FAIL(CXX_BOGUS_FLAG "CHECK_CXX_COMPILER_FLAG() succeeded, but should have failed")
  245. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  246. unset(C_STRICT_PROTOTYPES CACHE)
  247. CHECK_C_COMPILER_FLAG("-Werror;-Wstrict-prototypes" C_STRICT_PROTOTYPES)
  248. TEST_ASSERT(C_STRICT_PROTOTYPES "CHECK_C_COMPILER_FLAG failed -Werror -Wstrict-prototypes")
  249. endif()
  250. #######################################################################
  251. #
  252. # also test that the check_prototype_definition macro works
  253. include(CheckPrototypeDefinition)
  254. check_prototype_definition(remove
  255. "int remove(const char *pathname)"
  256. "0"
  257. "stdio.h"
  258. TEST_REMOVE_PROTO)
  259. test_assert(TEST_REMOVE_PROTO "check_prototype_definition for remove() failed")