Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / CTest / cmCTestBuildHandler.cxx
blob8de8f90a0c58a51218b21049615914bd026355c0
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCTestBuildHandler.cxx,v $
5 Language: C++
6 Date: $Date: 2009-02-12 18:00:22 $
7 Version: $Revision: 1.73 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
18 #include "cmCTestBuildHandler.h"
20 #include "cmCTest.h"
21 #include "cmake.h"
22 #include "cmMakefile.h"
23 #include "cmLocalGenerator.h"
24 #include "cmGlobalGenerator.h"
25 #include "cmGeneratedFileStream.h"
26 #include "cmXMLSafe.h"
27 #include "cmFileTimeComparison.h"
29 //#include <cmsys/RegularExpression.hxx>
30 #include <cmsys/Process.h>
31 #include <cmsys/Directory.hxx>
33 // used for sleep
34 #ifdef _WIN32
35 #include "windows.h"
36 #endif
38 #include <stdlib.h>
39 #include <time.h>
40 #include <math.h>
41 #include <float.h>
43 #if defined(__BORLANDC__)
44 # pragma warn -8060 /* possibly incorrect assignment */
45 #endif
47 static const char* cmCTestErrorMatches[] = {
48 "^[Bb]us [Ee]rror",
49 "^[Ss]egmentation [Vv]iolation",
50 "^[Ss]egmentation [Ff]ault",
51 "([^ :]+):([0-9]+): ([^ \\t])",
52 "([^:]+): error[ \\t]*[0-9]+[ \\t]*:",
53 "^Error ([0-9]+):",
54 "^Fatal",
55 "^Error: ",
56 "^Error ",
57 "[0-9] ERROR: ",
58 "^\"[^\"]+\", line [0-9]+: [^Ww]",
59 "^cc[^C]*CC: ERROR File = ([^,]+), Line = ([0-9]+)",
60 "^ld([^:])*:([ \\t])*ERROR([^:])*:",
61 "^ild:([ \\t])*\\(undefined symbol\\)",
62 "([^ :]+) : (error|fatal error|catastrophic error)",
63 "([^:]+): (Error:|error|undefined reference|multiply defined)",
64 "([^:]+)\\(([^\\)]+)\\) : (error|fatal error|catastrophic error)",
65 "^fatal error C[0-9]+:",
66 ": syntax error ",
67 "^collect2: ld returned 1 exit status",
68 "ld terminated with signal",
69 "Unsatisfied symbols:",
70 "^Unresolved:",
71 "Undefined symbols:",
72 "^Undefined[ \\t]+first referenced",
73 "^CMake Error:",
74 ":[ \\t]cannot find",
75 ":[ \\t]can't find",
76 ": \\*\\*\\* No rule to make target \\`.*\\'. Stop",
77 ": \\*\\*\\* No targets specified and no makefile found",
78 ": Invalid loader fixup for symbol",
79 ": Invalid fixups exist",
80 ": Can't find library for",
81 ": internal link edit command failed",
82 ": Unrecognized option \\`.*\\'",
83 "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\([^WI]\\)",
84 "ld: 0706-006 Cannot find or open library file: -l ",
85 "ild: \\(argument error\\) can't find library argument ::",
86 "^could not be found and will not be loaded.",
87 "s:616 string too big",
88 "make: Fatal error: ",
89 "ld: 0711-993 Error occurred while writing to the output file:",
90 "ld: fatal: ",
91 "final link failed:",
92 "make: \\*\\*\\*.*Error",
93 "make\\[.*\\]: \\*\\*\\*.*Error",
94 "\\*\\*\\* Error code",
95 "nternal error:",
96 "Makefile:[0-9]+: \\*\\*\\* .* Stop\\.",
97 ": No such file or directory",
98 ": Invalid argument",
99 "^The project cannot be built\\.",
103 static const char* cmCTestErrorExceptions[] = {
104 "instantiated from ",
105 "candidates are:",
106 ": warning",
107 ": \\(Warning\\)",
108 ": note",
109 "makefile:",
110 "Makefile:",
111 ":[ \\t]+Where:",
112 "([^ :]+):([0-9]+): Warning",
113 "------ Build started: .* ------",
117 static const char* cmCTestWarningMatches[] = {
118 "([^ :]+):([0-9]+): warning:",
119 "([^ :]+):([0-9]+): note:",
120 "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
121 "^ld([^:])*:([ \\t])*WARNING([^:])*:",
122 "([^:]+): warning ([0-9]+):",
123 "^\"[^\"]+\", line [0-9]+: [Ww](arning|arnung)",
124 "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
125 "^(Warning|Warnung) ([0-9]+):",
126 "^(Warning|Warnung) ",
127 "WARNING: ",
128 "([^ :]+) : warning",
129 "([^:]+): warning",
130 "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\([WI]\\)",
131 "^cxx: Warning:",
132 ".*file: .* has no symbols",
133 "([^ :]+):([0-9]+): (Warning|Warnung)",
134 "\\([0-9]*\\): remark #[0-9]*",
135 "\".*\", line [0-9]+: remark\\([0-9]*\\):",
136 "cc-[0-9]* CC: REMARK File = .*, Line = [0-9]*",
140 static const char* cmCTestWarningExceptions[] = {
141 "/usr/.*/X11/Xlib\\.h:[0-9]+: war.*: ANSI C\\+\\+ forbids declaration",
142 "/usr/.*/X11/Xutil\\.h:[0-9]+: war.*: ANSI C\\+\\+ forbids declaration",
143 "/usr/.*/X11/XResource\\.h:[0-9]+: war.*: ANSI C\\+\\+ forbids declaration",
144 "WARNING 84 :",
145 "WARNING 47 :",
146 "makefile:",
147 "Makefile:",
148 "warning: Clock skew detected. Your build may be incomplete.",
149 "/usr/openwin/include/GL/[^:]+:",
150 "bind_at_load",
151 "XrmQGetResource",
152 "IceFlush",
153 "warning LNK4089: all references to [^ \\t]+ discarded by .OPT:REF",
154 "ld32: WARNING 85: definition of dataKey in",
155 "cc: warning 422: Unknown option \"\\+b",
156 "_with_warning_C",
160 struct cmCTestBuildCompileErrorWarningRex
162 const char* RegularExpressionString;
163 int FileIndex;
164 int LineIndex;
167 static cmCTestBuildCompileErrorWarningRex
168 cmCTestWarningErrorFileLine[] = {
169 { "^Warning W[0-9]+ ([a-zA-Z.\\:/0-9_+ ~-]+) ([0-9]+):", 1, 2 },
170 { "^([a-zA-Z./0-9_+ ~-]+):([0-9]+):", 1, 2 },
171 { "^([a-zA-Z.\\:/0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
172 { "^[0-9]+>([a-zA-Z.\\:/0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
173 { "^([a-zA-Z./0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
174 { "\"([a-zA-Z./0-9_+ ~-]+)\", line ([0-9]+)", 1, 2 },
175 { "File = ([a-zA-Z./0-9_+ ~-]+), Line = ([0-9]+)", 1, 2 },
176 { 0, 0, 0 }
179 //----------------------------------------------------------------------
180 cmCTestBuildHandler::cmCTestBuildHandler()
182 this->MaxPreContext = 6;
183 this->MaxPostContext = 6;
185 this->MaxErrors = 50;
186 this->MaxWarnings = 50;
188 this->LastErrorOrWarning = this->ErrorsAndWarnings.end();
190 this->UseCTestLaunch = false;
193 //----------------------------------------------------------------------
194 void cmCTestBuildHandler::Initialize()
196 this->Superclass::Initialize();
197 this->StartBuild = "";
198 this->EndBuild = "";
199 this->CustomErrorMatches.clear();
200 this->CustomErrorExceptions.clear();
201 this->CustomWarningMatches.clear();
202 this->CustomWarningExceptions.clear();
203 this->ReallyCustomWarningMatches.clear();
204 this->ReallyCustomWarningExceptions.clear();
205 this->ErrorWarningFileLineRegex.clear();
207 this->ErrorMatchRegex.clear();
208 this->ErrorExceptionRegex.clear();
209 this->WarningMatchRegex.clear();
210 this->WarningExceptionRegex.clear();
211 this->BuildProcessingQueue.clear();
212 this->BuildProcessingErrorQueue.clear();
213 this->BuildOutputLogSize = 0;
214 this->CurrentProcessingLine.clear();
216 this->SimplifySourceDir = "";
217 this->SimplifyBuildDir = "";
218 this->OutputLineCounter = 0;
219 this->ErrorsAndWarnings.clear();
220 this->LastErrorOrWarning = this->ErrorsAndWarnings.end();
221 this->PostContextCount = 0;
222 this->MaxPreContext = 6;
223 this->MaxPostContext = 6;
224 this->PreContext.clear();
226 this->TotalErrors = 0;
227 this->TotalWarnings = 0;
228 this->LastTickChar = 0;
230 this->ErrorQuotaReached = false;
231 this->WarningQuotaReached = false;
233 this->MaxErrors = 50;
234 this->MaxWarnings = 50;
236 this->UseCTestLaunch = false;
239 //----------------------------------------------------------------------
240 void cmCTestBuildHandler::PopulateCustomVectors(cmMakefile *mf)
242 this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_MATCH",
243 this->CustomErrorMatches);
244 this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_EXCEPTION",
245 this->CustomErrorExceptions);
246 this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_MATCH",
247 this->CustomWarningMatches);
248 this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_EXCEPTION",
249 this->CustomWarningExceptions);
250 this->CTest->PopulateCustomInteger(mf,
251 "CTEST_CUSTOM_MAXIMUM_NUMBER_OF_ERRORS",
252 this->MaxErrors);
253 this->CTest->PopulateCustomInteger(mf,
254 "CTEST_CUSTOM_MAXIMUM_NUMBER_OF_WARNINGS",
255 this->MaxWarnings);
257 // Record the user-specified custom warning rules.
258 if(const char* customWarningMatchers =
259 mf->GetDefinition("CTEST_CUSTOM_WARNING_MATCH"))
261 cmSystemTools::ExpandListArgument(customWarningMatchers,
262 this->ReallyCustomWarningMatches);
264 if(const char* customWarningExceptions =
265 mf->GetDefinition("CTEST_CUSTOM_WARNING_EXCEPTION"))
267 cmSystemTools::ExpandListArgument(customWarningExceptions,
268 this->ReallyCustomWarningExceptions);
272 //----------------------------------------------------------------------
273 //clearly it would be nice if this were broken up into a few smaller
274 //functions and commented...
275 int cmCTestBuildHandler::ProcessHandler()
277 cmCTestLog(this->CTest, HANDLER_OUTPUT, "Build project" << std::endl);
279 // do we have time for this
280 if (this->CTest->GetRemainingTimeAllowed() < 120)
282 return 0;
285 int entry;
286 for ( entry = 0;
287 cmCTestWarningErrorFileLine[entry].RegularExpressionString;
288 ++ entry )
290 cmCTestBuildHandler::cmCTestCompileErrorWarningRex r;
291 if ( r.RegularExpression.compile(
292 cmCTestWarningErrorFileLine[entry].RegularExpressionString) )
294 r.FileIndex = cmCTestWarningErrorFileLine[entry].FileIndex;
295 r.LineIndex = cmCTestWarningErrorFileLine[entry].LineIndex;
296 this->ErrorWarningFileLineRegex.push_back(r);
298 else
300 cmCTestLog(this->CTest, ERROR_MESSAGE,
301 "Problem Compiling regular expression: "
302 << cmCTestWarningErrorFileLine[entry].RegularExpressionString
303 << std::endl);
307 // Determine build command and build directory
308 const std::string &makeCommand
309 = this->CTest->GetCTestConfiguration("MakeCommand");
310 cmCTestLog(this->CTest,
311 HANDLER_VERBOSE_OUTPUT, "MakeCommand:" << makeCommand <<
312 "\n");
313 if ( makeCommand.size() == 0 )
315 cmCTestLog(this->CTest, ERROR_MESSAGE,
316 "Cannot find MakeCommand key in the DartConfiguration.tcl"
317 << std::endl);
318 return -1;
320 const std::string &buildDirectory
321 = this->CTest->GetCTestConfiguration("BuildDirectory");
322 if ( buildDirectory.size() == 0 )
324 cmCTestLog(this->CTest, ERROR_MESSAGE,
325 "Cannot find BuildDirectory key in the DartConfiguration.tcl"
326 << std::endl);
327 return -1;
330 std::string const& useLaunchers =
331 this->CTest->GetCTestConfiguration("UseLaunchers");
332 this->UseCTestLaunch = cmSystemTools::IsOn(useLaunchers.c_str());
334 // Create a last build log
335 cmGeneratedFileStream ofs;
336 double elapsed_time_start = cmSystemTools::GetTime();
337 if ( !this->StartLogFile("Build", ofs) )
339 cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create build log file"
340 << std::endl);
343 // Create lists of regular expression strings for errors, error exceptions,
344 // warnings and warning exceptions.
345 std::vector<cmStdString>::size_type cc;
346 for ( cc = 0; cmCTestErrorMatches[cc]; cc ++ )
348 this->CustomErrorMatches.push_back(cmCTestErrorMatches[cc]);
350 for ( cc = 0; cmCTestErrorExceptions[cc]; cc ++ )
352 this->CustomErrorExceptions.push_back(cmCTestErrorExceptions[cc]);
354 for ( cc = 0; cmCTestWarningMatches[cc]; cc ++ )
356 this->CustomWarningMatches.push_back(cmCTestWarningMatches[cc]);
359 for ( cc = 0; cmCTestWarningExceptions[cc]; cc ++ )
361 this->CustomWarningExceptions.push_back(cmCTestWarningExceptions[cc]);
364 // Pre-compile regular expressions objects for all regular expressions
365 std::vector<cmStdString>::iterator it;
367 #define cmCTestBuildHandlerPopulateRegexVector(strings, regexes) \
368 regexes.clear(); \
369 cmCTestLog(this->CTest, DEBUG, this << "Add " #regexes \
370 << std::endl); \
371 for ( it = strings.begin(); it != strings.end(); ++it ) \
373 cmCTestLog(this->CTest, DEBUG, "Add " #strings ": " \
374 << it->c_str() << std::endl); \
375 regexes.push_back(it->c_str()); \
377 cmCTestBuildHandlerPopulateRegexVector(
378 this->CustomErrorMatches, this->ErrorMatchRegex);
379 cmCTestBuildHandlerPopulateRegexVector(
380 this->CustomErrorExceptions, this->ErrorExceptionRegex);
381 cmCTestBuildHandlerPopulateRegexVector(
382 this->CustomWarningMatches, this->WarningMatchRegex);
383 cmCTestBuildHandlerPopulateRegexVector(
384 this->CustomWarningExceptions, this->WarningExceptionRegex);
387 // Determine source and binary tree substitutions to simplify the output.
388 this->SimplifySourceDir = "";
389 this->SimplifyBuildDir = "";
390 if ( this->CTest->GetCTestConfiguration("SourceDirectory").size() > 20 )
392 std::string srcdir
393 = this->CTest->GetCTestConfiguration("SourceDirectory") + "/";
394 std::string srcdirrep;
395 for ( cc = srcdir.size()-2; cc > 0; cc -- )
397 if ( srcdir[cc] == '/' )
399 srcdirrep = srcdir.c_str() + cc;
400 srcdirrep = "/..." + srcdirrep;
401 srcdir = srcdir.substr(0, cc+1);
402 break;
405 this->SimplifySourceDir = srcdir;
407 if ( this->CTest->GetCTestConfiguration("BuildDirectory").size() > 20 )
409 std::string bindir
410 = this->CTest->GetCTestConfiguration("BuildDirectory") + "/";
411 std::string bindirrep;
412 for ( cc = bindir.size()-2; cc > 0; cc -- )
414 if ( bindir[cc] == '/' )
416 bindirrep = bindir.c_str() + cc;
417 bindirrep = "/..." + bindirrep;
418 bindir = bindir.substr(0, cc+1);
419 break;
422 this->SimplifyBuildDir = bindir;
426 // Ok, let's do the build
428 // Remember start build time
429 this->StartBuild = this->CTest->CurrentTime();
430 this->StartBuildTime = cmSystemTools::GetTime();
431 int retVal = 0;
432 int res = cmsysProcess_State_Exited;
433 if ( !this->CTest->GetShowOnly() )
435 res = this->RunMakeCommand(makeCommand.c_str(), &retVal,
436 buildDirectory.c_str(), 0, ofs);
438 else
440 cmCTestLog(this->CTest, DEBUG, "Build with command: " << makeCommand
441 << std::endl);
444 // Remember end build time and calculate elapsed time
445 this->EndBuild = this->CTest->CurrentTime();
446 this->EndBuildTime = cmSystemTools::GetTime();
447 double elapsed_build_time = cmSystemTools::GetTime() - elapsed_time_start;
448 if (res != cmsysProcess_State_Exited || retVal )
450 cmCTestLog(this->CTest, ERROR_MESSAGE, "Error(s) when building project"
451 << std::endl);
454 // Cleanups strings in the errors and warnings list.
455 t_ErrorsAndWarningsVector::iterator evit;
456 if ( !this->SimplifySourceDir.empty() )
458 for ( evit = this->ErrorsAndWarnings.begin();
459 evit != this->ErrorsAndWarnings.end();
460 ++ evit )
462 cmSystemTools::ReplaceString(
463 evit->Text, this->SimplifySourceDir.c_str(), "/.../");
464 cmSystemTools::ReplaceString(
465 evit->PreContext, this->SimplifySourceDir.c_str(), "/.../");
466 cmSystemTools::ReplaceString(
467 evit->PostContext, this->SimplifySourceDir.c_str(), "/.../");
471 if ( !this->SimplifyBuildDir.empty() )
473 for ( evit = this->ErrorsAndWarnings.begin();
474 evit != this->ErrorsAndWarnings.end();
475 ++ evit )
477 cmSystemTools::ReplaceString(
478 evit->Text, this->SimplifyBuildDir.c_str(), "/.../");
479 cmSystemTools::ReplaceString(
480 evit->PreContext, this->SimplifyBuildDir.c_str(), "/.../");
481 cmSystemTools::ReplaceString(
482 evit->PostContext, this->SimplifyBuildDir.c_str(), "/.../");
486 // Generate XML output
487 cmGeneratedFileStream xofs;
488 if(!this->StartResultingXML(cmCTest::PartBuild, "Build", xofs))
490 cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create build XML file"
491 << std::endl);
492 return -1;
494 this->GenerateXMLHeader(xofs);
495 if(this->UseCTestLaunch)
497 this->GenerateXMLLaunched(xofs);
499 else
501 this->GenerateXMLLogScraped(xofs);
503 this->GenerateXMLFooter(xofs, elapsed_build_time);
505 // Display message about number of errors and warnings
506 cmCTestLog(this->CTest, HANDLER_OUTPUT, " " << this->TotalErrors
507 << (this->TotalErrors >= this->MaxErrors ? " or more" : "")
508 << " Compiler errors" << std::endl);
509 cmCTestLog(this->CTest, HANDLER_OUTPUT, " " << this->TotalWarnings
510 << (this->TotalWarnings >= this->MaxWarnings ? " or more" : "")
511 << " Compiler warnings" << std::endl);
513 return retVal;
516 //----------------------------------------------------------------------------
517 void cmCTestBuildHandler::GenerateXMLHeader(std::ostream& os)
519 this->CTest->StartXML(os, this->AppendXML);
520 os << "<Build>\n"
521 << "\t<StartDateTime>" << this->StartBuild << "</StartDateTime>\n"
522 << "\t<StartBuildTime>" <<
523 static_cast<unsigned int>(this->StartBuildTime)
524 << "</StartBuildTime>\n"
525 << "<BuildCommand>"
526 << cmXMLSafe(
527 this->CTest->GetCTestConfiguration("MakeCommand"))
528 << "</BuildCommand>" << std::endl;
531 //----------------------------------------------------------------------------
532 class cmCTestBuildHandler::FragmentCompare
534 public:
535 FragmentCompare(cmFileTimeComparison* ftc): FTC(ftc) {}
536 bool operator()(std::string const& l, std::string const& r)
538 // Order files by modification time. Use lexicographic order
539 // among files with the same time.
540 int result;
541 if(this->FTC->FileTimeCompare(l.c_str(), r.c_str(), &result) &&
542 result != 0)
544 return result < 0;
546 else
548 return l < r;
551 private:
552 cmFileTimeComparison* FTC;
555 //----------------------------------------------------------------------------
556 void cmCTestBuildHandler::GenerateXMLLaunched(std::ostream& os)
558 if(this->CTestLaunchDir.empty())
560 return;
563 // Sort XML fragments in chronological order.
564 cmFileTimeComparison ftc;
565 FragmentCompare fragmentCompare(&ftc);
566 typedef std::set<cmStdString, FragmentCompare> Fragments;
567 Fragments fragments(fragmentCompare);
569 // Identify fragments on disk.
570 cmsys::Directory launchDir;
571 launchDir.Load(this->CTestLaunchDir.c_str());
572 unsigned long n = launchDir.GetNumberOfFiles();
573 for(unsigned long i=0; i < n; ++i)
575 const char* fname = launchDir.GetFile(i);
576 if(this->IsLaunchedErrorFile(fname))
578 fragments.insert(this->CTestLaunchDir + "/" + fname);
579 ++this->TotalErrors;
581 else if(this->IsLaunchedWarningFile(fname))
583 fragments.insert(this->CTestLaunchDir + "/" + fname);
584 ++this->TotalWarnings;
588 // Copy the fragments into the final XML file.
589 for(Fragments::const_iterator fi = fragments.begin();
590 fi != fragments.end(); ++fi)
592 this->GenerateXMLLaunchedFragment(os, fi->c_str());
596 //----------------------------------------------------------------------------
597 void cmCTestBuildHandler::GenerateXMLLogScraped(std::ostream& os)
599 std::vector<cmCTestBuildErrorWarning>& ew = this->ErrorsAndWarnings;
600 std::vector<cmCTestBuildErrorWarning>::iterator it;
602 // only report the first 50 warnings and first 50 errors
603 unsigned short numErrorsAllowed = this->MaxErrors;
604 unsigned short numWarningsAllowed = this->MaxWarnings;
605 std::string srcdir = this->CTest->GetCTestConfiguration("SourceDirectory");
606 // make sure the source dir is in the correct case on windows
607 // via a call to collapse full path.
608 srcdir = cmSystemTools::CollapseFullPath(srcdir.c_str());
609 srcdir += "/";
610 for ( it = ew.begin();
611 it != ew.end() && (numErrorsAllowed || numWarningsAllowed); it++ )
613 cmCTestBuildErrorWarning *cm = &(*it);
614 if (cm->Error && numErrorsAllowed ||
615 !cm->Error && numWarningsAllowed)
617 if (cm->Error)
619 numErrorsAllowed--;
621 else
623 numWarningsAllowed--;
625 os << "\t<" << (cm->Error ? "Error" : "Warning") << ">\n"
626 << "\t\t<BuildLogLine>" << cm->LogLine << "</BuildLogLine>\n"
627 << "\t\t<Text>" << cmXMLSafe(cm->Text).Quotes(false)
628 << "\n</Text>" << std::endl;
629 std::vector<cmCTestCompileErrorWarningRex>::iterator rit;
630 for ( rit = this->ErrorWarningFileLineRegex.begin();
631 rit != this->ErrorWarningFileLineRegex.end(); ++ rit )
633 cmsys::RegularExpression* re = &rit->RegularExpression;
634 if ( re->find(cm->Text.c_str() ) )
636 cm->SourceFile = re->match(rit->FileIndex);
637 // At this point we need to make this->SourceFile relative to
638 // the source root of the project, so cvs links will work
639 cmSystemTools::ConvertToUnixSlashes(cm->SourceFile);
640 if(cm->SourceFile.find("/.../") != cm->SourceFile.npos)
642 cmSystemTools::ReplaceString(cm->SourceFile, "/.../", "");
643 std::string::size_type p = cm->SourceFile.find("/");
644 if(p != cm->SourceFile.npos)
646 cm->SourceFile = cm->SourceFile.substr(
647 p+1, cm->SourceFile.size()-p);
650 else
652 // make sure it is a full path with the correct case
653 cm->SourceFile = cmSystemTools::CollapseFullPath(
654 cm->SourceFile.c_str());
655 cmSystemTools::ReplaceString(
656 cm->SourceFile, srcdir.c_str(), "");
658 cm->LineNumber = atoi(re->match(rit->LineIndex).c_str());
659 break;
662 if ( !cm->SourceFile.empty() && cm->LineNumber >= 0 )
664 if ( cm->SourceFile.size() > 0 )
666 os << "\t\t<SourceFile>" << cm->SourceFile << "</SourceFile>"
667 << std::endl;
669 if ( cm->SourceFileTail.size() > 0 )
671 os << "\t\t<SourceFileTail>" << cm->SourceFileTail
672 << "</SourceFileTail>" << std::endl;
674 if ( cm->LineNumber >= 0 )
676 os << "\t\t<SourceLineNumber>" << cm->LineNumber
677 << "</SourceLineNumber>" << std::endl;
680 os << "\t\t<PreContext>" << cmXMLSafe(cm->PreContext).Quotes(false)
681 << "</PreContext>\n"
682 << "\t\t<PostContext>" << cmXMLSafe(cm->PostContext).Quotes(false);
683 // is this the last warning or error, if so notify
684 if (cm->Error && !numErrorsAllowed ||
685 !cm->Error && !numWarningsAllowed)
687 os << "\nThe maximum number of reported warnings or errors has been "
688 "reached!!!\n";
690 os << "</PostContext>\n"
691 << "\t\t<RepeatCount>0</RepeatCount>\n"
692 << "</" << (cm->Error ? "Error" : "Warning") << ">\n\n"
693 << std::endl;
698 //----------------------------------------------------------------------------
699 void cmCTestBuildHandler::GenerateXMLFooter(std::ostream& os,
700 double elapsed_build_time)
702 os << "\t<Log Encoding=\"base64\" Compression=\"/bin/gzip\">\n\t</Log>\n"
703 << "\t<EndDateTime>" << this->EndBuild << "</EndDateTime>\n"
704 << "\t<EndBuildTime>" << static_cast<unsigned int>(this->EndBuildTime)
705 << "</EndBuildTime>\n"
706 << "<ElapsedMinutes>" << static_cast<int>(elapsed_build_time/6)/10.0
707 << "</ElapsedMinutes>"
708 << "</Build>" << std::endl;
709 this->CTest->EndXML(os);
712 //----------------------------------------------------------------------------
713 void cmCTestBuildHandler::GenerateXMLLaunchedFragment(std::ostream& os,
714 const char* fname)
716 std::ifstream fin(fname, std::ios::in | std::ios::binary);
717 std::string line;
718 while(cmSystemTools::GetLineFromStream(fin, line))
720 os << line << "\n";
724 //----------------------------------------------------------------------------
725 bool cmCTestBuildHandler::IsLaunchedErrorFile(const char* fname)
727 // error-{hash}.xml
728 return (strncmp(fname, "error-", 6) == 0 &&
729 strcmp(fname+strlen(fname)-4, ".xml") == 0);
732 //----------------------------------------------------------------------------
733 bool cmCTestBuildHandler::IsLaunchedWarningFile(const char* fname)
735 // warning-{hash}.xml
736 return (strncmp(fname, "warning-", 8) == 0 &&
737 strcmp(fname+strlen(fname)-4, ".xml") == 0);
740 //######################################################################
741 //######################################################################
742 //######################################################################
743 //######################################################################
745 //----------------------------------------------------------------------------
746 class cmCTestBuildHandler::LaunchHelper
748 public:
749 LaunchHelper(cmCTestBuildHandler* handler);
750 ~LaunchHelper();
751 private:
752 cmCTestBuildHandler* Handler;
753 cmCTest* CTest;
755 void WriteLauncherConfig();
756 void WriteScrapeMatchers(const char* purpose,
757 std::vector<std::string> const& matchers);
760 //----------------------------------------------------------------------------
761 cmCTestBuildHandler::LaunchHelper::LaunchHelper(cmCTestBuildHandler* handler):
762 Handler(handler), CTest(handler->CTest)
764 std::string tag = this->CTest->GetCurrentTag();
765 if(tag.empty())
767 // This is not for a dashboard submission, so there is no XML.
768 // Skip enabling the launchers.
769 this->Handler->UseCTestLaunch = false;
771 else
773 // Compute a directory in which to store launcher fragments.
774 std::string& launchDir = this->Handler->CTestLaunchDir;
775 launchDir = this->CTest->GetBinaryDir();
776 launchDir += "/Testing/";
777 launchDir += tag;
778 launchDir += "/Build";
780 // Clean out any existing launcher fragments.
781 cmSystemTools::RemoveADirectory(launchDir.c_str());
783 if(this->Handler->UseCTestLaunch)
785 // Enable launcher fragments.
786 cmSystemTools::MakeDirectory(launchDir.c_str());
787 this->WriteLauncherConfig();
788 std::string launchEnv = "CTEST_LAUNCH_LOGS=";
789 launchEnv += launchDir;
790 cmSystemTools::PutEnv(launchEnv.c_str());
794 // If not using launchers, make sure they passthru.
795 if(!this->Handler->UseCTestLaunch)
797 cmSystemTools::UnsetEnv("CTEST_LAUNCH_LOGS");
801 //----------------------------------------------------------------------------
802 cmCTestBuildHandler::LaunchHelper::~LaunchHelper()
804 if(this->Handler->UseCTestLaunch)
806 cmSystemTools::UnsetEnv("CTEST_LAUNCH_LOGS");
810 //----------------------------------------------------------------------------
811 void cmCTestBuildHandler::LaunchHelper::WriteLauncherConfig()
813 this->WriteScrapeMatchers("Warning",
814 this->Handler->ReallyCustomWarningMatches);
815 this->WriteScrapeMatchers("WarningSuppress",
816 this->Handler->ReallyCustomWarningExceptions);
818 // Give some testing configuration information to the launcher.
819 std::string fname = this->Handler->CTestLaunchDir;
820 fname += "/CTestLaunchConfig.cmake";
821 cmGeneratedFileStream fout(fname.c_str());
822 std::string srcdir = this->CTest->GetCTestConfiguration("SourceDirectory");
823 fout << "set(CTEST_SOURCE_DIRECTORY \"" << srcdir << "\")\n";
826 //----------------------------------------------------------------------------
827 void
828 cmCTestBuildHandler::LaunchHelper
829 ::WriteScrapeMatchers(const char* purpose,
830 std::vector<std::string> const& matchers)
832 if(matchers.empty())
834 return;
836 std::string fname = this->Handler->CTestLaunchDir;
837 fname += "/Custom";
838 fname += purpose;
839 fname += ".txt";
840 cmGeneratedFileStream fout(fname.c_str());
841 for(std::vector<std::string>::const_iterator mi = matchers.begin();
842 mi != matchers.end(); ++mi)
844 fout << *mi << "\n";
848 //----------------------------------------------------------------------
849 int cmCTestBuildHandler::RunMakeCommand(const char* command,
850 int* retVal, const char* dir, int timeout, std::ofstream& ofs)
852 // First generate the command and arguments
853 std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
855 if(args.size() < 1)
857 return false;
860 std::vector<const char*> argv;
861 for(std::vector<cmStdString>::const_iterator a = args.begin();
862 a != args.end(); ++a)
864 argv.push_back(a->c_str());
866 argv.push_back(0);
868 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run command:");
869 std::vector<const char*>::iterator ait;
870 for ( ait = argv.begin(); ait != argv.end() && *ait; ++ ait )
872 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " \"" << *ait << "\"");
874 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl);
876 // Optionally use make rule launchers to record errors and warnings.
877 LaunchHelper launchHelper(this);
878 static_cast<void>(launchHelper);
880 // Now create process object
881 cmsysProcess* cp = cmsysProcess_New();
882 cmsysProcess_SetCommand(cp, &*argv.begin());
883 cmsysProcess_SetWorkingDirectory(cp, dir);
884 cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
885 cmsysProcess_SetTimeout(cp, timeout);
886 cmsysProcess_Execute(cp);
888 // Initialize tick's
889 std::string::size_type tick = 0;
890 const std::string::size_type tick_len = 1024;
892 char* data;
893 int length;
894 cmCTestLog(this->CTest, HANDLER_OUTPUT,
895 " Each symbol represents " << tick_len << " bytes of output."
896 << std::endl
897 << (this->UseCTestLaunch? "" :
898 " '!' represents an error and '*' a warning.\n")
899 << " " << std::flush);
901 // Initialize building structures
902 this->BuildProcessingQueue.clear();
903 this->OutputLineCounter = 0;
904 this->ErrorsAndWarnings.clear();
905 this->TotalErrors = 0;
906 this->TotalWarnings = 0;
907 this->BuildOutputLogSize = 0;
908 this->LastTickChar = '.';
909 this->WarningQuotaReached = false;
910 this->ErrorQuotaReached = false;
912 // For every chunk of data
913 int res;
914 while((res = cmsysProcess_WaitForData(cp, &data, &length, 0)))
916 // Replace '\0' with '\n', since '\0' does not really make sense. This is
917 // for Visual Studio output
918 for(int cc =0; cc < length; ++cc)
920 if(data[cc] == 0)
922 data[cc] = '\n';
926 // Process the chunk of data
927 if ( res == cmsysProcess_Pipe_STDERR )
929 this->ProcessBuffer(data, length, tick, tick_len, ofs,
930 &this->BuildProcessingErrorQueue);
932 else
934 this->ProcessBuffer(data, length, tick, tick_len, ofs,
935 &this->BuildProcessingQueue);
939 this->ProcessBuffer(0, 0, tick, tick_len, ofs, &this->BuildProcessingQueue);
940 this->ProcessBuffer(0, 0, tick, tick_len, ofs,
941 &this->BuildProcessingErrorQueue);
942 cmCTestLog(this->CTest, OUTPUT, " Size of output: "
943 << int(this->BuildOutputLogSize / 1024.0) << "K" << std::endl);
945 // Properly handle output of the build command
946 cmsysProcess_WaitForExit(cp, 0);
947 int result = cmsysProcess_GetState(cp);
949 if(result == cmsysProcess_State_Exited)
951 if (retVal)
953 *retVal = cmsysProcess_GetExitValue(cp);
954 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
955 "Command exited with the value: " << *retVal << std::endl);
956 // if a non zero return value
957 if (*retVal)
959 // If there was an error running command, report that on the
960 // dashboard.
961 cmCTestBuildErrorWarning errorwarning;
962 errorwarning.LogLine = 1;
963 errorwarning.Text
964 = "*** WARNING non-zero return value in ctest from: ";
965 errorwarning.Text += argv[0];
966 errorwarning.PreContext = "";
967 errorwarning.PostContext = "";
968 errorwarning.Error = false;
969 this->ErrorsAndWarnings.push_back(errorwarning);
970 this->TotalWarnings ++;
974 else if(result == cmsysProcess_State_Exception)
976 if (retVal)
978 *retVal = cmsysProcess_GetExitException(cp);
979 cmCTestLog(this->CTest, WARNING, "There was an exception: " << *retVal
980 << std::endl);
983 else if(result == cmsysProcess_State_Expired)
985 cmCTestLog(this->CTest, WARNING, "There was a timeout" << std::endl);
987 else if(result == cmsysProcess_State_Error)
989 // If there was an error running command, report that on the dashboard.
990 cmCTestBuildErrorWarning errorwarning;
991 errorwarning.LogLine = 1;
992 errorwarning.Text = "*** ERROR executing: ";
993 errorwarning.Text += cmsysProcess_GetErrorString(cp);
994 errorwarning.PreContext = "";
995 errorwarning.PostContext = "";
996 errorwarning.Error = true;
997 this->ErrorsAndWarnings.push_back(errorwarning);
998 this->TotalErrors ++;
999 cmCTestLog(this->CTest, ERROR_MESSAGE, "There was an error: "
1000 << cmsysProcess_GetErrorString(cp) << std::endl);
1003 cmsysProcess_Delete(cp);
1004 return result;
1007 //######################################################################
1008 //######################################################################
1009 //######################################################################
1010 //######################################################################
1012 //----------------------------------------------------------------------
1013 void cmCTestBuildHandler::ProcessBuffer(const char* data, int length,
1014 size_t& tick, size_t tick_len, std::ofstream& ofs,
1015 t_BuildProcessingQueueType* queue)
1017 const std::string::size_type tick_line_len = 50;
1018 const char* ptr;
1019 for ( ptr = data; ptr < data+length; ptr ++ )
1021 queue->push_back(*ptr);
1023 this->BuildOutputLogSize += length;
1025 // until there are any lines left in the buffer
1026 while ( 1 )
1028 // Find the end of line
1029 t_BuildProcessingQueueType::iterator it;
1030 for ( it = queue->begin();
1031 it != queue->end();
1032 ++ it )
1034 if ( *it == '\n' )
1036 break;
1040 // Once certain number of errors or warnings reached, ignore future errors
1041 // or warnings.
1042 if ( this->TotalWarnings >= this->MaxWarnings )
1044 this->WarningQuotaReached = true;
1046 if ( this->TotalErrors >= this->MaxErrors )
1048 this->ErrorQuotaReached = true;
1051 // If the end of line was found
1052 if ( it != queue->end() )
1054 // Create a contiguous array for the line
1055 this->CurrentProcessingLine.clear();
1056 t_BuildProcessingQueueType::iterator cit;
1057 for ( cit = queue->begin(); cit != it; ++cit )
1059 this->CurrentProcessingLine.push_back(*cit);
1061 this->CurrentProcessingLine.push_back(0);
1062 const char* line = &*this->CurrentProcessingLine.begin();
1064 // Process the line
1065 int lineType = this->ProcessSingleLine(line);
1067 // Erase the line from the queue
1068 queue->erase(queue->begin(), it+1);
1070 // Depending on the line type, produce error or warning, or nothing
1071 cmCTestBuildErrorWarning errorwarning;
1072 bool found = false;
1073 switch ( lineType )
1075 case b_WARNING_LINE:
1076 this->LastTickChar = '*';
1077 errorwarning.Error = false;
1078 found = true;
1079 this->TotalWarnings ++;
1080 break;
1081 case b_ERROR_LINE:
1082 this->LastTickChar = '!';
1083 errorwarning.Error = true;
1084 found = true;
1085 this->TotalErrors ++;
1086 break;
1088 if ( found )
1090 // This is an error or warning, so generate report
1091 errorwarning.LogLine = static_cast<int>(this->OutputLineCounter+1);
1092 errorwarning.Text = line;
1093 errorwarning.PreContext = "";
1094 errorwarning.PostContext = "";
1096 // Copy pre-context to report
1097 std::deque<cmStdString>::iterator pcit;
1098 for ( pcit = this->PreContext.begin();
1099 pcit != this->PreContext.end();
1100 ++pcit )
1102 errorwarning.PreContext += *pcit + "\n";
1104 this->PreContext.clear();
1106 // Store report
1107 this->ErrorsAndWarnings.push_back(errorwarning);
1108 this->LastErrorOrWarning = this->ErrorsAndWarnings.end()-1;
1109 this->PostContextCount = 0;
1111 else
1113 // This is not an error or warning.
1114 // So, figure out if this is a post-context line
1115 if ( this->ErrorsAndWarnings.size() &&
1116 this->LastErrorOrWarning != this->ErrorsAndWarnings.end() &&
1117 this->PostContextCount < this->MaxPostContext )
1119 this->PostContextCount ++;
1120 this->LastErrorOrWarning->PostContext += line;
1121 if ( this->PostContextCount < this->MaxPostContext )
1123 this->LastErrorOrWarning->PostContext += "\n";
1126 else
1128 // Otherwise store pre-context for the next error
1129 this->PreContext.push_back(line);
1130 if ( this->PreContext.size() > this->MaxPreContext )
1132 this->PreContext.erase(this->PreContext.begin(),
1133 this->PreContext.end()-this->MaxPreContext);
1137 this->OutputLineCounter ++;
1139 else
1141 break;
1145 // Now that the buffer is processed, display missing ticks
1146 int tickDisplayed = false;
1147 while ( this->BuildOutputLogSize > (tick * tick_len) )
1149 tick ++;
1150 cmCTestLog(this->CTest, HANDLER_OUTPUT, this->LastTickChar);
1151 tickDisplayed = true;
1152 if ( tick % tick_line_len == 0 && tick > 0 )
1154 cmCTestLog(this->CTest, HANDLER_OUTPUT, " Size: "
1155 << int((this->BuildOutputLogSize / 1024.0) + 1) << "K" << std::endl
1156 << " ");
1159 if ( tickDisplayed )
1161 this->LastTickChar = '.';
1164 // And if this is verbose output, display the content of the chunk
1165 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
1166 cmCTestLogWrite(data, length));
1168 // Always store the chunk to the file
1169 ofs << cmCTestLogWrite(data, length);
1172 //----------------------------------------------------------------------
1173 int cmCTestBuildHandler::ProcessSingleLine(const char* data)
1175 if(this->UseCTestLaunch)
1177 // No log scraping when using launchers.
1178 return b_REGULAR_LINE;
1181 cmCTestLog(this->CTest, DEBUG, "Line: [" << data << "]" << std::endl);
1183 std::vector<cmsys::RegularExpression>::iterator it;
1185 int warningLine = 0;
1186 int errorLine = 0;
1188 // Check for regular expressions
1190 if ( !this->ErrorQuotaReached )
1192 // Errors
1193 int wrxCnt = 0;
1194 for ( it = this->ErrorMatchRegex.begin();
1195 it != this->ErrorMatchRegex.end();
1196 ++ it )
1198 if ( it->find(data) )
1200 errorLine = 1;
1201 cmCTestLog(this->CTest, DEBUG, " Error Line: " << data
1202 << " (matches: " << this->CustomErrorMatches[wrxCnt] << ")"
1203 << std::endl);
1204 break;
1206 wrxCnt ++;
1208 // Error exceptions
1209 wrxCnt = 0;
1210 for ( it = this->ErrorExceptionRegex.begin();
1211 it != this->ErrorExceptionRegex.end();
1212 ++ it )
1214 if ( it->find(data) )
1216 errorLine = 0;
1217 cmCTestLog(this->CTest, DEBUG, " Not an error Line: " << data
1218 << " (matches: " << this->CustomErrorExceptions[wrxCnt] << ")"
1219 << std::endl);
1220 break;
1222 wrxCnt ++;
1225 if ( !this->WarningQuotaReached )
1227 // Warnings
1228 int wrxCnt = 0;
1229 for ( it = this->WarningMatchRegex.begin();
1230 it != this->WarningMatchRegex.end();
1231 ++ it )
1233 if ( it->find(data) )
1235 warningLine = 1;
1236 cmCTestLog(this->CTest, DEBUG,
1237 " Warning Line: " << data
1238 << " (matches: " << this->CustomWarningMatches[wrxCnt] << ")"
1239 << std::endl);
1240 break;
1242 wrxCnt ++;
1245 wrxCnt = 0;
1246 // Warning exceptions
1247 for ( it = this->WarningExceptionRegex.begin();
1248 it != this->WarningExceptionRegex.end();
1249 ++ it )
1251 if ( it->find(data) )
1253 warningLine = 0;
1254 cmCTestLog(this->CTest, DEBUG, " Not a warning Line: " << data
1255 << " (matches: " << this->CustomWarningExceptions[wrxCnt] << ")"
1256 << std::endl);
1257 break;
1259 wrxCnt ++;
1262 if ( errorLine )
1264 return b_ERROR_LINE;
1266 if ( warningLine )
1268 return b_WARNING_LINE;
1270 return b_REGULAR_LINE;