Resync (forgot to add new files?)
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / CTest / cmCTestBuildHandler.cxx
blob937356d091722350b3e7fbe5722be53587bbe190
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCTestBuildHandler.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/30 16:17:36 $
7 Version: $Revision: 1.61 $
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"
27 //#include <cmsys/RegularExpression.hxx>
28 #include <cmsys/Process.h>
30 // used for sleep
31 #ifdef _WIN32
32 #include "windows.h"
33 #endif
35 #include <stdlib.h>
36 #include <time.h>
37 #include <math.h>
38 #include <float.h>
40 #if defined(__BORLANDC__)
41 # pragma warn -8060 /* possibly incorrect assignment */
42 #endif
44 static const char* cmCTestErrorMatches[] = {
45 "^[Bb]us [Ee]rror",
46 "^[Ss]egmentation [Vv]iolation",
47 "^[Ss]egmentation [Ff]ault",
48 "([^ :]+):([0-9]+): ([^ \\t])",
49 "([^:]+): error[ \\t]*[0-9]+[ \\t]*:",
50 "^Error ([0-9]+):",
51 "^Fatal",
52 "^Error: ",
53 "^Error ",
54 "[0-9] ERROR: ",
55 "^\"[^\"]+\", line [0-9]+: [^Ww]",
56 "^cc[^C]*CC: ERROR File = ([^,]+), Line = ([0-9]+)",
57 "^ld([^:])*:([ \\t])*ERROR([^:])*:",
58 "^ild:([ \\t])*\\(undefined symbol\\)",
59 "([^ :]+) : (error|fatal error|catastrophic error)",
60 "([^:]+): (Error:|error|undefined reference|multiply defined)",
61 "([^:]+)\\(([^\\)]+)\\) : (error|fatal error|catastrophic error)",
62 "^fatal error C[0-9]+:",
63 ": syntax error ",
64 "^collect2: ld returned 1 exit status",
65 "Unsatisfied symbols:",
66 "^Unresolved:",
67 "Undefined symbols:",
68 "^Undefined[ \\t]+first referenced",
69 "^CMake Error:",
70 ":[ \\t]cannot find",
71 ":[ \\t]can't find",
72 ": \\*\\*\\* No rule to make target \\`.*\\'. Stop",
73 ": \\*\\*\\* No targets specified and no makefile found",
74 ": Invalid loader fixup for symbol",
75 ": Invalid fixups exist",
76 ": Can't find library for",
77 ": internal link edit command failed",
78 ": Unrecognized option \\`.*\\'",
79 "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\([^WI]\\)",
80 "ld: 0706-006 Cannot find or open library file: -l ",
81 "ild: \\(argument error\\) can't find library argument ::",
82 "^could not be found and will not be loaded.",
83 "s:616 string too big",
84 "make: Fatal error: ",
85 "ld: 0711-993 Error occurred while writing to the output file:",
86 "ld: fatal: ",
87 "final link failed:",
88 "make: \\*\\*\\*.*Error",
89 "make\\[.*\\]: \\*\\*\\*.*Error",
90 "\\*\\*\\* Error code",
91 "nternal error:",
92 "Makefile:[0-9]+: \\*\\*\\* .* Stop\\.",
93 ": No such file or directory",
94 ": Invalid argument",
95 "^The project cannot be built\\.",
99 static const char* cmCTestErrorExceptions[] = {
100 "instantiated from ",
101 "candidates are:",
102 ": warning",
103 ": \\(Warning\\)",
104 ": note",
105 "makefile:",
106 "Makefile:",
107 ":[ \\t]+Where:",
108 "([^ :]+):([0-9]+): Warning",
109 "------ Build started: .* ------",
113 static const char* cmCTestWarningMatches[] = {
114 "([^ :]+):([0-9]+): warning:",
115 "([^ :]+):([0-9]+): note:",
116 "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
117 "^ld([^:])*:([ \\t])*WARNING([^:])*:",
118 "([^:]+): warning ([0-9]+):",
119 "^\"[^\"]+\", line [0-9]+: [Ww](arning|arnung)",
120 "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
121 "^(Warning|Warnung) ([0-9]+):",
122 "^(Warning|Warnung) ",
123 "WARNING: ",
124 "([^ :]+) : warning",
125 "([^:]+): warning",
126 "\", line [0-9]+\\.[0-9]+: [0-9]+-[0-9]+ \\([WI]\\)",
127 "^cxx: Warning:",
128 ".*file: .* has no symbols",
129 "([^ :]+):([0-9]+): (Warning|Warnung)",
130 "\\([0-9]*\\): remark #[0-9]*",
131 "\".*\", line [0-9]+: remark\\([0-9]*\\):",
132 "cc-[0-9]* CC: REMARK File = .*, Line = [0-9]*",
136 static const char* cmCTestWarningExceptions[] = {
137 "/usr/.*/X11/Xlib\\.h:[0-9]+: war.*: ANSI C\\+\\+ forbids declaration",
138 "/usr/.*/X11/Xutil\\.h:[0-9]+: war.*: ANSI C\\+\\+ forbids declaration",
139 "/usr/.*/X11/XResource\\.h:[0-9]+: war.*: ANSI C\\+\\+ forbids declaration",
140 "WARNING 84 :",
141 "WARNING 47 :",
142 "makefile:",
143 "Makefile:",
144 "warning: Clock skew detected. Your build may be incomplete.",
145 "/usr/openwin/include/GL/[^:]+:",
146 "bind_at_load",
147 "XrmQGetResource",
148 "IceFlush",
149 "warning LNK4089: all references to [^ \\t]+ discarded by .OPT:REF",
150 "ld32: WARNING 85: definition of dataKey in",
151 "cc: warning 422: Unknown option \"\\+b",
152 "_with_warning_C",
156 struct cmCTestBuildCompileErrorWarningRex
158 const char* RegularExpressionString;
159 int FileIndex;
160 int LineIndex;
163 static cmCTestBuildCompileErrorWarningRex
164 cmCTestWarningErrorFileLine[] = {
165 { "^Warning W[0-9]+ ([a-zA-Z.\\:/0-9_+ ~-]+) ([0-9]+):", 1, 2 },
166 { "^([a-zA-Z./0-9_+ ~-]+):([0-9]+):", 1, 2 },
167 { "^([a-zA-Z.\\:/0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
168 { "^([a-zA-Z./0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
169 { "\"([a-zA-Z./0-9_+ ~-]+)\", line ([0-9]+)", 1, 2 },
170 { "File = ([a-zA-Z./0-9_+ ~-]+), Line = ([0-9]+)", 1, 2 },
171 { 0, 0, 0 }
174 //----------------------------------------------------------------------
175 cmCTestBuildHandler::cmCTestBuildHandler()
177 this->MaxPreContext = 6;
178 this->MaxPostContext = 6;
180 this->MaxErrors = 50;
181 this->MaxWarnings = 50;
183 this->LastErrorOrWarning = this->ErrorsAndWarnings.end();
187 //----------------------------------------------------------------------
188 void cmCTestBuildHandler::Initialize()
190 this->Superclass::Initialize();
191 this->StartBuild = "";
192 this->EndBuild = "";
193 this->CustomErrorMatches.clear();
194 this->CustomErrorExceptions.clear();
195 this->CustomWarningMatches.clear();
196 this->CustomWarningExceptions.clear();
197 this->ErrorWarningFileLineRegex.clear();
199 this->ErrorMatchRegex.clear();
200 this->ErrorExceptionRegex.clear();
201 this->WarningMatchRegex.clear();
202 this->WarningExceptionRegex.clear();
203 this->BuildProcessingQueue.clear();
204 this->BuildProcessingErrorQueue.clear();
205 this->BuildOutputLogSize = 0;
206 this->CurrentProcessingLine.clear();
208 this->SimplifySourceDir = "";
209 this->SimplifyBuildDir = "";
210 this->OutputLineCounter = 0;
211 this->ErrorsAndWarnings.clear();
212 this->LastErrorOrWarning = this->ErrorsAndWarnings.end();
213 this->PostContextCount = 0;
214 this->MaxPreContext = 6;
215 this->MaxPostContext = 6;
216 this->PreContext.clear();
218 this->TotalErrors = 0;
219 this->TotalWarnings = 0;
220 this->LastTickChar = 0;
222 this->ErrorQuotaReached = false;
223 this->WarningQuotaReached = false;
225 this->MaxErrors = 50;
226 this->MaxWarnings = 50;
229 //----------------------------------------------------------------------
230 void cmCTestBuildHandler::PopulateCustomVectors(cmMakefile *mf)
232 this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_MATCH",
233 this->CustomErrorMatches);
234 this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_EXCEPTION",
235 this->CustomErrorExceptions);
236 this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_MATCH",
237 this->CustomWarningMatches);
238 this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_EXCEPTION",
239 this->CustomWarningExceptions);
240 this->CTest->PopulateCustomInteger(mf,
241 "CTEST_CUSTOM_MAXIMUM_NUMBER_OF_ERRORS",
242 this->MaxErrors);
243 this->CTest->PopulateCustomInteger(mf,
244 "CTEST_CUSTOM_MAXIMUM_NUMBER_OF_WARNINGS",
245 this->MaxWarnings);
248 //----------------------------------------------------------------------
249 //clearly it would be nice if this were broken up into a few smaller
250 //functions and commented...
251 int cmCTestBuildHandler::ProcessHandler()
253 cmCTestLog(this->CTest, HANDLER_OUTPUT, "Build project" << std::endl);
255 // do we have time for this
256 if (this->CTest->GetRemainingTimeAllowed() < 120)
258 return 0;
261 int entry;
262 for ( entry = 0;
263 cmCTestWarningErrorFileLine[entry].RegularExpressionString;
264 ++ entry )
266 cmCTestBuildHandler::cmCTestCompileErrorWarningRex r;
267 if ( r.RegularExpression.compile(
268 cmCTestWarningErrorFileLine[entry].RegularExpressionString) )
270 r.FileIndex = cmCTestWarningErrorFileLine[entry].FileIndex;
271 r.LineIndex = cmCTestWarningErrorFileLine[entry].LineIndex;
272 this->ErrorWarningFileLineRegex.push_back(r);
274 else
276 cmCTestLog(this->CTest, ERROR_MESSAGE,
277 "Problem Compiling regular expression: "
278 << cmCTestWarningErrorFileLine[entry].RegularExpressionString
279 << std::endl);
283 // Determine build command and build directory
284 const std::string &makeCommand
285 = this->CTest->GetCTestConfiguration("MakeCommand");
286 cmCTestLog(this->CTest,
287 HANDLER_VERBOSE_OUTPUT, "MakeCommand:" << makeCommand <<
288 "\n");
289 if ( makeCommand.size() == 0 )
291 cmCTestLog(this->CTest, ERROR_MESSAGE,
292 "Cannot find MakeCommand key in the DartConfiguration.tcl"
293 << std::endl);
294 return -1;
296 const std::string &buildDirectory
297 = this->CTest->GetCTestConfiguration("BuildDirectory");
298 if ( buildDirectory.size() == 0 )
300 cmCTestLog(this->CTest, ERROR_MESSAGE,
301 "Cannot find BuildDirectory key in the DartConfiguration.tcl"
302 << std::endl);
303 return -1;
306 // Create a last build log
307 cmGeneratedFileStream ofs;
308 double elapsed_time_start = cmSystemTools::GetTime();
309 if ( !this->StartLogFile("Build", ofs) )
311 cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create build log file"
312 << std::endl);
315 // Create lists of regular expression strings for errors, error exceptions,
316 // warnings and warning exceptions.
317 std::vector<cmStdString>::size_type cc;
318 for ( cc = 0; cmCTestErrorMatches[cc]; cc ++ )
320 this->CustomErrorMatches.push_back(cmCTestErrorMatches[cc]);
322 for ( cc = 0; cmCTestErrorExceptions[cc]; cc ++ )
324 this->CustomErrorExceptions.push_back(cmCTestErrorExceptions[cc]);
326 for ( cc = 0; cmCTestWarningMatches[cc]; cc ++ )
328 this->CustomWarningMatches.push_back(cmCTestWarningMatches[cc]);
331 for ( cc = 0; cmCTestWarningExceptions[cc]; cc ++ )
333 this->CustomWarningExceptions.push_back(cmCTestWarningExceptions[cc]);
336 // Pre-compile regular expressions objects for all regular expressions
337 std::vector<cmStdString>::iterator it;
339 #define cmCTestBuildHandlerPopulateRegexVector(strings, regexes) \
340 regexes.clear(); \
341 cmCTestLog(this->CTest, DEBUG, this << "Add " #regexes \
342 << std::endl); \
343 for ( it = strings.begin(); it != strings.end(); ++it ) \
345 cmCTestLog(this->CTest, DEBUG, "Add " #strings ": " \
346 << it->c_str() << std::endl); \
347 regexes.push_back(it->c_str()); \
349 cmCTestBuildHandlerPopulateRegexVector(
350 this->CustomErrorMatches, this->ErrorMatchRegex);
351 cmCTestBuildHandlerPopulateRegexVector(
352 this->CustomErrorExceptions, this->ErrorExceptionRegex);
353 cmCTestBuildHandlerPopulateRegexVector(
354 this->CustomWarningMatches, this->WarningMatchRegex);
355 cmCTestBuildHandlerPopulateRegexVector(
356 this->CustomWarningExceptions, this->WarningExceptionRegex);
359 // Determine source and binary tree substitutions to simplify the output.
360 this->SimplifySourceDir = "";
361 this->SimplifyBuildDir = "";
362 if ( this->CTest->GetCTestConfiguration("SourceDirectory").size() > 20 )
364 std::string srcdir
365 = this->CTest->GetCTestConfiguration("SourceDirectory") + "/";
366 std::string srcdirrep;
367 for ( cc = srcdir.size()-2; cc > 0; cc -- )
369 if ( srcdir[cc] == '/' )
371 srcdirrep = srcdir.c_str() + cc;
372 srcdirrep = "/..." + srcdirrep;
373 srcdir = srcdir.substr(0, cc+1);
374 break;
377 this->SimplifySourceDir = srcdir;
379 if ( this->CTest->GetCTestConfiguration("BuildDirectory").size() > 20 )
381 std::string bindir
382 = this->CTest->GetCTestConfiguration("BuildDirectory") + "/";
383 std::string bindirrep;
384 for ( cc = bindir.size()-2; cc > 0; cc -- )
386 if ( bindir[cc] == '/' )
388 bindirrep = bindir.c_str() + cc;
389 bindirrep = "/..." + bindirrep;
390 bindir = bindir.substr(0, cc+1);
391 break;
394 this->SimplifyBuildDir = bindir;
398 // Ok, let's do the build
400 // Remember start build time
401 this->StartBuild = this->CTest->CurrentTime();
402 this->StartBuildTime = cmSystemTools::GetTime();
403 int retVal = 0;
404 int res = cmsysProcess_State_Exited;
405 if ( !this->CTest->GetShowOnly() )
407 res = this->RunMakeCommand(makeCommand.c_str(), &retVal,
408 buildDirectory.c_str(), 0, ofs);
410 else
412 cmCTestLog(this->CTest, DEBUG, "Build with command: " << makeCommand
413 << std::endl);
416 // Remember end build time and calculate elapsed time
417 this->EndBuild = this->CTest->CurrentTime();
418 this->EndBuildTime = cmSystemTools::GetTime();
419 double elapsed_build_time = cmSystemTools::GetTime() - elapsed_time_start;
420 if (res != cmsysProcess_State_Exited || retVal )
422 cmCTestLog(this->CTest, ERROR_MESSAGE, "Error(s) when building project"
423 << std::endl);
426 // Cleanups strings in the errors and warnings list.
427 t_ErrorsAndWarningsVector::iterator evit;
428 if ( !this->SimplifySourceDir.empty() )
430 for ( evit = this->ErrorsAndWarnings.begin();
431 evit != this->ErrorsAndWarnings.end();
432 ++ evit )
434 cmSystemTools::ReplaceString(
435 evit->Text, this->SimplifySourceDir.c_str(), "/.../");
436 cmSystemTools::ReplaceString(
437 evit->PreContext, this->SimplifySourceDir.c_str(), "/.../");
438 cmSystemTools::ReplaceString(
439 evit->PostContext, this->SimplifySourceDir.c_str(), "/.../");
443 if ( !this->SimplifyBuildDir.empty() )
445 for ( evit = this->ErrorsAndWarnings.begin();
446 evit != this->ErrorsAndWarnings.end();
447 ++ evit )
449 cmSystemTools::ReplaceString(
450 evit->Text, this->SimplifyBuildDir.c_str(), "/.../");
451 cmSystemTools::ReplaceString(
452 evit->PreContext, this->SimplifyBuildDir.c_str(), "/.../");
453 cmSystemTools::ReplaceString(
454 evit->PostContext, this->SimplifyBuildDir.c_str(), "/.../");
458 // Display message about number of errors and warnings
459 cmCTestLog(this->CTest, HANDLER_OUTPUT, " " << this->TotalErrors
460 << (this->TotalErrors >= this->MaxErrors ? " or more" : "")
461 << " Compiler errors" << std::endl);
462 cmCTestLog(this->CTest, HANDLER_OUTPUT, " " << this->TotalWarnings
463 << (this->TotalWarnings >= this->MaxWarnings ? " or more" : "")
464 << " Compiler warnings" << std::endl);
466 // Generate XML output
467 cmGeneratedFileStream xofs;
468 if( !this->StartResultingXML("Build", xofs))
470 cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot create build XML file"
471 << std::endl);
472 return -1;
474 this->GenerateDartBuildOutput(
475 xofs, this->ErrorsAndWarnings, elapsed_build_time);
476 return 0;
479 //----------------------------------------------------------------------
480 void cmCTestBuildHandler::GenerateDartBuildOutput(
481 std::ostream& os,
482 std::vector<cmCTestBuildErrorWarning> ew,
483 double elapsed_build_time)
485 this->CTest->StartXML(os);
486 os << "<Build>\n"
487 << "\t<StartDateTime>" << this->StartBuild << "</StartDateTime>\n"
488 << "\t<StartBuildTime>" <<
489 static_cast<unsigned int>(this->StartBuildTime)
490 << "</StartBuildTime>\n"
491 << "<BuildCommand>"
492 << this->CTest->MakeXMLSafe(
493 this->CTest->GetCTestConfiguration("MakeCommand"))
494 << "</BuildCommand>" << std::endl;
496 std::vector<cmCTestBuildErrorWarning>::iterator it;
498 // only report the first 50 warnings and first 50 errors
499 unsigned short numErrorsAllowed = this->MaxErrors;
500 unsigned short numWarningsAllowed = this->MaxWarnings;
501 std::string srcdir = this->CTest->GetCTestConfiguration("SourceDirectory");
502 // make sure the source dir is in the correct case on windows
503 // via a call to collapse full path.
504 srcdir = cmSystemTools::CollapseFullPath(srcdir.c_str());
505 srcdir += "/";
506 for ( it = ew.begin();
507 it != ew.end() && (numErrorsAllowed || numWarningsAllowed); it++ )
509 cmCTestBuildErrorWarning *cm = &(*it);
510 if (cm->Error && numErrorsAllowed ||
511 !cm->Error && numWarningsAllowed)
513 if (cm->Error)
515 numErrorsAllowed--;
517 else
519 numWarningsAllowed--;
521 os << "\t<" << (cm->Error ? "Error" : "Warning") << ">\n"
522 << "\t\t<BuildLogLine>" << cm->LogLine << "</BuildLogLine>\n"
523 << "\t\t<Text>" << this->CTest->MakeXMLSafe(cm->Text)
524 << "\n</Text>" << std::endl;
525 std::vector<cmCTestCompileErrorWarningRex>::iterator rit;
526 for ( rit = this->ErrorWarningFileLineRegex.begin();
527 rit != this->ErrorWarningFileLineRegex.end(); ++ rit )
529 cmsys::RegularExpression* re = &rit->RegularExpression;
530 if ( re->find(cm->Text.c_str() ) )
532 cm->SourceFile = re->match(rit->FileIndex);
533 // At this point we need to make this->SourceFile relative to
534 // the source root of the project, so cvs links will work
535 cmSystemTools::ConvertToUnixSlashes(cm->SourceFile);
536 if(cm->SourceFile.find("/.../") != cm->SourceFile.npos)
538 cmSystemTools::ReplaceString(cm->SourceFile, "/.../", "");
539 std::string::size_type p = cm->SourceFile.find("/");
540 if(p != cm->SourceFile.npos)
542 cm->SourceFile = cm->SourceFile.substr(
543 p+1, cm->SourceFile.size()-p);
546 else
548 // make sure it is a full path with the correct case
549 cm->SourceFile = cmSystemTools::CollapseFullPath(
550 cm->SourceFile.c_str());
551 cmSystemTools::ReplaceString(
552 cm->SourceFile, srcdir.c_str(), "");
554 cm->LineNumber = atoi(re->match(rit->LineIndex).c_str());
555 break;
558 if ( !cm->SourceFile.empty() && cm->LineNumber >= 0 )
560 if ( cm->SourceFile.size() > 0 )
562 os << "\t\t<SourceFile>" << cm->SourceFile << "</SourceFile>"
563 << std::endl;
565 if ( cm->SourceFileTail.size() > 0 )
567 os << "\t\t<SourceFileTail>" << cm->SourceFileTail
568 << "</SourceFileTail>" << std::endl;
570 if ( cm->LineNumber >= 0 )
572 os << "\t\t<SourceLineNumber>" << cm->LineNumber
573 << "</SourceLineNumber>" << std::endl;
576 os << "\t\t<PreContext>" << this->CTest->MakeXMLSafe(cm->PreContext)
577 << "</PreContext>\n"
578 << "\t\t<PostContext>" << this->CTest->MakeXMLSafe(cm->PostContext);
579 // is this the last warning or error, if so notify
580 if (cm->Error && !numErrorsAllowed ||
581 !cm->Error && !numWarningsAllowed)
583 os << "\nThe maximum number of reported warnings or errors has been "
584 "reached!!!\n";
586 os << "</PostContext>\n"
587 << "\t\t<RepeatCount>0</RepeatCount>\n"
588 << "</" << (cm->Error ? "Error" : "Warning") << ">\n\n"
589 << std::endl;
592 os << "\t<Log Encoding=\"base64\" Compression=\"/bin/gzip\">\n\t</Log>\n"
593 << "\t<EndDateTime>" << this->EndBuild << "</EndDateTime>\n"
594 << "\t<EndBuildTime>" << static_cast<unsigned int>(this->EndBuildTime)
595 << "</EndBuildTime>\n"
596 << "<ElapsedMinutes>" << static_cast<int>(elapsed_build_time/6)/10.0
597 << "</ElapsedMinutes>"
598 << "</Build>" << std::endl;
599 this->CTest->EndXML(os);
602 //######################################################################
603 //######################################################################
604 //######################################################################
605 //######################################################################
607 //----------------------------------------------------------------------
608 int cmCTestBuildHandler::RunMakeCommand(const char* command,
609 int* retVal, const char* dir, int timeout, std::ofstream& ofs)
611 // First generate the command and arguments
612 std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
614 if(args.size() < 1)
616 return false;
619 std::vector<const char*> argv;
620 for(std::vector<cmStdString>::const_iterator a = args.begin();
621 a != args.end(); ++a)
623 argv.push_back(a->c_str());
625 argv.push_back(0);
627 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run command:");
628 std::vector<const char*>::iterator ait;
629 for ( ait = argv.begin(); ait != argv.end() && *ait; ++ ait )
631 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " \"" << *ait << "\"");
633 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl);
635 // Now create process object
636 cmsysProcess* cp = cmsysProcess_New();
637 cmsysProcess_SetCommand(cp, &*argv.begin());
638 cmsysProcess_SetWorkingDirectory(cp, dir);
639 cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
640 cmsysProcess_SetTimeout(cp, timeout);
641 cmsysProcess_Execute(cp);
643 // Initialize tick's
644 std::string::size_type tick = 0;
645 const std::string::size_type tick_len = 1024;
647 char* data;
648 int length;
649 cmCTestLog(this->CTest, HANDLER_OUTPUT,
650 " Each symbol represents " << tick_len << " bytes of output."
651 << std::endl
652 << " '!' represents an error and '*' a warning." << std::endl
653 << " " << std::flush);
655 // Initialize building structures
656 this->BuildProcessingQueue.clear();
657 this->OutputLineCounter = 0;
658 this->ErrorsAndWarnings.clear();
659 this->TotalErrors = 0;
660 this->TotalWarnings = 0;
661 this->BuildOutputLogSize = 0;
662 this->LastTickChar = '.';
663 this->WarningQuotaReached = false;
664 this->ErrorQuotaReached = false;
666 // For every chunk of data
667 int res;
668 while((res = cmsysProcess_WaitForData(cp, &data, &length, 0)))
670 // Replace '\0' with '\n', since '\0' does not really make sense. This is
671 // for Visual Studio output
672 for(int cc =0; cc < length; ++cc)
674 if(data[cc] == 0)
676 data[cc] = '\n';
680 // Process the chunk of data
681 if ( res == cmsysProcess_Pipe_STDERR )
683 this->ProcessBuffer(data, length, tick, tick_len, ofs,
684 &this->BuildProcessingErrorQueue);
686 else
688 this->ProcessBuffer(data, length, tick, tick_len, ofs,
689 &this->BuildProcessingQueue);
693 this->ProcessBuffer(0, 0, tick, tick_len, ofs, &this->BuildProcessingQueue);
694 this->ProcessBuffer(0, 0, tick, tick_len, ofs,
695 &this->BuildProcessingErrorQueue);
696 cmCTestLog(this->CTest, OUTPUT, " Size of output: "
697 << int(this->BuildOutputLogSize / 1024.0) << "K" << std::endl);
699 // Properly handle output of the build command
700 cmsysProcess_WaitForExit(cp, 0);
701 int result = cmsysProcess_GetState(cp);
703 if(result == cmsysProcess_State_Exited)
705 if (retVal)
707 *retVal = cmsysProcess_GetExitValue(cp);
708 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
709 "Command exited with the value: " << *retVal << std::endl);
710 // if a non zero return value
711 if (*retVal)
713 // If there was an error running command, report that on the
714 // dashboard.
715 cmCTestBuildErrorWarning errorwarning;
716 errorwarning.LogLine = 1;
717 errorwarning.Text
718 = "*** WARNING non-zero return value in ctest from: ";
719 errorwarning.Text += argv[0];
720 errorwarning.PreContext = "";
721 errorwarning.PostContext = "";
722 errorwarning.Error = false;
723 this->ErrorsAndWarnings.push_back(errorwarning);
724 this->TotalWarnings ++;
728 else if(result == cmsysProcess_State_Exception)
730 if (retVal)
732 *retVal = cmsysProcess_GetExitException(cp);
733 cmCTestLog(this->CTest, WARNING, "There was an exception: " << *retVal
734 << std::endl);
737 else if(result == cmsysProcess_State_Expired)
739 cmCTestLog(this->CTest, WARNING, "There was a timeout" << std::endl);
741 else if(result == cmsysProcess_State_Error)
743 // If there was an error running command, report that on the dashboard.
744 cmCTestBuildErrorWarning errorwarning;
745 errorwarning.LogLine = 1;
746 errorwarning.Text = "*** ERROR executing: ";
747 errorwarning.Text += cmsysProcess_GetErrorString(cp);
748 errorwarning.PreContext = "";
749 errorwarning.PostContext = "";
750 errorwarning.Error = true;
751 this->ErrorsAndWarnings.push_back(errorwarning);
752 this->TotalErrors ++;
753 cmCTestLog(this->CTest, ERROR_MESSAGE, "There was an error: "
754 << cmsysProcess_GetErrorString(cp) << std::endl);
757 cmsysProcess_Delete(cp);
759 return result;
762 //######################################################################
763 //######################################################################
764 //######################################################################
765 //######################################################################
767 //----------------------------------------------------------------------
768 void cmCTestBuildHandler::ProcessBuffer(const char* data, int length,
769 size_t& tick, size_t tick_len, std::ofstream& ofs,
770 t_BuildProcessingQueueType* queue)
772 const std::string::size_type tick_line_len = 50;
773 const char* ptr;
774 for ( ptr = data; ptr < data+length; ptr ++ )
776 queue->push_back(*ptr);
778 this->BuildOutputLogSize += length;
780 // until there are any lines left in the buffer
781 while ( 1 )
783 // Find the end of line
784 t_BuildProcessingQueueType::iterator it;
785 for ( it = queue->begin();
786 it != queue->end();
787 ++ it )
789 if ( *it == '\n' )
791 break;
795 // Once certain number of errors or warnings reached, ignore future errors
796 // or warnings.
797 if ( this->TotalWarnings >= this->MaxWarnings )
799 this->WarningQuotaReached = true;
801 if ( this->TotalErrors >= this->MaxErrors )
803 this->ErrorQuotaReached = true;
806 // If the end of line was found
807 if ( it != queue->end() )
809 // Create a contiguous array for the line
810 this->CurrentProcessingLine.clear();
811 t_BuildProcessingQueueType::iterator cit;
812 for ( cit = queue->begin(); cit != it; ++cit )
814 this->CurrentProcessingLine.push_back(*cit);
816 this->CurrentProcessingLine.push_back(0);
817 const char* line = &*this->CurrentProcessingLine.begin();
819 // Process the line
820 int lineType = this->ProcessSingleLine(line);
822 // Erase the line from the queue
823 queue->erase(queue->begin(), it+1);
825 // Depending on the line type, produce error or warning, or nothing
826 cmCTestBuildErrorWarning errorwarning;
827 bool found = false;
828 switch ( lineType )
830 case b_WARNING_LINE:
831 this->LastTickChar = '*';
832 errorwarning.Error = false;
833 found = true;
834 this->TotalWarnings ++;
835 break;
836 case b_ERROR_LINE:
837 this->LastTickChar = '!';
838 errorwarning.Error = true;
839 found = true;
840 this->TotalErrors ++;
841 break;
843 if ( found )
845 // This is an error or warning, so generate report
846 errorwarning.LogLine = static_cast<int>(this->OutputLineCounter+1);
847 errorwarning.Text = line;
848 errorwarning.PreContext = "";
849 errorwarning.PostContext = "";
851 // Copy pre-context to report
852 std::deque<cmStdString>::iterator pcit;
853 for ( pcit = this->PreContext.begin();
854 pcit != this->PreContext.end();
855 ++pcit )
857 errorwarning.PreContext += *pcit + "\n";
859 this->PreContext.clear();
861 // Store report
862 this->ErrorsAndWarnings.push_back(errorwarning);
863 this->LastErrorOrWarning = this->ErrorsAndWarnings.end()-1;
864 this->PostContextCount = 0;
866 else
868 // This is not an error or warning.
869 // So, figure out if this is a post-context line
870 if ( this->ErrorsAndWarnings.size() &&
871 this->LastErrorOrWarning != this->ErrorsAndWarnings.end() &&
872 this->PostContextCount < this->MaxPostContext )
874 this->PostContextCount ++;
875 this->LastErrorOrWarning->PostContext += line;
876 if ( this->PostContextCount < this->MaxPostContext )
878 this->LastErrorOrWarning->PostContext += "\n";
881 else
883 // Otherwise store pre-context for the next error
884 this->PreContext.push_back(line);
885 if ( this->PreContext.size() > this->MaxPreContext )
887 this->PreContext.erase(this->PreContext.begin(),
888 this->PreContext.end()-this->MaxPreContext);
892 this->OutputLineCounter ++;
894 else
896 break;
900 // Now that the buffer is processed, display missing ticks
901 int tickDisplayed = false;
902 while ( this->BuildOutputLogSize > (tick * tick_len) )
904 tick ++;
905 cmCTestLog(this->CTest, HANDLER_OUTPUT, this->LastTickChar);
906 tickDisplayed = true;
907 if ( tick % tick_line_len == 0 && tick > 0 )
909 cmCTestLog(this->CTest, HANDLER_OUTPUT, " Size: "
910 << int((this->BuildOutputLogSize / 1024.0) + 1) << "K" << std::endl
911 << " ");
914 if ( tickDisplayed )
916 this->LastTickChar = '.';
919 // And if this is verbose output, display the content of the chunk
920 cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
921 cmCTestLogWrite(data, length));
923 // Always store the chunk to the file
924 ofs << cmCTestLogWrite(data, length);
927 //----------------------------------------------------------------------
928 int cmCTestBuildHandler::ProcessSingleLine(const char* data)
930 cmCTestLog(this->CTest, DEBUG, "Line: [" << data << "]" << std::endl);
932 std::vector<cmsys::RegularExpression>::iterator it;
934 int warningLine = 0;
935 int errorLine = 0;
937 // Check for regular expressions
939 if ( !this->ErrorQuotaReached )
941 // Errors
942 int wrxCnt = 0;
943 for ( it = this->ErrorMatchRegex.begin();
944 it != this->ErrorMatchRegex.end();
945 ++ it )
947 if ( it->find(data) )
949 errorLine = 1;
950 cmCTestLog(this->CTest, DEBUG, " Error Line: " << data
951 << " (matches: " << this->CustomErrorMatches[wrxCnt] << ")"
952 << std::endl);
953 break;
955 wrxCnt ++;
957 // Error exceptions
958 wrxCnt = 0;
959 for ( it = this->ErrorExceptionRegex.begin();
960 it != this->ErrorExceptionRegex.end();
961 ++ it )
963 if ( it->find(data) )
965 errorLine = 0;
966 cmCTestLog(this->CTest, DEBUG, " Not an error Line: " << data
967 << " (matches: " << this->CustomErrorExceptions[wrxCnt] << ")"
968 << std::endl);
969 break;
971 wrxCnt ++;
974 if ( !this->WarningQuotaReached )
976 // Warnings
977 int wrxCnt = 0;
978 for ( it = this->WarningMatchRegex.begin();
979 it != this->WarningMatchRegex.end();
980 ++ it )
982 if ( it->find(data) )
984 warningLine = 1;
985 cmCTestLog(this->CTest, DEBUG,
986 " Warning Line: " << data
987 << " (matches: " << this->CustomWarningMatches[wrxCnt] << ")"
988 << std::endl);
989 break;
991 wrxCnt ++;
994 wrxCnt = 0;
995 // Warning exceptions
996 for ( it = this->WarningExceptionRegex.begin();
997 it != this->WarningExceptionRegex.end();
998 ++ it )
1000 if ( it->find(data) )
1002 warningLine = 0;
1003 cmCTestLog(this->CTest, DEBUG, " Not a warning Line: " << data
1004 << " (matches: " << this->CustomWarningExceptions[wrxCnt] << ")"
1005 << std::endl);
1006 break;
1008 wrxCnt ++;
1011 if ( errorLine )
1013 return b_ERROR_LINE;
1015 if ( warningLine )
1017 return b_WARNING_LINE;
1019 return b_REGULAR_LINE;