CMakeLists.txt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. # FILE(STRINGS) test
  7. FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in" infile_strings
  8. LIMIT_COUNT 1 LIMIT_INPUT 1024 LIMIT_OUTPUT 1024
  9. LENGTH_MINIMUM 10 LENGTH_MAXIMUM 23 REGEX include NEWLINE_CONSUME)
  10. SET(infile_strings_goal "#include \"includefile\"\n")
  11. IF("${infile_strings}" STREQUAL "${infile_strings_goal}")
  12. MESSAGE("FILE(STRINGS) correctly read [${infile_strings}]")
  13. ELSE("${infile_strings}" STREQUAL "${infile_strings_goal}")
  14. MESSAGE(SEND_ERROR
  15. "FILE(STRINGS) incorrectly read [${infile_strings}]")
  16. ENDIF("${infile_strings}" STREQUAL "${infile_strings_goal}")
  17. # test reading a file and getting its binary data as hex string
  18. FILE(READ "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infilehex LIMIT 4 HEX)
  19. IF(NOT "${infilehex}" STREQUAL "53313036")
  20. MESSAGE(SEND_ERROR
  21. "FILE(READ ... HEX) error, read: \"${infilehex}\", expected \"53313036\"")
  22. ENDIF(NOT "${infilehex}" STREQUAL "53313036")
  23. # test that FILE(STRINGS) also work with Intel hex and Motorola S-record files
  24. # this file has been created with "sdcc main.c"
  25. FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.ihx" infile_strings REGEX INFO)
  26. SET(infile_strings_goal "INFO:compiler\\[SDCC-HEX\\]")
  27. IF("${infile_strings}" MATCHES "${infile_strings_goal}")
  28. MESSAGE("FILE(STRINGS) correctly read from hex file [${infile_strings}]")
  29. ELSE("${infile_strings}" MATCHES "${infile_strings_goal}")
  30. MESSAGE(SEND_ERROR
  31. "FILE(STRINGS) incorrectly read from hex file [${infile_strings}]")
  32. ENDIF("${infile_strings}" MATCHES "${infile_strings_goal}")
  33. # this file has been created with "sdcc main.c --out-fmt-s19"
  34. FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/main.srec" infile_strings REGEX INFO)
  35. SET(infile_strings_goal "INFO:compiler\\[SDCC-SREC\\]")
  36. IF("${infile_strings}" MATCHES "${infile_strings_goal}")
  37. MESSAGE("FILE(STRINGS) correctly read from srec file [${infile_strings}]")
  38. ELSE("${infile_strings}" MATCHES "${infile_strings_goal}")
  39. MESSAGE(SEND_ERROR
  40. "FILE(STRINGS) incorrectly read from srec file [${infile_strings}]")
  41. ENDIF("${infile_strings}" MATCHES "${infile_strings_goal}")
  42. # String test
  43. STRING(REGEX MATCH "[cC][mM][aA][kK][eE]" rmvar "CMake is great")
  44. STRING(REGEX MATCHALL "[cC][mM][aA][kK][eE]" rmallvar "CMake is better than cmake or CMake")
  45. STRING(REGEX REPLACE "[Aa][uU][tT][oO]([cC][oO][nN][fF]|[mM][aA][kK][eE])"
  46. "CMake" rrepvar "People should use Autoconf and Automake")
  47. STRING(COMPARE EQUAL "CMake" "Autoconf" nceqvar)
  48. STRING(COMPARE EQUAL "CMake" "CMake" ceqvar)
  49. STRING(COMPARE NOTEQUAL "CMake" "Autoconf" cneqvar)
  50. STRING(COMPARE NOTEQUAL "CMake" "CMake" ncneqvar)
  51. STRING(COMPARE LESS "before" "after" nclvar)
  52. STRING(COMPARE LESS "max" "min" clvar)
  53. STRING(COMPARE GREATER "before" "after" cgvar)
  54. STRING(COMPARE GREATER "max" "min" ncgvar)
  55. STRING(ASCII 67 109 97 107 101 savar)
  56. STRING(TOUPPER "CMake" tuvar)
  57. STRING(TOLOWER "CMake" tlvar)
  58. STRING(REPLACE "Autoconf" "CMake" repvar "People should use Autoconf")
  59. IF("abc" STREQUAL "xyz")
  60. MESSAGE(SEND_ERROR "Problem with the IF(STREQUAL), \"abc\" and \"xyz\" considered equal")
  61. ENDIF("abc" STREQUAL "xyz")
  62. IF("CMake is cool" MATCHES "(CMake) (is).+")
  63. IF(NOT "${CMAKE_MATCH_0}" STREQUAL "CMake is cool")
  64. MESSAGE(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"CMake is cool\"")
  65. ENDIF(NOT "${CMAKE_MATCH_0}" STREQUAL "CMake is cool")
  66. IF(NOT "${CMAKE_MATCH_1}" STREQUAL "CMake")
  67. MESSAGE(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"CMake\"")
  68. ENDIF(NOT "${CMAKE_MATCH_1}" STREQUAL "CMake")
  69. IF(NOT "${CMAKE_MATCH_2}" STREQUAL "is")
  70. MESSAGE(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected \"is\"")
  71. ENDIF(NOT "${CMAKE_MATCH_2}" STREQUAL "is")
  72. ELSE("CMake is cool" MATCHES "(CMake) (is).+")
  73. MESSAGE(SEND_ERROR "Problem with the IF(MATCHES), no match found")
  74. ENDIF("CMake is cool" MATCHES "(CMake) (is).+")
  75. STRING(REGEX MATCH "(People).+CMake" matchResultVar "People should use CMake")
  76. IF(NOT "${matchResultVar}" STREQUAL "People should use CMake")
  77. MESSAGE(SEND_ERROR "STRING(REGEX MATCH) problem: \"${matchResultVar}\", expected \"People should use CMake\"")
  78. ENDIF(NOT "${matchResultVar}" STREQUAL "People should use CMake")
  79. IF(NOT "${CMAKE_MATCH_0}" STREQUAL "People should use CMake")
  80. MESSAGE(SEND_ERROR "CMAKE_MATCH_0 wrong: \"${CMAKE_MATCH_0}\", expected \"People should use CMake\"")
  81. ENDIF(NOT "${CMAKE_MATCH_0}" STREQUAL "People should use CMake")
  82. IF(NOT "${CMAKE_MATCH_1}" STREQUAL "People")
  83. MESSAGE(SEND_ERROR "CMAKE_MATCH_1 wrong: \"${CMAKE_MATCH_1}\", expected \"People\"")
  84. ENDIF(NOT "${CMAKE_MATCH_1}" STREQUAL "People")
  85. IF(NOT "${CMAKE_MATCH_2}" STREQUAL "")
  86. MESSAGE(SEND_ERROR "CMAKE_MATCH_2 wrong: \"${CMAKE_MATCH_2}\", expected empty string")
  87. ENDIF(NOT "${CMAKE_MATCH_2}" STREQUAL "")
  88. STRING(STRIP "
  89. ST1
  90. " ST1)
  91. STRING(STRIP "ST2 " ST2)
  92. STRING(STRIP " ST3" ST3)
  93. FOREACH(var ST1 ST2 ST3)
  94. IF("${var}" STREQUAL "${${var}}")
  95. MESSAGE("[${var}] == [${${var}}]")
  96. ELSE("${var}" STREQUAL "${${var}}")
  97. MESSAGE(SEND_ERROR "Problem with the STRIP command for ${var}: [${${var}}]")
  98. ENDIF("${var}" STREQUAL "${${var}}")
  99. ENDFOREACH(var)
  100. STRING(SUBSTRING "People should use Autoconf" 7 10 substringres)
  101. SET(substringres "Everybody ${substringres} CMake")
  102. STRING(LENGTH ${substringres} lengthres)
  103. FILE(RELATIVE_PATH relpath "/usr/local/bin" "/usr/X11R6/bin/xnest")
  104. # Escaping test
  105. SET(var "\\ \" \ \t \n \r \# \( \) \0")
  106. MESSAGE("Output: [${var}]")
  107. SET(var \\ \" \ \t \n \r \# \( \) \0)
  108. MESSAGE("Output: [${var}]")
  109. # Make-style unquoted argument test
  110. SET(var $(VAR1)$(VAR2)/$(VAR3))
  111. MESSAGE("Output: [${var}]")
  112. STRING(COMPARE EQUAL "${var}" "$(VAR1)$(VAR2)/$(VAR3)" result)
  113. IF(NOT result)
  114. MESSAGE(SEND_ERROR "Unquoted $(VAR) syntax is broken.")
  115. ENDIF(NOT result)
  116. # Make directories test
  117. FILE(MAKE_DIRECTORY
  118. "${CMAKE_CURRENT_BINARY_DIR}/Includes"
  119. "${CMAKE_CURRENT_BINARY_DIR}/Directory1"
  120. "${CMAKE_CURRENT_BINARY_DIR}/Directory2"
  121. )
  122. # Write results to the file (test write file)
  123. SET(file "${CMAKE_CURRENT_BINARY_DIR}/Includes/Values.h")
  124. FILE(WRITE "${file}" "/* this file is generated */\n")
  125. FOREACH(var
  126. rmvar
  127. rmallvar
  128. rrepvar
  129. repvar
  130. relpath
  131. substringres
  132. lengthres
  133. nceqvar
  134. ceqvar
  135. cneqvar
  136. ncneqvar
  137. nclvar
  138. clvar
  139. cgvar
  140. ncgvar
  141. savar
  142. tuvar
  143. tlvar)
  144. FILE(APPEND "${file}" "#define ${var} \"${${var}}\"\n")
  145. ENDFOREACH(var)
  146. # Verify that the file was created recently.
  147. IF(NOT "${file}" IS_NEWER_THAN "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in")
  148. MESSAGE(FATAL_ERROR "IF(FILE_IS_NEWER) does not seem to work.")
  149. ENDIF(NOT "${file}" IS_NEWER_THAN "${CMAKE_CURRENT_SOURCE_DIR}/InputFile.h.in")
  150. # Test configuration of the string
  151. SET(TEST_DEFINED 123)
  152. SET(TEST_NOT_DEFINED)
  153. STRING(CONFIGURE "${infile}" infile+-/out @ONLY)
  154. SET(infile "${infile+-/out}")
  155. # Write include file to a file
  156. STRING(REGEX REPLACE "includefile" "${file}" outfile "${infile}")
  157. FILE(WRITE "${CMAKE_CURRENT_BINARY_DIR}/OutputFile.h" "${outfile}")
  158. # Test file glob
  159. FILE(GLOB_RECURSE src_files "${CMAKE_CURRENT_SOURCE_DIR}/*")
  160. MESSAGE("Files in ${CMAKE_CURRENT_SOURCE_DIR} are ${src_files}")
  161. SET(expr "${CMAKE_CURRENT_SOURCE_DIR}/[sS][!a-su-zA-Z0-9][^a-qs-zA-Z0-9]ing?ile*.cxx")
  162. MESSAGE("Glob expression is [${expr}].")
  163. FILE(GLOB src_files "${expr}")
  164. MESSAGE("Globbed files [${src_files}].")
  165. ADD_EXECUTABLE(StringFileTest ${src_files})
  166. SET(expr "${CMAKE_CURRENT_SOURCE_DIR}/../*.cxx")
  167. FILE(GLOB_RECURSE rel_src_files RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/.." "${expr}")
  168. MESSAGE("Globbed files [${rel_src_files}].")
  169. # Test FOREACH range
  170. MESSAGE("Cheack if FOREACH with RANGE works")
  171. MACRO(TEST_RANGE ARGS CHECK)
  172. SET(r)
  173. FOREACH(a RANGE ${ARGS})
  174. SET(r ${r} ${a})
  175. ENDFOREACH(a)
  176. MESSAGE("FOREACH with RANGE ${ARGS} produces ${r}")
  177. IF("x${r}x" MATCHES "^x${CHECK}x$")
  178. ELSE("x${r}x" MATCHES "^x${CHECK}x$")
  179. MESSAGE(SEND_ERROR "The range resulted in: ${r} should be ${CHECK}")
  180. ENDIF("x${r}x" MATCHES "^x${CHECK}x$")
  181. ENDMACRO(TEST_RANGE)
  182. TEST_RANGE("5" "0;1;2;3;4;5")
  183. TEST_RANGE("3;5" "3;4;5")
  184. TEST_RANGE("5;3" "5;4;3")
  185. TEST_RANGE("3;10;2" "3;5;7;9")
  186. TEST_RANGE("10;0;-3" "10;7;4;1")