Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmCTest.cxx
blob17ede5f184f64e64db7ab70efbdd2d14b6689a61
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCTest.cxx,v $
5 Language: C++
6 Date: $Date: 2009-03-05 20:17:06 $
7 Version: $Revision: 1.357 $
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 =========================================================================*/
17 #include "cm_curl.h"
19 #include "cmCTest.h"
20 #include "cmake.h"
21 #include "cmMakefile.h"
22 #include "cmLocalGenerator.h"
23 #include "cmGlobalGenerator.h"
24 #include <cmsys/Directory.hxx>
25 #include <cmsys/SystemInformation.hxx>
26 #include "cmDynamicLoader.h"
27 #include "cmGeneratedFileStream.h"
28 #include "cmXMLSafe.h"
29 #include "cmVersionMacros.h"
30 #include "cmCTestCommand.h"
32 #include "cmCTestBuildHandler.h"
33 #include "cmCTestBuildAndTestHandler.h"
34 #include "cmCTestConfigureHandler.h"
35 #include "cmCTestCoverageHandler.h"
36 #include "cmCTestMemCheckHandler.h"
37 #include "cmCTestScriptHandler.h"
38 #include "cmCTestTestHandler.h"
39 #include "cmCTestUpdateHandler.h"
40 #include "cmCTestSubmitHandler.h"
42 #include "cmVersion.h"
44 #include <cmsys/RegularExpression.hxx>
45 #include <cmsys/Process.h>
46 #include <cmsys/Glob.hxx>
48 #include <stdlib.h>
49 #include <math.h>
50 #include <float.h>
51 #include <ctype.h>
53 #include <memory> // auto_ptr
55 #if defined(__BEOS__) && !defined(__HAIKU__)
56 #include <be/kernel/OS.h> /* disable_debugger() API. */
57 #endif
59 #if defined(__HAIKU__)
60 #include <os/kernel/OS.h> /* disable_debugger() API. */
61 #endif
64 #define DEBUGOUT std::cout << __LINE__ << " "; std::cout
65 #define DEBUGERR std::cerr << __LINE__ << " "; std::cerr
67 //----------------------------------------------------------------------
68 struct tm* cmCTest::GetNightlyTime(std::string str,
69 bool tomorrowtag)
71 struct tm* lctime;
72 time_t tctime = time(0);
73 lctime = gmtime(&tctime);
74 char buf[1024];
75 // add todays year day and month to the time in str because
76 // curl_getdate no longer assumes the day is today
77 sprintf(buf, "%d%02d%02d %s", lctime->tm_year+1900, lctime->tm_mday,
78 lctime->tm_mon, str.c_str());
79 cmCTestLog(this, OUTPUT, "Determine Nightly Start Time" << std::endl
80 << " Specified time: " << str.c_str() << std::endl);
81 //Convert the nightly start time to seconds. Since we are
82 //providing only a time and a timezone, the current date of
83 //the local machine is assumed. Consequently, nightlySeconds
84 //is the time at which the nightly dashboard was opened or
85 //will be opened on the date of the current client machine.
86 //As such, this time may be in the past or in the future.
87 time_t ntime = curl_getdate(buf, &tctime);
88 cmCTestLog(this, DEBUG, " Get curl time: " << ntime << std::endl);
89 tctime = time(0);
90 cmCTestLog(this, DEBUG, " Get the current time: " << tctime << std::endl);
92 const int dayLength = 24 * 60 * 60;
93 cmCTestLog(this, DEBUG, "Seconds: " << tctime << std::endl);
94 while ( ntime > tctime )
96 // If nightlySeconds is in the past, this is the current
97 // open dashboard, then return nightlySeconds. If
98 // nightlySeconds is in the future, this is the next
99 // dashboard to be opened, so subtract 24 hours to get the
100 // time of the current open dashboard
101 ntime -= dayLength;
102 cmCTestLog(this, DEBUG, "Pick yesterday" << std::endl);
103 cmCTestLog(this, DEBUG, " Future time, subtract day: " << ntime
104 << std::endl);
106 while ( tctime > (ntime + dayLength) )
108 ntime += dayLength;
109 cmCTestLog(this, DEBUG, " Past time, add day: " << ntime << std::endl);
111 cmCTestLog(this, DEBUG, "nightlySeconds: " << ntime << std::endl);
112 cmCTestLog(this, DEBUG, " Current time: " << tctime
113 << " Nightly time: " << ntime << std::endl);
114 if ( tomorrowtag )
116 cmCTestLog(this, OUTPUT, " Use future tag, Add a day" << std::endl);
117 ntime += dayLength;
119 lctime = gmtime(&ntime);
120 return lctime;
123 //----------------------------------------------------------------------
124 std::string cmCTest::CleanString(const std::string& str)
126 std::string::size_type spos = str.find_first_not_of(" \n\t\r\f\v");
127 std::string::size_type epos = str.find_last_not_of(" \n\t\r\f\v");
128 if ( spos == str.npos )
130 return std::string();
132 if ( epos != str.npos )
134 epos = epos - spos + 1;
136 return str.substr(spos, epos);
139 //----------------------------------------------------------------------
140 std::string cmCTest::CurrentTime()
142 time_t currenttime = time(0);
143 struct tm* t = localtime(&currenttime);
144 //return ::CleanString(ctime(&currenttime));
145 char current_time[1024];
146 if ( this->ShortDateFormat )
148 strftime(current_time, 1000, "%b %d %H:%M %Z", t);
150 else
152 strftime(current_time, 1000, "%a %b %d %H:%M:%S %Z %Y", t);
154 cmCTestLog(this, DEBUG, " Current_Time: " << current_time << std::endl);
155 return cmXMLSafe(cmCTest::CleanString(current_time)).str();
158 //----------------------------------------------------------------------
159 std::string cmCTest::MakeURLSafe(const std::string& str)
161 cmOStringStream ost;
162 char buffer[10];
163 for ( std::string::size_type pos = 0; pos < str.size(); pos ++ )
165 unsigned char ch = str[pos];
166 if ( ( ch > 126 || ch < 32 ||
167 ch == '&' ||
168 ch == '%' ||
169 ch == '+' ||
170 ch == '=' ||
171 ch == '@'
172 ) && ch != 9 )
174 sprintf(buffer, "%02x;", (unsigned int)ch);
175 ost << buffer;
177 else
179 ost << ch;
182 return ost.str();
185 //----------------------------------------------------------------------------
186 std::string cmCTest::DecodeURL(const std::string& in)
188 std::string out;
189 for(const char* c = in.c_str(); *c; ++c)
191 if(*c == '%' && isxdigit(*(c+1)) && isxdigit(*(c+2)))
193 char buf[3] = {*(c+1), *(c+2), 0};
194 out.append(1, char(strtoul(buf, 0, 16)));
195 c += 2;
197 else
199 out.append(1, *c);
202 return out;
205 //----------------------------------------------------------------------
206 cmCTest::cmCTest()
208 this->ParallelSubprocess = false;
209 this->ParallelLevel = 0;
210 this->SubmitIndex = 0;
211 this->ForceNewCTestProcess = false;
212 this->TomorrowTag = false;
213 this->Verbose = false;
215 this->Debug = false;
216 this->ShowLineNumbers = false;
217 this->Quiet = false;
218 this->ExtraVerbose = false;
219 this->ProduceXML = false;
220 this->ShowOnly = false;
221 this->RunConfigurationScript = false;
222 this->TestModel = cmCTest::EXPERIMENTAL;
223 this->MaxTestNameWidth = 30;
224 this->InteractiveDebugMode = true;
225 this->TimeOut = 0;
226 this->CompressXMLFiles = false;
227 this->CTestConfigFile = "";
228 this->OutputLogFile = 0;
229 this->OutputLogFileLastTag = -1;
230 this->SuppressUpdatingCTestConfiguration = false;
231 this->DartVersion = 1;
232 this->OutputTestOutputOnTestFailure = false;
233 if(cmSystemTools::GetEnv("CTEST_OUTPUT_ON_FAILURE"))
235 this->OutputTestOutputOnTestFailure = true;
237 this->InitStreams();
239 this->Parts[PartStart].SetName("Start");
240 this->Parts[PartUpdate].SetName("Update");
241 this->Parts[PartConfigure].SetName("Configure");
242 this->Parts[PartBuild].SetName("Build");
243 this->Parts[PartTest].SetName("Test");
244 this->Parts[PartCoverage].SetName("Coverage");
245 this->Parts[PartMemCheck].SetName("MemCheck");
246 this->Parts[PartSubmit].SetName("Submit");
247 this->Parts[PartNotes].SetName("Notes");
248 this->Parts[PartExtraFiles].SetName("ExtraFiles");
250 // Fill the part name-to-id map.
251 for(Part p = PartStart; p != PartCount; p = Part(p+1))
253 this->PartMap[cmSystemTools::LowerCase(this->Parts[p].GetName())] = p;
256 this->ShortDateFormat = true;
258 this->TestingHandlers["build"] = new cmCTestBuildHandler;
259 this->TestingHandlers["buildtest"] = new cmCTestBuildAndTestHandler;
260 this->TestingHandlers["coverage"] = new cmCTestCoverageHandler;
261 this->TestingHandlers["script"] = new cmCTestScriptHandler;
262 this->TestingHandlers["test"] = new cmCTestTestHandler;
263 this->TestingHandlers["update"] = new cmCTestUpdateHandler;
264 this->TestingHandlers["configure"] = new cmCTestConfigureHandler;
265 this->TestingHandlers["memcheck"] = new cmCTestMemCheckHandler;
266 this->TestingHandlers["submit"] = new cmCTestSubmitHandler;
268 cmCTest::t_TestingHandlers::iterator it;
269 for ( it = this->TestingHandlers.begin();
270 it != this->TestingHandlers.end(); ++ it )
272 it->second->SetCTestInstance(this);
275 // Make sure we can capture the build tool output.
276 cmSystemTools::EnableVSConsoleOutput();
279 //----------------------------------------------------------------------
280 cmCTest::~cmCTest()
282 cmCTest::t_TestingHandlers::iterator it;
283 for ( it = this->TestingHandlers.begin();
284 it != this->TestingHandlers.end(); ++ it )
286 delete it->second;
287 it->second = 0;
289 this->SetOutputLogFileName(0);
292 //----------------------------------------------------------------------------
293 cmCTest::Part cmCTest::GetPartFromName(const char* name)
295 // Look up by lower-case to make names case-insensitive.
296 std::string lower_name = cmSystemTools::LowerCase(name);
297 PartMapType::const_iterator i = this->PartMap.find(lower_name);
298 if(i != this->PartMap.end())
300 return i->second;
303 // The string does not name a valid part.
304 return PartCount;
307 //----------------------------------------------------------------------
308 int cmCTest::Initialize(const char* binary_dir, bool new_tag,
309 bool verbose_tag)
311 cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
312 if(!this->InteractiveDebugMode)
314 this->BlockTestErrorDiagnostics();
316 else
318 cmSystemTools::PutEnv("CTEST_INTERACTIVE_DEBUG_MODE=1");
321 this->BinaryDir = binary_dir;
322 cmSystemTools::ConvertToUnixSlashes(this->BinaryDir);
324 this->UpdateCTestConfiguration();
326 cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
327 if ( this->ProduceXML )
329 cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
330 cmCTestLog(this, OUTPUT,
331 " Site: " << this->GetCTestConfiguration("Site") << std::endl
332 << " Build name: " << this->GetCTestConfiguration("BuildName")
333 << std::endl);
334 cmCTestLog(this, DEBUG, "Produce XML is on" << std::endl);
335 if ( this->TestModel == cmCTest::NIGHTLY &&
336 this->GetCTestConfiguration("NightlyStartTime").empty() )
338 cmCTestLog(this, WARNING,
339 "WARNING: No nightly start time found please set in"
340 " CTestConfig.cmake or DartConfig.cmake" << std::endl);
341 cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
342 return 0;
346 cmake cm;
347 cmGlobalGenerator gg;
348 gg.SetCMakeInstance(&cm);
349 std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
350 lg->SetGlobalGenerator(&gg);
351 cmMakefile *mf = lg->GetMakefile();
352 if ( !this->ReadCustomConfigurationFileTree(this->BinaryDir.c_str(), mf) )
354 cmCTestLog(this, DEBUG, "Cannot find custom configuration file tree"
355 << std::endl);
356 return 0;
359 if ( this->ProduceXML )
361 std::string testingDir = this->BinaryDir + "/Testing";
362 if ( cmSystemTools::FileExists(testingDir.c_str()) )
364 if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
366 cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
367 << " is in the place of the testing directory" << std::endl);
368 return 0;
371 else
373 if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
375 cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory "
376 << testingDir << std::endl);
377 return 0;
380 std::string tagfile = testingDir + "/TAG";
381 std::ifstream tfin(tagfile.c_str());
382 std::string tag;
383 time_t tctime = time(0);
384 if ( this->TomorrowTag )
386 tctime += ( 24 * 60 * 60 );
388 struct tm *lctime = gmtime(&tctime);
389 if ( tfin && cmSystemTools::GetLineFromStream(tfin, tag) )
391 int year = 0;
392 int mon = 0;
393 int day = 0;
394 int hour = 0;
395 int min = 0;
396 sscanf(tag.c_str(), "%04d%02d%02d-%02d%02d",
397 &year, &mon, &day, &hour, &min);
398 if ( year != lctime->tm_year + 1900 ||
399 mon != lctime->tm_mon+1 ||
400 day != lctime->tm_mday )
402 tag = "";
404 std::string tagmode;
405 if ( cmSystemTools::GetLineFromStream(tfin, tagmode) )
407 if (tagmode.size() > 4 && !this->Parts[PartStart])
409 this->TestModel = cmCTest::GetTestModelFromString(tagmode.c_str());
412 tfin.close();
414 if (tag.size() == 0 || new_tag || this->Parts[PartStart])
416 cmCTestLog(this, DEBUG, "TestModel: " << this->GetTestModelString()
417 << std::endl);
418 cmCTestLog(this, DEBUG, "TestModel: " << this->TestModel << std::endl);
419 if ( this->TestModel == cmCTest::NIGHTLY )
421 lctime = this->GetNightlyTime(
422 this->GetCTestConfiguration("NightlyStartTime"), this->TomorrowTag);
424 char datestring[100];
425 sprintf(datestring, "%04d%02d%02d-%02d%02d",
426 lctime->tm_year + 1900,
427 lctime->tm_mon+1,
428 lctime->tm_mday,
429 lctime->tm_hour,
430 lctime->tm_min);
431 tag = datestring;
432 std::ofstream ofs(tagfile.c_str());
433 if ( ofs )
435 ofs << tag << std::endl;
436 ofs << this->GetTestModelString() << std::endl;
438 ofs.close();
439 if ( verbose_tag )
441 cmCTestLog(this, OUTPUT, "Create new tag: " << tag << " - "
442 << this->GetTestModelString() << std::endl);
445 this->CurrentTag = tag;
447 return 1;
450 //----------------------------------------------------------------------
451 bool cmCTest::InitializeFromCommand(cmCTestCommand* command, bool first)
453 if ( !first && !this->CurrentTag.empty() )
455 return true;
458 std::string src_dir
459 = this->GetCTestConfiguration("SourceDirectory").c_str();
460 std::string bld_dir = this->GetCTestConfiguration("BuildDirectory").c_str();
461 this->DartVersion = 1;
462 for(Part p = PartStart; p != PartCount; p = Part(p+1))
464 this->Parts[p].SubmitFiles.clear();
467 cmMakefile* mf = command->GetMakefile();
468 std::string fname = src_dir;
469 fname += "/CTestConfig.cmake";
470 cmSystemTools::ConvertToUnixSlashes(fname);
471 if ( cmSystemTools::FileExists(fname.c_str()) )
473 cmCTestLog(this, OUTPUT, " Reading ctest configuration file: "
474 << fname.c_str() << std::endl);
475 bool readit = mf->ReadListFile(mf->GetCurrentListFile(),
476 fname.c_str() );
477 if(!readit)
479 std::string m = "Could not find include file: ";
480 m += fname;
481 command->SetError(m.c_str());
482 return false;
485 else if ( !first )
487 cmCTestLog(this, WARNING, "Cannot locate CTest configuration: "
488 << fname.c_str() << std::endl);
490 else
492 cmCTestLog(this, HANDLER_OUTPUT, " Cannot locate CTest configuration: "
493 << fname.c_str() << std::endl
494 << " Delay the initialization of CTest" << std::endl);
497 this->SetCTestConfigurationFromCMakeVariable(mf, "NightlyStartTime",
498 "CTEST_NIGHTLY_START_TIME");
499 this->SetCTestConfigurationFromCMakeVariable(mf, "Site", "CTEST_SITE");
500 this->SetCTestConfigurationFromCMakeVariable(mf, "BuildName",
501 "CTEST_BUILD_NAME");
502 const char* dartVersion = mf->GetDefinition("CTEST_DART_SERVER_VERSION");
503 if ( dartVersion )
505 this->DartVersion = atoi(dartVersion);
506 if ( this->DartVersion < 0 )
508 cmCTestLog(this, ERROR_MESSAGE, "Invalid Dart server version: "
509 << dartVersion << ". Please specify the version number."
510 << std::endl);
511 return false;
515 if ( !this->Initialize(bld_dir.c_str(), true, false) )
517 if ( this->GetCTestConfiguration("NightlyStartTime").empty() && first)
519 return true;
521 return false;
523 cmCTestLog(this, OUTPUT, " Use " << this->GetTestModelString()
524 << " tag: " << this->GetCurrentTag() << std::endl);
525 return true;
529 //----------------------------------------------------------------------
530 bool cmCTest::UpdateCTestConfiguration()
532 if ( this->SuppressUpdatingCTestConfiguration )
534 return true;
536 std::string fileName = this->CTestConfigFile;
537 if ( fileName.empty() )
539 fileName = this->BinaryDir + "/CTestConfiguration.ini";
540 if ( !cmSystemTools::FileExists(fileName.c_str()) )
542 fileName = this->BinaryDir + "/DartConfiguration.tcl";
545 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "UpdateCTestConfiguration from :"
546 << fileName.c_str() << "\n");
547 if ( !cmSystemTools::FileExists(fileName.c_str()) )
549 // No need to exit if we are not producing XML
550 if ( this->ProduceXML )
552 cmCTestLog(this, ERROR_MESSAGE, "Cannot find file: " << fileName.c_str()
553 << std::endl);
554 return false;
557 else
559 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Parse Config file:"
560 << fileName.c_str() << "\n");
561 // parse the dart test file
562 std::ifstream fin(fileName.c_str());
563 if(!fin)
565 return false;
568 char buffer[1024];
569 while ( fin )
571 buffer[0] = 0;
572 fin.getline(buffer, 1023);
573 buffer[1023] = 0;
574 std::string line = cmCTest::CleanString(buffer);
575 if(line.size() == 0)
577 continue;
579 while ( fin && (line[line.size()-1] == '\\') )
581 line = line.substr(0, line.size()-1);
582 buffer[0] = 0;
583 fin.getline(buffer, 1023);
584 buffer[1023] = 0;
585 line += cmCTest::CleanString(buffer);
587 if ( line[0] == '#' )
589 continue;
591 std::string::size_type cpos = line.find_first_of(":");
592 if ( cpos == line.npos )
594 continue;
596 std::string key = line.substr(0, cpos);
597 std::string value
598 = cmCTest::CleanString(line.substr(cpos+1, line.npos));
599 this->CTestConfiguration[key] = value;
601 fin.close();
603 if ( !this->GetCTestConfiguration("BuildDirectory").empty() )
605 this->BinaryDir = this->GetCTestConfiguration("BuildDirectory");
606 cmSystemTools::ChangeDirectory(this->BinaryDir.c_str());
608 this->TimeOut = atoi(this->GetCTestConfiguration("TimeOut").c_str());
609 if ( this->ProduceXML )
611 this->CompressXMLFiles = cmSystemTools::IsOn(
612 this->GetCTestConfiguration("CompressSubmission").c_str());
614 return true;
617 //----------------------------------------------------------------------
618 void cmCTest::BlockTestErrorDiagnostics()
620 cmSystemTools::PutEnv("DART_TEST_FROM_DART=1");
621 cmSystemTools::PutEnv("DASHBOARD_TEST_FROM_CTEST=" CMake_VERSION);
622 #if defined(_WIN32)
623 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
624 #elif defined(__BEOS__) || defined(__HAIKU__)
625 disable_debugger(1);
626 #endif
629 //----------------------------------------------------------------------
630 void cmCTest::SetTestModel(int mode)
632 this->InteractiveDebugMode = false;
633 this->TestModel = mode;
636 //----------------------------------------------------------------------
637 bool cmCTest::SetTest(const char* ttype, bool report)
639 if ( cmSystemTools::LowerCase(ttype) == "all" )
641 for(Part p = PartStart; p != PartCount; p = Part(p+1))
643 this->Parts[p].Enable();
645 return true;
647 Part p = this->GetPartFromName(ttype);
648 if(p != PartCount)
650 this->Parts[p].Enable();
651 return true;
653 else
655 if ( report )
657 cmCTestLog(this, ERROR_MESSAGE, "Don't know about test \"" << ttype
658 << "\" yet..." << std::endl);
660 return false;
664 //----------------------------------------------------------------------
665 void cmCTest::Finalize()
669 //----------------------------------------------------------------------
670 bool cmCTest::OpenOutputFile(const std::string& path,
671 const std::string& name, cmGeneratedFileStream& stream,
672 bool compress)
674 std::string testingDir = this->BinaryDir + "/Testing";
675 if ( path.size() > 0 )
677 testingDir += "/" + path;
679 if ( cmSystemTools::FileExists(testingDir.c_str()) )
681 if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
683 cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
684 << " is in the place of the testing directory"
685 << std::endl);
686 return false;
689 else
691 if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
693 cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory " << testingDir
694 << std::endl);
695 return false;
698 std::string filename = testingDir + "/" + name;
699 stream.Open(filename.c_str());
700 if( !stream )
702 cmCTestLog(this, ERROR_MESSAGE, "Problem opening file: " << filename
703 << std::endl);
704 return false;
706 if ( compress )
708 if ( this->CompressXMLFiles )
710 stream.SetCompression(true);
713 return true;
716 //----------------------------------------------------------------------
717 bool cmCTest::AddIfExists(Part part, const char* file)
719 if ( this->CTestFileExists(file) )
721 this->AddSubmitFile(part, file);
723 else
725 std::string name = file;
726 name += ".gz";
727 if ( this->CTestFileExists(name.c_str()) )
729 this->AddSubmitFile(part, file);
731 else
733 return false;
736 return true;
739 //----------------------------------------------------------------------
740 bool cmCTest::CTestFileExists(const std::string& filename)
742 std::string testingDir = this->BinaryDir + "/Testing/" +
743 this->CurrentTag + "/" + filename;
744 return cmSystemTools::FileExists(testingDir.c_str());
747 //----------------------------------------------------------------------
748 cmCTestGenericHandler* cmCTest::GetInitializedHandler(const char* handler)
750 cmCTest::t_TestingHandlers::iterator it =
751 this->TestingHandlers.find(handler);
752 if ( it == this->TestingHandlers.end() )
754 return 0;
756 it->second->Initialize();
757 return it->second;
760 //----------------------------------------------------------------------
761 cmCTestGenericHandler* cmCTest::GetHandler(const char* handler)
763 cmCTest::t_TestingHandlers::iterator it =
764 this->TestingHandlers.find(handler);
765 if ( it == this->TestingHandlers.end() )
767 return 0;
769 return it->second;
772 //----------------------------------------------------------------------
773 int cmCTest::ExecuteHandler(const char* shandler)
775 cmCTestGenericHandler* handler = this->GetHandler(shandler);
776 if ( !handler )
778 return -1;
780 handler->Initialize();
781 return handler->ProcessHandler();
784 //----------------------------------------------------------------------
785 int cmCTest::ProcessTests()
787 int res = 0;
788 bool notest = true;
789 int update_count = 0;
791 // do not output startup if this is a sub-process for parallel tests
792 if(!this->GetParallelSubprocess())
794 cmCTestLog(this, OUTPUT, "Start processing tests" << std::endl);
797 for(Part p = PartStart; notest && p != PartCount; p = Part(p+1))
799 notest = !this->Parts[p];
801 if (this->Parts[PartUpdate] &&
802 (this->GetRemainingTimeAllowed() - 120 > 0))
804 cmCTestGenericHandler* uphandler = this->GetHandler("update");
805 uphandler->SetPersistentOption("SourceDirectory",
806 this->GetCTestConfiguration("SourceDirectory").c_str());
807 update_count = uphandler->ProcessHandler();
808 if ( update_count < 0 )
810 res |= cmCTest::UPDATE_ERRORS;
813 if ( this->TestModel == cmCTest::CONTINUOUS && !update_count )
815 return 0;
817 if (this->Parts[PartConfigure] &&
818 (this->GetRemainingTimeAllowed() - 120 > 0))
820 if (this->GetHandler("configure")->ProcessHandler() < 0)
822 res |= cmCTest::CONFIGURE_ERRORS;
825 if (this->Parts[PartBuild] &&
826 (this->GetRemainingTimeAllowed() - 120 > 0))
828 this->UpdateCTestConfiguration();
829 if (this->GetHandler("build")->ProcessHandler() < 0)
831 res |= cmCTest::BUILD_ERRORS;
834 if ((this->Parts[PartTest] || notest) &&
835 (this->GetRemainingTimeAllowed() - 120 > 0))
837 this->UpdateCTestConfiguration();
838 if (this->GetHandler("test")->ProcessHandler() < 0)
840 res |= cmCTest::TEST_ERRORS;
843 if (this->Parts[PartCoverage] &&
844 (this->GetRemainingTimeAllowed() - 120 > 0))
846 this->UpdateCTestConfiguration();
847 if (this->GetHandler("coverage")->ProcessHandler() < 0)
849 res |= cmCTest::COVERAGE_ERRORS;
852 if (this->Parts[PartMemCheck] &&
853 (this->GetRemainingTimeAllowed() - 120 > 0))
855 this->UpdateCTestConfiguration();
856 if (this->GetHandler("memcheck")->ProcessHandler() < 0)
858 res |= cmCTest::MEMORY_ERRORS;
861 if ( !notest )
863 std::string notes_dir = this->BinaryDir + "/Testing/Notes";
864 if ( cmSystemTools::FileIsDirectory(notes_dir.c_str()) )
866 cmsys::Directory d;
867 d.Load(notes_dir.c_str());
868 unsigned long kk;
869 for ( kk = 0; kk < d.GetNumberOfFiles(); kk ++ )
871 const char* file = d.GetFile(kk);
872 std::string fullname = notes_dir + "/" + file;
873 if ( cmSystemTools::FileExists(fullname.c_str()) &&
874 !cmSystemTools::FileIsDirectory(fullname.c_str()) )
876 if ( this->NotesFiles.size() > 0 )
878 this->NotesFiles += ";";
880 this->NotesFiles += fullname;
881 this->Parts[PartNotes].Enable();
886 if (this->Parts[PartNotes])
888 this->UpdateCTestConfiguration();
889 if ( this->NotesFiles.size() )
891 this->GenerateNotesFile(this->NotesFiles.c_str());
894 if (this->Parts[PartSubmit])
896 this->UpdateCTestConfiguration();
897 if (this->GetHandler("submit")->ProcessHandler() < 0)
899 res |= cmCTest::SUBMIT_ERRORS;
902 if ( res != 0 )
904 if(!this->GetParallelSubprocess())
906 cmCTestLog(this, ERROR_MESSAGE, "Errors while running CTest"
907 << std::endl);
910 return res;
913 //----------------------------------------------------------------------
914 std::string cmCTest::GetTestModelString()
916 if ( !this->SpecificTrack.empty() )
918 return this->SpecificTrack;
920 switch ( this->TestModel )
922 case cmCTest::NIGHTLY:
923 return "Nightly";
924 case cmCTest::CONTINUOUS:
925 return "Continuous";
927 return "Experimental";
930 //----------------------------------------------------------------------
931 int cmCTest::GetTestModelFromString(const char* str)
933 if ( !str )
935 return cmCTest::EXPERIMENTAL;
937 std::string rstr = cmSystemTools::LowerCase(str);
938 if ( strncmp(rstr.c_str(), "cont", 4) == 0 )
940 return cmCTest::CONTINUOUS;
942 if ( strncmp(rstr.c_str(), "nigh", 4) == 0 )
944 return cmCTest::NIGHTLY;
946 return cmCTest::EXPERIMENTAL;
949 //######################################################################
950 //######################################################################
951 //######################################################################
952 //######################################################################
954 //----------------------------------------------------------------------
955 int cmCTest::RunMakeCommand(const char* command, std::string* output,
956 int* retVal, const char* dir, int timeout, std::ofstream& ofs)
958 // First generate the command and arguments
959 std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
961 if(args.size() < 1)
963 return false;
966 std::vector<const char*> argv;
967 for(std::vector<cmStdString>::const_iterator a = args.begin();
968 a != args.end(); ++a)
970 argv.push_back(a->c_str());
972 argv.push_back(0);
974 if ( output )
976 *output = "";
979 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Run command:");
980 std::vector<const char*>::iterator ait;
981 for ( ait = argv.begin(); ait != argv.end() && *ait; ++ ait )
983 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, " \"" << *ait << "\"");
985 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, std::endl);
987 // Now create process object
988 cmsysProcess* cp = cmsysProcess_New();
989 cmsysProcess_SetCommand(cp, &*argv.begin());
990 cmsysProcess_SetWorkingDirectory(cp, dir);
991 cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
992 cmsysProcess_SetTimeout(cp, timeout);
993 cmsysProcess_Execute(cp);
995 // Initialize tick's
996 std::string::size_type tick = 0;
997 std::string::size_type tick_len = 1024;
998 std::string::size_type tick_line_len = 50;
1000 char* data;
1001 int length;
1002 cmCTestLog(this, HANDLER_OUTPUT,
1003 " Each . represents " << tick_len << " bytes of output" << std::endl
1004 << " " << std::flush);
1005 while(cmsysProcess_WaitForData(cp, &data, &length, 0))
1007 if ( output )
1009 for(int cc =0; cc < length; ++cc)
1011 if(data[cc] == 0)
1013 data[cc] = '\n';
1017 output->append(data, length);
1018 while ( output->size() > (tick * tick_len) )
1020 tick ++;
1021 cmCTestLog(this, HANDLER_OUTPUT, "." << std::flush);
1022 if ( tick % tick_line_len == 0 && tick > 0 )
1024 cmCTestLog(this, HANDLER_OUTPUT, " Size: "
1025 << int((output->size() / 1024.0) + 1) << "K" << std::endl
1026 << " " << std::flush);
1030 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
1031 if ( ofs )
1033 ofs << cmCTestLogWrite(data, length);
1036 cmCTestLog(this, OUTPUT, " Size of output: "
1037 << int(output->size() / 1024.0) << "K" << std::endl);
1039 cmsysProcess_WaitForExit(cp, 0);
1041 int result = cmsysProcess_GetState(cp);
1043 if(result == cmsysProcess_State_Exited)
1045 *retVal = cmsysProcess_GetExitValue(cp);
1046 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Command exited with the value: "
1047 << *retVal << std::endl);
1049 else if(result == cmsysProcess_State_Exception)
1051 *retVal = cmsysProcess_GetExitException(cp);
1052 cmCTestLog(this, WARNING, "There was an exception: " << *retVal
1053 << std::endl);
1055 else if(result == cmsysProcess_State_Expired)
1057 cmCTestLog(this, WARNING, "There was a timeout" << std::endl);
1059 else if(result == cmsysProcess_State_Error)
1061 *output += "\n*** ERROR executing: ";
1062 *output += cmsysProcess_GetErrorString(cp);
1063 *output += "\n***The build process failed.";
1064 cmCTestLog(this, ERROR_MESSAGE, "There was an error: "
1065 << cmsysProcess_GetErrorString(cp) << std::endl);
1068 cmsysProcess_Delete(cp);
1070 return result;
1073 //######################################################################
1074 //######################################################################
1075 //######################################################################
1076 //######################################################################
1078 //----------------------------------------------------------------------
1079 int cmCTest::RunTest(std::vector<const char*> argv,
1080 std::string* output, int *retVal,
1081 std::ostream* log, double testTimeOut,
1082 std::vector<std::string>* environment)
1084 std::vector<std::string> origEnv;
1085 bool modifyEnv = (environment && environment->size()>0);
1087 // determine how much time we have
1088 double timeout = this->GetRemainingTimeAllowed() - 120;
1089 if (this->TimeOut && this->TimeOut < timeout)
1091 timeout = this->TimeOut;
1093 if (testTimeOut
1094 && testTimeOut < this->GetRemainingTimeAllowed())
1096 timeout = testTimeOut;
1099 // always have at least 1 second if we got to here
1100 if (timeout <= 0)
1102 timeout = 1;
1104 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
1105 "Test timeout computed to be: " << timeout << "\n");
1106 if(cmSystemTools::SameFile(argv[0], this->CTestSelf.c_str()) &&
1107 !this->ForceNewCTestProcess)
1109 cmCTest inst;
1110 inst.ConfigType = this->ConfigType;
1111 inst.TimeOut = timeout;
1113 // Capture output of the child ctest.
1114 cmOStringStream oss;
1115 inst.SetStreams(&oss, &oss);
1117 std::vector<std::string> args;
1118 for(unsigned int i =0; i < argv.size(); ++i)
1120 if(argv[i])
1122 // make sure we pass the timeout in for any build and test
1123 // invocations. Since --build-generator is required this is a
1124 // good place to check for it, and to add the arguments in
1125 if (strcmp(argv[i],"--build-generator") == 0 && timeout)
1127 args.push_back("--test-timeout");
1128 cmOStringStream msg;
1129 msg << timeout;
1130 args.push_back(msg.str());
1132 args.push_back(argv[i]);
1135 if ( log )
1137 *log << "* Run internal CTest" << std::endl;
1139 std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory();
1141 if (modifyEnv)
1143 origEnv = cmSystemTools::AppendEnv(environment);
1146 *retVal = inst.Run(args, output);
1147 *output += oss.str();
1148 if ( log )
1150 *log << output->c_str();
1152 cmSystemTools::ChangeDirectory(oldpath.c_str());
1154 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
1155 "Internal cmCTest object used to run test." << std::endl
1156 << *output << std::endl);
1158 if (modifyEnv)
1160 cmSystemTools::RestoreEnv(origEnv);
1163 return cmsysProcess_State_Exited;
1165 std::vector<char> tempOutput;
1166 if ( output )
1168 *output = "";
1171 if (modifyEnv)
1173 origEnv = cmSystemTools::AppendEnv(environment);
1176 cmsysProcess* cp = cmsysProcess_New();
1177 cmsysProcess_SetCommand(cp, &*argv.begin());
1178 cmCTestLog(this, DEBUG, "Command is: " << argv[0] << std::endl);
1179 if(cmSystemTools::GetRunCommandHideConsole())
1181 cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
1184 cmsysProcess_SetTimeout(cp, timeout);
1185 cmsysProcess_Execute(cp);
1187 char* data;
1188 int length;
1189 while(cmsysProcess_WaitForData(cp, &data, &length, 0))
1191 if ( output )
1193 tempOutput.insert(tempOutput.end(), data, data+length);
1195 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
1196 if ( log )
1198 log->write(data, length);
1202 cmsysProcess_WaitForExit(cp, 0);
1203 if(output && tempOutput.begin() != tempOutput.end())
1205 output->append(&*tempOutput.begin(), tempOutput.size());
1207 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "-- Process completed"
1208 << std::endl);
1210 int result = cmsysProcess_GetState(cp);
1212 if(result == cmsysProcess_State_Exited)
1214 *retVal = cmsysProcess_GetExitValue(cp);
1215 if(*retVal != 0 && this->OutputTestOutputOnTestFailure)
1217 OutputTestErrors(tempOutput);
1220 else if(result == cmsysProcess_State_Exception)
1222 if(this->OutputTestOutputOnTestFailure)
1224 OutputTestErrors(tempOutput);
1226 *retVal = cmsysProcess_GetExitException(cp);
1227 std::string outerr = "\n*** Exception executing: ";
1228 outerr += cmsysProcess_GetExceptionString(cp);
1229 *output += outerr;
1230 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl
1231 << std::flush);
1233 else if(result == cmsysProcess_State_Error)
1235 std::string outerr = "\n*** ERROR executing: ";
1236 outerr += cmsysProcess_GetErrorString(cp);
1237 *output += outerr;
1238 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl
1239 << std::flush);
1241 cmsysProcess_Delete(cp);
1243 if (modifyEnv)
1245 cmSystemTools::RestoreEnv(origEnv);
1248 return result;
1251 //----------------------------------------------------------------------
1252 void cmCTest::StartXML(std::ostream& ostr, bool append)
1254 if(this->CurrentTag.empty())
1256 cmCTestLog(this, ERROR_MESSAGE,
1257 "Current Tag empty, this may mean"
1258 " NightlStartTime was not set correctly." << std::endl);
1259 cmSystemTools::SetFatalErrorOccured();
1261 // find out about the system
1262 cmsys::SystemInformation info;
1263 info.RunCPUCheck();
1264 info.RunOSCheck();
1265 info.RunMemoryCheck();
1266 ostr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1267 << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName")
1268 << "\"\n\tBuildStamp=\"" << this->CurrentTag << "-"
1269 << this->GetTestModelString() << "\"\n\tName=\""
1270 << this->GetCTestConfiguration("Site") << "\"\n\tGenerator=\"ctest-"
1271 << cmVersion::GetCMakeVersion() << "\"\n"
1272 << (append? "\tAppend=\"true\"\n":"")
1273 << "\tOSName=\"" << info.GetOSName() << "\"\n"
1274 << "\tHostname=\"" << info.GetHostname() << "\"\n"
1275 << "\tOSRelease=\"" << info.GetOSRelease() << "\"\n"
1276 << "\tOSVersion=\"" << info.GetOSVersion() << "\"\n"
1277 << "\tOSPlatform=\"" << info.GetOSPlatform() << "\"\n"
1278 << "\tIs64Bits=\"" << info.Is64Bits() << "\"\n"
1279 << "\tVendorString=\"" << info.GetVendorString() << "\"\n"
1280 << "\tVendorID=\"" << info.GetVendorID() << "\"\n"
1281 << "\tFamilyID=\"" << info.GetFamilyID() << "\"\n"
1282 << "\tModelID=\"" << info.GetModelID() << "\"\n"
1283 << "\tProcessorCacheSize=\"" << info.GetProcessorCacheSize() << "\"\n"
1284 << "\tNumberOfLogicalCPU=\"" << info.GetNumberOfLogicalCPU() << "\"\n"
1285 << "\tNumberOfPhysicalCPU=\""<< info.GetNumberOfPhysicalCPU() << "\"\n"
1286 << "\tTotalVirtualMemory=\"" << info.GetTotalVirtualMemory() << "\"\n"
1287 << "\tTotalPhysicalMemory=\""<< info.GetTotalPhysicalMemory() << "\"\n"
1288 << "\tLogicalProcessorsPerPhysical=\""
1289 << info.GetLogicalProcessorsPerPhysical() << "\"\n"
1290 << "\tProcessorClockFrequency=\""
1291 << info.GetProcessorClockFrequency() << "\"\n"
1292 << ">" << std::endl;
1293 this->AddSiteProperties(ostr);
1296 //----------------------------------------------------------------------
1297 void cmCTest::AddSiteProperties(std::ostream& ostr)
1299 cmCTestScriptHandler* ch =
1300 static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
1301 cmake* cm = ch->GetCMake();
1302 // if no CMake then this is the old style script and props like
1303 // this will not work anyway.
1304 if(!cm)
1306 return;
1308 // This code should go when cdash is changed to use labels only
1309 const char* subproject = cm->GetProperty("SubProject", cmProperty::GLOBAL);
1310 if(subproject)
1312 ostr << "<Subproject name=\"" << subproject << "\">\n";
1313 const char* labels =
1314 ch->GetCMake()->GetProperty("SubProjectLabels", cmProperty::GLOBAL);
1315 if(labels)
1317 ostr << " <Labels>\n";
1318 std::string l = labels;
1319 std::vector<std::string> args;
1320 cmSystemTools::ExpandListArgument(l, args);
1321 for(std::vector<std::string>::iterator i = args.begin();
1322 i != args.end(); ++i)
1324 ostr << " <Label>" << i->c_str() << "</Label>\n";
1326 ostr << " </Labels>\n";
1328 ostr << "</Subproject>\n";
1331 // This code should stay when cdash only does label based sub-projects
1332 const char* label = cm->GetProperty("Label", cmProperty::GLOBAL);
1333 if(label)
1335 ostr << "<Labels>\n";
1336 ostr << " <Label>" << label << "</Label>\n";
1337 ostr << "</Labels>\n";
1342 //----------------------------------------------------------------------
1343 void cmCTest::EndXML(std::ostream& ostr)
1345 ostr << "</Site>" << std::endl;
1348 //----------------------------------------------------------------------
1349 int cmCTest::GenerateCTestNotesOutput(std::ostream& os,
1350 const cmCTest::VectorOfStrings& files)
1352 cmCTest::VectorOfStrings::const_iterator it;
1353 os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1354 << "<?xml-stylesheet type=\"text/xsl\" "
1355 "href=\"Dart/Source/Server/XSL/Build.xsl "
1356 "<file:///Dart/Source/Server/XSL/Build.xsl> \"?>\n"
1357 << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName")
1358 << "\" BuildStamp=\""
1359 << this->CurrentTag << "-" << this->GetTestModelString() << "\" Name=\""
1360 << this->GetCTestConfiguration("Site") << "\" Generator=\"ctest"
1361 << cmVersion::GetCMakeVersion()
1362 << "\">\n";
1363 this->AddSiteProperties(os);
1364 os << "<Notes>" << std::endl;
1366 for ( it = files.begin(); it != files.end(); it ++ )
1368 cmCTestLog(this, OUTPUT, "\tAdd file: " << it->c_str() << std::endl);
1369 std::string note_time = this->CurrentTime();
1370 os << "<Note Name=\"" << cmXMLSafe(*it) << "\">\n"
1371 << "<Time>" << cmSystemTools::GetTime() << "</Time>\n"
1372 << "<DateTime>" << note_time << "</DateTime>\n"
1373 << "<Text>" << std::endl;
1374 std::ifstream ifs(it->c_str());
1375 if ( ifs )
1377 std::string line;
1378 while ( cmSystemTools::GetLineFromStream(ifs, line) )
1380 os << cmXMLSafe(line) << std::endl;
1382 ifs.close();
1384 else
1386 os << "Problem reading file: " << it->c_str() << std::endl;
1387 cmCTestLog(this, ERROR_MESSAGE, "Problem reading file: " << it->c_str()
1388 << " while creating notes" << std::endl);
1390 os << "</Text>\n"
1391 << "</Note>" << std::endl;
1393 os << "</Notes>\n"
1394 << "</Site>" << std::endl;
1395 return 1;
1398 //----------------------------------------------------------------------
1399 int cmCTest::GenerateNotesFile(const std::vector<cmStdString> &files)
1401 cmGeneratedFileStream ofs;
1402 if ( !this->OpenOutputFile(this->CurrentTag, "Notes.xml", ofs) )
1404 cmCTestLog(this, ERROR_MESSAGE, "Cannot open notes file" << std::endl);
1405 return 1;
1408 this->GenerateCTestNotesOutput(ofs, files);
1409 return 0;
1412 //----------------------------------------------------------------------
1413 int cmCTest::GenerateNotesFile(const char* cfiles)
1415 if ( !cfiles )
1417 return 1;
1420 std::vector<cmStdString> files;
1422 cmCTestLog(this, OUTPUT, "Create notes file" << std::endl);
1424 files = cmSystemTools::SplitString(cfiles, ';');
1425 if ( files.size() == 0 )
1427 return 1;
1430 return this->GenerateNotesFile(files);
1433 //----------------------------------------------------------------------
1434 bool cmCTest::SubmitExtraFiles(const std::vector<cmStdString> &files)
1436 std::vector<cmStdString>::const_iterator it;
1437 for ( it = files.begin();
1438 it != files.end();
1439 ++ it )
1441 if ( !cmSystemTools::FileExists(it->c_str()) )
1443 cmCTestLog(this, ERROR_MESSAGE, "Cannot find extra file: "
1444 << it->c_str() << " to submit."
1445 << std::endl;);
1446 return false;
1448 this->AddSubmitFile(PartExtraFiles, it->c_str());
1450 return true;
1453 //----------------------------------------------------------------------
1454 bool cmCTest::SubmitExtraFiles(const char* cfiles)
1456 if ( !cfiles )
1458 return 1;
1461 std::vector<cmStdString> files;
1463 cmCTestLog(this, OUTPUT, "Submit extra files" << std::endl);
1465 files = cmSystemTools::SplitString(cfiles, ';');
1466 if ( files.size() == 0 )
1468 return 1;
1471 return this->SubmitExtraFiles(files);
1475 //-------------------------------------------------------
1476 // for a -D argument convert the next argument into
1477 // the proper list of dashboard steps via SetTest
1478 bool cmCTest::AddTestsForDashboardType(std::string &targ)
1480 if ( targ == "Experimental" )
1482 this->SetTestModel(cmCTest::EXPERIMENTAL);
1483 this->SetTest("Start");
1484 this->SetTest("Configure");
1485 this->SetTest("Build");
1486 this->SetTest("Test");
1487 this->SetTest("Coverage");
1488 this->SetTest("Submit");
1490 else if ( targ == "ExperimentalStart" )
1492 this->SetTestModel(cmCTest::EXPERIMENTAL);
1493 this->SetTest("Start");
1495 else if ( targ == "ExperimentalUpdate" )
1497 this->SetTestModel(cmCTest::EXPERIMENTAL);
1498 this->SetTest("Update");
1500 else if ( targ == "ExperimentalConfigure" )
1502 this->SetTestModel(cmCTest::EXPERIMENTAL);
1503 this->SetTest("Configure");
1505 else if ( targ == "ExperimentalBuild" )
1507 this->SetTestModel(cmCTest::EXPERIMENTAL);
1508 this->SetTest("Build");
1510 else if ( targ == "ExperimentalTest" )
1512 this->SetTestModel(cmCTest::EXPERIMENTAL);
1513 this->SetTest("Test");
1515 else if ( targ == "ExperimentalMemCheck"
1516 || targ == "ExperimentalPurify" )
1518 this->SetTestModel(cmCTest::EXPERIMENTAL);
1519 this->SetTest("MemCheck");
1521 else if ( targ == "ExperimentalCoverage" )
1523 this->SetTestModel(cmCTest::EXPERIMENTAL);
1524 this->SetTest("Coverage");
1526 else if ( targ == "ExperimentalSubmit" )
1528 this->SetTestModel(cmCTest::EXPERIMENTAL);
1529 this->SetTest("Submit");
1531 else if ( targ == "Continuous" )
1533 this->SetTestModel(cmCTest::CONTINUOUS);
1534 this->SetTest("Start");
1535 this->SetTest("Update");
1536 this->SetTest("Configure");
1537 this->SetTest("Build");
1538 this->SetTest("Test");
1539 this->SetTest("Coverage");
1540 this->SetTest("Submit");
1542 else if ( targ == "ContinuousStart" )
1544 this->SetTestModel(cmCTest::CONTINUOUS);
1545 this->SetTest("Start");
1547 else if ( targ == "ContinuousUpdate" )
1549 this->SetTestModel(cmCTest::CONTINUOUS);
1550 this->SetTest("Update");
1552 else if ( targ == "ContinuousConfigure" )
1554 this->SetTestModel(cmCTest::CONTINUOUS);
1555 this->SetTest("Configure");
1557 else if ( targ == "ContinuousBuild" )
1559 this->SetTestModel(cmCTest::CONTINUOUS);
1560 this->SetTest("Build");
1562 else if ( targ == "ContinuousTest" )
1564 this->SetTestModel(cmCTest::CONTINUOUS);
1565 this->SetTest("Test");
1567 else if ( targ == "ContinuousMemCheck"
1568 || targ == "ContinuousPurify" )
1570 this->SetTestModel(cmCTest::CONTINUOUS);
1571 this->SetTest("MemCheck");
1573 else if ( targ == "ContinuousCoverage" )
1575 this->SetTestModel(cmCTest::CONTINUOUS);
1576 this->SetTest("Coverage");
1578 else if ( targ == "ContinuousSubmit" )
1580 this->SetTestModel(cmCTest::CONTINUOUS);
1581 this->SetTest("Submit");
1583 else if ( targ == "Nightly" )
1585 this->SetTestModel(cmCTest::NIGHTLY);
1586 this->SetTest("Start");
1587 this->SetTest("Update");
1588 this->SetTest("Configure");
1589 this->SetTest("Build");
1590 this->SetTest("Test");
1591 this->SetTest("Coverage");
1592 this->SetTest("Submit");
1594 else if ( targ == "NightlyStart" )
1596 this->SetTestModel(cmCTest::NIGHTLY);
1597 this->SetTest("Start");
1599 else if ( targ == "NightlyUpdate" )
1601 this->SetTestModel(cmCTest::NIGHTLY);
1602 this->SetTest("Update");
1604 else if ( targ == "NightlyConfigure" )
1606 this->SetTestModel(cmCTest::NIGHTLY);
1607 this->SetTest("Configure");
1609 else if ( targ == "NightlyBuild" )
1611 this->SetTestModel(cmCTest::NIGHTLY);
1612 this->SetTest("Build");
1614 else if ( targ == "NightlyTest" )
1616 this->SetTestModel(cmCTest::NIGHTLY);
1617 this->SetTest("Test");
1619 else if ( targ == "NightlyMemCheck"
1620 || targ == "NightlyPurify" )
1622 this->SetTestModel(cmCTest::NIGHTLY);
1623 this->SetTest("MemCheck");
1625 else if ( targ == "NightlyCoverage" )
1627 this->SetTestModel(cmCTest::NIGHTLY);
1628 this->SetTest("Coverage");
1630 else if ( targ == "NightlySubmit" )
1632 this->SetTestModel(cmCTest::NIGHTLY);
1633 this->SetTest("Submit");
1635 else if ( targ == "MemoryCheck" )
1637 this->SetTestModel(cmCTest::EXPERIMENTAL);
1638 this->SetTest("Start");
1639 this->SetTest("Configure");
1640 this->SetTest("Build");
1641 this->SetTest("MemCheck");
1642 this->SetTest("Coverage");
1643 this->SetTest("Submit");
1645 else if ( targ == "NightlyMemoryCheck" )
1647 this->SetTestModel(cmCTest::NIGHTLY);
1648 this->SetTest("Start");
1649 this->SetTest("Update");
1650 this->SetTest("Configure");
1651 this->SetTest("Build");
1652 this->SetTest("MemCheck");
1653 this->SetTest("Coverage");
1654 this->SetTest("Submit");
1656 else
1658 cmCTestLog(this, ERROR_MESSAGE,
1659 "CTest -D called with incorrect option: "
1660 << targ << std::endl);
1661 cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
1662 << " " << "ctest" << " -D Continuous" << std::endl
1663 << " " << "ctest"
1664 << " -D Continuous(Start|Update|Configure|Build)" << std::endl
1665 << " " << "ctest"
1666 << " -D Continuous(Test|Coverage|MemCheck|Submit)"
1667 << std::endl
1668 << " " << "ctest" << " -D Experimental" << std::endl
1669 << " " << "ctest"
1670 << " -D Experimental(Start|Update|Configure|Build)"
1671 << std::endl
1672 << " " << "ctest"
1673 << " -D Experimental(Test|Coverage|MemCheck|Submit)"
1674 << std::endl
1675 << " " << "ctest" << " -D Nightly" << std::endl
1676 << " " << "ctest"
1677 << " -D Nightly(Start|Update|Configure|Build)" << std::endl
1678 << " " << "ctest"
1679 << " -D Nightly(Test|Coverage|MemCheck|Submit)" << std::endl
1680 << " " << "ctest" << " -D NightlyMemoryCheck" << std::endl);
1681 return false;
1683 return true;
1687 //----------------------------------------------------------------------
1688 bool cmCTest::CheckArgument(const std::string& arg, const char* varg1,
1689 const char* varg2)
1691 if ( varg1 && arg == varg1 || varg2 && arg == varg2 )
1693 return true;
1695 return false;
1699 //----------------------------------------------------------------------
1700 // Processes one command line argument (and its arguments if any)
1701 // for many simple options and then returns
1702 void cmCTest::HandleCommandLineArguments(size_t &i,
1703 std::vector<std::string> &args)
1705 std::string arg = args[i];
1707 if(this->CheckArgument(arg, "-j", "--parallel") && i < args.size() - 1)
1709 i++;
1710 int plevel = atoi(args[i].c_str());
1711 this->SetParallelLevel(plevel);
1713 else if(arg.find("-j") == 0)
1715 int plevel = atoi(arg.substr(2).c_str());
1716 this->SetParallelLevel(plevel);
1718 if(this->CheckArgument(arg, "--internal-ctest-parallel")
1719 && i < args.size() - 1)
1721 i++;
1722 int pid = atoi(args[i].c_str());
1723 this->SetParallelSubprocessId(pid);
1724 this->SetParallelSubprocess();
1727 if(this->CheckArgument(arg, "--parallel-cache") && i < args.size() - 1)
1729 i++;
1730 this->SetParallelCacheFile(args[i].c_str());
1733 if(this->CheckArgument(arg, "-C", "--build-config") &&
1734 i < args.size() - 1)
1736 i++;
1737 this->SetConfigType(args[i].c_str());
1740 if(this->CheckArgument(arg, "--debug"))
1742 this->Debug = true;
1743 this->ShowLineNumbers = true;
1745 if(this->CheckArgument(arg, "--track") && i < args.size() - 1)
1747 i++;
1748 this->SpecificTrack = args[i];
1750 if(this->CheckArgument(arg, "--show-line-numbers"))
1752 this->ShowLineNumbers = true;
1754 if(this->CheckArgument(arg, "-Q", "--quiet"))
1756 this->Quiet = true;
1758 if(this->CheckArgument(arg, "-V", "--verbose"))
1760 this->Verbose = true;
1762 if(this->CheckArgument(arg, "-VV", "--extra-verbose"))
1764 this->ExtraVerbose = true;
1765 this->Verbose = true;
1767 if(this->CheckArgument(arg, "--output-on-failure"))
1769 this->OutputTestOutputOnTestFailure = true;
1772 if(this->CheckArgument(arg, "-N", "--show-only"))
1774 this->ShowOnly = true;
1777 if(this->CheckArgument(arg, "-O", "--output-log") && i < args.size() - 1 )
1779 i++;
1780 this->SetOutputLogFileName(args[i].c_str());
1783 if(this->CheckArgument(arg, "--tomorrow-tag"))
1785 this->TomorrowTag = true;
1787 if(this->CheckArgument(arg, "--force-new-ctest-process"))
1789 this->ForceNewCTestProcess = true;
1791 if(this->CheckArgument(arg, "-W", "--max-width") && i < args.size() - 1)
1793 i++;
1794 this->MaxTestNameWidth = atoi(args[i].c_str());
1796 if(this->CheckArgument(arg, "--interactive-debug-mode") &&
1797 i < args.size() - 1 )
1799 i++;
1800 this->InteractiveDebugMode = cmSystemTools::IsOn(args[i].c_str());
1802 if(this->CheckArgument(arg, "--submit-index") && i < args.size() - 1 )
1804 i++;
1805 this->SubmitIndex = atoi(args[i].c_str());
1806 if ( this->SubmitIndex < 0 )
1808 this->SubmitIndex = 0;
1812 if(this->CheckArgument(arg, "--overwrite") && i < args.size() - 1)
1814 i++;
1815 this->AddCTestConfigurationOverwrite(args[i].c_str());
1817 if(this->CheckArgument(arg, "-A", "--add-notes") && i < args.size() - 1)
1819 this->ProduceXML = true;
1820 this->SetTest("Notes");
1821 i++;
1822 this->SetNotesFiles(args[i].c_str());
1825 // options that control what tests are run
1826 if(this->CheckArgument(arg, "-I", "--tests-information") &&
1827 i < args.size() - 1)
1829 i++;
1830 this->GetHandler("test")->SetPersistentOption("TestsToRunInformation",
1831 args[i].c_str());
1832 this->GetHandler("memcheck")->
1833 SetPersistentOption("TestsToRunInformation",args[i].c_str());
1835 if(this->CheckArgument(arg, "-U", "--union"))
1837 this->GetHandler("test")->SetPersistentOption("UseUnion", "true");
1838 this->GetHandler("memcheck")->SetPersistentOption("UseUnion", "true");
1840 if(this->CheckArgument(arg, "-R", "--tests-regex") && i < args.size() - 1)
1842 i++;
1843 this->GetHandler("test")->
1844 SetPersistentOption("IncludeRegularExpression", args[i].c_str());
1845 this->GetHandler("memcheck")->
1846 SetPersistentOption("IncludeRegularExpression", args[i].c_str());
1848 if(this->CheckArgument(arg, "-L", "--label-regex") && i < args.size() - 1)
1850 i++;
1851 this->GetHandler("test")->
1852 SetPersistentOption("LabelRegularExpression", args[i].c_str());
1853 this->GetHandler("memcheck")->
1854 SetPersistentOption("LabelRegularExpression", args[i].c_str());
1856 if(this->CheckArgument(arg, "-LE", "--label-exclude") && i < args.size() - 1)
1858 i++;
1859 this->GetHandler("test")->
1860 SetPersistentOption("ExcludeLabelRegularExpression", args[i].c_str());
1861 this->GetHandler("memcheck")->
1862 SetPersistentOption("ExcludeLabelRegularExpression", args[i].c_str());
1865 if(this->CheckArgument(arg, "-E", "--exclude-regex") &&
1866 i < args.size() - 1)
1868 i++;
1869 this->GetHandler("test")->
1870 SetPersistentOption("ExcludeRegularExpression", args[i].c_str());
1871 this->GetHandler("memcheck")->
1872 SetPersistentOption("ExcludeRegularExpression", args[i].c_str());
1876 //----------------------------------------------------------------------
1877 // handle the -S -SR and -SP arguments
1878 void cmCTest::HandleScriptArguments(size_t &i,
1879 std::vector<std::string> &args,
1880 bool &SRArgumentSpecified)
1882 std::string arg = args[i];
1883 if(this->CheckArgument(arg, "-SP", "--script-new-process") &&
1884 i < args.size() - 1 )
1886 this->RunConfigurationScript = true;
1887 i++;
1888 cmCTestScriptHandler* ch
1889 = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
1890 // -SR is an internal argument, -SP should be ignored when it is passed
1891 if (!SRArgumentSpecified)
1893 ch->AddConfigurationScript(args[i].c_str(),false);
1897 if(this->CheckArgument(arg, "-SR", "--script-run") &&
1898 i < args.size() - 1 )
1900 SRArgumentSpecified = true;
1901 this->RunConfigurationScript = true;
1902 i++;
1903 cmCTestScriptHandler* ch
1904 = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
1905 ch->AddConfigurationScript(args[i].c_str(),true);
1908 if(this->CheckArgument(arg, "-S", "--script") && i < args.size() - 1 )
1910 this->RunConfigurationScript = true;
1911 i++;
1912 cmCTestScriptHandler* ch
1913 = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
1914 // -SR is an internal argument, -S should be ignored when it is passed
1915 if (!SRArgumentSpecified)
1917 ch->AddConfigurationScript(args[i].c_str(),true);
1922 //----------------------------------------------------------------------
1923 // the main entry point of ctest, called from main
1924 int cmCTest::Run(std::vector<std::string> &args, std::string* output)
1926 this->FindRunningCMake();
1927 const char* ctestExec = "ctest";
1928 bool cmakeAndTest = false;
1929 bool performSomeTest = true;
1930 bool SRArgumentSpecified = false;
1932 // copy the command line
1933 for(size_t i=0; i < args.size(); ++i)
1935 this->InitialCommandLineArguments.push_back(args[i]);
1938 // process the command line arguments
1939 for(size_t i=1; i < args.size(); ++i)
1941 // handle the simple commandline arguments
1942 this->HandleCommandLineArguments(i,args);
1944 // handle the script arguments -S -SR -SP
1945 this->HandleScriptArguments(i,args,SRArgumentSpecified);
1947 // handle a request for a dashboard
1948 std::string arg = args[i];
1949 if(this->CheckArgument(arg, "-D", "--dashboard") && i < args.size() - 1 )
1951 this->ProduceXML = true;
1952 i++;
1953 std::string targ = args[i];
1954 // AddTestsForDashboard parses the dashborad type and converts it
1955 // into the seperate stages
1956 if (!this->AddTestsForDashboardType(targ))
1958 performSomeTest = false;
1962 if(this->CheckArgument(arg, "-T", "--test-action") &&
1963 (i < args.size() -1) )
1965 this->ProduceXML = true;
1966 i++;
1967 if ( !this->SetTest(args[i].c_str(), false) )
1969 performSomeTest = false;
1970 cmCTestLog(this, ERROR_MESSAGE,
1971 "CTest -T called with incorrect option: "
1972 << args[i].c_str() << std::endl);
1973 cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
1974 << " " << ctestExec << " -T all" << std::endl
1975 << " " << ctestExec << " -T start" << std::endl
1976 << " " << ctestExec << " -T update" << std::endl
1977 << " " << ctestExec << " -T configure" << std::endl
1978 << " " << ctestExec << " -T build" << std::endl
1979 << " " << ctestExec << " -T test" << std::endl
1980 << " " << ctestExec << " -T coverage" << std::endl
1981 << " " << ctestExec << " -T memcheck" << std::endl
1982 << " " << ctestExec << " -T notes" << std::endl
1983 << " " << ctestExec << " -T submit" << std::endl);
1987 // what type of test model
1988 if(this->CheckArgument(arg, "-M", "--test-model") &&
1989 (i < args.size() -1) )
1991 i++;
1992 std::string const& str = args[i];
1993 if ( cmSystemTools::LowerCase(str) == "nightly" )
1995 this->SetTestModel(cmCTest::NIGHTLY);
1997 else if ( cmSystemTools::LowerCase(str) == "continuous" )
1999 this->SetTestModel(cmCTest::CONTINUOUS);
2001 else if ( cmSystemTools::LowerCase(str) == "experimental" )
2003 this->SetTestModel(cmCTest::EXPERIMENTAL);
2005 else
2007 performSomeTest = false;
2008 cmCTestLog(this, ERROR_MESSAGE,
2009 "CTest -M called with incorrect option: " << str.c_str()
2010 << std::endl);
2011 cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
2012 << " " << ctestExec << " -M Continuous" << std::endl
2013 << " " << ctestExec << " -M Experimental" << std::endl
2014 << " " << ctestExec << " -M Nightly" << std::endl);
2018 if(this->CheckArgument(arg, "--extra-submit") && i < args.size() - 1)
2020 this->ProduceXML = true;
2021 this->SetTest("Submit");
2022 i++;
2023 if ( !this->SubmitExtraFiles(args[i].c_str()) )
2025 return 0;
2029 // --build-and-test options
2030 if(this->CheckArgument(arg, "--build-and-test") && i < args.size() - 1)
2032 cmakeAndTest = true;
2035 // pass the argument to all the handlers as well, but i may no longer be
2036 // set to what it was originally so I'm not sure this is working as
2037 // intended
2038 cmCTest::t_TestingHandlers::iterator it;
2039 for ( it = this->TestingHandlers.begin();
2040 it != this->TestingHandlers.end();
2041 ++ it )
2043 if ( !it->second->ProcessCommandLineArguments(arg, i, args) )
2045 cmCTestLog(this, ERROR_MESSAGE,
2046 "Problem parsing command line arguments within a handler");
2047 return 0;
2050 } // the close of the for argument loop
2053 // now what sould cmake do? if --build-and-test was specified then
2054 // we run the build and test handler and return
2055 if(cmakeAndTest)
2057 this->Verbose = true;
2058 cmCTestBuildAndTestHandler* handler =
2059 static_cast<cmCTestBuildAndTestHandler*>(this->GetHandler("buildtest"));
2060 int retv = handler->ProcessHandler();
2061 *output = handler->GetOutput();
2062 #ifdef CMAKE_BUILD_WITH_CMAKE
2063 cmDynamicLoader::FlushCache();
2064 #endif
2065 if(retv != 0)
2067 cmCTestLog(this, DEBUG, "build and test failing returing: " << retv
2068 << std::endl);
2070 return retv;
2073 // if some tests must be run
2074 if(performSomeTest)
2076 int res;
2077 // call process directory
2078 if (this->RunConfigurationScript)
2080 if ( this->ExtraVerbose )
2082 cmCTestLog(this, OUTPUT, "* Extra verbosity turned on" << std::endl);
2084 cmCTest::t_TestingHandlers::iterator it;
2085 for ( it = this->TestingHandlers.begin();
2086 it != this->TestingHandlers.end();
2087 ++ it )
2089 it->second->SetVerbose(this->ExtraVerbose);
2090 it->second->SetSubmitIndex(this->SubmitIndex);
2092 this->GetHandler("script")->SetVerbose(this->Verbose);
2093 res = this->GetHandler("script")->ProcessHandler();
2094 if(res != 0)
2096 cmCTestLog(this, DEBUG, "running script failing returing: " << res
2097 << std::endl);
2101 else
2103 // What is this? -V seems to be the same as -VV,
2104 // and Verbose is always on in this case
2105 this->ExtraVerbose = this->Verbose;
2106 this->Verbose = true;
2107 cmCTest::t_TestingHandlers::iterator it;
2108 for ( it = this->TestingHandlers.begin();
2109 it != this->TestingHandlers.end();
2110 ++ it )
2112 it->second->SetVerbose(this->Verbose);
2113 it->second->SetSubmitIndex(this->SubmitIndex);
2115 if ( !this->Initialize(
2116 cmSystemTools::GetCurrentWorkingDirectory().c_str()) )
2118 res = 12;
2119 cmCTestLog(this, ERROR_MESSAGE, "Problem initializing the dashboard."
2120 << std::endl);
2122 else
2124 res = this->ProcessTests();
2126 this->Finalize();
2128 if(res != 0)
2130 cmCTestLog(this, DEBUG, "Running a test(s) failed returning : " << res
2131 << std::endl);
2133 return res;
2136 return 1;
2139 //----------------------------------------------------------------------
2140 void cmCTest::FindRunningCMake()
2142 // Find our own executable.
2143 this->CTestSelf = cmSystemTools::GetExecutableDirectory();
2144 this->CTestSelf += "/ctest";
2145 this->CTestSelf += cmSystemTools::GetExecutableExtension();
2146 if(!cmSystemTools::FileExists(this->CTestSelf.c_str()))
2148 cmSystemTools::Error("CTest executable cannot be found at ",
2149 this->CTestSelf.c_str());
2152 this->CMakeSelf = cmSystemTools::GetExecutableDirectory();
2153 this->CMakeSelf += "/cmake";
2154 this->CMakeSelf += cmSystemTools::GetExecutableExtension();
2155 if(!cmSystemTools::FileExists(this->CMakeSelf.c_str()))
2157 cmSystemTools::Error("CMake executable cannot be found at ",
2158 this->CMakeSelf.c_str());
2162 //----------------------------------------------------------------------
2163 void cmCTest::SetNotesFiles(const char* notes)
2165 if ( !notes )
2167 return;
2169 this->NotesFiles = notes;
2172 //----------------------------------------------------------------------
2173 int cmCTest::ReadCustomConfigurationFileTree(const char* dir, cmMakefile* mf)
2175 bool found = false;
2176 VectorOfStrings dirs;
2177 VectorOfStrings ndirs;
2178 cmCTestLog(this, DEBUG, "* Read custom CTest configuration directory: "
2179 << dir << std::endl);
2181 std::string fname = dir;
2182 fname += "/CTestCustom.cmake";
2183 cmCTestLog(this, DEBUG, "* Check for file: "
2184 << fname.c_str() << std::endl);
2185 if ( cmSystemTools::FileExists(fname.c_str()) )
2187 cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: "
2188 << fname.c_str() << std::endl);
2189 bool erroroc = cmSystemTools::GetErrorOccuredFlag();
2190 cmSystemTools::ResetErrorOccuredFlag();
2192 if ( !mf->ReadListFile(0, fname.c_str()) ||
2193 cmSystemTools::GetErrorOccuredFlag() )
2195 cmCTestLog(this, ERROR_MESSAGE,
2196 "Problem reading custom configuration: "
2197 << fname.c_str() << std::endl);
2199 found = true;
2200 if ( erroroc )
2202 cmSystemTools::SetErrorOccured();
2206 std::string rexpr = dir;
2207 rexpr += "/CTestCustom.ctest";
2208 cmCTestLog(this, DEBUG, "* Check for file: "
2209 << rexpr.c_str() << std::endl);
2210 if ( !found && cmSystemTools::FileExists(rexpr.c_str()) )
2212 cmsys::Glob gl;
2213 gl.RecurseOn();
2214 gl.FindFiles(rexpr);
2215 std::vector<std::string>& files = gl.GetFiles();
2216 std::vector<std::string>::iterator fileIt;
2217 for ( fileIt = files.begin(); fileIt != files.end();
2218 ++ fileIt )
2220 cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: "
2221 << fileIt->c_str() << std::endl);
2222 if ( !mf->ReadListFile(0, fileIt->c_str()) ||
2223 cmSystemTools::GetErrorOccuredFlag() )
2225 cmCTestLog(this, ERROR_MESSAGE,
2226 "Problem reading custom configuration: "
2227 << fileIt->c_str() << std::endl);
2230 found = true;
2233 if ( found )
2235 cmCTest::t_TestingHandlers::iterator it;
2236 for ( it = this->TestingHandlers.begin();
2237 it != this->TestingHandlers.end(); ++ it )
2239 cmCTestLog(this, DEBUG,
2240 "* Read custom CTest configuration vectors for handler: "
2241 << it->first.c_str() << " (" << it->second << ")" << std::endl);
2242 it->second->PopulateCustomVectors(mf);
2246 return 1;
2249 //----------------------------------------------------------------------
2250 void cmCTest::PopulateCustomVector(cmMakefile* mf, const char* def,
2251 VectorOfStrings& vec)
2253 if ( !def)
2255 return;
2257 const char* dval = mf->GetDefinition(def);
2258 if ( !dval )
2260 return;
2262 cmCTestLog(this, DEBUG, "PopulateCustomVector: " << def << std::endl);
2263 std::vector<std::string> slist;
2264 cmSystemTools::ExpandListArgument(dval, slist);
2265 std::vector<std::string>::iterator it;
2267 for ( it = slist.begin(); it != slist.end(); ++it )
2269 cmCTestLog(this, DEBUG, " -- " << it->c_str() << std::endl);
2270 vec.push_back(it->c_str());
2274 //----------------------------------------------------------------------
2275 void cmCTest::PopulateCustomInteger(cmMakefile* mf, const char* def, int& val)
2277 if ( !def)
2279 return;
2281 const char* dval = mf->GetDefinition(def);
2282 if ( !dval )
2284 return;
2286 val = atoi(dval);
2289 //----------------------------------------------------------------------
2290 std::string cmCTest::GetShortPathToFile(const char* cfname)
2292 const std::string& sourceDir
2293 = cmSystemTools::CollapseFullPath(
2294 this->GetCTestConfiguration("SourceDirectory").c_str());
2295 const std::string& buildDir
2296 = cmSystemTools::CollapseFullPath(
2297 this->GetCTestConfiguration("BuildDirectory").c_str());
2298 std::string fname = cmSystemTools::CollapseFullPath(cfname);
2300 // Find relative paths to both directories
2301 std::string srcRelpath
2302 = cmSystemTools::RelativePath(sourceDir.c_str(), fname.c_str());
2303 std::string bldRelpath
2304 = cmSystemTools::RelativePath(buildDir.c_str(), fname.c_str());
2306 // If any contains "." it is not parent directory
2307 bool inSrc = srcRelpath.find("..") == srcRelpath.npos;
2308 bool inBld = bldRelpath.find("..") == bldRelpath.npos;
2309 // TODO: Handle files with .. in their name
2311 std::string* res = 0;
2313 if ( inSrc && inBld )
2315 // If both have relative path with no dots, pick the shorter one
2316 if ( srcRelpath.size() < bldRelpath.size() )
2318 res = &srcRelpath;
2320 else
2322 res = &bldRelpath;
2325 else if ( inSrc )
2327 res = &srcRelpath;
2329 else if ( inBld )
2331 res = &bldRelpath;
2334 std::string path;
2336 if ( !res )
2338 path = fname;
2340 else
2342 cmSystemTools::ConvertToUnixSlashes(*res);
2344 path = "./" + *res;
2345 if ( path[path.size()-1] == '/' )
2347 path = path.substr(0, path.size()-1);
2351 cmsys::SystemTools::ReplaceString(path, ":", "_");
2352 cmsys::SystemTools::ReplaceString(path, " ", "_");
2353 return path;
2356 //----------------------------------------------------------------------
2357 std::string cmCTest::GetCTestConfiguration(const char *name)
2359 if ( this->CTestConfigurationOverwrites.find(name) !=
2360 this->CTestConfigurationOverwrites.end() )
2362 return this->CTestConfigurationOverwrites[name];
2364 return this->CTestConfiguration[name];
2367 //----------------------------------------------------------------------
2368 void cmCTest::EmptyCTestConfiguration()
2370 this->CTestConfiguration.clear();
2373 //----------------------------------------------------------------------
2374 void cmCTest::SetCTestConfiguration(const char *name, const char* value)
2376 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "SetCTestConfiguration:"
2377 << name << ":" << value << "\n");
2379 if ( !name )
2381 return;
2383 if ( !value )
2385 this->CTestConfiguration.erase(name);
2386 return;
2388 this->CTestConfiguration[name] = value;
2392 //----------------------------------------------------------------------
2393 std::string cmCTest::GetCurrentTag()
2395 return this->CurrentTag;
2398 //----------------------------------------------------------------------
2399 std::string cmCTest::GetBinaryDir()
2401 return this->BinaryDir;
2404 //----------------------------------------------------------------------
2405 std::string const& cmCTest::GetConfigType()
2407 return this->ConfigType;
2410 //----------------------------------------------------------------------
2411 bool cmCTest::GetShowOnly()
2413 return this->ShowOnly;
2416 //----------------------------------------------------------------------
2417 int cmCTest::GetMaxTestNameWidth() const
2419 return this->MaxTestNameWidth;
2422 //----------------------------------------------------------------------
2423 void cmCTest::SetProduceXML(bool v)
2425 this->ProduceXML = v;
2428 //----------------------------------------------------------------------
2429 bool cmCTest::GetProduceXML()
2431 return this->ProduceXML;
2434 //----------------------------------------------------------------------
2435 const char* cmCTest::GetSpecificTrack()
2437 if ( this->SpecificTrack.empty() )
2439 return 0;
2441 return this->SpecificTrack.c_str();
2444 //----------------------------------------------------------------------
2445 void cmCTest::SetSpecificTrack(const char* track)
2447 if ( !track )
2449 this->SpecificTrack = "";
2450 return;
2452 this->SpecificTrack = track;
2455 //----------------------------------------------------------------------
2456 void cmCTest::AddSubmitFile(Part part, const char* name)
2458 this->Parts[part].SubmitFiles.push_back(name);
2461 //----------------------------------------------------------------------
2462 void cmCTest::AddCTestConfigurationOverwrite(const char* encstr)
2464 std::string overStr = encstr;
2465 size_t epos = overStr.find("=");
2466 if ( epos == overStr.npos )
2468 cmCTestLog(this, ERROR_MESSAGE,
2469 "CTest configuration overwrite specified in the wrong format."
2470 << std::endl
2471 << "Valid format is: --overwrite key=value" << std::endl
2472 << "The specified was: --overwrite " << overStr.c_str() << std::endl);
2473 return;
2475 std::string key = overStr.substr(0, epos);
2476 std::string value = overStr.substr(epos+1, overStr.npos);
2477 this->CTestConfigurationOverwrites[key] = value;
2480 //----------------------------------------------------------------------
2481 void cmCTest::SetConfigType(const char* ct)
2483 this->ConfigType = ct?ct:"";
2484 cmSystemTools::ReplaceString(this->ConfigType, ".\\", "");
2485 std::string confTypeEnv
2486 = "CMAKE_CONFIG_TYPE=" + this->ConfigType;
2487 cmSystemTools::PutEnv(confTypeEnv.c_str());
2490 //----------------------------------------------------------------------
2491 bool cmCTest::SetCTestConfigurationFromCMakeVariable(cmMakefile* mf,
2492 const char* dconfig, const char* cmake_var)
2494 const char* ctvar;
2495 ctvar = mf->GetDefinition(cmake_var);
2496 if ( !ctvar )
2498 return false;
2500 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
2501 "SetCTestConfigurationFromCMakeVariable:"
2502 << dconfig << ":" << cmake_var);
2503 this->SetCTestConfiguration(dconfig, ctvar);
2504 return true;
2507 bool cmCTest::RunCommand(
2508 const char* command,
2509 std::string* stdOut,
2510 std::string* stdErr,
2511 int *retVal,
2512 const char* dir,
2513 double timeout)
2515 std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
2517 if(args.size() < 1)
2519 return false;
2522 std::vector<const char*> argv;
2523 for(std::vector<cmStdString>::const_iterator a = args.begin();
2524 a != args.end(); ++a)
2526 argv.push_back(a->c_str());
2528 argv.push_back(0);
2530 *stdOut = "";
2531 *stdErr = "";
2533 cmsysProcess* cp = cmsysProcess_New();
2534 cmsysProcess_SetCommand(cp, &*argv.begin());
2535 cmsysProcess_SetWorkingDirectory(cp, dir);
2536 if(cmSystemTools::GetRunCommandHideConsole())
2538 cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
2540 cmsysProcess_SetTimeout(cp, timeout);
2541 cmsysProcess_Execute(cp);
2543 std::vector<char> tempOutput;
2544 std::vector<char> tempError;
2545 char* data;
2546 int length;
2547 int res;
2548 bool done = false;
2549 while(!done)
2551 res = cmsysProcess_WaitForData(cp, &data, &length, 0);
2552 switch ( res )
2554 case cmsysProcess_Pipe_STDOUT:
2555 tempOutput.insert(tempOutput.end(), data, data+length);
2556 break;
2557 case cmsysProcess_Pipe_STDERR:
2558 tempError.insert(tempError.end(), data, data+length);
2559 break;
2560 default:
2561 done = true;
2563 if ( (res == cmsysProcess_Pipe_STDOUT ||
2564 res == cmsysProcess_Pipe_STDERR) && this->ExtraVerbose )
2566 cmSystemTools::Stdout(data, length);
2570 cmsysProcess_WaitForExit(cp, 0);
2571 if ( tempOutput.size() > 0 )
2573 stdOut->append(&*tempOutput.begin(), tempOutput.size());
2575 if ( tempError.size() > 0 )
2577 stdErr->append(&*tempError.begin(), tempError.size());
2580 bool result = true;
2581 if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
2583 if ( retVal )
2585 *retVal = cmsysProcess_GetExitValue(cp);
2587 else
2589 if ( cmsysProcess_GetExitValue(cp) != 0 )
2591 result = false;
2595 else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
2597 const char* exception_str = cmsysProcess_GetExceptionString(cp);
2598 cmCTestLog(this, ERROR_MESSAGE, exception_str << std::endl);
2599 stdErr->append(exception_str, strlen(exception_str));
2600 result = false;
2602 else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
2604 const char* error_str = cmsysProcess_GetErrorString(cp);
2605 cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
2606 stdErr->append(error_str, strlen(error_str));
2607 result = false;
2609 else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
2611 const char* error_str = "Process terminated due to timeout\n";
2612 cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
2613 stdErr->append(error_str, strlen(error_str));
2614 result = false;
2617 cmsysProcess_Delete(cp);
2618 return result;
2621 //----------------------------------------------------------------------
2622 void cmCTest::SetOutputLogFileName(const char* name)
2624 if ( this->OutputLogFile)
2626 delete this->OutputLogFile;
2627 this->OutputLogFile= 0;
2629 if ( name )
2631 this->OutputLogFile = new cmGeneratedFileStream(name);
2635 //----------------------------------------------------------------------
2636 static const char* cmCTestStringLogType[] =
2638 "DEBUG",
2639 "OUTPUT",
2640 "HANDLER_OUTPUT",
2641 "HANDLER_VERBOSE_OUTPUT",
2642 "WARNING",
2643 "ERROR_MESSAGE",
2647 //----------------------------------------------------------------------
2648 #ifdef cerr
2649 # undef cerr
2650 #endif
2651 #ifdef cout
2652 # undef cout
2653 #endif
2655 #define cmCTestLogOutputFileLine(stream) \
2656 if ( this->ShowLineNumbers ) \
2658 (stream) << std::endl << file << ":" << line << " "; \
2661 void cmCTest::InitStreams()
2663 // By default we write output to the process output streams.
2664 this->StreamOut = &std::cout;
2665 this->StreamErr = &std::cerr;
2668 void cmCTest::Log(int logType, const char* file, int line, const char* msg)
2670 if ( !msg || !*msg )
2672 return;
2674 if ( this->OutputLogFile )
2676 bool display = true;
2677 if ( logType == cmCTest::DEBUG && !this->Debug ) { display = false; }
2678 if ( logType == cmCTest::HANDLER_VERBOSE_OUTPUT && !this->Debug &&
2679 !this->ExtraVerbose ) { display = false; }
2680 if ( display )
2682 cmCTestLogOutputFileLine(*this->OutputLogFile);
2683 if ( logType != this->OutputLogFileLastTag )
2685 *this->OutputLogFile << "[";
2686 if ( logType >= OTHER || logType < 0 )
2688 *this->OutputLogFile << "OTHER";
2690 else
2692 *this->OutputLogFile << cmCTestStringLogType[logType];
2694 *this->OutputLogFile << "] " << std::endl << std::flush;
2696 *this->OutputLogFile << msg << std::flush;
2697 if ( logType != this->OutputLogFileLastTag )
2699 *this->OutputLogFile << std::endl << std::flush;
2700 this->OutputLogFileLastTag = logType;
2704 if ( !this->Quiet )
2706 std::ostream& out = *this->StreamOut;
2707 std::ostream& err = *this->StreamErr;
2708 switch ( logType )
2710 case DEBUG:
2711 if ( this->Debug )
2713 cmCTestLogOutputFileLine(out);
2714 out << msg;
2715 out.flush();
2717 break;
2718 case OUTPUT: case HANDLER_OUTPUT:
2719 if ( this->Debug || this->Verbose )
2721 cmCTestLogOutputFileLine(out);
2722 out << msg;
2723 out.flush();
2725 break;
2726 case HANDLER_VERBOSE_OUTPUT:
2727 if ( this->Debug || this->ExtraVerbose )
2729 cmCTestLogOutputFileLine(out);
2730 out << msg;
2731 out.flush();
2733 break;
2734 case WARNING:
2735 cmCTestLogOutputFileLine(err);
2736 err << msg;
2737 err.flush();
2738 break;
2739 case ERROR_MESSAGE:
2740 cmCTestLogOutputFileLine(err);
2741 err << msg;
2742 err.flush();
2743 cmSystemTools::SetErrorOccured();
2744 break;
2745 default:
2746 cmCTestLogOutputFileLine(out);
2747 out << msg;
2748 out.flush();
2753 //-------------------------------------------------------------------------
2754 double cmCTest::GetRemainingTimeAllowed()
2756 if (!this->GetHandler("script"))
2758 return 1.0e7;
2761 cmCTestScriptHandler* ch
2762 = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
2764 return ch->GetRemainingTimeAllowed();
2767 //----------------------------------------------------------------------
2768 void cmCTest::OutputTestErrors(std::vector<char> const &process_output)
2770 std::string test_outputs("\n*** Test Failed:\n");
2771 test_outputs.append(&*process_output.begin(), process_output.size());
2772 cmCTestLog(this, HANDLER_OUTPUT, test_outputs << std::endl << std::flush);