cmCTestBuildHandler.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. "make: \\*\\*\\*.*Error",
  69. 0
  70. };
  71. static const char* cmCTestErrorExceptions[] = {
  72. "instantiated from ",
  73. "candidates are:",
  74. ": warning",
  75. "makefile:",
  76. "Makefile:",
  77. ":[ \\t]+Where:",
  78. "([^ :]+):([0-9]+): Warning",
  79. 0
  80. };
  81. static const char* cmCTestWarningMatches[] = {
  82. "([^ :]+):([0-9]+): warning:",
  83. "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
  84. "^ld([^:])*:([ \\t])*WARNING([^:])*:",
  85. "([^:]+): warning ([0-9]+):",
  86. "^\"[^\"]+\", line [0-9]+: [Ww]arning",
  87. "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
  88. "^Warning ([0-9]+):",
  89. "^Warning ",
  90. "WARNING: ",
  91. "([^ :]+) : warning",
  92. "([^:]+): warning",
  93. "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\(W\\)",
  94. "^cxx: Warning:",
  95. ".*file: .* has no symbols",
  96. "([^ :]+):([0-9]+): Warning",
  97. "\\([0-9]*\\): remark #[0-9]*",
  98. "\".*\", line [0-9]+: remark\\([0-9]*\\):",
  99. "cc-[0-9]* CC: REMARK File = .*, Line = [0-9]*",
  100. 0
  101. };
  102. static const char* cmCTestWarningExceptions[] = {
  103. "/usr/openwin/include/X11/Xlib\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  104. "/usr/openwin/include/X11/Xutil\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  105. "/usr/openwin/include/X11/XResource\\.h:[0-9]+: warning: ANSI C\\+\\+ forbids declaration",
  106. "WARNING 84 :",
  107. "WARNING 47 :",
  108. "makefile:",
  109. "Makefile:",
  110. "warning: Clock skew detected. Your build may be incomplete.",
  111. "/usr/openwin/include/GL/[^:]+:",
  112. "bind_at_load",
  113. "XrmQGetResource",
  114. "IceFlush",
  115. "warning LNK4089: all references to [^ \\t]+ discarded by .OPT:REF",
  116. "ld32: WARNING 85: definition of dataKey in",
  117. "cc: warning 422: Unknown option \"\\+b",
  118. "_with_warning_C",
  119. 0
  120. };
  121. struct cmCTestBuildCompileErrorWarningRex
  122. {
  123. const char* m_RegularExpressionString;
  124. int m_FileIndex;
  125. int m_LineIndex;
  126. };
  127. static cmCTestBuildCompileErrorWarningRex
  128. cmCTestWarningErrorFileLine[] = {
  129. { "^Warning W[0-9]+ ([a-zA-Z.\\:/0-9_+ ~-]+) ([0-9]+):", 1, 2 },
  130. { "^([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_+ ~-]+)\", line ([0-9]+)", 1, 2 },
  134. { "File = ([a-zA-Z./0-9_+ ~-]+), Line = ([0-9]+)", 1, 2 },
  135. { 0, 0, 0 }
  136. };
  137. //----------------------------------------------------------------------
  138. cmCTestBuildHandler::cmCTestBuildHandler()
  139. {
  140. m_Verbose = false;
  141. m_CTest = 0;
  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::BuildDirectory(cmCTest *ctest_inst)
  176. {
  177. m_CTest = ctest_inst;
  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. std::ofstream 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. if ( ofs )
  218. {
  219. ofs.close();
  220. }
  221. std::vector<cmStdString>::size_type cc;
  222. if ( m_CTest->GetDartConfiguration("SourceDirectory").size() > 20 ||
  223. m_CTest->GetDartConfiguration("BuildDirectory").size() > 20 )
  224. {
  225. std::string srcdir = m_CTest->GetDartConfiguration("SourceDirectory") + "/";
  226. std::string bindir = m_CTest->GetDartConfiguration("BuildDirectory") + "/";
  227. std::string srcdirrep;
  228. std::string bindirrep;
  229. for ( cc = srcdir.size()-2; cc > 0; cc -- )
  230. {
  231. if ( srcdir[cc] == '/' )
  232. {
  233. srcdirrep = srcdir.c_str() + cc;
  234. srcdirrep = "/..." + srcdirrep;
  235. srcdir = srcdir.substr(0, cc+1);
  236. break;
  237. }
  238. }
  239. for ( cc = bindir.size()-2; cc > 0; cc -- )
  240. {
  241. if ( bindir[cc] == '/' )
  242. {
  243. bindirrep = bindir.c_str() + cc;
  244. bindirrep = "/..." + bindirrep;
  245. bindir = bindir.substr(0, cc+1);
  246. break;
  247. }
  248. }
  249. cmSystemTools::ReplaceString(output, srcdir.c_str(), "/.../"); //srcdirrep.c_str());
  250. cmSystemTools::ReplaceString(output, bindir.c_str(), "/.../"); //bindirrep.c_str());
  251. }
  252. // Parsing of output for errors and warnings.
  253. std::vector<cmStdString> lines;
  254. cmSystemTools::Split(output.c_str(), lines);
  255. // Lines are marked:
  256. // 0 - nothing
  257. // 1 - error
  258. // > 1 - warning
  259. std::vector<int> markedLines(lines.size(), 0);
  260. // Errors
  261. for ( cc = 0; cmCTestErrorMatches[cc]; cc ++ )
  262. {
  263. m_CustomErrorMatches.push_back(cmCTestErrorMatches[cc]);
  264. }
  265. for ( cc = 0; cmCTestErrorExceptions[cc]; cc ++ )
  266. {
  267. m_CustomErrorExceptions.push_back(cmCTestErrorExceptions[cc]);
  268. }
  269. for ( cc = 0; cmCTestWarningMatches[cc]; cc ++ )
  270. {
  271. m_CustomWarningMatches.push_back(cmCTestWarningMatches[cc]);
  272. }
  273. for ( cc = 0; cmCTestWarningExceptions[cc]; cc ++ )
  274. {
  275. m_CustomWarningExceptions.push_back(cmCTestWarningExceptions[cc]);
  276. }
  277. for ( cc = 0; cc < m_CustomErrorMatches.size(); cc ++ )
  278. {
  279. cmsys::RegularExpression re(m_CustomErrorMatches[cc].c_str());
  280. std::vector<cmStdString>::size_type kk;
  281. //cout << "error Line: " << m_CustomErrorMatches[cc] << endl;
  282. for ( kk = 0; kk < lines.size(); kk ++ )
  283. {
  284. //cout << " Line: " << lines[kk] << endl;
  285. if ( re.find(lines[kk]) )
  286. {
  287. //cout << "******************************" << endl;
  288. markedLines[kk] = 1;
  289. }
  290. }
  291. }
  292. // Warnings
  293. for ( cc = 0; cc < m_CustomWarningMatches.size(); cc ++ )
  294. {
  295. cmsys::RegularExpression re(m_CustomWarningMatches[cc].c_str());
  296. std::vector<cmStdString>::size_type kk;
  297. //cout << "warning Line: " << m_CustomWarningMatches[cc] << endl;
  298. for ( kk = 0; kk < lines.size(); kk ++ )
  299. {
  300. //cout << " Line: " << lines[kk] << endl;
  301. if ( re.find(lines[kk]) )
  302. {
  303. //cout << "******************************" << endl;
  304. markedLines[kk] += 2;
  305. }
  306. }
  307. }
  308. // Errors exceptions
  309. for ( cc = 0; cc < m_CustomErrorExceptions.size(); cc ++ )
  310. {
  311. cmsys::RegularExpression re(m_CustomErrorExceptions[cc].c_str());
  312. std::vector<int>::size_type kk;
  313. for ( kk =0; kk < markedLines.size(); kk ++ )
  314. {
  315. if ( markedLines[kk] == 1 )
  316. {
  317. if ( re.find(lines[kk]) )
  318. {
  319. markedLines[kk] = 0;
  320. }
  321. }
  322. }
  323. }
  324. // Warning exceptions
  325. for ( cc = 0; cc < m_CustomWarningExceptions.size(); cc ++ )
  326. {
  327. cmsys::RegularExpression re(m_CustomWarningExceptions[cc].c_str());
  328. std::vector<int>::size_type kk;
  329. for ( kk =0; kk < markedLines.size(); kk ++ )
  330. {
  331. if ( markedLines[kk] > 1 )
  332. {
  333. if ( re.find(lines[kk]) )
  334. {
  335. markedLines[kk] = 0;
  336. }
  337. }
  338. }
  339. }
  340. std::vector<cmCTestBuildErrorWarning> errorsWarnings;
  341. int errors = 0;
  342. int warnings = 0;
  343. std::vector<int>::size_type kk;
  344. cmCTestBuildErrorWarning errorwarning;
  345. for ( kk =0; kk < markedLines.size(); kk ++ )
  346. {
  347. errorwarning.m_LineNumber = -1;
  348. bool found = false;
  349. if ( markedLines[kk] == 1 )
  350. {
  351. //std::cout << "Error: " << lines[kk] << std::endl;
  352. errorwarning.m_Error = true;
  353. found = true;
  354. }
  355. else if ( markedLines[kk] > 1 )
  356. {
  357. //std::cout << "Warning: " << lines[kk] << std::endl;
  358. errorwarning.m_Error = false;
  359. found = true;
  360. }
  361. if ( found )
  362. {
  363. errorwarning.m_LogLine = static_cast<int>(kk+1);
  364. errorwarning.m_Text = lines[kk];
  365. errorwarning.m_PreContext = "";
  366. errorwarning.m_PostContext = "";
  367. std::vector<int>::size_type jj;
  368. std::vector<int>::size_type ll = 0;
  369. if ( kk > 6 )
  370. {
  371. ll = kk - 6;
  372. }
  373. for ( jj = kk-1;
  374. jj > 0 && jj > ll && markedLines[jj] != markedLines[kk];
  375. jj -- );
  376. while ( markedLines[jj] == markedLines[kk] && jj < kk )
  377. {
  378. jj ++;
  379. }
  380. for (; jj < kk; jj ++ )
  381. {
  382. errorwarning.m_PreContext += lines[jj] + "\n";
  383. }
  384. for ( jj = kk+1;
  385. jj < lines.size() && jj < kk + 7 && markedLines[jj] != markedLines[kk];
  386. jj ++ )
  387. {
  388. errorwarning.m_PostContext += lines[jj] + "\n";
  389. }
  390. errorsWarnings.push_back(errorwarning);
  391. if ( errorwarning.m_Error )
  392. {
  393. errors ++;
  394. }
  395. else
  396. {
  397. warnings ++;
  398. }
  399. }
  400. }
  401. std::cout << " " << errors << " Compiler errors" << std::endl;
  402. std::cout << " " << warnings << " Compiler warnings" << std::endl;
  403. if( !m_CTest->OpenOutputFile(m_CTest->GetCurrentTag(), "Build.xml", ofs) )
  404. {
  405. std::cerr << "Cannot create build XML file" << std::endl;
  406. return 1;
  407. }
  408. this->GenerateDartBuildOutput(ofs, 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. for ( it = ew.begin(); it != ew.end(); it++ )
  424. {
  425. cmCTestBuildErrorWarning *cm = &(*it);
  426. os << "\t<" << (cm->m_Error ? "Error" : "Warning") << ">\n"
  427. << "\t\t<BuildLogLine>" << cm->m_LogLine << "</BuildLogLine>\n"
  428. << "\t\t<Text>" << m_CTest->MakeXMLSafe(cm->m_Text)
  429. << "\n</Text>" << std::endl;
  430. std::vector<cmCTestCompileErrorWarningRex>::iterator rit;
  431. for ( rit = m_ErrorWarningFileLineRegex.begin();
  432. rit != m_ErrorWarningFileLineRegex.end(); ++ rit )
  433. {
  434. cmsys::RegularExpression* re = &rit->m_RegularExpression;
  435. if ( re->find(cm->m_Text.c_str() ) )
  436. {
  437. cm->m_SourceFile = re->match(rit->m_FileIndex);
  438. cm->m_LineNumber = atoi(re->match(rit->m_LineIndex).c_str());
  439. break;
  440. }
  441. }
  442. if ( cm->m_SourceFile.size() > 0 )
  443. {
  444. os << "\t\t<SourceFile>" << cm->m_SourceFile << "</SourceFile>"
  445. << std::endl;
  446. }
  447. if ( cm->m_SourceFileTail.size() > 0 )
  448. {
  449. os << "\t\t<SourceFileTail>" << cm->m_SourceFileTail
  450. << "</SourceFileTail>" << std::endl;
  451. }
  452. if ( cm->m_LineNumber >= 0 )
  453. {
  454. os << "\t\t<SourceLineNumber>" << cm->m_LineNumber
  455. << "</SourceLineNumber>" << std::endl;
  456. }
  457. os << "\t\t<PreContext>" << m_CTest->MakeXMLSafe(cm->m_PreContext)
  458. << "</PreContext>\n"
  459. << "\t\t<PostContext>" << m_CTest->MakeXMLSafe(cm->m_PostContext)
  460. << "</PostContext>\n"
  461. << "\t\t<RepeatCount>0</RepeatCount>\n"
  462. << "</" << (cm->m_Error ? "Error" : "Warning") << ">\n\n"
  463. << std::endl;
  464. }
  465. os << "\t<Log Encoding=\"base64\" Compression=\"/bin/gzip\">\n\t</Log>\n"
  466. << "\t<EndDateTime>" << m_EndBuild << "</EndDateTime>\n"
  467. << "<ElapsedMinutes>" << static_cast<int>(elapsed_build_time/6)/10.0
  468. << "</ElapsedMinutes>"
  469. << "</Build>" << std::endl;
  470. m_CTest->EndXML(os);
  471. }