cmCTestBuildHandler.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. 0
  72. };
  73. static const char* cmCTestErrorExceptions[] = {
  74. "instantiated from ",
  75. "candidates are:",
  76. ": warning",
  77. "makefile:",
  78. "Makefile:",
  79. ":[ \\t]+Where:",
  80. "([^ :]+):([0-9]+): Warning",
  81. 0
  82. };
  83. static const char* cmCTestWarningMatches[] = {
  84. "([^ :]+):([0-9]+): warning:",
  85. "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
  86. "^ld([^:])*:([ \\t])*WARNING([^:])*:",
  87. "([^:]+): warning ([0-9]+):",
  88. "^\"[^\"]+\", line [0-9]+: [Ww]arning",
  89. "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
  90. "^Warning ([0-9]+):",
  91. "^Warning ",
  92. "WARNING: ",
  93. "([^ :]+) : warning",
  94. "([^:]+): warning",
  95. "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\(W\\)",
  96. "^cxx: Warning:",
  97. ".*file: .* has no symbols",
  98. "([^ :]+):([0-9]+): Warning",
  99. "\\([0-9]*\\): remark #[0-9]*",
  100. "\".*\", line [0-9]+: remark\\([0-9]*\\):",
  101. "cc-[0-9]* CC: REMARK File = .*, Line = [0-9]*",
  102. 0
  103. };
  104. static const char* cmCTestWarningExceptions[] = {
  105. "/usr/openwin/include/X11/Xlib\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  106. "/usr/openwin/include/X11/Xutil\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  107. "/usr/openwin/include/X11/XResource\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  108. "WARNING 84 :",
  109. "WARNING 47 :",
  110. "makefile:",
  111. "Makefile:",
  112. "warning: Clock skew detected. Your build may be incomplete.",
  113. "/usr/openwin/include/GL/[^:]+:",
  114. "bind_at_load",
  115. "XrmQGetResource",
  116. "IceFlush",
  117. "warning LNK4089: all references to [^ \\t]+ discarded by .OPT:REF",
  118. "ld32: WARNING 85: definition of dataKey in",
  119. "cc: warning 422: Unknown option \"\\+b",
  120. "_with_warning_C",
  121. 0
  122. };
  123. struct cmCTestBuildCompileErrorWarningRex
  124. {
  125. const char* m_RegularExpressionString;
  126. int m_FileIndex;
  127. int m_LineIndex;
  128. };
  129. static cmCTestBuildCompileErrorWarningRex
  130. cmCTestWarningErrorFileLine[] = {
  131. { "^Warning W[0-9]+ ([a-zA-Z.\\:/0-9_+ ~-]+) ([0-9]+):", 1, 2 },
  132. { "^([a-zA-Z./0-9_+ ~-]+):([0-9]+):", 1, 2 },
  133. { "^([a-zA-Z.\\:/0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
  134. { "^([a-zA-Z./0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
  135. { "\"([a-zA-Z./0-9_+ ~-]+)\", line ([0-9]+)", 1, 2 },
  136. { "File = ([a-zA-Z./0-9_+ ~-]+), Line = ([0-9]+)", 1, 2 },
  137. { 0, 0, 0 }
  138. };
  139. //----------------------------------------------------------------------
  140. cmCTestBuildHandler::cmCTestBuildHandler()
  141. {
  142. int cc;
  143. for ( cc = 0; cmCTestWarningErrorFileLine[cc].m_RegularExpressionString; ++ cc )
  144. {
  145. cmCTestBuildHandler::cmCTestCompileErrorWarningRex r;
  146. if ( r.m_RegularExpression.compile(
  147. cmCTestWarningErrorFileLine[cc].m_RegularExpressionString) )
  148. {
  149. r.m_FileIndex = cmCTestWarningErrorFileLine[cc].m_FileIndex;
  150. r.m_LineIndex = cmCTestWarningErrorFileLine[cc].m_LineIndex;
  151. m_ErrorWarningFileLineRegex.push_back(r);
  152. }
  153. else
  154. {
  155. std::cout << "Problem Compiling regular expression: "
  156. << cmCTestWarningErrorFileLine[cc].m_RegularExpressionString << std::endl;
  157. }
  158. }
  159. }
  160. //----------------------------------------------------------------------
  161. void cmCTestBuildHandler::PopulateCustomVectors(cmMakefile *mf)
  162. {
  163. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_MATCH",
  164. m_CustomErrorMatches);
  165. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_EXCEPTION",
  166. m_CustomErrorExceptions);
  167. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_MATCH",
  168. m_CustomWarningMatches);
  169. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_EXCEPTION",
  170. m_CustomWarningExceptions);
  171. }
  172. //----------------------------------------------------------------------
  173. //clearly it would be nice if this were broken up into a few smaller
  174. //functions and commented...
  175. int cmCTestBuildHandler::ProcessHandler()
  176. {
  177. std::cout << "Build project" << std::endl;
  178. std::string makeCommand = m_CTest->GetDartConfiguration("MakeCommand");
  179. if ( makeCommand.size() == 0 )
  180. {
  181. std::cerr << "Cannot find MakeCommand key in the DartConfiguration.tcl" << std::endl;
  182. return -1;
  183. }
  184. std::string buildDirectory = m_CTest->GetDartConfiguration("BuildDirectory");
  185. if ( buildDirectory.size() == 0 )
  186. {
  187. std::cerr << "Cannot find BuildDirectory key in the DartConfiguration.tcl" << std::endl;
  188. return -1;
  189. }
  190. cmGeneratedFileStream ofs;
  191. double elapsed_time_start = cmSystemTools::GetTime();
  192. if ( !m_CTest->OpenOutputFile("Temporary", "LastBuild.log", ofs) )
  193. {
  194. std::cerr << "Cannot create LastBuild.log file" << std::endl;
  195. }
  196. m_StartBuild = m_CTest->CurrentTime();
  197. std::string output;
  198. int retVal = 0;
  199. int res = cmsysProcess_State_Exited;
  200. if ( !m_CTest->GetShowOnly() )
  201. {
  202. res = m_CTest->RunMakeCommand(makeCommand.c_str(), &output,
  203. &retVal, buildDirectory.c_str(),
  204. m_Verbose, 0, ofs);
  205. }
  206. else
  207. {
  208. std::cout << "Build with command: " << makeCommand << std::endl;
  209. }
  210. m_EndBuild = m_CTest->CurrentTime();
  211. double elapsed_build_time = cmSystemTools::GetTime() - elapsed_time_start;
  212. if (res != cmsysProcess_State_Exited || retVal )
  213. {
  214. std::cerr << "Error(s) when building project" << std::endl;
  215. }
  216. std::vector<cmStdString>::size_type cc;
  217. if ( m_CTest->GetDartConfiguration("SourceDirectory").size() > 20 ||
  218. m_CTest->GetDartConfiguration("BuildDirectory").size() > 20 )
  219. {
  220. std::string srcdir = m_CTest->GetDartConfiguration("SourceDirectory") + "/";
  221. std::string bindir = m_CTest->GetDartConfiguration("BuildDirectory") + "/";
  222. std::string srcdirrep;
  223. std::string bindirrep;
  224. for ( cc = srcdir.size()-2; cc > 0; cc -- )
  225. {
  226. if ( srcdir[cc] == '/' )
  227. {
  228. srcdirrep = srcdir.c_str() + cc;
  229. srcdirrep = "/..." + srcdirrep;
  230. srcdir = srcdir.substr(0, cc+1);
  231. break;
  232. }
  233. }
  234. for ( cc = bindir.size()-2; cc > 0; cc -- )
  235. {
  236. if ( bindir[cc] == '/' )
  237. {
  238. bindirrep = bindir.c_str() + cc;
  239. bindirrep = "/..." + bindirrep;
  240. bindir = bindir.substr(0, cc+1);
  241. break;
  242. }
  243. }
  244. cmSystemTools::ReplaceString(output, srcdir.c_str(), "/.../"); //srcdirrep.c_str());
  245. cmSystemTools::ReplaceString(output, bindir.c_str(), "/.../"); //bindirrep.c_str());
  246. }
  247. // Parsing of output for errors and warnings.
  248. std::vector<cmStdString> lines;
  249. cmSystemTools::Split(output.c_str(), lines);
  250. // Lines are marked:
  251. // 0 - nothing
  252. // 1 - error
  253. // > 1 - warning
  254. std::vector<int> markedLines(lines.size(), 0);
  255. // Errors
  256. for ( cc = 0; cmCTestErrorMatches[cc]; cc ++ )
  257. {
  258. m_CustomErrorMatches.push_back(cmCTestErrorMatches[cc]);
  259. }
  260. for ( cc = 0; cmCTestErrorExceptions[cc]; cc ++ )
  261. {
  262. m_CustomErrorExceptions.push_back(cmCTestErrorExceptions[cc]);
  263. }
  264. for ( cc = 0; cmCTestWarningMatches[cc]; cc ++ )
  265. {
  266. m_CustomWarningMatches.push_back(cmCTestWarningMatches[cc]);
  267. }
  268. for ( cc = 0; cmCTestWarningExceptions[cc]; cc ++ )
  269. {
  270. m_CustomWarningExceptions.push_back(cmCTestWarningExceptions[cc]);
  271. }
  272. for ( cc = 0; cc < m_CustomErrorMatches.size(); cc ++ )
  273. {
  274. cmsys::RegularExpression re(m_CustomErrorMatches[cc].c_str());
  275. std::vector<cmStdString>::size_type kk;
  276. //cout << "error Line: " << m_CustomErrorMatches[cc] << endl;
  277. for ( kk = 0; kk < lines.size(); kk ++ )
  278. {
  279. //cout << " Line: " << lines[kk] << endl;
  280. if ( re.find(lines[kk]) )
  281. {
  282. //cout << "******************************" << endl;
  283. markedLines[kk] = 1;
  284. }
  285. }
  286. }
  287. // Warnings
  288. for ( cc = 0; cc < m_CustomWarningMatches.size(); cc ++ )
  289. {
  290. cmsys::RegularExpression re(m_CustomWarningMatches[cc].c_str());
  291. std::vector<cmStdString>::size_type kk;
  292. //cout << "warning Line: " << m_CustomWarningMatches[cc] << endl;
  293. for ( kk = 0; kk < lines.size(); kk ++ )
  294. {
  295. //cout << " Line: " << lines[kk] << endl;
  296. if ( re.find(lines[kk]) )
  297. {
  298. //cout << "******************************" << endl;
  299. markedLines[kk] += 2;
  300. }
  301. }
  302. }
  303. // the two follwing blocks of code appear wrong to me - Ken
  304. // I belive that the middle if tests should be %2 type tests
  305. // need to close the loop with Andy
  306. // Errors exceptions
  307. for ( cc = 0; cc < m_CustomErrorExceptions.size(); cc ++ )
  308. {
  309. cmsys::RegularExpression re(m_CustomErrorExceptions[cc].c_str());
  310. std::vector<int>::size_type kk;
  311. for ( kk =0; kk < markedLines.size(); kk ++ )
  312. {
  313. if ( markedLines[kk] == 1 )
  314. {
  315. if ( re.find(lines[kk]) )
  316. {
  317. markedLines[kk] = 0;
  318. }
  319. }
  320. }
  321. }
  322. // Warning exceptions
  323. for ( cc = 0; cc < m_CustomWarningExceptions.size(); cc ++ )
  324. {
  325. cmsys::RegularExpression re(m_CustomWarningExceptions[cc].c_str());
  326. std::vector<int>::size_type kk;
  327. for ( kk =0; kk < markedLines.size(); kk ++ )
  328. {
  329. if ( markedLines[kk] > 1 )
  330. {
  331. if ( re.find(lines[kk]) )
  332. {
  333. markedLines[kk] = 0;
  334. }
  335. }
  336. }
  337. }
  338. std::vector<cmCTestBuildErrorWarning> errorsWarnings;
  339. int errors = 0;
  340. int warnings = 0;
  341. std::vector<int>::size_type kk;
  342. cmCTestBuildErrorWarning errorwarning;
  343. for ( kk =0; kk < markedLines.size(); kk ++ )
  344. {
  345. errorwarning.m_LineNumber = -1;
  346. bool found = false;
  347. if ( markedLines[kk] == 1 )
  348. {
  349. //std::cout << "Error: " << lines[kk] << std::endl;
  350. errorwarning.m_Error = true;
  351. found = true;
  352. }
  353. else if ( markedLines[kk] > 1 )
  354. {
  355. //std::cout << "Warning: " << lines[kk] << std::endl;
  356. errorwarning.m_Error = false;
  357. found = true;
  358. }
  359. if ( found )
  360. {
  361. errorwarning.m_LogLine = static_cast<int>(kk+1);
  362. errorwarning.m_Text = lines[kk];
  363. errorwarning.m_PreContext = "";
  364. errorwarning.m_PostContext = "";
  365. std::vector<int>::size_type jj;
  366. std::vector<int>::size_type ll = 0;
  367. if ( kk > 6 )
  368. {
  369. ll = kk - 6;
  370. }
  371. for ( jj = kk-1;
  372. jj > 0 && jj > ll && markedLines[jj] != markedLines[kk];
  373. jj -- );
  374. while ( markedLines[jj] == markedLines[kk] && jj < kk )
  375. {
  376. jj ++;
  377. }
  378. for (; jj < kk; jj ++ )
  379. {
  380. errorwarning.m_PreContext += lines[jj] + "\n";
  381. }
  382. for ( jj = kk+1;
  383. jj < lines.size() && jj < kk + 7 && markedLines[jj] != markedLines[kk];
  384. jj ++ )
  385. {
  386. errorwarning.m_PostContext += lines[jj] + "\n";
  387. }
  388. errorsWarnings.push_back(errorwarning);
  389. if ( errorwarning.m_Error )
  390. {
  391. errors ++;
  392. }
  393. else
  394. {
  395. warnings ++;
  396. }
  397. }
  398. }
  399. std::cout << " " << errors << " Compiler errors" << std::endl;
  400. std::cout << " " << warnings << " Compiler warnings" << std::endl;
  401. cmGeneratedFileStream xofs;
  402. if( !m_CTest->OpenOutputFile(m_CTest->GetCurrentTag(), "Build.xml", xofs, true) )
  403. {
  404. std::cerr << "Cannot create build XML file" << std::endl;
  405. return -1;
  406. }
  407. this->GenerateDartBuildOutput(xofs, errorsWarnings, elapsed_build_time);
  408. return 0;
  409. }
  410. void cmCTestBuildHandler::GenerateDartBuildOutput(
  411. std::ostream& os,
  412. std::vector<cmCTestBuildErrorWarning> ew,
  413. double elapsed_build_time)
  414. {
  415. m_CTest->StartXML(os);
  416. os << "<Build>\n"
  417. << "\t<StartDateTime>" << m_StartBuild << "</StartDateTime>\n"
  418. << "<BuildCommand>"
  419. << m_CTest->MakeXMLSafe(m_CTest->GetDartConfiguration("MakeCommand"))
  420. << "</BuildCommand>" << std::endl;
  421. std::vector<cmCTestBuildErrorWarning>::iterator it;
  422. // only report the first 50 warnings and first 50 errors
  423. unsigned short numErrorsAllowed = 50;
  424. unsigned short numWarningsAllowed = 50;
  425. for ( it = ew.begin();
  426. it != ew.end() && (numErrorsAllowed || numWarningsAllowed); it++ )
  427. {
  428. cmCTestBuildErrorWarning *cm = &(*it);
  429. if (cm->m_Error && numErrorsAllowed ||
  430. !cm->m_Error && numWarningsAllowed)
  431. {
  432. if (cm->m_Error)
  433. {
  434. numErrorsAllowed--;
  435. }
  436. else
  437. {
  438. numWarningsAllowed--;
  439. }
  440. os << "\t<" << (cm->m_Error ? "Error" : "Warning") << ">\n"
  441. << "\t\t<BuildLogLine>" << cm->m_LogLine << "</BuildLogLine>\n"
  442. << "\t\t<Text>" << m_CTest->MakeXMLSafe(cm->m_Text)
  443. << "\n</Text>" << std::endl;
  444. std::vector<cmCTestCompileErrorWarningRex>::iterator rit;
  445. for ( rit = m_ErrorWarningFileLineRegex.begin();
  446. rit != m_ErrorWarningFileLineRegex.end(); ++ rit )
  447. {
  448. cmsys::RegularExpression* re = &rit->m_RegularExpression;
  449. if ( re->find(cm->m_Text.c_str() ) )
  450. {
  451. cm->m_SourceFile = re->match(rit->m_FileIndex);
  452. cm->m_LineNumber = atoi(re->match(rit->m_LineIndex).c_str());
  453. break;
  454. }
  455. }
  456. if ( cm->m_SourceFile.size() > 0 )
  457. {
  458. os << "\t\t<SourceFile>" << cm->m_SourceFile << "</SourceFile>"
  459. << std::endl;
  460. }
  461. if ( cm->m_SourceFileTail.size() > 0 )
  462. {
  463. os << "\t\t<SourceFileTail>" << cm->m_SourceFileTail
  464. << "</SourceFileTail>" << std::endl;
  465. }
  466. if ( cm->m_LineNumber >= 0 )
  467. {
  468. os << "\t\t<SourceLineNumber>" << cm->m_LineNumber
  469. << "</SourceLineNumber>" << std::endl;
  470. }
  471. os << "\t\t<PreContext>" << m_CTest->MakeXMLSafe(cm->m_PreContext)
  472. << "</PreContext>\n"
  473. << "\t\t<PostContext>" << m_CTest->MakeXMLSafe(cm->m_PostContext);
  474. // is this the last warning or error, if so notify
  475. if (cm->m_Error && !numErrorsAllowed ||
  476. !cm->m_Error && !numWarningsAllowed)
  477. {
  478. os << "\nThe maximum number of reported warnings or errors has been reached!!!\n";
  479. }
  480. os << "</PostContext>\n"
  481. << "\t\t<RepeatCount>0</RepeatCount>\n"
  482. << "</" << (cm->m_Error ? "Error" : "Warning") << ">\n\n"
  483. << std::endl;
  484. }
  485. }
  486. os << "\t<Log Encoding=\"base64\" Compression=\"/bin/gzip\">\n\t</Log>\n"
  487. << "\t<EndDateTime>" << m_EndBuild << "</EndDateTime>\n"
  488. << "<ElapsedMinutes>" << static_cast<int>(elapsed_build_time/6)/10.0
  489. << "</ElapsedMinutes>"
  490. << "</Build>" << std::endl;
  491. m_CTest->EndXML(os);
  492. }