cmCTestBuildHandler.cxx 16 KB

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