cmCTestBuildHandler.cxx 17 KB

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