CMakeLists.txt 10 KB

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