cmCTestBuildHandler.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. //----------------------------------------------------------------------
  30. cmCTestBuildHandler::cmCTestBuildHandler()
  31. {
  32. m_Verbose = false;
  33. m_CTest = 0;
  34. }
  35. static const char* cmCTestErrorMatches[] = {
  36. "^[Bb]us [Ee]rror",
  37. "^[Ss]egmentation [Vv]iolation",
  38. "^[Ss]egmentation [Ff]ault",
  39. "([^ :]+):([0-9]+): ([^ \\t])",
  40. "([^:]+): error[ \\t]*[0-9]+[ \\t]*:",
  41. "^Error ([0-9]+):",
  42. "^Fatal",
  43. "^Error: ",
  44. "^Error ",
  45. "[0-9] ERROR: ",
  46. "^\"[^\"]+\", line [0-9]+: [^Ww]",
  47. "^cc[^C]*CC: ERROR File = ([^,]+), Line = ([0-9]+)",
  48. "^ld([^:])*:([ \\t])*ERROR([^:])*:",
  49. "^ild:([ \\t])*\\(undefined symbol\\)",
  50. "([^ :]+) : (error|fatal error|catastrophic error)",
  51. "([^:]+): (Error:|error|undefined reference|multiply defined)",
  52. "([^:]+)\\(([^\\)]+)\\) : (error|fatal error|catastrophic error)",
  53. "^fatal error C[0-9]+:",
  54. ": syntax error ",
  55. "^collect2: ld returned 1 exit status",
  56. "Unsatisfied symbols:",
  57. "^Unresolved:",
  58. "Undefined symbols:",
  59. "^Undefined[ \\t]+first referenced",
  60. "^CMake Error:",
  61. ":[ \\t]cannot find",
  62. ":[ \\t]can't find",
  63. ": \\*\\*\\* No rule to make target \\`.*\\'. Stop",
  64. ": Invalid loader fixup for symbol",
  65. ": internal link edit command failed",
  66. ": Unrecognized option \\`.*\\'",
  67. "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\([^W]\\)",
  68. "ld: 0706-006 Cannot find or open library file: -l ",
  69. "ild: \\(argument error\\) can't find library argument ::",
  70. "^could not be found and will not be loaded.",
  71. "s:616 string too big",
  72. "make: Fatal error: ",
  73. "ld: 0711-993 Error occurred while writing to the output file:",
  74. "make: \\*\\*\\*.*Error",
  75. 0
  76. };
  77. static const char* cmCTestErrorExceptions[] = {
  78. "instantiated from ",
  79. "candidates are:",
  80. ": warning",
  81. "makefile:",
  82. "Makefile:",
  83. ":[ \\t]+Where:",
  84. "([^ :]+):([0-9]+): Warning",
  85. 0
  86. };
  87. static const char* cmCTestWarningMatches[] = {
  88. "([^ :]+):([0-9]+): warning:",
  89. "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
  90. "^ld([^:])*:([ \\t])*WARNING([^:])*:",
  91. "([^:]+): warning ([0-9]+):",
  92. "^\"[^\"]+\", line [0-9]+: [Ww]arning",
  93. "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
  94. "^Warning ([0-9]+):",
  95. "^Warning ",
  96. "WARNING: ",
  97. "([^ :]+) : warning",
  98. "([^:]+): warning",
  99. "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\(W\\)",
  100. "^cxx: Warning:",
  101. ".*file: .* has no symbols",
  102. "([^ :]+):([0-9]+): Warning",
  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. //----------------------------------------------------------------------
  125. void cmCTestBuildHandler::PopulateCustomVectors(cmMakefile *mf)
  126. {
  127. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_MATCH",
  128. m_CustomErrorMatches);
  129. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_EXCEPTION",
  130. m_CustomErrorExceptions);
  131. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_MATCH",
  132. m_CustomWarningMatches);
  133. cmCTest::PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_EXCEPTION",
  134. m_CustomWarningExceptions);
  135. }
  136. //----------------------------------------------------------------------
  137. //clearly it would be nice if this were broken up into a few smaller
  138. //functions and commented...
  139. int cmCTestBuildHandler::BuildDirectory(cmCTest *ctest_inst)
  140. {
  141. m_CTest = ctest_inst;
  142. std::cout << "Build project" << std::endl;
  143. std::string makeCommand = m_CTest->GetDartConfiguration("MakeCommand");
  144. if ( makeCommand.size() == 0 )
  145. {
  146. std::cerr << "Cannot find MakeCommand key in the DartConfiguration.tcl" << std::endl;
  147. return 1;
  148. }
  149. std::string buildDirectory = m_CTest->GetDartConfiguration("BuildDirectory");
  150. if ( buildDirectory.size() == 0 )
  151. {
  152. std::cerr << "Cannot find BuildDirectory key in the DartConfiguration.tcl" << std::endl;
  153. return 1;
  154. }
  155. std::ofstream ofs;
  156. double elapsed_time_start = cmSystemTools::GetTime();
  157. if ( !m_CTest->OpenOutputFile("Temporary", "LastBuild.log", ofs) )
  158. {
  159. std::cerr << "Cannot create LastBuild.log file" << std::endl;
  160. }
  161. m_StartBuild = m_CTest->CurrentTime();
  162. std::string output;
  163. int retVal = 0;
  164. int res = cmsysProcess_State_Exited;
  165. if ( !m_CTest->GetShowOnly() )
  166. {
  167. res = m_CTest->RunMakeCommand(makeCommand.c_str(), &output,
  168. &retVal, buildDirectory.c_str(),
  169. m_Verbose, 0, ofs);
  170. }
  171. else
  172. {
  173. std::cout << "Build with command: " << makeCommand << std::endl;
  174. }
  175. m_EndBuild = m_CTest->CurrentTime();
  176. double elapsed_build_time = cmSystemTools::GetTime() - elapsed_time_start;
  177. if (res != cmsysProcess_State_Exited || retVal )
  178. {
  179. std::cerr << "Error(s) when building project" << std::endl;
  180. }
  181. if ( ofs )
  182. {
  183. ofs.close();
  184. }
  185. std::vector<cmStdString>::size_type cc;
  186. if ( m_CTest->GetDartConfiguration("SourceDirectory").size() > 20 ||
  187. m_CTest->GetDartConfiguration("BuildDirectory").size() > 20 )
  188. {
  189. std::string srcdir = m_CTest->GetDartConfiguration("SourceDirectory") + "/";
  190. std::string bindir = m_CTest->GetDartConfiguration("BuildDirectory") + "/";
  191. std::string srcdirrep;
  192. std::string bindirrep;
  193. for ( cc = srcdir.size()-2; cc > 0; cc -- )
  194. {
  195. if ( srcdir[cc] == '/' )
  196. {
  197. srcdirrep = srcdir.c_str() + cc;
  198. srcdirrep = "/..." + srcdirrep;
  199. srcdir = srcdir.substr(0, cc+1);
  200. break;
  201. }
  202. }
  203. for ( cc = bindir.size()-2; cc > 0; cc -- )
  204. {
  205. if ( bindir[cc] == '/' )
  206. {
  207. bindirrep = bindir.c_str() + cc;
  208. bindirrep = "/..." + bindirrep;
  209. bindir = bindir.substr(0, cc+1);
  210. break;
  211. }
  212. }
  213. cmSystemTools::ReplaceString(output, srcdir.c_str(), "/.../"); //srcdirrep.c_str());
  214. cmSystemTools::ReplaceString(output, bindir.c_str(), "/.../"); //bindirrep.c_str());
  215. }
  216. // Parsing of output for errors and warnings.
  217. std::vector<cmStdString> lines;
  218. cmSystemTools::Split(output.c_str(), lines);
  219. // Lines are marked:
  220. // 0 - nothing
  221. // 1 - error
  222. // > 1 - warning
  223. std::vector<int> markedLines(lines.size(), 0);
  224. // Errors
  225. for ( cc = 0; cmCTestErrorMatches[cc]; cc ++ )
  226. {
  227. m_CustomErrorMatches.push_back(cmCTestErrorMatches[cc]);
  228. }
  229. for ( cc = 0; cmCTestErrorExceptions[cc]; cc ++ )
  230. {
  231. m_CustomErrorExceptions.push_back(cmCTestErrorExceptions[cc]);
  232. }
  233. for ( cc = 0; cmCTestWarningMatches[cc]; cc ++ )
  234. {
  235. m_CustomWarningMatches.push_back(cmCTestWarningMatches[cc]);
  236. }
  237. for ( cc = 0; cmCTestWarningExceptions[cc]; cc ++ )
  238. {
  239. m_CustomWarningExceptions.push_back(cmCTestWarningExceptions[cc]);
  240. }
  241. for ( cc = 0; cc < m_CustomErrorMatches.size(); cc ++ )
  242. {
  243. cmsys::RegularExpression re(m_CustomErrorMatches[cc].c_str());
  244. std::vector<cmStdString>::size_type kk;
  245. for ( kk = 0; kk < lines.size(); kk ++ )
  246. {
  247. if ( re.find(lines[kk]) )
  248. {
  249. markedLines[kk] = 1;
  250. }
  251. }
  252. }
  253. // Warnings
  254. for ( cc = 0; cc < m_CustomWarningMatches.size(); cc ++ )
  255. {
  256. cmsys::RegularExpression re(m_CustomWarningMatches[cc].c_str());
  257. std::vector<cmStdString>::size_type kk;
  258. for ( kk = 0; kk < lines.size(); kk ++ )
  259. {
  260. if ( re.find(lines[kk]) )
  261. {
  262. markedLines[kk] += 2;
  263. }
  264. }
  265. }
  266. // Errors exceptions
  267. for ( cc = 0; cc < m_CustomErrorExceptions.size(); cc ++ )
  268. {
  269. cmsys::RegularExpression re(m_CustomErrorExceptions[cc].c_str());
  270. std::vector<int>::size_type kk;
  271. for ( kk =0; kk < markedLines.size(); kk ++ )
  272. {
  273. if ( markedLines[kk] == 1 )
  274. {
  275. if ( re.find(lines[kk]) )
  276. {
  277. markedLines[kk] = 0;
  278. }
  279. }
  280. }
  281. }
  282. // Warning exceptions
  283. for ( cc = 0; cc < m_CustomWarningExceptions.size(); cc ++ )
  284. {
  285. cmsys::RegularExpression re(m_CustomWarningExceptions[cc].c_str());
  286. std::vector<int>::size_type kk;
  287. for ( kk =0; kk < markedLines.size(); kk ++ )
  288. {
  289. if ( markedLines[kk] > 1 )
  290. {
  291. if ( re.find(lines[kk]) )
  292. {
  293. markedLines[kk] = 0;
  294. }
  295. }
  296. }
  297. }
  298. std::vector<cmCTestBuildErrorWarning> errorsWarnings;
  299. int errors = 0;
  300. int warnings = 0;
  301. std::vector<int>::size_type kk;
  302. cmCTestBuildErrorWarning errorwarning;
  303. for ( kk =0; kk < markedLines.size(); kk ++ )
  304. {
  305. errorwarning.m_LineNumber = -1;
  306. bool found = false;
  307. if ( markedLines[kk] == 1 )
  308. {
  309. //std::cout << "Error: " << lines[kk] << std::endl;
  310. errorwarning.m_Error = true;
  311. found = true;
  312. }
  313. else if ( markedLines[kk] > 1 )
  314. {
  315. //std::cout << "Warning: " << lines[kk] << std::endl;
  316. errorwarning.m_Error = false;
  317. found = true;
  318. }
  319. if ( found )
  320. {
  321. errorwarning.m_LogLine = static_cast<int>(kk+1);
  322. errorwarning.m_Text = lines[kk];
  323. errorwarning.m_PreContext = "";
  324. errorwarning.m_PostContext = "";
  325. std::vector<int>::size_type jj;
  326. std::vector<int>::size_type ll = 0;
  327. if ( kk > 6 )
  328. {
  329. ll = kk - 6;
  330. }
  331. for ( jj = kk-1;
  332. jj > 0 && jj > ll && markedLines[jj] != markedLines[kk];
  333. jj -- );
  334. while ( markedLines[jj] == markedLines[kk] && jj < kk )
  335. {
  336. jj ++;
  337. }
  338. for (; jj < kk; jj ++ )
  339. {
  340. errorwarning.m_PreContext += lines[jj] + "\n";
  341. }
  342. for ( jj = kk+1;
  343. jj < lines.size() && jj < kk + 7 && markedLines[jj] != markedLines[kk];
  344. jj ++ )
  345. {
  346. errorwarning.m_PostContext += lines[jj] + "\n";
  347. }
  348. errorsWarnings.push_back(errorwarning);
  349. if ( errorwarning.m_Error )
  350. {
  351. errors ++;
  352. }
  353. else
  354. {
  355. warnings ++;
  356. }
  357. }
  358. }
  359. std::cout << " " << errors << " Compiler errors" << std::endl;
  360. std::cout << " " << warnings << " Compiler warnings" << std::endl;
  361. if( !m_CTest->OpenOutputFile(m_CTest->GetCurrentTag(), "Build.xml", ofs) )
  362. {
  363. std::cerr << "Cannot create build XML file" << std::endl;
  364. return 1;
  365. }
  366. this->GenerateDartBuildOutput(ofs, errorsWarnings, elapsed_build_time);
  367. return 0;
  368. }
  369. void cmCTestBuildHandler::GenerateDartBuildOutput(
  370. std::ostream& os,
  371. std::vector<cmCTestBuildErrorWarning> ew,
  372. double elapsed_build_time)
  373. {
  374. m_CTest->StartXML(os);
  375. os << "<Build>\n"
  376. << "\t<StartDateTime>" << m_StartBuild << "</StartDateTime>\n"
  377. << "<BuildCommand>"
  378. << m_CTest->MakeXMLSafe(m_CTest->GetDartConfiguration("MakeCommand"))
  379. << "</BuildCommand>" << std::endl;
  380. std::vector<cmCTestBuildErrorWarning>::iterator it;
  381. for ( it = ew.begin(); it != ew.end(); it++ )
  382. {
  383. cmCTestBuildErrorWarning *cm = &(*it);
  384. os << "\t<" << (cm->m_Error ? "Error" : "Warning") << ">\n"
  385. << "\t\t<BuildLogLine>" << cm->m_LogLine << "</BuildLogLine>\n"
  386. << "\t\t<Text>" << m_CTest->MakeXMLSafe(cm->m_Text)
  387. << "\n</Text>" << std::endl;
  388. if ( cm->m_SourceFile.size() > 0 )
  389. {
  390. os << "\t\t<SourceFile>" << cm->m_SourceFile << "</SourceFile>"
  391. << std::endl;
  392. }
  393. if ( cm->m_SourceFileTail.size() > 0 )
  394. {
  395. os << "\t\t<SourceFileTail>" << cm->m_SourceFileTail
  396. << "</SourceFileTail>" << std::endl;
  397. }
  398. if ( cm->m_LineNumber >= 0 )
  399. {
  400. os << "\t\t<SourceLineNumber>" << cm->m_LineNumber
  401. << "</SourceLineNumber>" << std::endl;
  402. }
  403. os << "\t\t<PreContext>" << m_CTest->MakeXMLSafe(cm->m_PreContext)
  404. << "</PreContext>\n"
  405. << "\t\t<PostContext>" << m_CTest->MakeXMLSafe(cm->m_PostContext)
  406. << "</PostContext>\n"
  407. << "\t\t<RepeatCount>0</RepeatCount>\n"
  408. << "</" << (cm->m_Error ? "Error" : "Warning") << ">\n\n"
  409. << std::endl;
  410. }
  411. os << "\t<Log Encoding=\"base64\" Compression=\"/bin/gzip\">\n\t</Log>\n"
  412. << "\t<EndDateTime>" << m_EndBuild << "</EndDateTime>\n"
  413. << "<ElapsedMinutes>" << static_cast<int>(elapsed_build_time/6)/10.0
  414. << "</ElapsedMinutes>"
  415. << "</Build>" << std::endl;
  416. m_CTest->EndXML(os);
  417. }