CTestUpdateCommon.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #-----------------------------------------------------------------------------
  2. # Function to run a child process and report output only on error.
  3. function(run_child)
  4. execute_process(${ARGN}
  5. RESULT_VARIABLE FAILED
  6. OUTPUT_VARIABLE OUTPUT
  7. ERROR_VARIABLE OUTPUT
  8. OUTPUT_STRIP_TRAILING_WHITESPACE
  9. ERROR_STRIP_TRAILING_WHITESPACE
  10. )
  11. if(FAILED)
  12. string(REPLACE "\n" "\n " OUTPUT "${OUTPUT}")
  13. message(FATAL_ERROR "Child failed (${FAILED}), output is\n ${OUTPUT}\n"
  14. "Command = [${ARGN}]\n")
  15. endif()
  16. # Pass output back up to the parent scope for possible further inspection.
  17. set(OUTPUT "${OUTPUT}" PARENT_SCOPE)
  18. endfunction()
  19. #-----------------------------------------------------------------------------
  20. # Function to find the Update.xml file and check for expected entries.
  21. function(check_updates build)
  22. # Find the Update.xml file for the given build tree
  23. set(PATTERN ${TOP}/${build}/Testing/*/Update.xml)
  24. file(GLOB UPDATE_XML_FILE RELATIVE ${TOP} ${PATTERN})
  25. string(REGEX REPLACE "//Update.xml$" "/Update.xml"
  26. UPDATE_XML_FILE "${UPDATE_XML_FILE}"
  27. )
  28. if(NOT UPDATE_XML_FILE)
  29. message(FATAL_ERROR "Cannot find Update.xml with pattern\n ${PATTERN}")
  30. endif()
  31. message(" found ${UPDATE_XML_FILE}")
  32. set(max_update_xml_size 16384)
  33. # Read entries from the Update.xml file
  34. set(types "Updated|Modified|Conflicting")
  35. file(STRINGS ${TOP}/${UPDATE_XML_FILE} UPDATE_XML_ENTRIES
  36. REGEX "<(${types}|FullName)>"
  37. LIMIT_INPUT ${max_update_xml_size}
  38. )
  39. string(REGEX REPLACE
  40. "[ \t]*<(${types})>[ \t]*;[ \t]*<FullName>([^<]*)</FullName>"
  41. "\\1{\\2}" UPDATE_XML_ENTRIES "${UPDATE_XML_ENTRIES}")
  42. # If specified, remove the given prefix from the files in Update.xml.
  43. # Some VCS systems, like Perforce, return absolute locations
  44. if(DEFINED REPOSITORY_FILE_PREFIX)
  45. string(REPLACE
  46. "${REPOSITORY_FILE_PREFIX}" ""
  47. UPDATE_XML_ENTRIES "${UPDATE_XML_ENTRIES}")
  48. endif()
  49. # Compare expected and actual entries
  50. set(EXTRA "${UPDATE_XML_ENTRIES}")
  51. list(REMOVE_ITEM EXTRA ${ARGN} ${UPDATE_EXTRA} ${UPDATE_MAYBE})
  52. set(MISSING "${ARGN}" ${UPDATE_EXTRA})
  53. if(NOT "" STREQUAL "${UPDATE_XML_ENTRIES}")
  54. list(REMOVE_ITEM MISSING ${UPDATE_XML_ENTRIES})
  55. endif()
  56. if(NOT UPDATE_NOT_GLOBAL)
  57. set(rev_elements Revision PriorRevision ${UPDATE_GLOBAL_ELEMENTS})
  58. string(REPLACE ";" "|" rev_regex "${rev_elements}")
  59. set(rev_regex "^\t<(${rev_regex})>[^<\n]+</(${rev_regex})>$")
  60. file(STRINGS ${TOP}/${UPDATE_XML_FILE} UPDATE_XML_REVISIONS
  61. REGEX "${rev_regex}"
  62. LIMIT_INPUT ${max_update_xml_size}
  63. )
  64. foreach(r IN LISTS UPDATE_XML_REVISIONS)
  65. string(REGEX REPLACE "${rev_regex}" "\\1" element "${r}")
  66. set(element_${element} 1)
  67. endforeach()
  68. foreach(element ${rev_elements})
  69. if(NOT element_${element})
  70. list(APPEND MISSING "global <${element}> element")
  71. endif()
  72. endforeach()
  73. endif()
  74. # Report the result
  75. set(MSG "")
  76. if(MISSING)
  77. # List the missing entries
  78. string(APPEND MSG "Update.xml is missing expected entries:\n")
  79. foreach(f ${MISSING})
  80. string(APPEND MSG " ${f}\n")
  81. endforeach()
  82. else()
  83. # Success
  84. message(" no entries missing from Update.xml")
  85. endif()
  86. # Report the result
  87. if(EXTRA)
  88. # List the extra entries
  89. string(APPEND MSG "Update.xml has extra unexpected entries:\n")
  90. foreach(f ${EXTRA})
  91. string(APPEND MSG " ${f}\n")
  92. endforeach()
  93. else()
  94. # Success
  95. message(" no extra entries in Update.xml")
  96. endif()
  97. if(MSG)
  98. # Provide the log file
  99. file(GLOB UPDATE_LOG_FILE
  100. ${TOP}/${build}/Testing/Temporary/LastUpdate*.log)
  101. if(UPDATE_LOG_FILE)
  102. file(READ ${UPDATE_LOG_FILE} UPDATE_LOG LIMIT ${max_update_xml_size})
  103. string(REPLACE "\n" "\n " UPDATE_LOG "${UPDATE_LOG}")
  104. string(APPEND MSG "Update log:\n ${UPDATE_LOG}")
  105. else()
  106. string(APPEND MSG "No update log found!")
  107. endif()
  108. # Display the error message
  109. message(FATAL_ERROR "${MSG}")
  110. endif()
  111. endfunction()
  112. #-----------------------------------------------------------------------------
  113. # Function to create initial content.
  114. function(create_content dir)
  115. file(MAKE_DIRECTORY ${TOP}/${dir})
  116. # An example CTest project configuration file.
  117. file(WRITE ${TOP}/${dir}/CTestConfig.cmake
  118. "# CTest Configuration File
  119. set(CTEST_NIGHTLY_START_TIME \"21:00:00 EDT\")
  120. ")
  121. # Some other files.
  122. file(WRITE ${TOP}/${dir}/foo.txt "foo\n")
  123. file(WRITE ${TOP}/${dir}/bar.txt "bar\n")
  124. endfunction()
  125. #-----------------------------------------------------------------------------
  126. # Function to update content.
  127. function(update_content dir added_var removed_var dirs_var)
  128. file(APPEND ${TOP}/${dir}/foo.txt "foo line 2\n")
  129. file(WRITE ${TOP}/${dir}/zot.txt "zot\n")
  130. file(REMOVE ${TOP}/${dir}/bar.txt)
  131. file(MAKE_DIRECTORY ${TOP}/${dir}/subdir)
  132. file(WRITE ${TOP}/${dir}/subdir/foo.txt "foo\n")
  133. file(WRITE ${TOP}/${dir}/subdir/bar.txt "bar\n")
  134. set(${dirs_var} subdir PARENT_SCOPE)
  135. set(${added_var} zot.txt subdir/foo.txt subdir/bar.txt PARENT_SCOPE)
  136. set(${removed_var} bar.txt PARENT_SCOPE)
  137. endfunction()
  138. #-----------------------------------------------------------------------------
  139. # Function to change existing files
  140. function(change_content dir)
  141. file(APPEND ${TOP}/${dir}/foo.txt "foo line 3\n")
  142. file(APPEND ${TOP}/${dir}/subdir/foo.txt "foo line 2\n")
  143. endfunction()
  144. #-----------------------------------------------------------------------------
  145. # Function to create local modifications before update
  146. function(modify_content dir)
  147. file(APPEND ${TOP}/${dir}/CTestConfig.cmake "# local modification\n")
  148. endfunction()
  149. #-----------------------------------------------------------------------------
  150. # Function to write CTestConfiguration.ini content.
  151. function(create_build_tree src_dir bin_dir)
  152. file(MAKE_DIRECTORY ${TOP}/${bin_dir})
  153. file(WRITE ${TOP}/${bin_dir}/CTestConfiguration.ini
  154. "# CTest Configuration File
  155. SourceDirectory: ${TOP}/${src_dir}
  156. BuildDirectory: ${TOP}/${bin_dir}
  157. Site: test.site
  158. BuildName: user-test
  159. ")
  160. endfunction()
  161. #-----------------------------------------------------------------------------
  162. # Function to write the dashboard test script.
  163. function(create_dashboard_script bin_dir custom_text)
  164. if (NOT ctest_update_check)
  165. set(ctest_update_check [[
  166. if(ret LESS 0)
  167. message(FATAL_ERROR "ctest_update failed with ${ret}")
  168. endif()
  169. ]])
  170. endif()
  171. # Write the dashboard script.
  172. file(WRITE ${TOP}/${bin_dir}.cmake
  173. "# CTest Dashboard Script
  174. set(CTEST_DASHBOARD_ROOT \"${TOP}\")
  175. set(CTEST_SITE test.site)
  176. set(CTEST_BUILD_NAME dash-test)
  177. set(CTEST_SOURCE_DIRECTORY \${CTEST_DASHBOARD_ROOT}/dash-source)
  178. set(CTEST_BINARY_DIRECTORY \${CTEST_DASHBOARD_ROOT}/${bin_dir})
  179. ${custom_text}
  180. # Start a dashboard and run the update step
  181. ctest_start(Experimental)
  182. ctest_update(SOURCE \${CTEST_SOURCE_DIRECTORY} RETURN_VALUE ret ${ctest_update_args})
  183. ${ctest_update_check}")
  184. endfunction()
  185. #-----------------------------------------------------------------------------
  186. # Function to run the dashboard through the command line
  187. function(run_dashboard_command_line bin_dir)
  188. run_child(
  189. WORKING_DIRECTORY ${TOP}/${bin_dir}
  190. COMMAND ${CMAKE_CTEST_COMMAND} -M Experimental -T Start -T Update
  191. )
  192. # Verify the updates reported by CTest.
  193. list(APPEND UPDATE_MAYBE Updated{subdir})
  194. set(_modified Modified{CTestConfig.cmake})
  195. if(UPDATE_NO_MODIFIED)
  196. set(_modified "")
  197. endif()
  198. check_updates(${bin_dir}
  199. Updated{foo.txt}
  200. Updated{bar.txt}
  201. Updated{zot.txt}
  202. Updated{subdir/foo.txt}
  203. Updated{subdir/bar.txt}
  204. ${_modified}
  205. )
  206. endfunction()
  207. #-----------------------------------------------------------------------------
  208. # Function to find the Update.xml file and make sure
  209. # it only has the Revision in it and no updates
  210. function(check_no_update bin_dir)
  211. set(PATTERN ${TOP}/${bin_dir}/Testing/*/Update.xml)
  212. file(GLOB UPDATE_XML_FILE RELATIVE ${TOP} ${PATTERN})
  213. string(REGEX REPLACE "//Update.xml$" "/Update.xml"
  214. UPDATE_XML_FILE "${UPDATE_XML_FILE}")
  215. message(" found ${UPDATE_XML_FILE}")
  216. set(rev_regex "Revision|PriorRevision")
  217. file(STRINGS ${TOP}/${UPDATE_XML_FILE} UPDATE_XML_REVISIONS
  218. REGEX "^\t<(${rev_regex})>[^<\n]+</(${rev_regex})>$"
  219. )
  220. set(found_revisons FALSE)
  221. foreach(r IN LISTS UPDATE_XML_REVISIONS)
  222. if("${r}" MATCHES "PriorRevision")
  223. message(FATAL_ERROR "Found PriorRevision in no update test")
  224. endif()
  225. if("${r}" MATCHES "<Revision>")
  226. set(found_revisons TRUE)
  227. endif()
  228. endforeach()
  229. if(found_revisons)
  230. message(" found <Revision> in no update test")
  231. else()
  232. message(FATAL_ERROR " missing <Revision> in no update test")
  233. endif()
  234. endfunction()
  235. #-----------------------------------------------------------------------------
  236. # Function to find the Update.xml file and make sure
  237. # it only has the UpdateReturnStatus failure message and no updates.
  238. function(check_fail_update bin_dir)
  239. set(PATTERN ${TOP}/${bin_dir}/Testing/*/Update.xml)
  240. file(GLOB UPDATE_XML_FILE RELATIVE ${TOP} ${PATTERN})
  241. string(REGEX REPLACE "//Update.xml$" "/Update.xml"
  242. UPDATE_XML_FILE "${UPDATE_XML_FILE}")
  243. message(" found ${UPDATE_XML_FILE}")
  244. file(STRINGS ${TOP}/${UPDATE_XML_FILE} UPDATE_XML_STATUS
  245. REGEX "^\t<UpdateReturnStatus>[^<\n]+"
  246. )
  247. if(UPDATE_XML_STATUS MATCHES "Update command failed")
  248. message(" correctly found 'Update command failed'")
  249. else()
  250. message(FATAL_ERROR " missing 'Update command failed'")
  251. endif()
  252. endfunction()
  253. #-----------------------------------------------------------------------------
  254. # Function to run the dashboard through a script
  255. function(run_dashboard_script bin_dir)
  256. run_child(
  257. WORKING_DIRECTORY ${TOP}
  258. COMMAND ${CMAKE_CTEST_COMMAND} -S ${bin_dir}.cmake -V
  259. )
  260. # Verify the updates reported by CTest.
  261. list(APPEND UPDATE_MAYBE Updated{subdir} Updated{CTestConfig.cmake})
  262. if(NO_UPDATE)
  263. check_no_update(${bin_dir})
  264. elseif(FAIL_UPDATE)
  265. check_fail_update(${bin_dir})
  266. else()
  267. check_updates(${bin_dir}
  268. Updated{foo.txt}
  269. Updated{bar.txt}
  270. Updated{zot.txt}
  271. Updated{subdir/foo.txt}
  272. Updated{subdir/bar.txt}
  273. )
  274. endif()
  275. # Pass console output up to the parent, in case they'd like to inspect it.
  276. set(OUTPUT "${OUTPUT}" PARENT_SCOPE)
  277. endfunction()
  278. #-----------------------------------------------------------------------------
  279. # Function to initialize the testing directory.
  280. function(init_testing)
  281. file(REMOVE_RECURSE ${TOP})
  282. file(MAKE_DIRECTORY ${TOP})
  283. endfunction()