cmCTestBuildHandler.cxx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmCTestBuildHandler.h"
  4. #include <cstdlib>
  5. #include <memory>
  6. #include <set>
  7. #include <utility>
  8. #include <cm/string_view>
  9. #include <cmext/algorithm>
  10. #include <cm3p/json/value.h>
  11. #include <cm3p/uv.h>
  12. #include "cmsys/Directory.hxx"
  13. #include "cmsys/FStream.hxx"
  14. #include "cmCTest.h"
  15. #include "cmCTestLaunchReporter.h"
  16. #include "cmDuration.h"
  17. #include "cmFileTimeCache.h"
  18. #include "cmGeneratedFileStream.h"
  19. #include "cmInstrumentation.h"
  20. #include "cmInstrumentationQuery.h"
  21. #include "cmJSONState.h"
  22. #include "cmList.h"
  23. #include "cmMakefile.h"
  24. #include "cmProcessOutput.h"
  25. #include "cmStringAlgorithms.h"
  26. #include "cmStringReplaceHelper.h"
  27. #include "cmSystemTools.h"
  28. #include "cmUVHandlePtr.h"
  29. #include "cmUVProcessChain.h"
  30. #include "cmUVStream.h"
  31. #include "cmValue.h"
  32. #include "cmXMLWriter.h"
  33. static char const* cmCTestErrorMatches[] = {
  34. "^[Bb]us [Ee]rror", // noqa: spellcheck disable-line
  35. "^[Ss]egmentation [Vv]iolation",
  36. "^[Ss]egmentation [Ff]ault",
  37. ":.*[Pp]ermission [Dd]enied",
  38. "([^ :]+):([0-9]+): ([^ \\t])",
  39. "([^:]+): error[ \\t]*[0-9]+[ \\t]*:",
  40. "^Error ([0-9]+):",
  41. "^Fatal",
  42. "^Error: ",
  43. "^Error ",
  44. "[0-9] ERROR: ",
  45. R"(^"[^"]+", line [0-9]+: [^Ww])",
  46. "^cc[^C]*CC: ERROR File = ([^,]+), Line = ([0-9]+)",
  47. "^ld([^:])*:([ \\t])*ERROR([^:])*:",
  48. R"(^ild:([ \t])*\(undefined symbol\))",
  49. "([^ :]+) : (error|fatal error|catastrophic error)",
  50. "([^:]+): (Error:|error|undefined reference|multiply defined)",
  51. R"(([^:]+)\(([^\)]+)\) ?: (error|fatal error|catastrophic error))",
  52. "^fatal error C[0-9]+:",
  53. ": syntax error ",
  54. "^collect2: ld returned 1 exit status",
  55. "ld terminated with signal",
  56. "Unsatisfied symbol",
  57. "^Unresolved:",
  58. "Undefined symbol",
  59. "^Undefined[ \\t]+first referenced",
  60. "^CMake Error.*:",
  61. ":[ \\t]cannot find",
  62. ":[ \\t]can't find",
  63. R"(: \*\*\* No rule to make target [`'].*\'. Stop)",
  64. R"(: \*\*\* No targets specified and no makefile found)",
  65. ": Invalid loader fixup for symbol",
  66. ": Invalid fixups exist",
  67. ": Can't find library for",
  68. ": internal link edit command failed",
  69. ": Unrecognized option [`'].*\\'",
  70. R"(", line [0-9]+\.[0-9]+: [0-9]+-[0-9]+ \([^WI]\))",
  71. "ld: 0706-006 Cannot find or open library file: -l ",
  72. "ild: \\(argument error\\) can't find library argument ::",
  73. "^could not be found and will not be loaded.",
  74. "s:616 string too big",
  75. "make: Fatal error: ",
  76. "ld: 0711-993 Error occurred while writing to the output file:",
  77. "ld: fatal: ",
  78. "final link failed:",
  79. R"(make: \*\*\*.*Error)",
  80. R"(make\[.*\]: \*\*\*.*Error)",
  81. R"(\*\*\* Error code)",
  82. "nternal error:",
  83. R"(Makefile:[0-9]+: \*\*\* .* Stop\.)",
  84. ": No such file or directory",
  85. ": Invalid argument",
  86. "^The project cannot be built\\.",
  87. "^\\[ERROR\\]",
  88. "^Command .* failed with exit code",
  89. "lcc: \"([^\"]+)\", (line|строка) ([0-9]+): (error|ошибка)",
  90. nullptr
  91. };
  92. static char const* cmCTestErrorExceptions[] = {
  93. "instantiated from ",
  94. "candidates are:",
  95. ": warning",
  96. ": WARNING",
  97. ": \\(Warning\\)",
  98. ": note",
  99. "Note:",
  100. "makefile:",
  101. "Makefile:",
  102. ":[ \\t]+Where:",
  103. "([^ :]+):([0-9]+): Warning",
  104. "------ Build started: .* ------",
  105. nullptr
  106. };
  107. static char const* cmCTestWarningMatches[] = {
  108. "([^ :]+):([0-9]+): warning:",
  109. "([^ :]+):([0-9]+): note:",
  110. "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
  111. "^ld([^:])*:([ \\t])*WARNING([^:])*:",
  112. "([^:]+): warning ([0-9]+):",
  113. R"(^"[^"]+", line [0-9]+: [Ww](arning|arnung))",
  114. "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
  115. "^(Warning|Warnung) ([0-9]+):",
  116. "^(Warning|Warnung)[ :]",
  117. "WARNING: ",
  118. "([^ :]+) : warning",
  119. "([^:]+): warning",
  120. R"(", line [0-9]+\.[0-9]+: [0-9]+-[0-9]+ \([WI]\))",
  121. "^cxx: Warning:",
  122. ".*file: .* has no symbols",
  123. "([^ :]+):([0-9]+): (Warning|Warnung)",
  124. "\\([0-9]*\\): remark #[0-9]*",
  125. R"(".*", line [0-9]+: remark\([0-9]*\):)",
  126. "cc-[0-9]* CC: REMARK File = .*, Line = [0-9]*",
  127. "^CMake Warning.*:",
  128. "^\\[WARNING\\]",
  129. "lcc: \"([^\"]+)\", (line|строка) ([0-9]+): (warning|предупреждение)",
  130. nullptr
  131. };
  132. static char const* cmCTestWarningExceptions[] = {
  133. R"(/usr/.*/X11/Xlib\.h:[0-9]+: war.*: ANSI C\+\+ forbids declaration)",
  134. R"(/usr/.*/X11/Xutil\.h:[0-9]+: war.*: ANSI C\+\+ forbids declaration)",
  135. R"(/usr/.*/X11/XResource\.h:[0-9]+: war.*: ANSI C\+\+ forbids declaration)",
  136. "WARNING 84 :",
  137. "WARNING 47 :",
  138. "makefile:",
  139. "Makefile:",
  140. "warning: Clock skew detected. Your build may be incomplete.",
  141. "/usr/openwin/include/GL/[^:]+:",
  142. "bind_at_load",
  143. "XrmQGetResource",
  144. "IceFlush",
  145. "warning LNK4089: all references to [^ \\t]+ discarded by .OPT:REF",
  146. "ld32: WARNING 85: definition of dataKey in",
  147. "cc: warning 422: Unknown option \"\\+b",
  148. "_with_warning_C",
  149. nullptr
  150. };
  151. struct cmCTestBuildCompileErrorWarningRex
  152. {
  153. char const* RegularExpressionString;
  154. int FileIndex;
  155. int LineIndex;
  156. };
  157. static cmCTestBuildCompileErrorWarningRex cmCTestWarningErrorFileLine[] = {
  158. { "^Warning W[0-9]+ ([a-zA-Z.\\:/0-9_+ ~-]+) ([0-9]+):", 1, 2 },
  159. { "^([a-zA-Z./0-9_+ ~-]+):([0-9]+):", 1, 2 },
  160. { R"(^([a-zA-Z.\:/0-9_+ ~-]+)\(([0-9]+)\))", 1, 2 },
  161. { R"(^[0-9]+>([a-zA-Z.\:/0-9_+ ~-]+)\(([0-9]+)\))", 1, 2 },
  162. { "^([a-zA-Z./0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
  163. { "\"([a-zA-Z./0-9_+ ~-]+)\", line ([0-9]+)", 1, 2 },
  164. { "File = ([a-zA-Z./0-9_+ ~-]+), Line = ([0-9]+)", 1, 2 },
  165. { "lcc: \"([^\"]+)\", (line|строка) ([0-9]+): "
  166. "(error|ошибка|warning|предупреждение)",
  167. 1, 3 },
  168. { nullptr, 0, 0 }
  169. };
  170. cmCTestBuildHandler::cmCTestBuildHandler(cmCTest* ctest)
  171. : Superclass(ctest)
  172. , LastErrorOrWarning(this->ErrorsAndWarnings.end())
  173. {
  174. }
  175. void cmCTestBuildHandler::PopulateCustomVectors(cmMakefile* mf)
  176. {
  177. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_MATCH",
  178. this->CustomErrorMatches);
  179. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_EXCEPTION",
  180. this->CustomErrorExceptions);
  181. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_MATCH",
  182. this->CustomWarningMatches);
  183. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_EXCEPTION",
  184. this->CustomWarningExceptions);
  185. this->CTest->PopulateCustomInteger(
  186. mf, "CTEST_CUSTOM_MAXIMUM_NUMBER_OF_ERRORS", this->MaxErrors);
  187. this->CTest->PopulateCustomInteger(
  188. mf, "CTEST_CUSTOM_MAXIMUM_NUMBER_OF_WARNINGS", this->MaxWarnings);
  189. int n = -1;
  190. this->CTest->PopulateCustomInteger(mf, "CTEST_CUSTOM_ERROR_PRE_CONTEXT", n);
  191. if (n != -1) {
  192. this->MaxPreContext = static_cast<size_t>(n);
  193. }
  194. n = -1;
  195. this->CTest->PopulateCustomInteger(mf, "CTEST_CUSTOM_ERROR_POST_CONTEXT", n);
  196. if (n != -1) {
  197. this->MaxPostContext = static_cast<size_t>(n);
  198. }
  199. // Record the user-specified custom warning rules.
  200. if (cmValue customWarningMatchers =
  201. mf->GetDefinition("CTEST_CUSTOM_WARNING_MATCH")) {
  202. cmExpandList(*customWarningMatchers, this->ReallyCustomWarningMatches);
  203. }
  204. if (cmValue customWarningExceptions =
  205. mf->GetDefinition("CTEST_CUSTOM_WARNING_EXCEPTION")) {
  206. cmExpandList(*customWarningExceptions,
  207. this->ReallyCustomWarningExceptions);
  208. }
  209. }
  210. std::string cmCTestBuildHandler::GetMakeCommand()
  211. {
  212. std::string makeCommand = this->CTest->GetCTestConfiguration("MakeCommand");
  213. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  214. "MakeCommand:" << makeCommand << "\n", this->Quiet);
  215. std::string configType = this->CTest->GetConfigType();
  216. if (configType.empty()) {
  217. configType =
  218. this->CTest->GetCTestConfiguration("DefaultCTestConfigurationType");
  219. }
  220. if (configType.empty()) {
  221. configType = "Release";
  222. }
  223. cmSystemTools::ReplaceString(makeCommand, "${CTEST_CONFIGURATION_TYPE}",
  224. configType.c_str());
  225. return makeCommand;
  226. }
  227. // clearly it would be nice if this were broken up into a few smaller
  228. // functions and commented...
  229. int cmCTestBuildHandler::ProcessHandler()
  230. {
  231. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "Build project" << std::endl,
  232. this->Quiet);
  233. // do we have time for this
  234. if (this->CTest->GetRemainingTimeAllowed() < std::chrono::minutes(2)) {
  235. return 0;
  236. }
  237. int entry;
  238. for (entry = 0; cmCTestWarningErrorFileLine[entry].RegularExpressionString;
  239. ++entry) {
  240. cmCTestBuildHandler::cmCTestCompileErrorWarningRex r;
  241. if (r.RegularExpression.compile(
  242. cmCTestWarningErrorFileLine[entry].RegularExpressionString)) {
  243. r.FileIndex = cmCTestWarningErrorFileLine[entry].FileIndex;
  244. r.LineIndex = cmCTestWarningErrorFileLine[entry].LineIndex;
  245. this->ErrorWarningFileLineRegex.push_back(std::move(r));
  246. } else {
  247. cmCTestLog(
  248. this->CTest, ERROR_MESSAGE,
  249. "Problem Compiling regular expression: "
  250. << cmCTestWarningErrorFileLine[entry].RegularExpressionString
  251. << std::endl);
  252. }
  253. }
  254. // Determine build command and build directory
  255. std::string makeCommand = this->GetMakeCommand();
  256. if (makeCommand.empty()) {
  257. cmCTestLog(this->CTest, ERROR_MESSAGE,
  258. "Cannot find MakeCommand key in the DartConfiguration.tcl"
  259. << std::endl);
  260. return -1;
  261. }
  262. std::string const& buildDirectory =
  263. this->CTest->GetCTestConfiguration("BuildDirectory");
  264. if (buildDirectory.empty()) {
  265. cmCTestLog(this->CTest, ERROR_MESSAGE,
  266. "Cannot find BuildDirectory key in the DartConfiguration.tcl"
  267. << std::endl);
  268. return -1;
  269. }
  270. std::string const& useLaunchers =
  271. this->CTest->GetCTestConfiguration("UseLaunchers");
  272. this->UseCTestLaunch = cmIsOn(useLaunchers);
  273. // Create a last build log
  274. cmGeneratedFileStream ofs;
  275. auto elapsed_time_start = std::chrono::steady_clock::now();
  276. if (!this->StartLogFile("Build", ofs)) {
  277. cmCTestLog(this->CTest, ERROR_MESSAGE,
  278. "Cannot create build log file" << std::endl);
  279. }
  280. this->LogFileName = ofs.GetTempName();
  281. // Create lists of regular expression strings for errors, error exceptions,
  282. // warnings and warning exceptions.
  283. std::vector<std::string>::size_type cc;
  284. for (cc = 0; cmCTestErrorMatches[cc]; cc++) {
  285. this->CustomErrorMatches.emplace_back(cmCTestErrorMatches[cc]);
  286. }
  287. for (cc = 0; cmCTestErrorExceptions[cc]; cc++) {
  288. this->CustomErrorExceptions.emplace_back(cmCTestErrorExceptions[cc]);
  289. }
  290. for (cc = 0; cmCTestWarningMatches[cc]; cc++) {
  291. this->CustomWarningMatches.emplace_back(cmCTestWarningMatches[cc]);
  292. }
  293. for (cc = 0; cmCTestWarningExceptions[cc]; cc++) {
  294. this->CustomWarningExceptions.emplace_back(cmCTestWarningExceptions[cc]);
  295. }
  296. // Pre-compile regular expressions objects for all regular expressions
  297. #define cmCTestBuildHandlerPopulateRegexVector(strings, regexes) \
  298. do { \
  299. regexes.clear(); \
  300. cmCTestOptionalLog(this->CTest, DEBUG, \
  301. this << "Add " #regexes << std::endl, this->Quiet); \
  302. for (std::string const& s : (strings)) { \
  303. cmCTestOptionalLog(this->CTest, DEBUG, \
  304. "Add " #strings ": " << s << std::endl, \
  305. this->Quiet); \
  306. (regexes).emplace_back(s); \
  307. } \
  308. } while (false)
  309. cmCTestBuildHandlerPopulateRegexVector(this->CustomErrorMatches,
  310. this->ErrorMatchRegex);
  311. cmCTestBuildHandlerPopulateRegexVector(this->CustomErrorExceptions,
  312. this->ErrorExceptionRegex);
  313. cmCTestBuildHandlerPopulateRegexVector(this->CustomWarningMatches,
  314. this->WarningMatchRegex);
  315. cmCTestBuildHandlerPopulateRegexVector(this->CustomWarningExceptions,
  316. this->WarningExceptionRegex);
  317. // Determine source and binary tree substitutions to simplify the output.
  318. this->SimplifySourceDir.clear();
  319. this->SimplifyBuildDir.clear();
  320. if (this->CTest->GetCTestConfiguration("SourceDirectory").size() > 20) {
  321. std::string srcdir =
  322. this->CTest->GetCTestConfiguration("SourceDirectory") + "/";
  323. cc = srcdir.rfind('/', srcdir.size() - 2);
  324. if (cc != std::string::npos) {
  325. srcdir.resize(cc + 1);
  326. this->SimplifySourceDir = std::move(srcdir);
  327. }
  328. }
  329. if (this->CTest->GetCTestConfiguration("BuildDirectory").size() > 20) {
  330. std::string bindir =
  331. this->CTest->GetCTestConfiguration("BuildDirectory") + "/";
  332. cc = bindir.rfind('/', bindir.size() - 2);
  333. if (cc != std::string::npos) {
  334. bindir.resize(cc + 1);
  335. this->SimplifyBuildDir = std::move(bindir);
  336. }
  337. }
  338. // Ok, let's do the build
  339. // Remember start build time
  340. this->StartBuild = this->CTest->CurrentTime();
  341. this->StartBuildTime = std::chrono::system_clock::now();
  342. cmStringReplaceHelper colorRemover("\x1b\\[[0-9;]*m", "", nullptr);
  343. this->ColorRemover = &colorRemover;
  344. int retVal = 0;
  345. bool res = true;
  346. if (!this->CTest->GetShowOnly()) {
  347. res = this->RunMakeCommand(makeCommand, &retVal, buildDirectory.c_str(), 0,
  348. ofs);
  349. } else {
  350. cmCTestOptionalLog(this->CTest, DEBUG,
  351. "Build with command: " << makeCommand << std::endl,
  352. this->Quiet);
  353. }
  354. // Remember end build time and calculate elapsed time
  355. this->EndBuild = this->CTest->CurrentTime();
  356. this->EndBuildTime = std::chrono::system_clock::now();
  357. auto elapsed_build_time =
  358. std::chrono::steady_clock::now() - elapsed_time_start;
  359. // Cleanups strings in the errors and warnings list.
  360. if (!this->SimplifySourceDir.empty()) {
  361. for (cmCTestBuildErrorWarning& evit : this->ErrorsAndWarnings) {
  362. cmSystemTools::ReplaceString(evit.Text, this->SimplifySourceDir.c_str(),
  363. "/.../");
  364. cmSystemTools::ReplaceString(evit.PreContext,
  365. this->SimplifySourceDir.c_str(), "/.../");
  366. cmSystemTools::ReplaceString(evit.PostContext,
  367. this->SimplifySourceDir.c_str(), "/.../");
  368. }
  369. }
  370. if (!this->SimplifyBuildDir.empty()) {
  371. for (cmCTestBuildErrorWarning& evit : this->ErrorsAndWarnings) {
  372. cmSystemTools::ReplaceString(evit.Text, this->SimplifyBuildDir.c_str(),
  373. "/.../");
  374. cmSystemTools::ReplaceString(evit.PreContext,
  375. this->SimplifyBuildDir.c_str(), "/.../");
  376. cmSystemTools::ReplaceString(evit.PostContext,
  377. this->SimplifyBuildDir.c_str(), "/.../");
  378. }
  379. }
  380. // Generate XML output
  381. cmGeneratedFileStream xofs;
  382. if (!this->StartResultingXML(cmCTest::PartBuild, "Build", xofs)) {
  383. cmCTestLog(this->CTest, ERROR_MESSAGE,
  384. "Cannot create build XML file" << std::endl);
  385. return -1;
  386. }
  387. cmXMLWriter xml(xofs);
  388. this->GenerateXMLHeader(xml);
  389. if (this->UseCTestLaunch) {
  390. this->GenerateXMLLaunched(xml);
  391. } else {
  392. this->GenerateXMLLogScraped(xml);
  393. }
  394. this->CTest->GetInstrumentation().CollectTimingData(
  395. cmInstrumentationQuery::Hook::PrepareForCDash);
  396. this->GenerateInstrumentationXML(xml);
  397. this->GenerateXMLFooter(xml, elapsed_build_time);
  398. if (!res || retVal || this->TotalErrors > 0) {
  399. cmCTestLog(this->CTest, ERROR_MESSAGE,
  400. "Error(s) when building project" << std::endl);
  401. }
  402. // Display message about number of errors and warnings
  403. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  404. " " << this->TotalErrors
  405. << (this->TotalErrors >= this->MaxErrors ? " or more" : "")
  406. << " Compiler errors" << std::endl);
  407. cmCTestLog(
  408. this->CTest, HANDLER_OUTPUT,
  409. " " << this->TotalWarnings
  410. << (this->TotalWarnings >= this->MaxWarnings ? " or more" : "")
  411. << " Compiler warnings" << std::endl);
  412. return retVal;
  413. }
  414. void cmCTestBuildHandler::GenerateXMLHeader(cmXMLWriter& xml)
  415. {
  416. this->CTest->StartXML(xml, this->CMake, this->AppendXML);
  417. this->CTest->GenerateSubprojectsOutput(xml);
  418. xml.StartElement("Build");
  419. xml.Element("StartDateTime", this->StartBuild);
  420. xml.Element("StartBuildTime", this->StartBuildTime);
  421. xml.Element("BuildCommand", this->GetMakeCommand());
  422. }
  423. class cmCTestBuildHandler::FragmentCompare
  424. {
  425. public:
  426. FragmentCompare(cmFileTimeCache* ftc)
  427. : FTC(ftc)
  428. {
  429. }
  430. FragmentCompare() = default;
  431. bool operator()(std::string const& l, std::string const& r) const
  432. {
  433. // Order files by modification time. Use lexicographic order
  434. // among files with the same time.
  435. int result;
  436. if (this->FTC->Compare(l, r, &result) && result != 0) {
  437. return result < 0;
  438. }
  439. return l < r;
  440. }
  441. private:
  442. cmFileTimeCache* FTC = nullptr;
  443. };
  444. void cmCTestBuildHandler::GenerateXMLLaunched(cmXMLWriter& xml)
  445. {
  446. if (this->CTestLaunchDir.empty()) {
  447. return;
  448. }
  449. // Sort XML fragments in chronological order.
  450. cmFileTimeCache ftc;
  451. FragmentCompare fragmentCompare(&ftc);
  452. using Fragments = std::set<std::string, FragmentCompare>;
  453. Fragments fragments(fragmentCompare);
  454. // only report the first 50 warnings and first 50 errors
  455. int numErrorsAllowed = this->MaxErrors;
  456. int numWarningsAllowed = this->MaxWarnings;
  457. // Identify fragments on disk.
  458. cmsys::Directory launchDir;
  459. launchDir.Load(this->CTestLaunchDir);
  460. unsigned long n = launchDir.GetNumberOfFiles();
  461. for (unsigned long i = 0; i < n; ++i) {
  462. char const* fname = launchDir.GetFile(i);
  463. if (this->IsLaunchedErrorFile(fname) && numErrorsAllowed) {
  464. numErrorsAllowed--;
  465. fragments.insert(this->CTestLaunchDir + '/' + fname);
  466. ++this->TotalErrors;
  467. } else if (this->IsLaunchedWarningFile(fname) && numWarningsAllowed) {
  468. numWarningsAllowed--;
  469. fragments.insert(this->CTestLaunchDir + '/' + fname);
  470. ++this->TotalWarnings;
  471. }
  472. }
  473. // Copy the fragments into the final XML file.
  474. for (std::string const& f : fragments) {
  475. xml.FragmentFile(f.c_str());
  476. }
  477. }
  478. void cmCTestBuildHandler::GenerateXMLLogScraped(cmXMLWriter& xml)
  479. {
  480. std::vector<cmCTestBuildErrorWarning>& ew = this->ErrorsAndWarnings;
  481. std::vector<cmCTestBuildErrorWarning>::iterator it;
  482. // only report the first 50 warnings and first 50 errors
  483. int numErrorsAllowed = this->MaxErrors;
  484. int numWarningsAllowed = this->MaxWarnings;
  485. std::string srcdir = this->CTest->GetCTestConfiguration("SourceDirectory");
  486. for (it = ew.begin();
  487. it != ew.end() && (numErrorsAllowed || numWarningsAllowed); it++) {
  488. cmCTestBuildErrorWarning* cm = &(*it);
  489. if ((cm->Error && numErrorsAllowed) ||
  490. (!cm->Error && numWarningsAllowed)) {
  491. if (cm->Error) {
  492. numErrorsAllowed--;
  493. } else {
  494. numWarningsAllowed--;
  495. }
  496. xml.StartElement(cm->Error ? "Error" : "Warning");
  497. xml.Element("BuildLogLine", cm->LogLine);
  498. xml.Element("Text", cm->Text);
  499. for (cmCTestCompileErrorWarningRex& rit :
  500. this->ErrorWarningFileLineRegex) {
  501. cmsys::RegularExpression* re = &rit.RegularExpression;
  502. if (re->find(cm->Text)) {
  503. cm->SourceFile = re->match(rit.FileIndex);
  504. // At this point we need to make this->SourceFile relative to
  505. // the source root of the project, so cvs links will work
  506. cmSystemTools::ConvertToUnixSlashes(cm->SourceFile);
  507. if (cm->SourceFile.find("/.../") != std::string::npos) {
  508. cmSystemTools::ReplaceString(cm->SourceFile, "/.../", "");
  509. std::string::size_type p = cm->SourceFile.find('/');
  510. if (p != std::string::npos) {
  511. cm->SourceFile =
  512. cm->SourceFile.substr(p + 1, cm->SourceFile.size() - p);
  513. }
  514. } else {
  515. // make sure it is a full path with the correct case
  516. cm->SourceFile =
  517. cmSystemTools::ToNormalizedPathOnDisk(cm->SourceFile);
  518. cmSystemTools::ReplaceString(cm->SourceFile, srcdir, "");
  519. }
  520. cm->LineNumber = atoi(re->match(rit.LineIndex).c_str());
  521. break;
  522. }
  523. }
  524. if (!cm->SourceFile.empty() && cm->LineNumber >= 0) {
  525. if (!cm->SourceFile.empty()) {
  526. xml.Element("SourceFile", cm->SourceFile);
  527. }
  528. if (!cm->SourceFileTail.empty()) {
  529. xml.Element("SourceFileTail", cm->SourceFileTail);
  530. }
  531. if (cm->LineNumber >= 0) {
  532. xml.Element("SourceLineNumber", cm->LineNumber);
  533. }
  534. }
  535. xml.Element("PreContext", cm->PreContext);
  536. xml.StartElement("PostContext");
  537. xml.Content(cm->PostContext);
  538. // is this the last warning or error, if so notify
  539. if ((cm->Error && !numErrorsAllowed) ||
  540. (!cm->Error && !numWarningsAllowed)) {
  541. xml.Content("\nThe maximum number of reported warnings or errors "
  542. "has been reached!!!\n");
  543. }
  544. xml.EndElement(); // PostContext
  545. xml.Element("RepeatCount", "0");
  546. xml.EndElement(); // "Error" / "Warning"
  547. }
  548. }
  549. }
  550. void cmCTestBuildHandler::GenerateInstrumentationXML(cmXMLWriter& xml)
  551. {
  552. // Record instrumentation data on a per-target basis.
  553. cmsys::Directory targets_dir;
  554. std::string targets_snippet_dir = cmStrCat(
  555. this->CTest->GetInstrumentation().GetCDashDir(), "/build/targets");
  556. if (targets_dir.Load(targets_snippet_dir) &&
  557. targets_dir.GetNumberOfFiles() > 0) {
  558. xml.StartElement("Targets");
  559. for (unsigned int i = 0; i < targets_dir.GetNumberOfFiles(); i++) {
  560. if (!targets_dir.FileIsDirectory(i)) {
  561. continue;
  562. }
  563. std::string target_name = targets_dir.GetFile(i);
  564. if (target_name == "." || target_name == "..") {
  565. continue;
  566. }
  567. std::string target_type = "UNKNOWN";
  568. xml.StartElement("Target");
  569. xml.Attribute("name", target_name);
  570. // Load latest CMake content file
  571. Json::Value target_data;
  572. std::string cmake_content_file =
  573. cmStrCat(CTest->GetInstrumentation().GetDataDir(), "/content/",
  574. this->CTest->GetInstrumentation().GetFileByTimestamp(
  575. cmInstrumentation::LatestOrOldest::Latest, "content"));
  576. if (!cmake_content_file.empty() &&
  577. cmSystemTools::FileExists(cmake_content_file)) {
  578. Json::Value cmake_content;
  579. cmJSONState parseState =
  580. cmJSONState(cmake_content_file, &cmake_content);
  581. if (!parseState.errors.empty()) {
  582. cmSystemTools::Error(parseState.GetErrorMessage(true));
  583. break;
  584. }
  585. if (cmake_content.isMember("targets")) {
  586. target_data = cmake_content["targets"];
  587. }
  588. }
  589. // Extract targetType and targetLabels
  590. if (target_data.isObject() && target_data.isMember(target_name)) {
  591. target_type = target_data[target_name]["type"].asString();
  592. if (!target_data[target_name]["labels"].empty()) {
  593. xml.StartElement("Labels");
  594. for (auto const& json_label_item :
  595. target_data[target_name]["labels"]) {
  596. xml.Element("Label", json_label_item.asString());
  597. }
  598. xml.EndElement(); // Labels
  599. }
  600. }
  601. xml.Attribute("type", target_type);
  602. // Write instrumendation data for this target.
  603. std::string target_subdir = cmStrCat("build/targets/", target_name);
  604. this->CTest->ConvertInstrumentationSnippetsToXML(xml, target_subdir);
  605. std::string target_dir_fullpath = cmStrCat(
  606. this->CTest->GetInstrumentation().GetCDashDir(), '/', target_subdir);
  607. if (cmSystemTools::FileIsDirectory(target_dir_fullpath)) {
  608. cmSystemTools::RemoveADirectory(target_dir_fullpath);
  609. }
  610. xml.EndElement(); // Target
  611. }
  612. xml.EndElement(); // Targets
  613. }
  614. // Also record instrumentation data for custom commands (no target).
  615. this->CTest->ConvertInstrumentationSnippetsToXML(xml, "build/commands");
  616. }
  617. void cmCTestBuildHandler::GenerateXMLFooter(cmXMLWriter& xml,
  618. cmDuration elapsed_build_time)
  619. {
  620. xml.StartElement("Log");
  621. xml.Attribute("Encoding", "base64");
  622. xml.Attribute("Compression", "bin/gzip");
  623. xml.EndElement(); // Log
  624. xml.Element("EndDateTime", this->EndBuild);
  625. xml.Element("EndBuildTime", this->EndBuildTime);
  626. xml.Element(
  627. "ElapsedMinutes",
  628. std::chrono::duration_cast<std::chrono::minutes>(elapsed_build_time)
  629. .count());
  630. xml.EndElement(); // Build
  631. this->CTest->EndXML(xml);
  632. }
  633. bool cmCTestBuildHandler::IsLaunchedErrorFile(char const* fname)
  634. {
  635. // error-{hash}.xml
  636. return (cmHasLiteralPrefix(fname, "error-") &&
  637. cmHasLiteralSuffix(fname, ".xml"));
  638. }
  639. bool cmCTestBuildHandler::IsLaunchedWarningFile(char const* fname)
  640. {
  641. // warning-{hash}.xml
  642. return (cmHasLiteralPrefix(fname, "warning-") &&
  643. cmHasLiteralSuffix(fname, ".xml"));
  644. }
  645. // ######################################################################
  646. // ######################################################################
  647. // ######################################################################
  648. // ######################################################################
  649. class cmCTestBuildHandler::LaunchHelper
  650. {
  651. public:
  652. LaunchHelper(cmCTestBuildHandler* handler);
  653. ~LaunchHelper();
  654. LaunchHelper(LaunchHelper const&) = delete;
  655. LaunchHelper& operator=(LaunchHelper const&) = delete;
  656. private:
  657. cmCTestBuildHandler* Handler;
  658. cmCTest* CTest;
  659. void WriteLauncherConfig();
  660. void WriteScrapeMatchers(char const* purpose,
  661. std::vector<std::string> const& matchers);
  662. };
  663. cmCTestBuildHandler::LaunchHelper::LaunchHelper(cmCTestBuildHandler* handler)
  664. : Handler(handler)
  665. , CTest(handler->CTest)
  666. {
  667. std::string tag = this->CTest->GetCurrentTag();
  668. if (tag.empty()) {
  669. // This is not for a dashboard submission, so there is no XML.
  670. // Skip enabling the launchers.
  671. this->Handler->UseCTestLaunch = false;
  672. } else {
  673. // Compute a directory in which to store launcher fragments.
  674. std::string& launchDir = this->Handler->CTestLaunchDir;
  675. launchDir =
  676. cmStrCat(this->CTest->GetBinaryDir(), "/Testing/", tag, "/Build");
  677. // Clean out any existing launcher fragments.
  678. cmSystemTools::RemoveADirectory(launchDir);
  679. if (this->Handler->UseCTestLaunch) {
  680. // Enable launcher fragments.
  681. cmSystemTools::MakeDirectory(launchDir);
  682. this->WriteLauncherConfig();
  683. std::string launchEnv = cmStrCat("CTEST_LAUNCH_LOGS=", launchDir);
  684. cmSystemTools::PutEnv(launchEnv);
  685. }
  686. }
  687. // If not using launchers, make sure they passthru.
  688. if (!this->Handler->UseCTestLaunch) {
  689. cmSystemTools::UnsetEnv("CTEST_LAUNCH_LOGS");
  690. }
  691. }
  692. cmCTestBuildHandler::LaunchHelper::~LaunchHelper()
  693. {
  694. if (this->Handler->UseCTestLaunch) {
  695. cmSystemTools::UnsetEnv("CTEST_LAUNCH_LOGS");
  696. }
  697. }
  698. void cmCTestBuildHandler::LaunchHelper::WriteLauncherConfig()
  699. {
  700. this->WriteScrapeMatchers("Warning",
  701. this->Handler->ReallyCustomWarningMatches);
  702. this->WriteScrapeMatchers("WarningSuppress",
  703. this->Handler->ReallyCustomWarningExceptions);
  704. // Give some testing configuration information to the launcher.
  705. std::string fname =
  706. cmStrCat(this->Handler->CTestLaunchDir, "/CTestLaunchConfig.cmake");
  707. cmGeneratedFileStream fout(fname);
  708. std::string srcdir = this->CTest->GetCTestConfiguration("SourceDirectory");
  709. fout << "set(CTEST_SOURCE_DIRECTORY \"" << srcdir << "\")\n";
  710. }
  711. void cmCTestBuildHandler::LaunchHelper::WriteScrapeMatchers(
  712. char const* purpose, std::vector<std::string> const& matchers)
  713. {
  714. if (matchers.empty()) {
  715. return;
  716. }
  717. std::string fname =
  718. cmStrCat(this->Handler->CTestLaunchDir, "/Custom", purpose, ".txt");
  719. cmGeneratedFileStream fout(fname);
  720. for (std::string const& m : matchers) {
  721. fout << m << "\n";
  722. }
  723. }
  724. bool cmCTestBuildHandler::RunMakeCommand(std::string const& command,
  725. int* retVal, char const* dir,
  726. int timeout, std::ostream& ofs,
  727. Encoding encoding)
  728. {
  729. // First generate the command and arguments
  730. std::vector<std::string> args = cmSystemTools::ParseArguments(command);
  731. if (args.empty()) {
  732. return false;
  733. }
  734. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  735. "Run command:", this->Quiet);
  736. for (auto const& arg : args) {
  737. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  738. " \"" << arg << "\"", this->Quiet);
  739. }
  740. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
  741. this->Quiet);
  742. // Optionally use make rule launchers to record errors and warnings.
  743. LaunchHelper launchHelper(this);
  744. static_cast<void>(launchHelper);
  745. // Now create process object
  746. cmUVProcessChainBuilder builder;
  747. builder.AddCommand(args)
  748. .SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
  749. .SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
  750. if (dir) {
  751. builder.SetWorkingDirectory(dir);
  752. }
  753. auto chain = builder.Start();
  754. // Initialize tick's
  755. std::string::size_type tick = 0;
  756. static constexpr std::string::size_type tick_len = 1024;
  757. cmProcessOutput processOutput(encoding);
  758. cmCTestOptionalLog(
  759. this->CTest, HANDLER_PROGRESS_OUTPUT,
  760. " Each symbol represents "
  761. << tick_len << " bytes of output." << std::endl
  762. << (this->UseCTestLaunch
  763. ? ""
  764. : " '!' represents an error and '*' a warning.\n")
  765. << " " << std::flush,
  766. this->Quiet);
  767. // Initialize building structures
  768. this->BuildProcessingQueue.clear();
  769. this->OutputLineCounter = 0;
  770. this->ErrorsAndWarnings.clear();
  771. this->TotalErrors = 0;
  772. this->TotalWarnings = 0;
  773. this->BuildOutputLogSize = 0;
  774. this->LastTickChar = '.';
  775. this->WarningQuotaReached = false;
  776. this->ErrorQuotaReached = false;
  777. cm::uv_timer_ptr timer;
  778. bool timedOut = false;
  779. timer.init(chain.GetLoop(), &timedOut);
  780. if (timeout > 0) {
  781. timer.start(
  782. [](uv_timer_t* t) {
  783. auto* timedOutPtr = static_cast<bool*>(t->data);
  784. *timedOutPtr = true;
  785. },
  786. timeout * 1000, 0, cm::uv_update_time::yes);
  787. }
  788. // For every chunk of data
  789. cm::uv_pipe_ptr outputStream;
  790. bool outFinished = false;
  791. cm::uv_pipe_ptr errorStream;
  792. bool errFinished = false;
  793. auto startRead = [this, &chain, &processOutput, &tick,
  794. &ofs](cm::uv_pipe_ptr& pipe, int stream,
  795. t_BuildProcessingQueueType& queue, bool& finished,
  796. int id) -> std::unique_ptr<cmUVStreamReadHandle> {
  797. pipe.init(chain.GetLoop(), 0);
  798. uv_pipe_open(pipe, stream);
  799. return cmUVStreamRead(
  800. pipe,
  801. [this, &processOutput, &queue, id, &tick, &ofs](std::vector<char> data) {
  802. // Replace '\0' with '\n', since '\0' does not really make sense. This
  803. // is for Visual Studio output
  804. for (auto& c : data) {
  805. if (c == 0) {
  806. c = '\n';
  807. }
  808. }
  809. // Process the chunk of data
  810. std::string strdata;
  811. processOutput.DecodeText(data.data(), data.size(), strdata, id);
  812. this->ProcessBuffer(strdata.c_str(), strdata.size(), tick, tick_len,
  813. ofs, &queue);
  814. },
  815. [this, &processOutput, &queue, id, &tick, &ofs, &finished]() {
  816. std::string strdata;
  817. processOutput.DecodeText(std::string(), strdata, id);
  818. if (!strdata.empty()) {
  819. this->ProcessBuffer(strdata.c_str(), strdata.size(), tick, tick_len,
  820. ofs, &queue);
  821. }
  822. finished = true;
  823. });
  824. };
  825. auto outputHandle = startRead(outputStream, chain.OutputStream(),
  826. this->BuildProcessingQueue, outFinished, 1);
  827. auto errorHandle =
  828. startRead(errorStream, chain.ErrorStream(),
  829. this->BuildProcessingErrorQueue, errFinished, 2);
  830. while (!timedOut && !(outFinished && errFinished && chain.Finished())) {
  831. uv_run(&chain.GetLoop(), UV_RUN_ONCE);
  832. }
  833. this->ProcessBuffer(nullptr, 0, tick, tick_len, ofs,
  834. &this->BuildProcessingQueue);
  835. this->ProcessBuffer(nullptr, 0, tick, tick_len, ofs,
  836. &this->BuildProcessingErrorQueue);
  837. cmCTestOptionalLog(this->CTest, HANDLER_PROGRESS_OUTPUT,
  838. " Size of output: "
  839. << ((this->BuildOutputLogSize + 512) / 1024) << "K"
  840. << std::endl,
  841. this->Quiet);
  842. if (chain.Finished()) {
  843. auto const& status = chain.GetStatus(0);
  844. auto exception = status.GetException();
  845. switch (exception.first) {
  846. case cmUVProcessChain::ExceptionCode::None:
  847. if (retVal) {
  848. *retVal = static_cast<int>(status.ExitStatus);
  849. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  850. "Command exited with the value: " << *retVal
  851. << std::endl,
  852. this->Quiet);
  853. // if a non zero return value
  854. if (*retVal) {
  855. // If there was an error running command, report that on the
  856. // dashboard.
  857. if (this->UseCTestLaunch) {
  858. // For launchers, do not record this top-level error if other
  859. // more granular build errors have already been captured.
  860. bool launcherXMLFound = false;
  861. cmsys::Directory launchDir;
  862. launchDir.Load(this->CTestLaunchDir);
  863. unsigned long n = launchDir.GetNumberOfFiles();
  864. for (unsigned long i = 0; i < n; ++i) {
  865. char const* fname = launchDir.GetFile(i);
  866. if (cmHasLiteralSuffix(fname, ".xml")) {
  867. launcherXMLFound = true;
  868. break;
  869. }
  870. }
  871. if (!launcherXMLFound) {
  872. cmCTestLaunchReporter reporter;
  873. reporter.RealArgs = args;
  874. reporter.ComputeFileNames();
  875. reporter.ExitCode = *retVal;
  876. reporter.Status = status;
  877. // Use temporary BuildLog file to populate this error for
  878. // CDash.
  879. ofs.flush();
  880. reporter.LogOut = this->LogFileName;
  881. reporter.WriteXML();
  882. }
  883. } else {
  884. cmCTestBuildErrorWarning errorwarning;
  885. errorwarning.LineNumber = 0;
  886. errorwarning.LogLine = 1;
  887. errorwarning.Text = cmStrCat(
  888. "*** WARNING non-zero return value in ctest from: ", args[0]);
  889. errorwarning.PreContext.clear();
  890. errorwarning.PostContext.clear();
  891. errorwarning.Error = false;
  892. this->ErrorsAndWarnings.push_back(std::move(errorwarning));
  893. this->TotalWarnings++;
  894. }
  895. }
  896. }
  897. break;
  898. case cmUVProcessChain::ExceptionCode::Spawn: {
  899. // If there was an error running command, report that on the dashboard.
  900. cmCTestBuildErrorWarning errorwarning;
  901. errorwarning.LineNumber = 0;
  902. errorwarning.LogLine = 1;
  903. errorwarning.Text =
  904. cmStrCat("*** ERROR executing: ", exception.second);
  905. errorwarning.PreContext.clear();
  906. errorwarning.PostContext.clear();
  907. errorwarning.Error = true;
  908. this->ErrorsAndWarnings.push_back(std::move(errorwarning));
  909. this->TotalErrors++;
  910. cmCTestLog(this->CTest, ERROR_MESSAGE,
  911. "There was an error: " << exception.second << std::endl);
  912. } break;
  913. default:
  914. if (retVal) {
  915. *retVal = status.TermSignal;
  916. cmCTestOptionalLog(
  917. this->CTest, WARNING,
  918. "There was an exception: " << *retVal << std::endl, this->Quiet);
  919. }
  920. break;
  921. }
  922. } else {
  923. chain.Terminate();
  924. cmCTestOptionalLog(this->CTest, WARNING,
  925. "There was a timeout" << std::endl, this->Quiet);
  926. }
  927. return true;
  928. }
  929. // ######################################################################
  930. // ######################################################################
  931. // ######################################################################
  932. // ######################################################################
  933. void cmCTestBuildHandler::ProcessBuffer(char const* data, size_t length,
  934. size_t& tick, size_t tick_len,
  935. std::ostream& ofs,
  936. t_BuildProcessingQueueType* queue)
  937. {
  938. std::string::size_type const tick_line_len = 50;
  939. char const* ptr;
  940. for (ptr = data; ptr < data + length; ptr++) {
  941. queue->push_back(*ptr);
  942. }
  943. this->BuildOutputLogSize += length;
  944. // until there are any lines left in the buffer
  945. while (true) {
  946. // Find the end of line
  947. t_BuildProcessingQueueType::iterator it;
  948. for (it = queue->begin(); it != queue->end(); ++it) {
  949. if (*it == '\n') {
  950. break;
  951. }
  952. }
  953. // Once certain number of errors or warnings reached, ignore future errors
  954. // or warnings.
  955. if (this->TotalWarnings >= this->MaxWarnings) {
  956. this->WarningQuotaReached = true;
  957. }
  958. if (this->TotalErrors >= this->MaxErrors) {
  959. this->ErrorQuotaReached = true;
  960. }
  961. // If the end of line was found
  962. if (it != queue->end()) {
  963. // Create a contiguous array for the line
  964. this->CurrentProcessingLine.clear();
  965. cm::append(this->CurrentProcessingLine, queue->begin(), it);
  966. this->CurrentProcessingLine.push_back(0);
  967. char const* line = this->CurrentProcessingLine.data();
  968. // Process the line
  969. int lineType = this->ProcessSingleLine(line);
  970. // Erase the line from the queue
  971. queue->erase(queue->begin(), it + 1);
  972. // Depending on the line type, produce error or warning, or nothing
  973. cmCTestBuildErrorWarning errorwarning;
  974. bool found = false;
  975. switch (lineType) {
  976. case b_WARNING_LINE:
  977. this->LastTickChar = '*';
  978. errorwarning.Error = false;
  979. found = true;
  980. this->TotalWarnings++;
  981. break;
  982. case b_ERROR_LINE:
  983. this->LastTickChar = '!';
  984. errorwarning.Error = true;
  985. found = true;
  986. this->TotalErrors++;
  987. break;
  988. }
  989. if (found) {
  990. // This is an error or warning, so generate report
  991. errorwarning.LogLine = static_cast<int>(this->OutputLineCounter + 1);
  992. errorwarning.Text = line;
  993. errorwarning.PreContext.clear();
  994. errorwarning.PostContext.clear();
  995. // Copy pre-context to report
  996. for (std::string const& pc : this->PreContext) {
  997. errorwarning.PreContext += pc + "\n";
  998. }
  999. this->PreContext.clear();
  1000. // Store report
  1001. this->ErrorsAndWarnings.push_back(std::move(errorwarning));
  1002. this->LastErrorOrWarning = this->ErrorsAndWarnings.end() - 1;
  1003. this->PostContextCount = 0;
  1004. } else {
  1005. // This is not an error or warning.
  1006. // So, figure out if this is a post-context line
  1007. if (!this->ErrorsAndWarnings.empty() &&
  1008. this->LastErrorOrWarning != this->ErrorsAndWarnings.end() &&
  1009. this->PostContextCount < this->MaxPostContext) {
  1010. this->PostContextCount++;
  1011. this->LastErrorOrWarning->PostContext += line;
  1012. if (this->PostContextCount < this->MaxPostContext) {
  1013. this->LastErrorOrWarning->PostContext += "\n";
  1014. }
  1015. } else {
  1016. // Otherwise store pre-context for the next error
  1017. this->PreContext.emplace_back(line);
  1018. if (this->PreContext.size() > this->MaxPreContext) {
  1019. this->PreContext.erase(this->PreContext.begin(),
  1020. this->PreContext.end() -
  1021. this->MaxPreContext);
  1022. }
  1023. }
  1024. }
  1025. this->OutputLineCounter++;
  1026. } else {
  1027. break;
  1028. }
  1029. }
  1030. // Now that the buffer is processed, display missing ticks
  1031. int tickDisplayed = false;
  1032. while (this->BuildOutputLogSize > (tick * tick_len)) {
  1033. tick++;
  1034. cmCTestOptionalLog(this->CTest, HANDLER_PROGRESS_OUTPUT,
  1035. this->LastTickChar, this->Quiet);
  1036. tickDisplayed = true;
  1037. if (tick % tick_line_len == 0 && tick > 0) {
  1038. cmCTestOptionalLog(this->CTest, HANDLER_PROGRESS_OUTPUT,
  1039. " Size: "
  1040. << ((this->BuildOutputLogSize + 512) / 1024) << "K"
  1041. << std::endl
  1042. << " ",
  1043. this->Quiet);
  1044. }
  1045. }
  1046. if (tickDisplayed) {
  1047. this->LastTickChar = '.';
  1048. }
  1049. // And if this is verbose output, display the content of the chunk
  1050. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1051. cm::string_view(data, length));
  1052. // Always store the chunk to the file
  1053. ofs << cm::string_view(data, length);
  1054. }
  1055. int cmCTestBuildHandler::ProcessSingleLine(char const* data)
  1056. {
  1057. if (this->UseCTestLaunch) {
  1058. // No log scraping when using launchers.
  1059. return b_REGULAR_LINE;
  1060. }
  1061. // Ignore ANSI color codes when checking for errors and warnings.
  1062. std::string input(data);
  1063. std::string line;
  1064. this->ColorRemover->Replace(input, line);
  1065. cmCTestOptionalLog(this->CTest, DEBUG, "Line: [" << line << "]" << std::endl,
  1066. this->Quiet);
  1067. int warningLine = 0;
  1068. int errorLine = 0;
  1069. // Check for regular expressions
  1070. if (!this->ErrorQuotaReached) {
  1071. // Errors
  1072. int wrxCnt = 0;
  1073. for (cmsys::RegularExpression& rx : this->ErrorMatchRegex) {
  1074. if (rx.find(line.c_str())) {
  1075. errorLine = 1;
  1076. cmCTestOptionalLog(this->CTest, DEBUG,
  1077. " Error Line: " << line << " (matches: "
  1078. << this->CustomErrorMatches[wrxCnt]
  1079. << ")" << std::endl,
  1080. this->Quiet);
  1081. break;
  1082. }
  1083. wrxCnt++;
  1084. }
  1085. // Error exceptions
  1086. wrxCnt = 0;
  1087. for (cmsys::RegularExpression& rx : this->ErrorExceptionRegex) {
  1088. if (rx.find(line.c_str())) {
  1089. errorLine = 0;
  1090. cmCTestOptionalLog(this->CTest, DEBUG,
  1091. " Not an error Line: "
  1092. << line << " (matches: "
  1093. << this->CustomErrorExceptions[wrxCnt] << ")"
  1094. << std::endl,
  1095. this->Quiet);
  1096. break;
  1097. }
  1098. wrxCnt++;
  1099. }
  1100. }
  1101. if (!this->WarningQuotaReached) {
  1102. // Warnings
  1103. int wrxCnt = 0;
  1104. for (cmsys::RegularExpression& rx : this->WarningMatchRegex) {
  1105. if (rx.find(line.c_str())) {
  1106. warningLine = 1;
  1107. cmCTestOptionalLog(this->CTest, DEBUG,
  1108. " Warning Line: "
  1109. << line << " (matches: "
  1110. << this->CustomWarningMatches[wrxCnt] << ")"
  1111. << std::endl,
  1112. this->Quiet);
  1113. break;
  1114. }
  1115. wrxCnt++;
  1116. }
  1117. wrxCnt = 0;
  1118. // Warning exceptions
  1119. for (cmsys::RegularExpression& rx : this->WarningExceptionRegex) {
  1120. if (rx.find(line.c_str())) {
  1121. warningLine = 0;
  1122. cmCTestOptionalLog(this->CTest, DEBUG,
  1123. " Not a warning Line: "
  1124. << line << " (matches: "
  1125. << this->CustomWarningExceptions[wrxCnt] << ")"
  1126. << std::endl,
  1127. this->Quiet);
  1128. break;
  1129. }
  1130. wrxCnt++;
  1131. }
  1132. }
  1133. if (errorLine) {
  1134. return b_ERROR_LINE;
  1135. }
  1136. if (warningLine) {
  1137. return b_WARNING_LINE;
  1138. }
  1139. return b_REGULAR_LINE;
  1140. }