CMakeLists.txt 9.9 KB

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