CMakeLists.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #
  2. # A more complex test case
  3. #
  4. SET(CMAKE_BACKWARDS_COMPATIBILITY 1.4)
  5. PROJECT (Complex)
  6. #
  7. # Define a macro
  8. #
  9. MACRO(ASSERT value msg)
  10. IF (NOT ${value})
  11. MESSAGE ("Assertion failure:" ${msg} )
  12. ENDIF (NOT ${value})
  13. ENDMACRO(ASSERT)
  14. # invoke the macro
  15. ASSERT(Complex_BINARY_DIR "The PROJECT command is broken")
  16. #
  17. # Define a var args macro, it must take two or four args
  18. #
  19. MACRO(TEST_ARGC value1 value2)
  20. ADD_DEFINITIONS(${value1} ${value2})
  21. IF (${ARGC} MATCHES 4)
  22. ADD_DEFINITIONS(${ARGV2} ${ARGV3})
  23. ENDIF (${ARGC} MATCHES 4)
  24. ENDMACRO(TEST_ARGC)
  25. # invoke the macro
  26. TEST_ARGC(-DCMAKE_ARGV1 -DCMAKE_ARGV2 -DCMAKE_ARGV3 -DCMAKE_ARGV4)
  27. MACRO(TEST_VAR_ARG fa)
  28. IF("${ARGV}" MATCHES "^1;2;3$")
  29. MESSAGE(STATUS "ARGV works")
  30. ELSE("${ARGV}" MATCHES "^1;2;3$")
  31. MESSAGE(FATAL_ERROR "ARGV does not work; got \"${ARGV}\" instead of \"1;2;3\"")
  32. ENDIF("${ARGV}" MATCHES "^1;2;3$")
  33. IF("${ARGN}" MATCHES "^2;3$")
  34. MESSAGE(STATUS "ARGN works")
  35. ELSE("${ARGN}" MATCHES "^2;3$")
  36. MESSAGE(FATAL_ERROR "ARGV does not work; got \"${ARGN}\" instead of \"2;3\"")
  37. ENDIF("${ARGN}" MATCHES "^2;3$")
  38. ENDMACRO(TEST_VAR_ARG)
  39. TEST_VAR_ARG(1 2 3)
  40. #
  41. # Use the ansi CXX compile flag for building cmake
  42. #
  43. IF (CMAKE_ANSI_CXXFLAGS)
  44. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_ANSI_CXXFLAGS}")
  45. ENDIF (CMAKE_ANSI_CXXFLAGS)
  46. IF(CMAKE_SYSTEM MATCHES "OSF1-V.*")
  47. IF(NOT CMAKE_COMPILER_IS_GNUCXX)
  48. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -timplicit_local -no_implicit_include ")
  49. ENDIF(NOT CMAKE_COMPILER_IS_GNUCXX)
  50. ENDIF(CMAKE_SYSTEM MATCHES "OSF1-V.*")
  51. ADD_DEFINITIONS(-DCMAKE_IS_FUN)
  52. ADD_DEFINITIONS(-DCMAKE_IS_REALLY_FUN)
  53. SET(TEST_SEP "a b c")
  54. SEPARATE_ARGUMENTS(TEST_SEP)
  55. #
  56. # Include vars from a file and from a cache
  57. #
  58. IF (EXISTS ${Complex_SOURCE_DIR}/VarTests.cmake)
  59. INCLUDE(${Complex_SOURCE_DIR}/VarTests.cmake)
  60. ENDIF (EXISTS ${Complex_SOURCE_DIR}/VarTests.cmake)
  61. INCLUDE(fileshouldnotbehere OPTIONAL)
  62. LOAD_CACHE(${Complex_SOURCE_DIR}/Cache
  63. EXCLUDE
  64. CACHE_TEST_VAR_EXCLUDED
  65. INCLUDE_INTERNALS
  66. CACHE_TEST_VAR_INTERNAL)
  67. LOAD_CACHE(${Complex_SOURCE_DIR}/Cache READ_WITH_PREFIX foo CACHE_TEST_VAR2)
  68. IF(${fooCACHE_TEST_VAR2} MATCHES bar)
  69. MESSAGE("Load cache worked: ${fooCACHE_TEST_VAR2}")
  70. ELSE(${fooCACHE_TEST_VAR2} MATCHES bar)
  71. MESSAGE(FATAL_ERROR "Load cache with prefix failed: ${fooCACHE_TEST_VAR2}")
  72. ENDIF(${fooCACHE_TEST_VAR2} MATCHES bar)
  73. #
  74. # Specify include and lib dirs
  75. # (BEFORE is for coverage)
  76. #
  77. INCLUDE_DIRECTORIES(
  78. ${Complex_SOURCE_DIR}/Library
  79. ${Complex_SOURCE_DIR}/../../Source
  80. ${Complex_BINARY_DIR}/../../Source
  81. )
  82. INCLUDE_DIRECTORIES(BEFORE
  83. ${Complex_BINARY_DIR}
  84. )
  85. INCLUDE_REGULAR_EXPRESSION("^(cmTest|file|sharedFile).*$" "^cmMissing")
  86. LINK_DIRECTORIES(
  87. ${Complex_BINARY_DIR}/Library
  88. )
  89. #
  90. # check for SET CACHE FORCE
  91. #
  92. SET(FORCE_TEST 1 CACHE STRING "a test")
  93. SET(FORCE_TEST 0 CACHE STRING "a test" FORCE)
  94. #
  95. # Lib and exe path
  96. #
  97. SET (LIBRARY_OUTPUT_PATH
  98. ${Complex_BINARY_DIR}/bin/ CACHE INTERNAL
  99. "Single output directory for building all libraries.")
  100. SET (EXECUTABLE_OUTPUT_PATH
  101. ${Complex_BINARY_DIR}/bin/ CACHE INTERNAL
  102. "Single output directory for building all executables.")
  103. MESSAGE (Test " " escape " " semi-colon " " \; \;)
  104. #
  105. # Exec program (TODO: test a result)
  106. # Increase coverage.
  107. #
  108. MESSAGE("\nIgnore this message")
  109. OPTION(NO_EXEC_PROGRAM "Do not test EXEC_PROGRAM" 0)
  110. IF (NOT NO_EXEC_PROGRAM)
  111. EXEC_PROGRAM(${CMAKE_COMMAND} ARGS -E echo NO_EXEC_PROGRAM "${Complex_BINARY_DIR}")
  112. ELSE (NOT NO_EXEC_PROGRAM)
  113. MESSAGE("Set this option ON")
  114. ENDIF (NOT NO_EXEC_PROGRAM)
  115. MARK_AS_ADVANCED(NO_EXEC_PROGRAM)
  116. MARK_AS_ADVANCED(CLEAR NO_EXEC_PROGRAM)
  117. # Execute a process. Add coverage for this command.
  118. EXECUTE_PROCESS(
  119. COMMAND ${CMAKE_COMMAND} -E echo "ABCDEFG"
  120. OUTPUT_VARIABLE TEST_OUT
  121. )
  122. IF("${TEST_OUT}" MATCHES "^ABCDEFG\n$")
  123. ELSE("${TEST_OUT}" MATCHES "^ABCDEFG\n$")
  124. MESSAGE(SEND_ERROR "EXECUTE_PROCESS output test failed: [${TEST_OUT}]")
  125. ENDIF("${TEST_OUT}" MATCHES "^ABCDEFG\n$")
  126. EXECUTE_PROCESS(
  127. COMMAND ${CMAKE_COMMAND} -E echo "ABCDEFG"
  128. COMMAND /process/does/not/exist
  129. OUTPUT_QUIET
  130. ERROR_QUIET
  131. RESULT_VARIABLE RESULT
  132. )
  133. IF("${RESULT}" MATCHES "^0$")
  134. MESSAGE(SEND_ERROR
  135. "EXECUTE_PROCESS result test failed with RESULT=[${RESULT}]")
  136. ELSE("${RESULT}" MATCHES "^0$")
  137. MESSAGE(STATUS "EXECUTE_PROCESS result test passed with RESULT=[${RESULT}]")
  138. ENDIF("${RESULT}" MATCHES "^0$")
  139. #
  140. # Create directory.
  141. # The 'complex' executable will then test if this dir exists,
  142. # sadly it won't be able to remove it.
  143. #
  144. MAKE_DIRECTORY("${Complex_BINARY_DIR}/make_dir")
  145. #
  146. # Test FIND_LIBARY
  147. # Create a dummy empty lib
  148. #
  149. CONFIGURE_FILE(
  150. ${Complex_SOURCE_DIR}/Library/dummy
  151. ${Complex_BINARY_DIR}/Library/dummylib.lib
  152. COPYONLY IMMEDIATE)
  153. FOREACH (ext ${CMAKE_SHLIB_SUFFIX};.so;.a;.sl)
  154. CONFIGURE_FILE(
  155. ${Complex_SOURCE_DIR}/Library/dummy
  156. ${Complex_BINARY_DIR}/Library/libdummylib${ext}
  157. COPYONLY IMMEDIATE)
  158. ENDFOREACH (ext)
  159. FIND_LIBRARY(FIND_DUMMY_LIB
  160. dummylib
  161. PATHS
  162. ${Complex_BINARY_DIR}/Library DOC "find dummy lib")
  163. FIND_LIBRARY(FIND_DUMMY_LIB
  164. NAMES dummylib dummylib2
  165. PATHS
  166. ${Complex_BINARY_DIR}/Library DOC "find dummy lib")
  167. #
  168. # Test SET_SOURCE_FILES_PROPERTIES
  169. #
  170. SET_SOURCE_FILES_PROPERTIES(nonexisting_file2
  171. GENERATED
  172. ABSTRACT
  173. WRAP_EXCLUDE
  174. COMPILE_FLAGS "-foo -bar")
  175. GET_SOURCE_FILE_PROPERTY(FILE_HAS_ABSTRACT nonexisting_file2 ABSTRACT)
  176. GET_SOURCE_FILE_PROPERTY(FILE_HAS_WRAP_EXCLUDE nonexisting_file2 WRAP_EXCLUDE)
  177. GET_SOURCE_FILE_PROPERTY(FILE_COMPILE_FLAGS nonexisting_file2 COMPILE_FLAGS)
  178. SET_SOURCE_FILES_PROPERTIES(nonexisting_file3 PROPERTIES
  179. GENERATED 1
  180. ABSTRACT 1
  181. WRAP_EXCLUDE 1
  182. COMPILE_FLAGS "-foo -bar")
  183. GET_SOURCE_FILE_PROPERTY(FILE_HAS_ABSTRACT nonexisting_file3 ABSTRACT)
  184. GET_SOURCE_FILE_PROPERTY(FILE_HAS_WRAP_EXCLUDE nonexisting_file3 WRAP_EXCLUDE)
  185. GET_SOURCE_FILE_PROPERTY(FILE_COMPILE_FLAGS nonexisting_file3 COMPILE_FLAGS)
  186. #
  187. # Test registry (win32)
  188. # Create a file, put its path in a registry key, try to find the file in that
  189. # path using that registry key, then remove the file and the key
  190. #
  191. IF (WIN32)
  192. IF (NOT UNIX)
  193. SET(dir "${Complex_BINARY_DIR}/registry_dir")
  194. SET(file "registry_test_dummy")
  195. SET(hkey "HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Tests\\Complex;registry_test")
  196. CONFIGURE_FILE(
  197. ${Complex_SOURCE_DIR}/Library/dummy
  198. "${dir}/${file}"
  199. COPYONLY IMMEDIATE)
  200. EXEC_PROGRAM(${CMAKE_COMMAND} ARGS "-E write_regv \"${hkey}\" \"${dir}\"")
  201. FIND_PATH(REGISTRY_TEST_PATH
  202. ${file}
  203. "[${hkey}]" DOC "Registry_Test_Path")
  204. EXEC_PROGRAM(${CMAKE_COMMAND} ARGS "-E delete_regv \"${hkey}\"")
  205. EXEC_PROGRAM(${CMAKE_COMMAND} ARGS "-E remove \"${dir}/${file}\"")
  206. ENDIF (NOT UNIX)
  207. ENDIF (WIN32)
  208. #
  209. # Test a set and a remove
  210. #
  211. SET(REMOVE_STRING a b c d e f)
  212. SET(removeVar1 c e)
  213. REMOVE(REMOVE_STRING ${removeVar1} f)
  214. #
  215. # Test an IF inside a FOREACH.
  216. #
  217. FOREACH(x "a")
  218. IF(${x} MATCHES "a")
  219. # Should always execute.
  220. SET(IF_INSIDE_FOREACH_THEN_EXECUTED 1)
  221. ELSE(${x} MATCHES "a")
  222. # Should never execute.
  223. SET(IF_INSIDE_FOREACH_ELSE_EXECUTED 1)
  224. ENDIF(${x} MATCHES "a")
  225. ENDFOREACH(x)
  226. # test WHILE command
  227. SET (while_var 1)
  228. WHILE (while_var LESS 1000)
  229. SET(while_var ${while_var}0)
  230. ENDWHILE(while_var LESS 1000)
  231. SET(SHOULD_BE_ZERO )
  232. SET(SHOULD_BE_ONE 1)
  233. #
  234. # Configure file
  235. # (plug vars to #define so that they can be tested)
  236. #
  237. CONFIGURE_FILE(
  238. ${Complex_SOURCE_DIR}/cmTestConfigure.h.in
  239. ${Complex_BINARY_DIR}/cmTestConfigure.h)
  240. SET(STRING_WITH_QUOTES "\"hello world\"")
  241. # test CONFIGURE_FILE with ESCAPE_QUOTES on
  242. CONFIGURE_FILE(
  243. ${Complex_SOURCE_DIR}/cmTestConfigureEscape.h.in
  244. ${Complex_BINARY_DIR}/cmTestConfigureEscape.h ESCAPE_QUOTES)
  245. # Test regular expression commands.
  246. STRING(REGEX MATCH "b" RESULT "abc")
  247. IF(NOT RESULT)
  248. MESSAGE(SEND_ERROR "STRING(REGEX MATCH ... ) test failed.")
  249. ENDIF(NOT RESULT)
  250. STRING(REGEX MATCHALL "b" RESULT "abcb")
  251. IF(NOT RESULT)
  252. MESSAGE(SEND_ERROR "STRING(REGEX MATCHALL ... ) test failed.")
  253. ENDIF(NOT RESULT)
  254. STRING(REGEX REPLACE ".([bd])." "[\\1]" RESULT "a(b)c(d)e")
  255. IF("x${RESULT}" MATCHES "^xa\\[b\\]c\\[d\\]e$")
  256. SET(STRING_REGEX_PASSED 1)
  257. ENDIF("x${RESULT}" MATCHES "^xa\\[b\\]c\\[d\\]e$")
  258. IF(NOT STRING_REGEX_PASSED)
  259. MESSAGE(SEND_ERROR
  260. "STRING(REGEX REPLACE ... ) test failed (\"${RESULT}\" v. \"a[b]c[d]e\")")
  261. ENDIF(NOT STRING_REGEX_PASSED)
  262. #
  263. # Create the libs and the main exe
  264. #
  265. ADD_SUBDIRECTORY(Library)
  266. ADD_SUBDIRECTORY(Executable)
  267. SUBDIR_DEPENDS(Executable Library)
  268. EXPORT_LIBRARY_DEPENDENCIES(${Complex_BINARY_DIR}/ComplexLibraryDepends.cmake)
  269. INCLUDE(${Complex_BINARY_DIR}/ComplexLibraryDepends.cmake OPTIONAL)