cmCTestBuildHandler.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestBuildHandler.h"
  14. #include "cmCTest.h"
  15. #include "cmake.h"
  16. #include "cmMakefile.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmGlobalGenerator.h"
  19. #include "cmGeneratedFileStream.h"
  20. //#include <cmsys/RegularExpression.hxx>
  21. #include <cmsys/Process.h>
  22. // used for sleep
  23. #ifdef _WIN32
  24. #include "windows.h"
  25. #endif
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <math.h>
  29. #include <float.h>
  30. static const char* cmCTestErrorMatches[] = {
  31. "^[Bb]us [Ee]rror",
  32. "^[Ss]egmentation [Vv]iolation",
  33. "^[Ss]egmentation [Ff]ault",
  34. "([^ :]+):([0-9]+): ([^ \\t])",
  35. "([^:]+): error[ \\t]*[0-9]+[ \\t]*:",
  36. "^Error ([0-9]+):",
  37. "^Fatal",
  38. "^Error: ",
  39. "^Error ",
  40. "[0-9] ERROR: ",
  41. "^\"[^\"]+\", line [0-9]+: [^Ww]",
  42. "^cc[^C]*CC: ERROR File = ([^,]+), Line = ([0-9]+)",
  43. "^ld([^:])*:([ \\t])*ERROR([^:])*:",
  44. "^ild:([ \\t])*\\(undefined symbol\\)",
  45. "([^ :]+) : (error|fatal error|catastrophic error)",
  46. "([^:]+): (Error:|error|undefined reference|multiply defined)",
  47. "([^:]+)\\(([^\\)]+)\\) : (error|fatal error|catastrophic error)",
  48. "^fatal error C[0-9]+:",
  49. ": syntax error ",
  50. "^collect2: ld returned 1 exit status",
  51. "Unsatisfied symbols:",
  52. "^Unresolved:",
  53. "Undefined symbols:",
  54. "^Undefined[ \\t]+first referenced",
  55. "^CMake Error:",
  56. ":[ \\t]cannot find",
  57. ":[ \\t]can't find",
  58. ": \\*\\*\\* No rule to make target \\`.*\\'. Stop",
  59. ": Invalid loader fixup for symbol",
  60. ": internal link edit command failed",
  61. ": Unrecognized option \\`.*\\'",
  62. "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\([^W]\\)",
  63. "ld: 0706-006 Cannot find or open library file: -l ",
  64. "ild: \\(argument error\\) can't find library argument ::",
  65. "^could not be found and will not be loaded.",
  66. "s:616 string too big",
  67. "make: Fatal error: ",
  68. "ld: 0711-993 Error occurred while writing to the output file:",
  69. "ld: fatal: ",
  70. "make: \\*\\*\\*.*Error",
  71. "\\*\\*\\* Error code",
  72. "^Internal error:",
  73. "Makefile:[0-9]+: \\*\\*\\* .* Stop\\.",
  74. 0
  75. };
  76. static const char* cmCTestErrorExceptions[] = {
  77. "instantiated from ",
  78. "candidates are:",
  79. ": warning",
  80. "makefile:",
  81. "Makefile:",
  82. ":[ \\t]+Where:",
  83. "([^ :]+):([0-9]+): Warning",
  84. 0
  85. };
  86. static const char* cmCTestWarningMatches[] = {
  87. "([^ :]+):([0-9]+): warning:",
  88. "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
  89. "^ld([^:])*:([ \\t])*WARNING([^:])*:",
  90. "([^:]+): warning ([0-9]+):",
  91. "^\"[^\"]+\", line [0-9]+: [Ww]arning",
  92. "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
  93. "^Warning ([0-9]+):",
  94. "^Warning ",
  95. "WARNING: ",
  96. "([^ :]+) : warning",
  97. "([^:]+): warning",
  98. "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\(W\\)",
  99. "^cxx: Warning:",
  100. ".*file: .* has no symbols",
  101. "([^ :]+):([0-9]+): Warning",
  102. "\\([0-9]*\\): remark #[0-9]*",
  103. "\".*\", line [0-9]+: remark\\([0-9]*\\):",
  104. "cc-[0-9]* CC: REMARK File = .*, Line = [0-9]*",
  105. 0
  106. };
  107. static const char* cmCTestWarningExceptions[] = {
  108. "/usr/openwin/include/X11/Xlib\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  109. "/usr/openwin/include/X11/Xutil\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  110. "/usr/openwin/include/X11/XResource\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  111. "WARNING 84 :",
  112. "WARNING 47 :",
  113. "makefile:",
  114. "Makefile:",
  115. "warning: Clock skew detected. Your build may be incomplete.",
  116. "/usr/openwin/include/GL/[^:]+:",
  117. "bind_at_load",
  118. "XrmQGetResource",
  119. "IceFlush",
  120. "warning LNK4089: all references to [^ \\t]+ discarded by .OPT:REF",
  121. "ld32: WARNING 85: definition of dataKey in",
  122. "cc: warning 422: Unknown option \"\\+b",
  123. "_with_warning_C",
  124. 0
  125. };
  126. struct cmCTestBuildCompileErrorWarningRex
  127. {
  128. const char* m_RegularExpressionString;
  129. int m_FileIndex;
  130. int m_LineIndex;
  131. };
  132. static cmCTestBuildCompileErrorWarningRex
  133. cmCTestWarningErrorFileLine[] = {
  134. { "^Warning W[0-9]+ ([a-zA-Z.\\:/0-9_+ ~-]+) ([0-9]+):", 1, 2 },
  135. { "^([a-zA-Z./0-9_+ ~-]+):([0-9]+):", 1, 2 },
  136. { "^([a-zA-Z.\\:/0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
  137. { "^([a-zA-Z./0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
  138. { "\"([a-zA-Z./0-9_+ ~-]+)\", line ([0-9]+)", 1, 2 },
  139. { "File = ([a-zA-Z./0-9_+ ~-]+), Line = ([0-9]+)", 1, 2 },
  140. { 0, 0, 0 }
  141. };
  142. //----------------------------------------------------------------------
  143. cmCTestBuildHandler::cmCTestBuildHandler()
  144. {
  145. m_MaxPreContext = 6;
  146. m_MaxPostContext = 6;
  147. m_MaxErrors = 50;
  148. m_MaxWarnings = 50;
  149. m_LastErrorOrWarning = m_ErrorsAndWarnings.end();
  150. int cc;
  151. for ( cc = 0; cmCTestWarningErrorFileLine[cc].m_RegularExpressionString; ++ cc )
  152. {
  153. cmCTestBuildHandler::cmCTestCompileErrorWarningRex r;
  154. if ( r.m_RegularExpression.compile(
  155. cmCTestWarningErrorFileLine[cc].m_RegularExpressionString) )
  156. {
  157. r.m_FileIndex = cmCTestWarningErrorFileLine[cc].m_FileIndex;
  158. r.m_LineIndex = cmCTestWarningErrorFileLine[cc].m_LineIndex;
  159. m_ErrorWarningFileLineRegex.push_back(r);
  160. }
  161. else
  162. {
  163. cmCTestLog(m_CTest, ERROR_MESSAGE, "Problem Compiling regular expression: "
  164. << cmCTestWarningErrorFileLine[cc].m_RegularExpressionString << std::endl);
  165. }
  166. }
  167. }
  168. //----------------------------------------------------------------------
  169. void cmCTestBuildHandler::PopulateCustomVectors(cmMakefile *mf)
  170. {
  171. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_MATCH",
  172. m_CustomErrorMatches);
  173. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_EXCEPTION",
  174. m_CustomErrorExceptions);
  175. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_MATCH",
  176. m_CustomWarningMatches);
  177. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_EXCEPTION",
  178. m_CustomWarningExceptions);
  179. }
  180. //----------------------------------------------------------------------
  181. //clearly it would be nice if this were broken up into a few smaller
  182. //functions and commented...
  183. int cmCTestBuildHandler::ProcessHandler()
  184. {
  185. cmCTestLog(m_CTest, HANDLER_OUTPUT, "Build project" << std::endl);
  186. // Determine build command and build directory
  187. const std::string &makeCommand = m_CTest->GetCTestConfiguration("MakeCommand");
  188. if ( makeCommand.size() == 0 )
  189. {
  190. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot find MakeCommand key in the DartConfiguration.tcl" << std::endl);
  191. return -1;
  192. }
  193. const std::string &buildDirectory = m_CTest->GetCTestConfiguration("BuildDirectory");
  194. if ( buildDirectory.size() == 0 )
  195. {
  196. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot find BuildDirectory key in the DartConfiguration.tcl" << std::endl);
  197. return -1;
  198. }
  199. // Create a last build log
  200. cmGeneratedFileStream ofs;
  201. double elapsed_time_start = cmSystemTools::GetTime();
  202. if ( !m_CTest->OpenOutputFile("Temporary", "LastBuild.log", ofs) )
  203. {
  204. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot create LastBuild.log file" << std::endl);
  205. }
  206. // Create lists of regular expression strings for errors, error exceptions,
  207. // warnings and warning exceptions.
  208. std::vector<cmStdString>::size_type cc;
  209. for ( cc = 0; cmCTestErrorMatches[cc]; cc ++ )
  210. {
  211. m_CustomErrorMatches.push_back(cmCTestErrorMatches[cc]);
  212. }
  213. for ( cc = 0; cmCTestErrorExceptions[cc]; cc ++ )
  214. {
  215. m_CustomErrorExceptions.push_back(cmCTestErrorExceptions[cc]);
  216. }
  217. for ( cc = 0; cmCTestWarningMatches[cc]; cc ++ )
  218. {
  219. m_CustomWarningMatches.push_back(cmCTestWarningMatches[cc]);
  220. }
  221. for ( cc = 0; cmCTestWarningExceptions[cc]; cc ++ )
  222. {
  223. m_CustomWarningExceptions.push_back(cmCTestWarningExceptions[cc]);
  224. }
  225. // Pre-compile regular expressions objects for all regular expressions
  226. std::vector<cmStdString>::iterator it;
  227. #define cmCTestBuildHandlerPopulateRegexVector(strings, regexes) \
  228. regexes.clear(); \
  229. for ( it = strings.begin(); it != strings.end(); ++it ) \
  230. { \
  231. regexes.push_back(it->c_str()); \
  232. }
  233. cmCTestBuildHandlerPopulateRegexVector(m_CustomErrorMatches, m_ErrorMatchRegex);
  234. cmCTestBuildHandlerPopulateRegexVector(m_CustomErrorExceptions, m_ErrorExceptionRegex);
  235. cmCTestBuildHandlerPopulateRegexVector(m_CustomWarningMatches, m_WarningMatchRegex);
  236. cmCTestBuildHandlerPopulateRegexVector(m_CustomWarningExceptions, m_WarningExceptionRegex);
  237. // Determine source and binary tree substitutions to simplify the output.
  238. m_SimplifySourceDir = "";
  239. m_SimplifyBuildDir = "";
  240. if ( m_CTest->GetCTestConfiguration("SourceDirectory").size() > 20 )
  241. {
  242. std::string srcdir = m_CTest->GetCTestConfiguration("SourceDirectory") + "/";
  243. std::string srcdirrep;
  244. for ( cc = srcdir.size()-2; cc > 0; cc -- )
  245. {
  246. if ( srcdir[cc] == '/' )
  247. {
  248. srcdirrep = srcdir.c_str() + cc;
  249. srcdirrep = "/..." + srcdirrep;
  250. srcdir = srcdir.substr(0, cc+1);
  251. break;
  252. }
  253. }
  254. m_SimplifySourceDir = srcdir;
  255. }
  256. if ( m_CTest->GetCTestConfiguration("BuildDirectory").size() > 20 )
  257. {
  258. std::string bindir = m_CTest->GetCTestConfiguration("BuildDirectory") + "/";
  259. std::string bindirrep;
  260. for ( cc = bindir.size()-2; cc > 0; cc -- )
  261. {
  262. if ( bindir[cc] == '/' )
  263. {
  264. bindirrep = bindir.c_str() + cc;
  265. bindirrep = "/..." + bindirrep;
  266. bindir = bindir.substr(0, cc+1);
  267. break;
  268. }
  269. }
  270. m_SimplifyBuildDir = bindir;
  271. }
  272. // Ok, let's do the build
  273. // Remember start build time
  274. m_StartBuild = m_CTest->CurrentTime();
  275. int retVal = 0;
  276. int res = cmsysProcess_State_Exited;
  277. if ( !m_CTest->GetShowOnly() )
  278. {
  279. res = this->RunMakeCommand(makeCommand.c_str(), &retVal, buildDirectory.c_str(), 0, ofs);
  280. }
  281. else
  282. {
  283. cmCTestLog(m_CTest, DEBUG, "Build with command: " << makeCommand << std::endl);
  284. }
  285. // Remember end build time and calculate elapsed time
  286. m_EndBuild = m_CTest->CurrentTime();
  287. double elapsed_build_time = cmSystemTools::GetTime() - elapsed_time_start;
  288. if (res != cmsysProcess_State_Exited || retVal )
  289. {
  290. cmCTestLog(m_CTest, ERROR_MESSAGE, "Error(s) when building project" << std::endl);
  291. }
  292. // Cleanups strings in the errors and warnings list.
  293. t_ErrorsAndWarningsVector::iterator evit;
  294. if ( !m_SimplifySourceDir.empty() )
  295. {
  296. for ( evit = m_ErrorsAndWarnings.begin(); evit != m_ErrorsAndWarnings.end(); ++ evit )
  297. {
  298. cmSystemTools::ReplaceString(evit->m_Text, m_SimplifySourceDir.c_str(), "/.../");
  299. cmSystemTools::ReplaceString(evit->m_PreContext, m_SimplifySourceDir.c_str(), "/.../");
  300. cmSystemTools::ReplaceString(evit->m_PostContext, m_SimplifySourceDir.c_str(), "/.../");
  301. }
  302. }
  303. if ( !m_SimplifyBuildDir.empty() )
  304. {
  305. for ( evit = m_ErrorsAndWarnings.begin(); evit != m_ErrorsAndWarnings.end(); ++ evit )
  306. {
  307. cmSystemTools::ReplaceString(evit->m_Text, m_SimplifyBuildDir.c_str(), "/.../");
  308. cmSystemTools::ReplaceString(evit->m_PreContext, m_SimplifyBuildDir.c_str(), "/.../");
  309. cmSystemTools::ReplaceString(evit->m_PostContext, m_SimplifyBuildDir.c_str(), "/.../");
  310. }
  311. }
  312. // Display message about number of errors and warnings
  313. cmCTestLog(m_CTest, HANDLER_OUTPUT, " " << m_TotalErrors
  314. << (m_TotalErrors >= m_MaxErrors ? " or more" : "")
  315. << " Compiler errors" << std::endl);
  316. cmCTestLog(m_CTest, HANDLER_OUTPUT, " " << m_TotalWarnings
  317. << (m_TotalWarnings >= m_MaxWarnings ? " or more" : "")
  318. << " Compiler warnings" << std::endl);
  319. // Generate XML output
  320. cmGeneratedFileStream xofs;
  321. if( !m_CTest->OpenOutputFile(m_CTest->GetCurrentTag(), "Build.xml", xofs, true) )
  322. {
  323. cmCTestLog(m_CTest, ERROR_MESSAGE, "Cannot create build XML file" << std::endl);
  324. return -1;
  325. }
  326. this->GenerateDartBuildOutput(xofs, m_ErrorsAndWarnings, elapsed_build_time);
  327. return 0;
  328. }
  329. //----------------------------------------------------------------------
  330. void cmCTestBuildHandler::GenerateDartBuildOutput(
  331. std::ostream& os,
  332. std::vector<cmCTestBuildErrorWarning> ew,
  333. double elapsed_build_time)
  334. {
  335. m_CTest->StartXML(os);
  336. os << "<Build>\n"
  337. << "\t<StartDateTime>" << m_StartBuild << "</StartDateTime>\n"
  338. << "<BuildCommand>"
  339. << m_CTest->MakeXMLSafe(m_CTest->GetCTestConfiguration("MakeCommand"))
  340. << "</BuildCommand>" << std::endl;
  341. std::vector<cmCTestBuildErrorWarning>::iterator it;
  342. // only report the first 50 warnings and first 50 errors
  343. unsigned short numErrorsAllowed = m_MaxErrors;
  344. unsigned short numWarningsAllowed = m_MaxWarnings;
  345. std::string srcdir = m_CTest->GetCTestConfiguration("SourceDirectory");
  346. // make sure the source dir is in the correct case on windows
  347. // via a call to collapse full path.
  348. srcdir = cmSystemTools::CollapseFullPath(srcdir.c_str());
  349. srcdir += "/";
  350. for ( it = ew.begin();
  351. it != ew.end() && (numErrorsAllowed || numWarningsAllowed); it++ )
  352. {
  353. cmCTestBuildErrorWarning *cm = &(*it);
  354. if (cm->m_Error && numErrorsAllowed ||
  355. !cm->m_Error && numWarningsAllowed)
  356. {
  357. if (cm->m_Error)
  358. {
  359. numErrorsAllowed--;
  360. }
  361. else
  362. {
  363. numWarningsAllowed--;
  364. }
  365. os << "\t<" << (cm->m_Error ? "Error" : "Warning") << ">\n"
  366. << "\t\t<BuildLogLine>" << cm->m_LogLine << "</BuildLogLine>\n"
  367. << "\t\t<Text>" << m_CTest->MakeXMLSafe(cm->m_Text)
  368. << "\n</Text>" << std::endl;
  369. std::vector<cmCTestCompileErrorWarningRex>::iterator rit;
  370. for ( rit = m_ErrorWarningFileLineRegex.begin();
  371. rit != m_ErrorWarningFileLineRegex.end(); ++ rit )
  372. {
  373. cmsys::RegularExpression* re = &rit->m_RegularExpression;
  374. if ( re->find(cm->m_Text.c_str() ) )
  375. {
  376. cm->m_SourceFile = re->match(rit->m_FileIndex);
  377. // At this point we need to make m_SourceFile relative to
  378. // the source root of the project, so cvs links will work
  379. cmSystemTools::ConvertToUnixSlashes(cm->m_SourceFile);
  380. if(cm->m_SourceFile.find("/.../") != cm->m_SourceFile.npos)
  381. {
  382. cmSystemTools::ReplaceString(cm->m_SourceFile, "/.../", "");
  383. std::string::size_type p = cm->m_SourceFile.find("/");
  384. if(p != cm->m_SourceFile.npos)
  385. {
  386. cm->m_SourceFile = cm->m_SourceFile.substr(p+1, cm->m_SourceFile.size()-p);
  387. }
  388. }
  389. else
  390. {
  391. // make sure it is a full path with the correct case
  392. cm->m_SourceFile = cmSystemTools::CollapseFullPath(cm->m_SourceFile.c_str());
  393. cmSystemTools::ReplaceString(cm->m_SourceFile, srcdir.c_str(), "");
  394. }
  395. cm->m_LineNumber = atoi(re->match(rit->m_LineIndex).c_str());
  396. break;
  397. }
  398. }
  399. if ( cm->m_SourceFile.size() > 0 )
  400. {
  401. os << "\t\t<SourceFile>" << cm->m_SourceFile << "</SourceFile>"
  402. << std::endl;
  403. }
  404. if ( cm->m_SourceFileTail.size() > 0 )
  405. {
  406. os << "\t\t<SourceFileTail>" << cm->m_SourceFileTail
  407. << "</SourceFileTail>" << std::endl;
  408. }
  409. if ( cm->m_LineNumber >= 0 )
  410. {
  411. os << "\t\t<SourceLineNumber>" << cm->m_LineNumber
  412. << "</SourceLineNumber>" << std::endl;
  413. }
  414. os << "\t\t<PreContext>" << m_CTest->MakeXMLSafe(cm->m_PreContext)
  415. << "</PreContext>\n"
  416. << "\t\t<PostContext>" << m_CTest->MakeXMLSafe(cm->m_PostContext);
  417. // is this the last warning or error, if so notify
  418. if (cm->m_Error && !numErrorsAllowed ||
  419. !cm->m_Error && !numWarningsAllowed)
  420. {
  421. os << "\nThe maximum number of reported warnings or errors has been reached!!!\n";
  422. }
  423. os << "</PostContext>\n"
  424. << "\t\t<RepeatCount>0</RepeatCount>\n"
  425. << "</" << (cm->m_Error ? "Error" : "Warning") << ">\n\n"
  426. << std::endl;
  427. }
  428. }
  429. os << "\t<Log Encoding=\"base64\" Compression=\"/bin/gzip\">\n\t</Log>\n"
  430. << "\t<EndDateTime>" << m_EndBuild << "</EndDateTime>\n"
  431. << "<ElapsedMinutes>" << static_cast<int>(elapsed_build_time/6)/10.0
  432. << "</ElapsedMinutes>"
  433. << "</Build>" << std::endl;
  434. m_CTest->EndXML(os);
  435. }
  436. //######################################################################
  437. //######################################################################
  438. //######################################################################
  439. //######################################################################
  440. //----------------------------------------------------------------------
  441. int cmCTestBuildHandler::RunMakeCommand(const char* command,
  442. int* retVal, const char* dir, int timeout, std::ofstream& ofs)
  443. {
  444. // First generate the command and arguments
  445. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  446. if(args.size() < 1)
  447. {
  448. return false;
  449. }
  450. std::vector<const char*> argv;
  451. for(std::vector<cmStdString>::const_iterator a = args.begin();
  452. a != args.end(); ++a)
  453. {
  454. argv.push_back(a->c_str());
  455. }
  456. argv.push_back(0);
  457. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Run command:");
  458. std::vector<const char*>::iterator ait;
  459. for ( ait = argv.begin(); ait != argv.end(); ++ ait )
  460. {
  461. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, " \"" << *ait << "\"");
  462. }
  463. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, std::endl);
  464. // Now create process object
  465. cmsysProcess* cp = cmsysProcess_New();
  466. cmsysProcess_SetCommand(cp, &*argv.begin());
  467. cmsysProcess_SetWorkingDirectory(cp, dir);
  468. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  469. cmsysProcess_SetTimeout(cp, timeout);
  470. cmsysProcess_Execute(cp);
  471. // Initialize tick's
  472. std::string::size_type tick = 0;
  473. const std::string::size_type tick_len = 1024;
  474. char* data;
  475. int length;
  476. cmCTestLog(m_CTest, HANDLER_OUTPUT,
  477. " Each symbol represents " << tick_len << " bytes of output." << std::endl
  478. << " '!' represents an error and '*' a warning." << std::endl
  479. << " " << std::flush);
  480. // Initialize building structures
  481. m_BuildProcessingQueue.clear();
  482. m_OutputLineCounter = 0;
  483. m_ErrorsAndWarnings.clear();
  484. m_TotalErrors = 0;
  485. m_TotalWarnings = 0;
  486. m_BuildOutputLogSize = 0;
  487. m_LastTickChar = '.';
  488. m_WarningQuotaReached = false;
  489. m_ErrorQuotaReached = false;
  490. // For every chunk of data
  491. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  492. {
  493. // Replace '\0' with '\n', since '\0' does not really make sense. This is
  494. // for Visual Studio output
  495. for(int cc =0; cc < length; ++cc)
  496. {
  497. if(data[cc] == 0)
  498. {
  499. data[cc] = '\n';
  500. }
  501. }
  502. // Process the chunk of data
  503. this->ProcessBuffer(data, length, tick, tick_len, ofs);
  504. }
  505. this->ProcessBuffer(0, 0, tick, tick_len, ofs);
  506. cmCTestLog(m_CTest, OUTPUT, " Size of output: "
  507. << int(m_BuildOutputLogSize / 1024.0) << "K" << std::endl);
  508. // Properly handle output of the build command
  509. cmsysProcess_WaitForExit(cp, 0);
  510. int result = cmsysProcess_GetState(cp);
  511. if(result == cmsysProcess_State_Exited)
  512. {
  513. *retVal = cmsysProcess_GetExitValue(cp);
  514. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, "Command exited with the value: " << *retVal << std::endl);
  515. }
  516. else if(result == cmsysProcess_State_Exception)
  517. {
  518. *retVal = cmsysProcess_GetExitException(cp);
  519. cmCTestLog(m_CTest, WARNING, "There was an exception: " << *retVal << std::endl);
  520. }
  521. else if(result == cmsysProcess_State_Expired)
  522. {
  523. cmCTestLog(m_CTest, WARNING, "There was a timeout" << std::endl);
  524. }
  525. else if(result == cmsysProcess_State_Error)
  526. {
  527. // If there was an error running command, report that on the dashboard.
  528. cmCTestBuildErrorWarning errorwarning;
  529. errorwarning.m_LogLine = 1;
  530. errorwarning.m_Text = "*** ERROR executing: ";
  531. errorwarning.m_Text += cmsysProcess_GetErrorString(cp);
  532. errorwarning.m_PreContext = "";
  533. errorwarning.m_PostContext = "";
  534. errorwarning.m_Error = true;
  535. m_ErrorsAndWarnings.push_back(errorwarning);
  536. m_TotalErrors ++;
  537. cmCTestLog(this, ERROR_MESSAGE, "There was an error: " << cmsysProcess_GetErrorString(cp) << std::endl);
  538. }
  539. cmsysProcess_Delete(cp);
  540. return result;
  541. }
  542. //######################################################################
  543. //######################################################################
  544. //######################################################################
  545. //######################################################################
  546. //----------------------------------------------------------------------
  547. void cmCTestBuildHandler::ProcessBuffer(const char* data, int length, size_t& tick, size_t tick_len,
  548. std::ofstream& ofs)
  549. {
  550. #undef cerr
  551. const std::string::size_type tick_line_len = 50;
  552. const char* ptr;
  553. for ( ptr = data; ptr < data+length; ptr ++ )
  554. {
  555. m_BuildProcessingQueue.push_back(*ptr);
  556. }
  557. m_BuildOutputLogSize += length;
  558. // until there are any lines left in the buffer
  559. while ( 1 )
  560. {
  561. // Find the end of line
  562. t_BuildProcessingQueueType::iterator it;
  563. for ( it = m_BuildProcessingQueue.begin();
  564. it != m_BuildProcessingQueue.end();
  565. ++ it )
  566. {
  567. if ( *it == '\n' )
  568. {
  569. break;
  570. }
  571. }
  572. // Once certain number of errors or warnings reached, ignore future errors or warnings.
  573. if ( m_TotalWarnings >= m_MaxWarnings )
  574. {
  575. m_WarningQuotaReached = true;
  576. }
  577. if ( m_TotalErrors >= m_MaxErrors )
  578. {
  579. m_ErrorQuotaReached = true;
  580. }
  581. // If the end of line was found
  582. if ( it != m_BuildProcessingQueue.end() )
  583. {
  584. // Create a contiguous array for the line
  585. m_CurrentProcessingLine.clear();
  586. t_BuildProcessingQueueType::iterator cit;
  587. for ( cit = m_BuildProcessingQueue.begin(); cit != it; ++cit )
  588. {
  589. m_CurrentProcessingLine.push_back(*cit);
  590. }
  591. m_CurrentProcessingLine.push_back(0);
  592. const char* line = &*m_CurrentProcessingLine.begin();
  593. // Process the line
  594. int lineType = this->ProcessSingleLine(line);
  595. // Erase the line from the queue
  596. m_BuildProcessingQueue.erase(m_BuildProcessingQueue.begin(), it+1);
  597. // Depending on the line type, produce error or warning, or nothing
  598. cmCTestBuildErrorWarning errorwarning;
  599. bool found = false;
  600. switch ( lineType )
  601. {
  602. case b_WARNING_LINE:
  603. m_LastTickChar = '*';
  604. errorwarning.m_Error = false;
  605. found = true;
  606. m_TotalWarnings ++;
  607. break;
  608. case b_ERROR_LINE:
  609. m_LastTickChar = '!';
  610. errorwarning.m_Error = true;
  611. found = true;
  612. m_TotalErrors ++;
  613. break;
  614. }
  615. if ( found )
  616. {
  617. // This is an error or warning, so generate report
  618. errorwarning.m_LogLine = static_cast<int>(m_OutputLineCounter+1);
  619. errorwarning.m_Text = line;
  620. errorwarning.m_PreContext = "";
  621. errorwarning.m_PostContext = "";
  622. // Copy pre-context to report
  623. std::deque<cmStdString>::iterator pcit;
  624. for ( pcit = m_PreContext.begin(); pcit != m_PreContext.end(); ++pcit )
  625. {
  626. errorwarning.m_PreContext += *pcit + "\n";
  627. }
  628. m_PreContext.clear();
  629. // Store report
  630. m_ErrorsAndWarnings.push_back(errorwarning);
  631. m_LastErrorOrWarning = m_ErrorsAndWarnings.end()-1;
  632. m_PostContextCount = 0;
  633. }
  634. else
  635. {
  636. // This is not an error or warning.
  637. // So, figure out if this is a post-context line
  638. if ( m_LastErrorOrWarning != m_ErrorsAndWarnings.end() && m_PostContextCount < m_MaxPostContext )
  639. {
  640. m_PostContextCount ++;
  641. m_LastErrorOrWarning->m_PostContext += line;
  642. if ( m_PostContextCount < m_MaxPostContext )
  643. {
  644. m_LastErrorOrWarning->m_PostContext += "\n";
  645. }
  646. }
  647. else
  648. {
  649. // Otherwise store pre-context for the next error
  650. m_PreContext.push_back(line);
  651. if ( m_PreContext.size() > m_MaxPreContext )
  652. {
  653. m_PreContext.erase(m_PreContext.begin(), m_PreContext.end()-m_MaxPreContext);
  654. }
  655. }
  656. }
  657. m_OutputLineCounter ++;
  658. }
  659. else
  660. {
  661. break;
  662. }
  663. }
  664. // Now that the buffer is processed, display missing ticks
  665. int tickDisplayed = false;
  666. while ( m_BuildOutputLogSize > (tick * tick_len) )
  667. {
  668. tick ++;
  669. cmCTestLog(m_CTest, HANDLER_OUTPUT, m_LastTickChar);
  670. tickDisplayed = true;
  671. if ( tick % tick_line_len == 0 && tick > 0 )
  672. {
  673. cmCTestLog(m_CTest, HANDLER_OUTPUT, " Size: "
  674. << int((m_BuildOutputLogSize / 1024.0) + 1) << "K" << std::endl
  675. << " ");
  676. }
  677. }
  678. if ( tickDisplayed )
  679. {
  680. m_LastTickChar = '.';
  681. }
  682. // And if this is verbose output, display the content of the chunk
  683. cmCTestLog(m_CTest, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
  684. // Always store the chunk to the file
  685. ofs << cmCTestLogWrite(data, length);
  686. }
  687. //----------------------------------------------------------------------
  688. int cmCTestBuildHandler::ProcessSingleLine(const char* data)
  689. {
  690. cmCTestLog(m_CTest, DEBUG, "Line: [" << data << "]" << std::endl);
  691. std::vector<cmsys::RegularExpression>::iterator it;
  692. int warningLine = 0;
  693. int errorLine = 0;
  694. // Check for regular expressions
  695. if ( !m_ErrorQuotaReached )
  696. {
  697. // Errors
  698. for ( it = m_ErrorMatchRegex.begin(); it != m_ErrorMatchRegex.end(); ++ it )
  699. {
  700. if ( it->find(data) )
  701. {
  702. errorLine = 1;
  703. cmCTestLog(m_CTest, DEBUG, " Error Line: " << data << std::endl);
  704. break;
  705. }
  706. }
  707. // Error exceptions
  708. for ( it = m_ErrorExceptionRegex.begin(); it != m_ErrorExceptionRegex.end(); ++ it )
  709. {
  710. if ( it->find(data) )
  711. {
  712. errorLine = 0;
  713. cmCTestLog(m_CTest, DEBUG, " Not an error Line: " << data << std::endl);
  714. break;
  715. }
  716. }
  717. }
  718. if ( !m_WarningQuotaReached )
  719. {
  720. // Warnings
  721. for ( it = m_WarningMatchRegex.begin(); it != m_WarningMatchRegex.end(); ++ it )
  722. {
  723. if ( it->find(data) )
  724. {
  725. warningLine = 1;
  726. cmCTestLog(m_CTest, DEBUG, " Warning Line: " << data << std::endl);
  727. break;
  728. }
  729. }
  730. // Warning exceptions
  731. for ( it = m_WarningExceptionRegex.begin(); it != m_WarningExceptionRegex.end(); ++ it )
  732. {
  733. if ( it->find(data) )
  734. {
  735. warningLine = 0;
  736. cmCTestLog(m_CTest, DEBUG, " Not a warning Line: " << data << std::endl);
  737. break;
  738. }
  739. }
  740. }
  741. if ( errorLine )
  742. {
  743. return b_ERROR_LINE;
  744. }
  745. if ( warningLine )
  746. {
  747. return b_WARNING_LINE;
  748. }
  749. return b_REGULAR_LINE;
  750. }