cmCTestBuildHandler.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 <cmsys/RegularExpression.hxx>
  20. #include <cmsys/Process.h>
  21. // used for sleep
  22. #ifdef _WIN32
  23. #include "windows.h"
  24. #endif
  25. #include <stdlib.h>
  26. #include <time.h>
  27. #include <math.h>
  28. #include <float.h>
  29. static const char* cmCTestErrorMatches[] = {
  30. "^[Bb]us [Ee]rror",
  31. "^[Ss]egmentation [Vv]iolation",
  32. "^[Ss]egmentation [Ff]ault",
  33. "([^ :]+):([0-9]+): ([^ \\t])",
  34. "([^:]+): error[ \\t]*[0-9]+[ \\t]*:",
  35. "^Error ([0-9]+):",
  36. "^Fatal",
  37. "^Error: ",
  38. "^Error ",
  39. "[0-9] ERROR: ",
  40. "^\"[^\"]+\", line [0-9]+: [^Ww]",
  41. "^cc[^C]*CC: ERROR File = ([^,]+), Line = ([0-9]+)",
  42. "^ld([^:])*:([ \\t])*ERROR([^:])*:",
  43. "^ild:([ \\t])*\\(undefined symbol\\)",
  44. "([^ :]+) : (error|fatal error|catastrophic error)",
  45. "([^:]+): (Error:|error|undefined reference|multiply defined)",
  46. "([^:]+)\\(([^\\)]+)\\) : (error|fatal error|catastrophic error)",
  47. "^fatal error C[0-9]+:",
  48. ": syntax error ",
  49. "^collect2: ld returned 1 exit status",
  50. "Unsatisfied symbols:",
  51. "^Unresolved:",
  52. "Undefined symbols:",
  53. "^Undefined[ \\t]+first referenced",
  54. "^CMake Error:",
  55. ":[ \\t]cannot find",
  56. ":[ \\t]can't find",
  57. ": \\*\\*\\* No rule to make target \\`.*\\'. Stop",
  58. ": Invalid loader fixup for symbol",
  59. ": internal link edit command failed",
  60. ": Unrecognized option \\`.*\\'",
  61. "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\([^W]\\)",
  62. "ld: 0706-006 Cannot find or open library file: -l ",
  63. "ild: \\(argument error\\) can't find library argument ::",
  64. "^could not be found and will not be loaded.",
  65. "s:616 string too big",
  66. "make: Fatal error: ",
  67. "ld: 0711-993 Error occurred while writing to the output file:",
  68. "ld: fatal: ",
  69. "make: \\*\\*\\*.*Error",
  70. 0
  71. };
  72. static const char* cmCTestErrorExceptions[] = {
  73. "instantiated from ",
  74. "candidates are:",
  75. ": warning",
  76. "makefile:",
  77. "Makefile:",
  78. ":[ \\t]+Where:",
  79. "([^ :]+):([0-9]+): Warning",
  80. 0
  81. };
  82. static const char* cmCTestWarningMatches[] = {
  83. "([^ :]+):([0-9]+): warning:",
  84. "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
  85. "^ld([^:])*:([ \\t])*WARNING([^:])*:",
  86. "([^:]+): warning ([0-9]+):",
  87. "^\"[^\"]+\", line [0-9]+: [Ww]arning",
  88. "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
  89. "^Warning ([0-9]+):",
  90. "^Warning ",
  91. "WARNING: ",
  92. "([^ :]+) : warning",
  93. "([^:]+): warning",
  94. "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\(W\\)",
  95. "^cxx: Warning:",
  96. ".*file: .* has no symbols",
  97. "([^ :]+):([0-9]+): Warning",
  98. "\\([0-9]*\\): remark #[0-9]*",
  99. "\".*\", line [0-9]+: remark\\([0-9]*\\):",
  100. "cc-[0-9]* CC: REMARK File = .*, Line = [0-9]*",
  101. 0
  102. };
  103. static const char* cmCTestWarningExceptions[] = {
  104. "/usr/openwin/include/X11/Xlib\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  105. "/usr/openwin/include/X11/Xutil\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  106. "/usr/openwin/include/X11/XResource\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  107. "WARNING 84 :",
  108. "WARNING 47 :",
  109. "makefile:",
  110. "Makefile:",
  111. "warning: Clock skew detected. Your build may be incomplete.",
  112. "/usr/openwin/include/GL/[^:]+:",
  113. "bind_at_load",
  114. "XrmQGetResource",
  115. "IceFlush",
  116. "warning LNK4089: all references to [^ \\t]+ discarded by .OPT:REF",
  117. "ld32: WARNING 85: definition of dataKey in",
  118. "cc: warning 422: Unknown option \"\\+b",
  119. "_with_warning_C",
  120. 0
  121. };
  122. struct cmCTestBuildCompileErrorWarningRex
  123. {
  124. const char* m_RegularExpressionString;
  125. int m_FileIndex;
  126. int m_LineIndex;
  127. };
  128. static cmCTestBuildCompileErrorWarningRex
  129. cmCTestWarningErrorFileLine[] = {
  130. { "^Warning W[0-9]+ ([a-zA-Z.\\:/0-9_+ ~-]+) ([0-9]+):", 1, 2 },
  131. { "^([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_+ ~-]+)\", line ([0-9]+)", 1, 2 },
  135. { "File = ([a-zA-Z./0-9_+ ~-]+), Line = ([0-9]+)", 1, 2 },
  136. { 0, 0, 0 }
  137. };
  138. //----------------------------------------------------------------------
  139. cmCTestBuildHandler::cmCTestBuildHandler()
  140. {
  141. m_Verbose = false;
  142. m_CTest = 0;
  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::BuildDirectory(cmCTest *ctest_inst)
  177. {
  178. m_CTest = ctest_inst;
  179. std::cout << "Build project" << std::endl;
  180. std::string makeCommand = m_CTest->GetDartConfiguration("MakeCommand");
  181. if ( makeCommand.size() == 0 )
  182. {
  183. std::cerr << "Cannot find MakeCommand key in the DartConfiguration.tcl" << std::endl;
  184. return 1;
  185. }
  186. std::string buildDirectory = m_CTest->GetDartConfiguration("BuildDirectory");
  187. if ( buildDirectory.size() == 0 )
  188. {
  189. std::cerr << "Cannot find BuildDirectory key in the DartConfiguration.tcl" << std::endl;
  190. return 1;
  191. }
  192. std::ofstream ofs;
  193. double elapsed_time_start = cmSystemTools::GetTime();
  194. if ( !m_CTest->OpenOutputFile("Temporary", "LastBuild.log", ofs) )
  195. {
  196. std::cerr << "Cannot create LastBuild.log file" << std::endl;
  197. }
  198. m_StartBuild = m_CTest->CurrentTime();
  199. std::string output;
  200. int retVal = 0;
  201. int res = cmsysProcess_State_Exited;
  202. if ( !m_CTest->GetShowOnly() )
  203. {
  204. res = m_CTest->RunMakeCommand(makeCommand.c_str(), &output,
  205. &retVal, buildDirectory.c_str(),
  206. m_Verbose, 0, ofs);
  207. }
  208. else
  209. {
  210. std::cout << "Build with command: " << makeCommand << std::endl;
  211. }
  212. m_EndBuild = m_CTest->CurrentTime();
  213. double elapsed_build_time = cmSystemTools::GetTime() - elapsed_time_start;
  214. if (res != cmsysProcess_State_Exited || retVal )
  215. {
  216. std::cerr << "Error(s) when building project" << std::endl;
  217. }
  218. if ( ofs )
  219. {
  220. ofs.close();
  221. }
  222. std::vector<cmStdString>::size_type cc;
  223. if ( m_CTest->GetDartConfiguration("SourceDirectory").size() > 20 ||
  224. m_CTest->GetDartConfiguration("BuildDirectory").size() > 20 )
  225. {
  226. std::string srcdir = m_CTest->GetDartConfiguration("SourceDirectory") + "/";
  227. std::string bindir = m_CTest->GetDartConfiguration("BuildDirectory") + "/";
  228. std::string srcdirrep;
  229. std::string bindirrep;
  230. for ( cc = srcdir.size()-2; cc > 0; cc -- )
  231. {
  232. if ( srcdir[cc] == '/' )
  233. {
  234. srcdirrep = srcdir.c_str() + cc;
  235. srcdirrep = "/..." + srcdirrep;
  236. srcdir = srcdir.substr(0, cc+1);
  237. break;
  238. }
  239. }
  240. for ( cc = bindir.size()-2; cc > 0; cc -- )
  241. {
  242. if ( bindir[cc] == '/' )
  243. {
  244. bindirrep = bindir.c_str() + cc;
  245. bindirrep = "/..." + bindirrep;
  246. bindir = bindir.substr(0, cc+1);
  247. break;
  248. }
  249. }
  250. cmSystemTools::ReplaceString(output, srcdir.c_str(), "/.../"); //srcdirrep.c_str());
  251. cmSystemTools::ReplaceString(output, bindir.c_str(), "/.../"); //bindirrep.c_str());
  252. }
  253. // Parsing of output for errors and warnings.
  254. std::vector<cmStdString> lines;
  255. cmSystemTools::Split(output.c_str(), lines);
  256. // Lines are marked:
  257. // 0 - nothing
  258. // 1 - error
  259. // > 1 - warning
  260. std::vector<int> markedLines(lines.size(), 0);
  261. // Errors
  262. for ( cc = 0; cmCTestErrorMatches[cc]; cc ++ )
  263. {
  264. m_CustomErrorMatches.push_back(cmCTestErrorMatches[cc]);
  265. }
  266. for ( cc = 0; cmCTestErrorExceptions[cc]; cc ++ )
  267. {
  268. m_CustomErrorExceptions.push_back(cmCTestErrorExceptions[cc]);
  269. }
  270. for ( cc = 0; cmCTestWarningMatches[cc]; cc ++ )
  271. {
  272. m_CustomWarningMatches.push_back(cmCTestWarningMatches[cc]);
  273. }
  274. for ( cc = 0; cmCTestWarningExceptions[cc]; cc ++ )
  275. {
  276. m_CustomWarningExceptions.push_back(cmCTestWarningExceptions[cc]);
  277. }
  278. for ( cc = 0; cc < m_CustomErrorMatches.size(); cc ++ )
  279. {
  280. cmsys::RegularExpression re(m_CustomErrorMatches[cc].c_str());
  281. std::vector<cmStdString>::size_type kk;
  282. //cout << "error Line: " << m_CustomErrorMatches[cc] << endl;
  283. for ( kk = 0; kk < lines.size(); kk ++ )
  284. {
  285. //cout << " Line: " << lines[kk] << endl;
  286. if ( re.find(lines[kk]) )
  287. {
  288. //cout << "******************************" << endl;
  289. markedLines[kk] = 1;
  290. }
  291. }
  292. }
  293. // Warnings
  294. for ( cc = 0; cc < m_CustomWarningMatches.size(); cc ++ )
  295. {
  296. cmsys::RegularExpression re(m_CustomWarningMatches[cc].c_str());
  297. std::vector<cmStdString>::size_type kk;
  298. //cout << "warning Line: " << m_CustomWarningMatches[cc] << endl;
  299. for ( kk = 0; kk < lines.size(); kk ++ )
  300. {
  301. //cout << " Line: " << lines[kk] << endl;
  302. if ( re.find(lines[kk]) )
  303. {
  304. //cout << "******************************" << endl;
  305. markedLines[kk] += 2;
  306. }
  307. }
  308. }
  309. // the two follwing blocks of code appear wrong to me - Ken
  310. // I belive that the middle if tests should be %2 type tests
  311. // need to close the loop with Andy
  312. // Errors exceptions
  313. for ( cc = 0; cc < m_CustomErrorExceptions.size(); cc ++ )
  314. {
  315. cmsys::RegularExpression re(m_CustomErrorExceptions[cc].c_str());
  316. std::vector<int>::size_type kk;
  317. for ( kk =0; kk < markedLines.size(); kk ++ )
  318. {
  319. if ( markedLines[kk] == 1 )
  320. {
  321. if ( re.find(lines[kk]) )
  322. {
  323. markedLines[kk] = 0;
  324. }
  325. }
  326. }
  327. }
  328. // Warning exceptions
  329. for ( cc = 0; cc < m_CustomWarningExceptions.size(); cc ++ )
  330. {
  331. cmsys::RegularExpression re(m_CustomWarningExceptions[cc].c_str());
  332. std::vector<int>::size_type kk;
  333. for ( kk =0; kk < markedLines.size(); kk ++ )
  334. {
  335. if ( markedLines[kk] > 1 )
  336. {
  337. if ( re.find(lines[kk]) )
  338. {
  339. markedLines[kk] = 0;
  340. }
  341. }
  342. }
  343. }
  344. std::vector<cmCTestBuildErrorWarning> errorsWarnings;
  345. int errors = 0;
  346. int warnings = 0;
  347. std::vector<int>::size_type kk;
  348. cmCTestBuildErrorWarning errorwarning;
  349. for ( kk =0; kk < markedLines.size(); kk ++ )
  350. {
  351. errorwarning.m_LineNumber = -1;
  352. bool found = false;
  353. if ( markedLines[kk] == 1 )
  354. {
  355. //std::cout << "Error: " << lines[kk] << std::endl;
  356. errorwarning.m_Error = true;
  357. found = true;
  358. }
  359. else if ( markedLines[kk] > 1 )
  360. {
  361. //std::cout << "Warning: " << lines[kk] << std::endl;
  362. errorwarning.m_Error = false;
  363. found = true;
  364. }
  365. if ( found )
  366. {
  367. errorwarning.m_LogLine = static_cast<int>(kk+1);
  368. errorwarning.m_Text = lines[kk];
  369. errorwarning.m_PreContext = "";
  370. errorwarning.m_PostContext = "";
  371. std::vector<int>::size_type jj;
  372. std::vector<int>::size_type ll = 0;
  373. if ( kk > 6 )
  374. {
  375. ll = kk - 6;
  376. }
  377. for ( jj = kk-1;
  378. jj > 0 && jj > ll && markedLines[jj] != markedLines[kk];
  379. jj -- );
  380. while ( markedLines[jj] == markedLines[kk] && jj < kk )
  381. {
  382. jj ++;
  383. }
  384. for (; jj < kk; jj ++ )
  385. {
  386. errorwarning.m_PreContext += lines[jj] + "\n";
  387. }
  388. for ( jj = kk+1;
  389. jj < lines.size() && jj < kk + 7 && markedLines[jj] != markedLines[kk];
  390. jj ++ )
  391. {
  392. errorwarning.m_PostContext += lines[jj] + "\n";
  393. }
  394. errorsWarnings.push_back(errorwarning);
  395. if ( errorwarning.m_Error )
  396. {
  397. errors ++;
  398. }
  399. else
  400. {
  401. warnings ++;
  402. }
  403. }
  404. }
  405. std::cout << " " << errors << " Compiler errors" << std::endl;
  406. std::cout << " " << warnings << " Compiler warnings" << std::endl;
  407. if( !m_CTest->OpenOutputFile(m_CTest->GetCurrentTag(), "Build.xml", ofs) )
  408. {
  409. std::cerr << "Cannot create build XML file" << std::endl;
  410. return 1;
  411. }
  412. this->GenerateDartBuildOutput(ofs, errorsWarnings, elapsed_build_time);
  413. return 0;
  414. }
  415. void cmCTestBuildHandler::GenerateDartBuildOutput(
  416. std::ostream& os,
  417. std::vector<cmCTestBuildErrorWarning> ew,
  418. double elapsed_build_time)
  419. {
  420. m_CTest->StartXML(os);
  421. os << "<Build>\n"
  422. << "\t<StartDateTime>" << m_StartBuild << "</StartDateTime>\n"
  423. << "<BuildCommand>"
  424. << m_CTest->MakeXMLSafe(m_CTest->GetDartConfiguration("MakeCommand"))
  425. << "</BuildCommand>" << std::endl;
  426. std::vector<cmCTestBuildErrorWarning>::iterator it;
  427. // only report the first 50 warnings and first 50 errors
  428. unsigned short numErrorsAllowed = 50;
  429. unsigned short numWarningsAllowed = 50;
  430. for ( it = ew.begin();
  431. it != ew.end() && (numErrorsAllowed || numWarningsAllowed); it++ )
  432. {
  433. cmCTestBuildErrorWarning *cm = &(*it);
  434. if (cm->m_Error && numErrorsAllowed ||
  435. !cm->m_Error && numWarningsAllowed)
  436. {
  437. if (cm->m_Error)
  438. {
  439. numErrorsAllowed--;
  440. }
  441. else
  442. {
  443. numWarningsAllowed--;
  444. }
  445. os << "\t<" << (cm->m_Error ? "Error" : "Warning") << ">\n"
  446. << "\t\t<BuildLogLine>" << cm->m_LogLine << "</BuildLogLine>\n"
  447. << "\t\t<Text>" << m_CTest->MakeXMLSafe(cm->m_Text)
  448. << "\n</Text>" << std::endl;
  449. std::vector<cmCTestCompileErrorWarningRex>::iterator rit;
  450. for ( rit = m_ErrorWarningFileLineRegex.begin();
  451. rit != m_ErrorWarningFileLineRegex.end(); ++ rit )
  452. {
  453. cmsys::RegularExpression* re = &rit->m_RegularExpression;
  454. if ( re->find(cm->m_Text.c_str() ) )
  455. {
  456. cm->m_SourceFile = re->match(rit->m_FileIndex);
  457. cm->m_LineNumber = atoi(re->match(rit->m_LineIndex).c_str());
  458. break;
  459. }
  460. }
  461. if ( cm->m_SourceFile.size() > 0 )
  462. {
  463. os << "\t\t<SourceFile>" << cm->m_SourceFile << "</SourceFile>"
  464. << std::endl;
  465. }
  466. if ( cm->m_SourceFileTail.size() > 0 )
  467. {
  468. os << "\t\t<SourceFileTail>" << cm->m_SourceFileTail
  469. << "</SourceFileTail>" << std::endl;
  470. }
  471. if ( cm->m_LineNumber >= 0 )
  472. {
  473. os << "\t\t<SourceLineNumber>" << cm->m_LineNumber
  474. << "</SourceLineNumber>" << std::endl;
  475. }
  476. os << "\t\t<PreContext>" << m_CTest->MakeXMLSafe(cm->m_PreContext)
  477. << "</PreContext>\n"
  478. << "\t\t<PostContext>" << m_CTest->MakeXMLSafe(cm->m_PostContext);
  479. // is this the last warning or error, if so notify
  480. if (cm->m_Error && !numErrorsAllowed ||
  481. !cm->m_Error && !numWarningsAllowed)
  482. {
  483. os << "\nThe maximum number of reported warnings or errors has been reached!!!\n";
  484. }
  485. os << "</PostContext>\n"
  486. << "\t\t<RepeatCount>0</RepeatCount>\n"
  487. << "</" << (cm->m_Error ? "Error" : "Warning") << ">\n\n"
  488. << std::endl;
  489. }
  490. }
  491. os << "\t<Log Encoding=\"base64\" Compression=\"/bin/gzip\">\n\t</Log>\n"
  492. << "\t<EndDateTime>" << m_EndBuild << "</EndDateTime>\n"
  493. << "<ElapsedMinutes>" << static_cast<int>(elapsed_build_time/6)/10.0
  494. << "</ElapsedMinutes>"
  495. << "</Build>" << std::endl;
  496. m_CTest->EndXML(os);
  497. }