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