CMakeLists.txt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. cmake_minimum_required (VERSION 2.6)
  2. project(StringFileTest)
  3. include_directories(${StringFileTest_BINARY_DIR})
  4. # Read file test
  5. file(READ "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile)
  6. # Test reading a binary file into hex representation
  7. file(READ "${CMAKE_CURRENT_SOURCE_DIR}/test.bin" hexContents HEX)
  8. if("${hexContents}" STREQUAL "0001027700")
  9. message("file(READ HEX) correctly read [${hexContents}]")
  10. else()
  11. message(SEND_ERROR "file(READ HEX) incorrectly read [${hexContents}], but expected was [0001027700]")
  12. endif()
  13. # file(STRINGS) test
  14. file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile_strings
  15. LIMIT_COUNT 1 LIMIT_INPUT 1024 LIMIT_OUTPUT 1024
  16. LENGTH_MINIMUM 10 LENGTH_MAXIMUM 23 REGEX include NEWLINE_CONSUME)
  17. set(infile_strings_goal "#include \"includefile\"\n")
  18. if("${infile_strings}" STREQUAL "${infile_strings_goal}")
  19. message("file(STRINGS) correctly read [${infile_strings}]")
  20. else()
  21. message(SEND_ERROR
  22. "file(STRINGS) incorrectly read [${infile_strings}]")
  23. endif()
  24. # test reading a file and getting its binary data as hex string
  25. file(READ "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infilehex LIMIT 4 HEX)
  26. if(NOT "${infilehex}" STREQUAL "53313036")
  27. message(SEND_ERROR
  28. "file(READ ... HEX) error, read: \"${infilehex}\", expected \"53313036\"")
  29. endif()
  30. # test that file(STRINGS) also work with Intel hex and Motorola S-record files
  31. # this file has been created with "sdcc main.c"
  32. file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.ihx" infile_strings REGEX INFO)
  33. set(infile_strings_goal "INFO:compiler\\[SDCC-HEX\\]")
  34. if("${infile_strings}" MATCHES "${infile_strings_goal}")
  35. message("file(STRINGS) correctly read from hex file [${infile_strings}]")
  36. else()
  37. message(SEND_ERROR
  38. "file(STRINGS) incorrectly read from hex file [${infile_strings}]")
  39. endif()
  40. # this file has been created with "sdcc main.c --out-fmt-s19"
  41. file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infile_strings REGEX INFO)
  42. set(infile_strings_goal "INFO:compiler\\[SDCC-SREC\\]")
  43. if("${infile_strings}" MATCHES "${infile_strings_goal}")
  44. message("file(STRINGS) correctly read from srec file [${infile_strings}]")
  45. else()
  46. message(SEND_ERROR
  47. "file(STRINGS) incorrectly read from srec file [${infile_strings}]")
  48. endif()
  49. # String test
  50. string(REGEX MATCH "[cC][mM][aA][kK][eE]" rmvar "CMake is great")
  51. string(REGEX MATCHALL "[cC][mM][aA][kK][eE]" rmallvar "CMake is better than cmake or CMake")
  52. string(REGEX REPLACE "[Aa][uU][tT][oO]([cC][oO][nN][fF]|[mM][aA][kK][eE])"
  53. "CMake" rrepvar "People should use Autoconf and Automake")
  54. string(COMPARE EQUAL "CMake" "Autoconf" nceqvar)
  55. string(COMPARE EQUAL "CMake" "CMake" ceqvar)
  56. string(COMPARE NOTEQUAL "CMake" "Autoconf" cneqvar)
  57. string(COMPARE NOTEQUAL "CMake" "CMake" ncneqvar)
  58. string(COMPARE LESS "before" "after" nclvar)
  59. string(COMPARE LESS "max" "min" clvar)
  60. string(COMPARE GREATER "before" "after" cgvar)
  61. string(COMPARE GREATER "max" "min" ncgvar)
  62. string(ASCII 67 109 97 107 101 savar)
  63. string(TOUPPER "CMake" tuvar)
  64. string(TOLOWER "CMake" tlvar)
  65. string(REPLACE "Autoconf" "CMake" repvar "People should use Autoconf")
  66. if("abc" STREQUAL "xyz")
  67. message(SEND_ERROR "Problem with the if(STREQUAL), \"abc\" and \"xyz\" considered equal")
  68. endif()
  69. if("CMake is cool" MATCHES "(CMake) (is).+")
  70. if(NOT "${CMAKE_MATCH_0}" STREQUAL "CMake is cool")
  71. message(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"CMake is cool\"")
  72. endif()
  73. if(NOT "${CMAKE_MATCH_1}" STREQUAL "CMake")
  74. message(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"CMake\"")
  75. endif()
  76. if(NOT "${CMAKE_MATCH_2}" STREQUAL "is")
  77. message(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected \"is\"")
  78. endif()
  79. else()
  80. message(SEND_ERROR "Problem with the if(MATCHES), no match found")
  81. endif()
  82. string(REGEX MATCH "(People).+CMake" matchResultVar "People should use CMake")
  83. if(NOT "${matchResultVar}" STREQUAL "People should use CMake")
  84. message(SEND_ERROR "string(REGEX MATCH) problem: \"${matchResultVar}\", expected \"People should use CMake\"")
  85. endif()
  86. if(NOT "${CMAKE_MATCH_0}" STREQUAL "People should use CMake")
  87. message(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"People should use CMake\"")
  88. endif()
  89. if(NOT "${CMAKE_MATCH_1}" STREQUAL "People")
  90. message(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"People\"")
  91. endif()
  92. if(NOT "${CMAKE_MATCH_2}" STREQUAL "")
  93. message(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected empty string")
  94. endif()
  95. string(STRIP "
  96. ST1
  97. " ST1)
  98. string(STRIP "ST2 " ST2)
  99. string(STRIP " ST3" ST3)
  100. foreach(var ST1 ST2 ST3)
  101. if("${var}" STREQUAL "${${var}}")
  102. message("[${var}] == [${${var}}]")
  103. else()
  104. message(SEND_ERROR "Problem with the STRIP command for ${var}: [${${var}}]")
  105. endif()
  106. endforeach()
  107. string(SUBSTRING "People should use Autoconf" 7 10 substringres)
  108. set(substringres "Everybody ${substringres} CMake")
  109. string(LENGTH ${substringres} lengthres)
  110. file(RELATIVE_PATH relpath "/usr/local/bin" "/usr/X11R6/bin/xnest")
  111. # Make-style unquoted argument test
  112. set(var $(VAR1)$(VAR2)/$(VAR3))
  113. message("Output: [${var}]")
  114. string(COMPARE EQUAL "${var}" "$(VAR1)$(VAR2)/$(VAR3)" result)
  115. if(NOT result)
  116. message(SEND_ERROR "Unquoted $(VAR) syntax is broken.")
  117. endif()
  118. # Make directories test
  119. file(MAKE_DIRECTORY
  120. "${CMAKE_CURRENT_BINARY_DIR}/Includes"
  121. "${CMAKE_CURRENT_BINARY_DIR}/Directory1"
  122. "${CMAKE_CURRENT_BINARY_DIR}/Directory2"
  123. )
  124. # Write results to the file (test write file)
  125. set(file "${CMAKE_CURRENT_BINARY_DIR}/Includes/Values.h")
  126. file(WRITE "${file}" "/* this file is generated */\n")
  127. foreach(var
  128. rmvar
  129. rmallvar
  130. rrepvar
  131. repvar
  132. relpath
  133. substringres
  134. lengthres
  135. nceqvar
  136. ceqvar
  137. cneqvar
  138. ncneqvar
  139. nclvar
  140. clvar
  141. cgvar
  142. ncgvar
  143. savar
  144. tuvar
  145. tlvar)
  146. file(APPEND "${file}" "#define ${var} \"${${var}}\"\n")
  147. endforeach()
  148. # Verify that the file was created recently.
  149. if(NOT "${file}" IS_NEWER_THAN "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in")
  150. message(FATAL_ERROR "if(FILE_IS_NEWER) does not seem to work.")
  151. endif()
  152. # Test configuration of the string
  153. set(TEST_DEFINED 123)
  154. set(TEST_NOT_DEFINED)
  155. string(CONFIGURE "${infile}" infile+-/out @ONLY)
  156. set(infile "${infile+-/out}")
  157. # Write include file to a file
  158. string(REGEX REPLACE "includefile" "${file}" outfile "${infile}")
  159. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h-tmp" "${outfile}")
  160. file(RENAME "${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h-tmp"
  161. "${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h")
  162. # Test file copy with relative paths
  163. file(COPY .
  164. DESTINATION src
  165. FILE_PERMISSIONS OWNER_READ # test no OWNER_WRITE
  166. DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
  167. FILES_MATCHING PATTERN *.cxx # Only copy the main source file
  168. REGEX /src$ EXCLUDE # Block recursion for in-source build
  169. )
  170. # Test file glob
  171. file(GLOB_RECURSE src_files "${CMAKE_CURRENT_SOURCE_DIR}/*")
  172. message("Files in ${CMAKE_CURRENT_SOURCE_DIR} are ${src_files}")
  173. set(expr "${CMAKE_CURRENT_BINARY_DIR}/src/[sS][!a-su-zA-Z0-9][^a-qs-zA-Z0-9]ing?ile*.cxx")
  174. message("Glob expression is [${expr}].")
  175. file(GLOB src_files "${expr}")
  176. message("Globbed files [${src_files}].")
  177. add_executable(StringFileTest ${src_files})
  178. set(expr "${CMAKE_CURRENT_SOURCE_DIR}/../*.cxx")
  179. file(GLOB_RECURSE rel_src_files RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/.." "${expr}")
  180. message("Globbed files [${rel_src_files}].")
  181. # Test FOREACH range
  182. message("Cheack if FOREACH with RANGE works")
  183. macro(TEST_RANGE ARGS CHECK)
  184. set(r)
  185. foreach(a RANGE ${ARGS})
  186. set(r ${r} ${a})
  187. endforeach()
  188. message("FOREACH with RANGE ${ARGS} produces ${r}")
  189. if("x${r}x" MATCHES "^x${CHECK}x$")
  190. else()
  191. message(SEND_ERROR "The range resulted in: ${r} should be ${CHECK}")
  192. endif()
  193. endmacro()
  194. TEST_RANGE("5" "0;1;2;3;4;5")
  195. TEST_RANGE("3;5" "3;4;5")
  196. TEST_RANGE("5;3" "5;4;3")
  197. TEST_RANGE("3;10;2" "3;5;7;9")
  198. TEST_RANGE("10;0;-3" "10;7;4;1")
  199. # Test FOREACH IN signature
  200. set(list1 "" a "")
  201. set(list2 a "" b)
  202. set(var_)
  203. set(var_a)
  204. set(var_b)
  205. foreach(item IN LISTS list1 list2 ITEMS "" a "")
  206. set(var_${item} "${var_${item}}x")
  207. endforeach()
  208. if(NOT "${var_}" STREQUAL "xxxxx")
  209. message(FATAL_ERROR "count incorrect for \"\": [${var_}]")
  210. endif()
  211. if(NOT "${var_a}" STREQUAL "xxx")
  212. message(FATAL_ERROR "count incorrect for \"a\": [${var_a}]")
  213. endif()
  214. if(NOT "${var_b}" STREQUAL "x")
  215. message(FATAL_ERROR "count incorrect \"b\": [${var_b}]")
  216. endif()
  217. # Test SUBSTRING command
  218. set(ST_INPUTSTRING "0123456789")
  219. string(SUBSTRING ${ST_INPUTSTRING} 3 0 ST_EMPTY)
  220. string(SUBSTRING ${ST_INPUTSTRING} 1 1 ST_ONE)
  221. string(SUBSTRING ${ST_INPUTSTRING} 0 10 ST_ALL)
  222. string(SUBSTRING ${ST_INPUTSTRING} 0 -1 ST_ALL_MINUS)
  223. string(SUBSTRING ${ST_INPUTSTRING} 9 -1 ST_NINE)
  224. if(ST_EMPTY)
  225. message(SEND_ERROR "SUBSTRING with length 0 does not return an empty string")
  226. endif()
  227. if(NOT ST_ONE STREQUAL "1")
  228. message(SEND_ERROR "SUBSTING command does not cut the correct selected character, was \"" ${ST_ONE} "\", should be \"1\"")
  229. endif()
  230. if(NOT ST_INPUTSTRING STREQUAL ST_ALL)
  231. message(SEND_ERROR "SUBSTRING does not return the whole string when selected with length")
  232. endif()
  233. if(NOT ST_INPUTSTRING STREQUAL ST_ALL_MINUS)
  234. message(SEND_ERROR "SUBSTRING does not return the whole string when selected with -1")
  235. endif()
  236. if(NOT ST_NINE STREQUAL "9")
  237. message(SEND_ERROR "SUBSTRING does not return the tail when selected with -1")
  238. endif()
  239. string(MAKE_C_IDENTIFIER "1one-two$" MCI_1)
  240. if(NOT MCI_1 STREQUAL _1one_two_)
  241. message(SEND_ERROR "MAKE_C_IDENTIFIER did not create expected result.")
  242. endif()
  243. string(GENEX_STRIP "one;$<1:two;three>;four;$<TARGET_OBJECTS:some_target>" strip_result)
  244. if (NOT strip_result STREQUAL "one;four")
  245. message(SEND_ERROR "GENEX_STRIP did not create expected result: ${strip_result}")
  246. endif()