BUG: Fix find_* search order with path suffixes
[cmake.git] / Source / cmCTest.cxx
blob77c1ba3747b04f098c19a2a1f760356aff0af45f
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCTest.cxx,v $
5 Language: C++
6 Date: $Date: 2008-10-01 13:04:26 $
7 Version: $Revision: 1.339 $
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 "cmCTestCommand.h"
30 #include "cmCTestBuildHandler.h"
31 #include "cmCTestBuildAndTestHandler.h"
32 #include "cmCTestConfigureHandler.h"
33 #include "cmCTestCoverageHandler.h"
34 #include "cmCTestMemCheckHandler.h"
35 #include "cmCTestScriptHandler.h"
36 #include "cmCTestTestHandler.h"
37 #include "cmCTestUpdateHandler.h"
38 #include "cmCTestSubmitHandler.h"
40 #include "cmVersion.h"
42 #include <cmsys/RegularExpression.hxx>
43 #include <cmsys/Process.h>
44 #include <cmsys/Glob.hxx>
46 #include <stdlib.h>
47 #include <math.h>
48 #include <float.h>
50 #include <memory> // auto_ptr
52 #if defined(__BEOS__) && !defined(__HAIKU__)
53 #include <be/kernel/OS.h> /* disable_debugger() API. */
54 #endif
56 #if defined(__HAIKU__)
57 #include <os/kernel/OS.h> /* disable_debugger() API. */
58 #endif
61 #define DEBUGOUT std::cout << __LINE__ << " "; std::cout
62 #define DEBUGERR std::cerr << __LINE__ << " "; std::cerr
64 //----------------------------------------------------------------------
65 struct tm* cmCTest::GetNightlyTime(std::string str,
66 bool tomorrowtag)
68 struct tm* lctime;
69 time_t tctime = time(0);
70 lctime = gmtime(&tctime);
71 char buf[1024];
72 // add todays year day and month to the time in str because
73 // curl_getdate no longer assumes the day is today
74 sprintf(buf, "%d%02d%02d %s", lctime->tm_year+1900, lctime->tm_mday,
75 lctime->tm_mon, str.c_str());
76 cmCTestLog(this, OUTPUT, "Determine Nightly Start Time" << std::endl
77 << " Specified time: " << str.c_str() << std::endl);
78 //Convert the nightly start time to seconds. Since we are
79 //providing only a time and a timezone, the current date of
80 //the local machine is assumed. Consequently, nightlySeconds
81 //is the time at which the nightly dashboard was opened or
82 //will be opened on the date of the current client machine.
83 //As such, this time may be in the past or in the future.
84 time_t ntime = curl_getdate(buf, &tctime);
85 cmCTestLog(this, DEBUG, " Get curl time: " << ntime << std::endl);
86 tctime = time(0);
87 cmCTestLog(this, DEBUG, " Get the current time: " << tctime << std::endl);
89 const int dayLength = 24 * 60 * 60;
90 cmCTestLog(this, DEBUG, "Seconds: " << tctime << std::endl);
91 while ( ntime > tctime )
93 // If nightlySeconds is in the past, this is the current
94 // open dashboard, then return nightlySeconds. If
95 // nightlySeconds is in the future, this is the next
96 // dashboard to be opened, so subtract 24 hours to get the
97 // time of the current open dashboard
98 ntime -= dayLength;
99 cmCTestLog(this, DEBUG, "Pick yesterday" << std::endl);
100 cmCTestLog(this, DEBUG, " Future time, subtract day: " << ntime
101 << std::endl);
103 while ( tctime > (ntime + dayLength) )
105 ntime += dayLength;
106 cmCTestLog(this, DEBUG, " Past time, add day: " << ntime << std::endl);
108 cmCTestLog(this, DEBUG, "nightlySeconds: " << ntime << std::endl);
109 cmCTestLog(this, DEBUG, " Current time: " << tctime
110 << " Nightly time: " << ntime << std::endl);
111 if ( tomorrowtag )
113 cmCTestLog(this, OUTPUT, " Use future tag, Add a day" << std::endl);
114 ntime += dayLength;
116 lctime = gmtime(&ntime);
117 return lctime;
120 //----------------------------------------------------------------------
121 std::string cmCTest::CleanString(const std::string& str)
123 std::string::size_type spos = str.find_first_not_of(" \n\t\r\f\v");
124 std::string::size_type epos = str.find_last_not_of(" \n\t\r\f\v");
125 if ( spos == str.npos )
127 return std::string();
129 if ( epos != str.npos )
131 epos = epos - spos + 1;
133 return str.substr(spos, epos);
136 //----------------------------------------------------------------------
137 std::string cmCTest::CurrentTime()
139 time_t currenttime = time(0);
140 struct tm* t = localtime(&currenttime);
141 //return ::CleanString(ctime(&currenttime));
142 char current_time[1024];
143 if ( this->ShortDateFormat )
145 strftime(current_time, 1000, "%b %d %H:%M %Z", t);
147 else
149 strftime(current_time, 1000, "%a %b %d %H:%M:%S %Z %Y", t);
151 cmCTestLog(this, DEBUG, " Current_Time: " << current_time << std::endl);
152 return cmCTest::MakeXMLSafe(cmCTest::CleanString(current_time));
156 //----------------------------------------------------------------------
157 std::string cmCTest::MakeXMLSafe(const std::string& str)
159 std::vector<char> result;
160 result.reserve(500);
161 const char* pos = str.c_str();
162 for ( ;*pos; ++pos)
164 char ch = *pos;
165 if ( (ch > 126 || ch < 32) && ch != 9 &&
166 ch != 10 && ch != 13 && ch != '\r' )
168 char buffer[33];
169 sprintf(buffer, "&lt;%d&gt;", (int)ch);
170 //sprintf(buffer, "&#x%0x;", (unsigned int)ch);
171 result.insert(result.end(), buffer, buffer+strlen(buffer));
173 else
175 const char* const encodedChars[] = {
176 "&amp;",
177 "&lt;",
178 "&gt;"
180 switch ( ch )
182 case '&':
183 result.insert(result.end(), encodedChars[0], encodedChars[0]+5);
184 break;
185 case '<':
186 result.insert(result.end(), encodedChars[1], encodedChars[1]+4);
187 break;
188 case '>':
189 result.insert(result.end(), encodedChars[2], encodedChars[2]+4);
190 break;
191 case '\n':
192 result.push_back('\n');
193 break;
194 case '\r': break; // Ignore \r
195 default:
196 result.push_back(ch);
200 if ( result.size() == 0 )
202 return "";
204 return std::string(&*result.begin(), result.size());
207 //----------------------------------------------------------------------
208 std::string cmCTest::MakeURLSafe(const std::string& str)
210 cmOStringStream ost;
211 char buffer[10];
212 for ( std::string::size_type pos = 0; pos < str.size(); pos ++ )
214 unsigned char ch = str[pos];
215 if ( ( ch > 126 || ch < 32 ||
216 ch == '&' ||
217 ch == '%' ||
218 ch == '+' ||
219 ch == '=' ||
220 ch == '@'
221 ) && ch != 9 )
223 sprintf(buffer, "%02x;", (unsigned int)ch);
224 ost << buffer;
226 else
228 ost << ch;
231 return ost.str();
234 //----------------------------------------------------------------------
235 cmCTest::cmCTest()
237 this->ParallelSubprocess = false;
238 this->ParallelLevel = 0;
239 this->SubmitIndex = 0;
240 this->ForceNewCTestProcess = false;
241 this->TomorrowTag = false;
242 this->Verbose = false;
244 this->Debug = false;
245 this->ShowLineNumbers = false;
246 this->Quiet = false;
247 this->ExtraVerbose = false;
248 this->ProduceXML = false;
249 this->ShowOnly = false;
250 this->RunConfigurationScript = false;
251 this->TestModel = cmCTest::EXPERIMENTAL;
252 this->MaxTestNameWidth = 30;
253 this->InteractiveDebugMode = true;
254 this->TimeOut = 0;
255 this->CompressXMLFiles = false;
256 this->CTestConfigFile = "";
257 this->OutputLogFile = 0;
258 this->OutputLogFileLastTag = -1;
259 this->SuppressUpdatingCTestConfiguration = false;
260 this->DartVersion = 1;
262 int cc;
263 for ( cc=0; cc < cmCTest::LAST_TEST; cc ++ )
265 this->Tests[cc] = 0;
267 this->ShortDateFormat = true;
269 this->TestingHandlers["build"] = new cmCTestBuildHandler;
270 this->TestingHandlers["buildtest"] = new cmCTestBuildAndTestHandler;
271 this->TestingHandlers["coverage"] = new cmCTestCoverageHandler;
272 this->TestingHandlers["script"] = new cmCTestScriptHandler;
273 this->TestingHandlers["test"] = new cmCTestTestHandler;
274 this->TestingHandlers["update"] = new cmCTestUpdateHandler;
275 this->TestingHandlers["configure"] = new cmCTestConfigureHandler;
276 this->TestingHandlers["memcheck"] = new cmCTestMemCheckHandler;
277 this->TestingHandlers["submit"] = new cmCTestSubmitHandler;
279 cmCTest::t_TestingHandlers::iterator it;
280 for ( it = this->TestingHandlers.begin();
281 it != this->TestingHandlers.end(); ++ it )
283 it->second->SetCTestInstance(this);
286 // Make sure we can capture the build tool output.
287 cmSystemTools::EnableVSConsoleOutput();
290 //----------------------------------------------------------------------
291 cmCTest::~cmCTest()
293 cmCTest::t_TestingHandlers::iterator it;
294 for ( it = this->TestingHandlers.begin();
295 it != this->TestingHandlers.end(); ++ it )
297 delete it->second;
298 it->second = 0;
300 this->SetOutputLogFileName(0);
303 //----------------------------------------------------------------------
304 int cmCTest::Initialize(const char* binary_dir, bool new_tag,
305 bool verbose_tag)
307 cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
308 if(!this->InteractiveDebugMode)
310 this->BlockTestErrorDiagnostics();
312 else
314 cmSystemTools::PutEnv("CTEST_INTERACTIVE_DEBUG_MODE=1");
317 this->BinaryDir = binary_dir;
318 cmSystemTools::ConvertToUnixSlashes(this->BinaryDir);
320 this->UpdateCTestConfiguration();
322 cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
323 if ( this->ProduceXML )
325 cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
326 cmCTestLog(this, OUTPUT,
327 " Site: " << this->GetCTestConfiguration("Site") << std::endl
328 << " Build name: " << this->GetCTestConfiguration("BuildName")
329 << std::endl);
330 cmCTestLog(this, DEBUG, "Produce XML is on" << std::endl);
331 if ( this->TestModel == cmCTest::NIGHTLY &&
332 this->GetCTestConfiguration("NightlyStartTime").empty() )
334 cmCTestLog(this, WARNING,
335 "WARNING: No nightly start time found please set in"
336 " CTestConfig.cmake or DartConfig.cmake" << std::endl);
337 cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
338 return 0;
342 cmake cm;
343 cmGlobalGenerator gg;
344 gg.SetCMakeInstance(&cm);
345 std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
346 lg->SetGlobalGenerator(&gg);
347 cmMakefile *mf = lg->GetMakefile();
348 if ( !this->ReadCustomConfigurationFileTree(this->BinaryDir.c_str(), mf) )
350 cmCTestLog(this, DEBUG, "Cannot find custom configuration file tree"
351 << std::endl);
352 return 0;
355 if ( this->ProduceXML )
357 std::string testingDir = this->BinaryDir + "/Testing";
358 if ( cmSystemTools::FileExists(testingDir.c_str()) )
360 if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
362 cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
363 << " is in the place of the testing directory" << std::endl);
364 return 0;
367 else
369 if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
371 cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory "
372 << testingDir << std::endl);
373 return 0;
376 std::string tagfile = testingDir + "/TAG";
377 std::ifstream tfin(tagfile.c_str());
378 std::string tag;
379 time_t tctime = time(0);
380 if ( this->TomorrowTag )
382 tctime += ( 24 * 60 * 60 );
384 struct tm *lctime = gmtime(&tctime);
385 if ( tfin && cmSystemTools::GetLineFromStream(tfin, tag) )
387 int year = 0;
388 int mon = 0;
389 int day = 0;
390 int hour = 0;
391 int min = 0;
392 sscanf(tag.c_str(), "%04d%02d%02d-%02d%02d",
393 &year, &mon, &day, &hour, &min);
394 if ( year != lctime->tm_year + 1900 ||
395 mon != lctime->tm_mon+1 ||
396 day != lctime->tm_mday )
398 tag = "";
400 std::string tagmode;
401 if ( cmSystemTools::GetLineFromStream(tfin, tagmode) )
403 if ( tagmode.size() > 4 && !( this->Tests[cmCTest::START_TEST] ||
404 this->Tests[ALL_TEST] ))
406 this->TestModel = cmCTest::GetTestModelFromString(tagmode.c_str());
409 tfin.close();
411 if ( tag.size() == 0 || new_tag || this->Tests[cmCTest::START_TEST] ||
412 this->Tests[ALL_TEST])
414 cmCTestLog(this, DEBUG, "TestModel: " << this->GetTestModelString()
415 << std::endl);
416 cmCTestLog(this, DEBUG, "TestModel: " << this->TestModel << std::endl);
417 if ( this->TestModel == cmCTest::NIGHTLY )
419 lctime = this->GetNightlyTime(
420 this->GetCTestConfiguration("NightlyStartTime"), this->TomorrowTag);
422 char datestring[100];
423 sprintf(datestring, "%04d%02d%02d-%02d%02d",
424 lctime->tm_year + 1900,
425 lctime->tm_mon+1,
426 lctime->tm_mday,
427 lctime->tm_hour,
428 lctime->tm_min);
429 tag = datestring;
430 std::ofstream ofs(tagfile.c_str());
431 if ( ofs )
433 ofs << tag << std::endl;
434 ofs << this->GetTestModelString() << std::endl;
436 ofs.close();
437 if ( verbose_tag )
439 cmCTestLog(this, OUTPUT, "Create new tag: " << tag << " - "
440 << this->GetTestModelString() << std::endl);
443 this->CurrentTag = tag;
445 return 1;
448 //----------------------------------------------------------------------
449 bool cmCTest::InitializeFromCommand(cmCTestCommand* command, bool first)
451 if ( !first && !this->CurrentTag.empty() )
453 return true;
456 std::string src_dir
457 = this->GetCTestConfiguration("SourceDirectory").c_str();
458 std::string bld_dir = this->GetCTestConfiguration("BuildDirectory").c_str();
459 this->DartVersion = 1;
460 this->SubmitFiles.clear();
462 cmMakefile* mf = command->GetMakefile();
463 std::string fname = src_dir;
464 fname += "/CTestConfig.cmake";
465 cmSystemTools::ConvertToUnixSlashes(fname);
466 if ( cmSystemTools::FileExists(fname.c_str()) )
468 cmCTestLog(this, OUTPUT, " Reading ctest configuration file: "
469 << fname.c_str() << std::endl);
470 bool readit = mf->ReadListFile(mf->GetCurrentListFile(),
471 fname.c_str() );
472 if(!readit)
474 std::string m = "Could not find include file: ";
475 m += fname;
476 command->SetError(m.c_str());
477 return false;
480 else if ( !first )
482 cmCTestLog(this, WARNING, "Cannot locate CTest configuration: "
483 << fname.c_str() << std::endl);
485 else
487 cmCTestLog(this, HANDLER_OUTPUT, " Cannot locate CTest configuration: "
488 << fname.c_str() << std::endl
489 << " Delay the initialization of CTest" << std::endl);
492 this->SetCTestConfigurationFromCMakeVariable(mf, "NightlyStartTime",
493 "CTEST_NIGHTLY_START_TIME");
494 this->SetCTestConfigurationFromCMakeVariable(mf, "Site", "CTEST_SITE");
495 this->SetCTestConfigurationFromCMakeVariable(mf, "BuildName",
496 "CTEST_BUILD_NAME");
497 const char* dartVersion = mf->GetDefinition("CTEST_DART_SERVER_VERSION");
498 if ( dartVersion )
500 this->DartVersion = atoi(dartVersion);
501 if ( this->DartVersion < 0 )
503 cmCTestLog(this, ERROR_MESSAGE, "Invalid Dart server version: "
504 << dartVersion << ". Please specify the version number."
505 << std::endl);
506 return false;
510 if ( !this->Initialize(bld_dir.c_str(), true, false) )
512 if ( this->GetCTestConfiguration("NightlyStartTime").empty() && first)
514 return true;
516 return false;
518 cmCTestLog(this, OUTPUT, " Use " << this->GetTestModelString()
519 << " tag: " << this->GetCurrentTag() << std::endl);
520 return true;
524 //----------------------------------------------------------------------
525 bool cmCTest::UpdateCTestConfiguration()
527 if ( this->SuppressUpdatingCTestConfiguration )
529 return true;
531 std::string fileName = this->CTestConfigFile;
532 if ( fileName.empty() )
534 fileName = this->BinaryDir + "/CTestConfiguration.ini";
535 if ( !cmSystemTools::FileExists(fileName.c_str()) )
537 fileName = this->BinaryDir + "/DartConfiguration.tcl";
540 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "UpdateCTestConfiguration from :"
541 << fileName.c_str() << "\n");
542 if ( !cmSystemTools::FileExists(fileName.c_str()) )
544 // No need to exit if we are not producing XML
545 if ( this->ProduceXML )
547 cmCTestLog(this, ERROR_MESSAGE, "Cannot find file: " << fileName.c_str()
548 << std::endl);
549 return false;
552 else
554 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Parse Config file:"
555 << fileName.c_str() << "\n");
556 // parse the dart test file
557 std::ifstream fin(fileName.c_str());
558 if(!fin)
560 return false;
563 char buffer[1024];
564 while ( fin )
566 buffer[0] = 0;
567 fin.getline(buffer, 1023);
568 buffer[1023] = 0;
569 std::string line = cmCTest::CleanString(buffer);
570 if(line.size() == 0)
572 continue;
574 while ( fin && (line[line.size()-1] == '\\') )
576 line = line.substr(0, line.size()-1);
577 buffer[0] = 0;
578 fin.getline(buffer, 1023);
579 buffer[1023] = 0;
580 line += cmCTest::CleanString(buffer);
582 if ( line[0] == '#' )
584 continue;
586 std::string::size_type cpos = line.find_first_of(":");
587 if ( cpos == line.npos )
589 continue;
591 std::string key = line.substr(0, cpos);
592 std::string value
593 = cmCTest::CleanString(line.substr(cpos+1, line.npos));
594 this->CTestConfiguration[key] = value;
596 fin.close();
598 if ( !this->GetCTestConfiguration("BuildDirectory").empty() )
600 this->BinaryDir = this->GetCTestConfiguration("BuildDirectory");
601 cmSystemTools::ChangeDirectory(this->BinaryDir.c_str());
603 this->TimeOut = atoi(this->GetCTestConfiguration("TimeOut").c_str());
604 if ( this->ProduceXML )
606 this->CompressXMLFiles = cmSystemTools::IsOn(
607 this->GetCTestConfiguration("CompressSubmission").c_str());
609 return true;
612 //----------------------------------------------------------------------
613 void cmCTest::BlockTestErrorDiagnostics()
615 cmSystemTools::PutEnv("DART_TEST_FROM_DART=1");
616 cmSystemTools::PutEnv("DASHBOARD_TEST_FROM_CTEST=" CMake_VERSION);
617 #if defined(_WIN32)
618 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
619 #elif defined(__BEOS__) || defined(__HAIKU__)
620 disable_debugger(1);
621 #endif
624 //----------------------------------------------------------------------
625 void cmCTest::SetTestModel(int mode)
627 this->InteractiveDebugMode = false;
628 this->TestModel = mode;
631 //----------------------------------------------------------------------
632 bool cmCTest::SetTest(const char* ttype, bool report)
634 if ( cmSystemTools::LowerCase(ttype) == "all" )
636 this->Tests[cmCTest::ALL_TEST] = 1;
638 else if ( cmSystemTools::LowerCase(ttype) == "start" )
640 this->Tests[cmCTest::START_TEST] = 1;
642 else if ( cmSystemTools::LowerCase(ttype) == "update" )
644 this->Tests[cmCTest::UPDATE_TEST] = 1;
646 else if ( cmSystemTools::LowerCase(ttype) == "configure" )
648 this->Tests[cmCTest::CONFIGURE_TEST] = 1;
650 else if ( cmSystemTools::LowerCase(ttype) == "build" )
652 this->Tests[cmCTest::BUILD_TEST] = 1;
654 else if ( cmSystemTools::LowerCase(ttype) == "test" )
656 this->Tests[cmCTest::TEST_TEST] = 1;
658 else if ( cmSystemTools::LowerCase(ttype) == "coverage" )
660 this->Tests[cmCTest::COVERAGE_TEST] = 1;
662 else if ( cmSystemTools::LowerCase(ttype) == "memcheck" )
664 this->Tests[cmCTest::MEMCHECK_TEST] = 1;
666 else if ( cmSystemTools::LowerCase(ttype) == "notes" )
668 this->Tests[cmCTest::NOTES_TEST] = 1;
670 else if ( cmSystemTools::LowerCase(ttype) == "submit" )
672 this->Tests[cmCTest::SUBMIT_TEST] = 1;
674 else
676 if ( report )
678 cmCTestLog(this, ERROR_MESSAGE, "Don't know about test \"" << ttype
679 << "\" yet..." << std::endl);
681 return false;
683 return true;
686 //----------------------------------------------------------------------
687 void cmCTest::Finalize()
691 //----------------------------------------------------------------------
692 bool cmCTest::OpenOutputFile(const std::string& path,
693 const std::string& name, cmGeneratedFileStream& stream,
694 bool compress)
696 std::string testingDir = this->BinaryDir + "/Testing";
697 if ( path.size() > 0 )
699 testingDir += "/" + path;
701 if ( cmSystemTools::FileExists(testingDir.c_str()) )
703 if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
705 cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
706 << " is in the place of the testing directory"
707 << std::endl);
708 return false;
711 else
713 if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
715 cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory " << testingDir
716 << std::endl);
717 return false;
720 std::string filename = testingDir + "/" + name;
721 stream.Open(filename.c_str());
722 if( !stream )
724 cmCTestLog(this, ERROR_MESSAGE, "Problem opening file: " << filename
725 << std::endl);
726 return false;
728 if ( compress )
730 if ( this->CompressXMLFiles )
732 stream.SetCompression(true);
735 return true;
738 //----------------------------------------------------------------------
739 bool cmCTest::AddIfExists(SetOfStrings& files, const char* file)
741 if ( this->CTestFileExists(file) )
743 files.insert(file);
745 else
747 std::string name = file;
748 name += ".gz";
749 if ( this->CTestFileExists(name.c_str()) )
751 files.insert(name.c_str());
753 else
755 return false;
758 return true;
761 //----------------------------------------------------------------------
762 bool cmCTest::CTestFileExists(const std::string& filename)
764 std::string testingDir = this->BinaryDir + "/Testing/" +
765 this->CurrentTag + "/" + filename;
766 return cmSystemTools::FileExists(testingDir.c_str());
769 //----------------------------------------------------------------------
770 cmCTestGenericHandler* cmCTest::GetInitializedHandler(const char* handler)
772 cmCTest::t_TestingHandlers::iterator it =
773 this->TestingHandlers.find(handler);
774 if ( it == this->TestingHandlers.end() )
776 return 0;
778 it->second->Initialize();
779 return it->second;
782 //----------------------------------------------------------------------
783 cmCTestGenericHandler* cmCTest::GetHandler(const char* handler)
785 cmCTest::t_TestingHandlers::iterator it =
786 this->TestingHandlers.find(handler);
787 if ( it == this->TestingHandlers.end() )
789 return 0;
791 return it->second;
794 //----------------------------------------------------------------------
795 int cmCTest::ExecuteHandler(const char* shandler)
797 cmCTestGenericHandler* handler = this->GetHandler(shandler);
798 if ( !handler )
800 return -1;
802 handler->Initialize();
803 return handler->ProcessHandler();
806 //----------------------------------------------------------------------
807 int cmCTest::ProcessTests()
809 int res = 0;
810 bool notest = true;
811 int cc;
812 int update_count = 0;
814 // do not output startup if this is a sub-process for parallel tests
815 if(!this->GetParallelSubprocess())
817 cmCTestLog(this, OUTPUT, "Start processing tests" << std::endl);
820 for ( cc = 0; cc < LAST_TEST; cc ++ )
822 if ( this->Tests[cc] )
824 notest = false;
825 break;
828 if (( this->Tests[UPDATE_TEST] || this->Tests[ALL_TEST] ) &&
829 (this->GetRemainingTimeAllowed() - 120 > 0))
831 cmCTestGenericHandler* uphandler = this->GetHandler("update");
832 uphandler->SetPersistentOption("SourceDirectory",
833 this->GetCTestConfiguration("SourceDirectory").c_str());
834 update_count = uphandler->ProcessHandler();
835 if ( update_count < 0 )
837 res |= cmCTest::UPDATE_ERRORS;
840 if ( this->TestModel == cmCTest::CONTINUOUS && !update_count )
842 return 0;
844 if (( this->Tests[CONFIGURE_TEST] || this->Tests[ALL_TEST] )&&
845 (this->GetRemainingTimeAllowed() - 120 > 0))
847 if (this->GetHandler("configure")->ProcessHandler() < 0)
849 res |= cmCTest::CONFIGURE_ERRORS;
852 if (( this->Tests[BUILD_TEST] || this->Tests[ALL_TEST] )&&
853 (this->GetRemainingTimeAllowed() - 120 > 0))
855 this->UpdateCTestConfiguration();
856 if (this->GetHandler("build")->ProcessHandler() < 0)
858 res |= cmCTest::BUILD_ERRORS;
861 if (( this->Tests[TEST_TEST] || this->Tests[ALL_TEST] || notest ) &&
862 (this->GetRemainingTimeAllowed() - 120 > 0))
864 this->UpdateCTestConfiguration();
865 if (this->GetHandler("test")->ProcessHandler() < 0)
867 res |= cmCTest::TEST_ERRORS;
870 if (( this->Tests[COVERAGE_TEST] || this->Tests[ALL_TEST] ) &&
871 (this->GetRemainingTimeAllowed() - 120 > 0))
873 this->UpdateCTestConfiguration();
874 if (this->GetHandler("coverage")->ProcessHandler() < 0)
876 res |= cmCTest::COVERAGE_ERRORS;
879 if (( this->Tests[MEMCHECK_TEST] || this->Tests[ALL_TEST] )&&
880 (this->GetRemainingTimeAllowed() - 120 > 0))
882 this->UpdateCTestConfiguration();
883 if (this->GetHandler("memcheck")->ProcessHandler() < 0)
885 res |= cmCTest::MEMORY_ERRORS;
888 if ( !notest )
890 std::string notes_dir = this->BinaryDir + "/Testing/Notes";
891 if ( cmSystemTools::FileIsDirectory(notes_dir.c_str()) )
893 cmsys::Directory d;
894 d.Load(notes_dir.c_str());
895 unsigned long kk;
896 for ( kk = 0; kk < d.GetNumberOfFiles(); kk ++ )
898 const char* file = d.GetFile(kk);
899 std::string fullname = notes_dir + "/" + file;
900 if ( cmSystemTools::FileExists(fullname.c_str()) &&
901 !cmSystemTools::FileIsDirectory(fullname.c_str()) )
903 if ( this->NotesFiles.size() > 0 )
905 this->NotesFiles += ";";
907 this->NotesFiles += fullname;
908 this->Tests[NOTES_TEST] = 1;
913 if ( this->Tests[NOTES_TEST] || this->Tests[ALL_TEST] )
915 this->UpdateCTestConfiguration();
916 if ( this->NotesFiles.size() )
918 this->GenerateNotesFile(this->NotesFiles.c_str());
921 if ( this->Tests[SUBMIT_TEST] || this->Tests[ALL_TEST] )
923 this->UpdateCTestConfiguration();
924 if (this->GetHandler("submit")->ProcessHandler() < 0)
926 res |= cmCTest::SUBMIT_ERRORS;
929 if ( res != 0 )
931 if(!this->GetParallelSubprocess())
933 cmCTestLog(this, ERROR_MESSAGE, "Errors while running CTest"
934 << std::endl);
937 return res;
940 //----------------------------------------------------------------------
941 std::string cmCTest::GetTestModelString()
943 if ( !this->SpecificTrack.empty() )
945 return this->SpecificTrack;
947 switch ( this->TestModel )
949 case cmCTest::NIGHTLY:
950 return "Nightly";
951 case cmCTest::CONTINUOUS:
952 return "Continuous";
954 return "Experimental";
957 //----------------------------------------------------------------------
958 int cmCTest::GetTestModelFromString(const char* str)
960 if ( !str )
962 return cmCTest::EXPERIMENTAL;
964 std::string rstr = cmSystemTools::LowerCase(str);
965 if ( strncmp(rstr.c_str(), "cont", 4) == 0 )
967 return cmCTest::CONTINUOUS;
969 if ( strncmp(rstr.c_str(), "nigh", 4) == 0 )
971 return cmCTest::NIGHTLY;
973 return cmCTest::EXPERIMENTAL;
976 //######################################################################
977 //######################################################################
978 //######################################################################
979 //######################################################################
981 //----------------------------------------------------------------------
982 int cmCTest::RunMakeCommand(const char* command, std::string* output,
983 int* retVal, const char* dir, int timeout, std::ofstream& ofs)
985 // First generate the command and arguments
986 std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
988 if(args.size() < 1)
990 return false;
993 std::vector<const char*> argv;
994 for(std::vector<cmStdString>::const_iterator a = args.begin();
995 a != args.end(); ++a)
997 argv.push_back(a->c_str());
999 argv.push_back(0);
1001 if ( output )
1003 *output = "";
1006 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Run command:");
1007 std::vector<const char*>::iterator ait;
1008 for ( ait = argv.begin(); ait != argv.end() && *ait; ++ ait )
1010 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, " \"" << *ait << "\"");
1012 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, std::endl);
1014 // Now create process object
1015 cmsysProcess* cp = cmsysProcess_New();
1016 cmsysProcess_SetCommand(cp, &*argv.begin());
1017 cmsysProcess_SetWorkingDirectory(cp, dir);
1018 cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
1019 cmsysProcess_SetTimeout(cp, timeout);
1020 cmsysProcess_Execute(cp);
1022 // Initialize tick's
1023 std::string::size_type tick = 0;
1024 std::string::size_type tick_len = 1024;
1025 std::string::size_type tick_line_len = 50;
1027 char* data;
1028 int length;
1029 cmCTestLog(this, HANDLER_OUTPUT,
1030 " Each . represents " << tick_len << " bytes of output" << std::endl
1031 << " " << std::flush);
1032 while(cmsysProcess_WaitForData(cp, &data, &length, 0))
1034 if ( output )
1036 for(int cc =0; cc < length; ++cc)
1038 if(data[cc] == 0)
1040 data[cc] = '\n';
1044 output->append(data, length);
1045 while ( output->size() > (tick * tick_len) )
1047 tick ++;
1048 cmCTestLog(this, HANDLER_OUTPUT, "." << std::flush);
1049 if ( tick % tick_line_len == 0 && tick > 0 )
1051 cmCTestLog(this, HANDLER_OUTPUT, " Size: "
1052 << int((output->size() / 1024.0) + 1) << "K" << std::endl
1053 << " " << std::flush);
1057 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
1058 if ( ofs )
1060 ofs << cmCTestLogWrite(data, length);
1063 cmCTestLog(this, OUTPUT, " Size of output: "
1064 << int(output->size() / 1024.0) << "K" << std::endl);
1066 cmsysProcess_WaitForExit(cp, 0);
1068 int result = cmsysProcess_GetState(cp);
1070 if(result == cmsysProcess_State_Exited)
1072 *retVal = cmsysProcess_GetExitValue(cp);
1073 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Command exited with the value: "
1074 << *retVal << std::endl);
1076 else if(result == cmsysProcess_State_Exception)
1078 *retVal = cmsysProcess_GetExitException(cp);
1079 cmCTestLog(this, WARNING, "There was an exception: " << *retVal
1080 << std::endl);
1082 else if(result == cmsysProcess_State_Expired)
1084 cmCTestLog(this, WARNING, "There was a timeout" << std::endl);
1086 else if(result == cmsysProcess_State_Error)
1088 *output += "\n*** ERROR executing: ";
1089 *output += cmsysProcess_GetErrorString(cp);
1090 *output += "\n***The build process failed.";
1091 cmCTestLog(this, ERROR_MESSAGE, "There was an error: "
1092 << cmsysProcess_GetErrorString(cp) << std::endl);
1095 cmsysProcess_Delete(cp);
1097 return result;
1100 //######################################################################
1101 //######################################################################
1102 //######################################################################
1103 //######################################################################
1105 //----------------------------------------------------------------------
1106 int cmCTest::RunTest(std::vector<const char*> argv,
1107 std::string* output, int *retVal,
1108 std::ostream* log, double testTimeOut)
1110 // determine how much time we have
1111 double timeout = this->GetRemainingTimeAllowed() - 120;
1112 if (this->TimeOut && this->TimeOut < timeout)
1114 timeout = this->TimeOut;
1116 if (testTimeOut
1117 && testTimeOut < this->GetRemainingTimeAllowed())
1119 timeout = testTimeOut;
1122 // always have at least 1 second if we got to here
1123 if (timeout <= 0)
1125 timeout = 1;
1127 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
1128 "Test timeout computed to be: " << timeout << "\n");
1129 if(cmSystemTools::SameFile(argv[0], this->CTestSelf.c_str()) &&
1130 !this->ForceNewCTestProcess)
1132 cmCTest inst;
1133 inst.ConfigType = this->ConfigType;
1134 inst.TimeOut = timeout;
1135 std::vector<std::string> args;
1136 for(unsigned int i =0; i < argv.size(); ++i)
1138 if(argv[i])
1140 // make sure we pass the timeout in for any build and test
1141 // invocations. Since --build-generator is required this is a
1142 // good place to check for it, and to add the arguments in
1143 if (strcmp(argv[i],"--build-generator") == 0 && timeout)
1145 args.push_back("--test-timeout");
1146 cmOStringStream msg;
1147 msg << timeout;
1148 args.push_back(msg.str());
1150 args.push_back(argv[i]);
1153 if ( *log )
1155 *log << "* Run internal CTest" << std::endl;
1157 std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory();
1159 *retVal = inst.Run(args, output);
1160 if ( *log )
1162 *log << output->c_str();
1164 cmSystemTools::ChangeDirectory(oldpath.c_str());
1166 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
1167 "Internal cmCTest object used to run test." << std::endl
1168 << *output << std::endl);
1169 return cmsysProcess_State_Exited;
1171 std::vector<char> tempOutput;
1172 if ( output )
1174 *output = "";
1177 cmsysProcess* cp = cmsysProcess_New();
1178 cmsysProcess_SetCommand(cp, &*argv.begin());
1179 cmCTestLog(this, DEBUG, "Command is: " << argv[0] << std::endl);
1180 if(cmSystemTools::GetRunCommandHideConsole())
1182 cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
1185 cmsysProcess_SetTimeout(cp, timeout);
1186 cmsysProcess_Execute(cp);
1188 char* data;
1189 int length;
1190 while(cmsysProcess_WaitForData(cp, &data, &length, 0))
1192 if ( output )
1194 tempOutput.insert(tempOutput.end(), data, data+length);
1196 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
1197 if ( log )
1199 log->write(data, length);
1203 cmsysProcess_WaitForExit(cp, 0);
1204 if(output && tempOutput.begin() != tempOutput.end())
1206 output->append(&*tempOutput.begin(), tempOutput.size());
1208 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "-- Process completed"
1209 << std::endl);
1211 int result = cmsysProcess_GetState(cp);
1213 if(result == cmsysProcess_State_Exited)
1215 *retVal = cmsysProcess_GetExitValue(cp);
1217 else if(result == cmsysProcess_State_Exception)
1219 *retVal = cmsysProcess_GetExitException(cp);
1220 std::string outerr = "\n*** Exception executing: ";
1221 outerr += cmsysProcess_GetExceptionString(cp);
1222 *output += outerr;
1223 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl
1224 << std::flush);
1226 else if(result == cmsysProcess_State_Error)
1228 std::string outerr = "\n*** ERROR executing: ";
1229 outerr += cmsysProcess_GetErrorString(cp);
1230 *output += outerr;
1231 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl
1232 << std::flush);
1234 cmsysProcess_Delete(cp);
1236 return result;
1239 //----------------------------------------------------------------------
1240 void cmCTest::StartXML(std::ostream& ostr)
1242 if(this->CurrentTag.empty())
1244 cmCTestLog(this, ERROR_MESSAGE,
1245 "Current Tag empty, this may mean"
1246 " NightlStartTime was not set correctly." << std::endl);
1247 cmSystemTools::SetFatalErrorOccured();
1249 // find out about the system
1250 cmsys::SystemInformation info;
1251 info.RunCPUCheck();
1252 info.RunOSCheck();
1253 info.RunMemoryCheck();
1254 ostr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1255 << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName")
1256 << "\"\n\tBuildStamp=\"" << this->CurrentTag << "-"
1257 << this->GetTestModelString() << "\"\n\tName=\""
1258 << this->GetCTestConfiguration("Site") << "\"\n\tGenerator=\"ctest"
1259 << cmVersion::GetCMakeVersion() << "\""
1260 << "\tOSName=\"" << info.GetOSName() << "\"\n"
1261 << "\tHostname=\"" << info.GetHostname() << "\"\n"
1262 << "\tOSRelease=\"" << info.GetOSRelease() << "\"\n"
1263 << "\tOSVersion=\"" << info.GetOSVersion() << "\"\n"
1264 << "\tOSPlatform=\"" << info.GetOSPlatform() << "\"\n"
1265 << "\tIs64Bits=\"" << info.Is64Bits() << "\"\n"
1266 << "\tVendorString=\"" << info.GetVendorString() << "\"\n"
1267 << "\tVendorID=\"" << info.GetVendorID() << "\"\n"
1268 << "\tFamilyID=\"" << info.GetFamilyID() << "\"\n"
1269 << "\tModelID=\"" << info.GetModelID() << "\"\n"
1270 << "\tProcessorCacheSize=\"" << info.GetProcessorCacheSize() << "\"\n"
1271 << "\tNumberOfLogicalCPU=\"" << info.GetNumberOfLogicalCPU() << "\"\n"
1272 << "\tNumberOfPhysicalCPU=\""<< info.GetNumberOfPhysicalCPU() << "\"\n"
1273 << "\tTotalVirtualMemory=\"" << info.GetTotalVirtualMemory() << "\"\n"
1274 << "\tTotalPhysicalMemory=\""<< info.GetTotalPhysicalMemory() << "\"\n"
1275 << "\tLogicalProcessorsPerPhysical=\""
1276 << info.GetLogicalProcessorsPerPhysical() << "\"\n"
1277 << "\tProcessorClockFrequency=\""
1278 << info.GetProcessorClockFrequency() << "\"\n"
1279 << ">" << std::endl;
1282 //----------------------------------------------------------------------
1283 void cmCTest::EndXML(std::ostream& ostr)
1285 ostr << "</Site>" << std::endl;
1288 //----------------------------------------------------------------------
1289 int cmCTest::GenerateCTestNotesOutput(std::ostream& os,
1290 const cmCTest::VectorOfStrings& files)
1292 cmCTest::VectorOfStrings::const_iterator it;
1293 os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1294 << "<?xml-stylesheet type=\"text/xsl\" "
1295 "href=\"Dart/Source/Server/XSL/Build.xsl "
1296 "<file:///Dart/Source/Server/XSL/Build.xsl> \"?>\n"
1297 << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName")
1298 << "\" BuildStamp=\""
1299 << this->CurrentTag << "-" << this->GetTestModelString() << "\" Name=\""
1300 << this->GetCTestConfiguration("Site") << "\" Generator=\"ctest"
1301 << cmVersion::GetCMakeVersion()
1302 << "\">\n"
1303 << "<Notes>" << std::endl;
1305 for ( it = files.begin(); it != files.end(); it ++ )
1307 cmCTestLog(this, OUTPUT, "\tAdd file: " << it->c_str() << std::endl);
1308 std::string note_time = this->CurrentTime();
1309 os << "<Note Name=\"" << this->MakeXMLSafe(it->c_str()) << "\">\n"
1310 << "<Time>" << cmSystemTools::GetTime() << "</Time>\n"
1311 << "<DateTime>" << note_time << "</DateTime>\n"
1312 << "<Text>" << std::endl;
1313 std::ifstream ifs(it->c_str());
1314 if ( ifs )
1316 std::string line;
1317 while ( cmSystemTools::GetLineFromStream(ifs, line) )
1319 os << this->MakeXMLSafe(line) << std::endl;
1321 ifs.close();
1323 else
1325 os << "Problem reading file: " << it->c_str() << std::endl;
1326 cmCTestLog(this, ERROR_MESSAGE, "Problem reading file: " << it->c_str()
1327 << " while creating notes" << std::endl);
1329 os << "</Text>\n"
1330 << "</Note>" << std::endl;
1332 os << "</Notes>\n"
1333 << "</Site>" << std::endl;
1334 return 1;
1337 //----------------------------------------------------------------------
1338 int cmCTest::GenerateNotesFile(const std::vector<cmStdString> &files)
1340 cmGeneratedFileStream ofs;
1341 if ( !this->OpenOutputFile(this->CurrentTag, "Notes.xml", ofs) )
1343 cmCTestLog(this, ERROR_MESSAGE, "Cannot open notes file" << std::endl);
1344 return 1;
1347 this->GenerateCTestNotesOutput(ofs, files);
1348 return 0;
1351 //----------------------------------------------------------------------
1352 int cmCTest::GenerateNotesFile(const char* cfiles)
1354 if ( !cfiles )
1356 return 1;
1359 std::vector<cmStdString> files;
1361 cmCTestLog(this, OUTPUT, "Create notes file" << std::endl);
1363 files = cmSystemTools::SplitString(cfiles, ';');
1364 if ( files.size() == 0 )
1366 return 1;
1369 return this->GenerateNotesFile(files);
1372 //----------------------------------------------------------------------
1373 bool cmCTest::SubmitExtraFiles(const std::vector<cmStdString> &files)
1375 std::vector<cmStdString>::const_iterator it;
1376 for ( it = files.begin();
1377 it != files.end();
1378 ++ it )
1380 if ( !cmSystemTools::FileExists(it->c_str()) )
1382 cmCTestLog(this, ERROR_MESSAGE, "Cannot find extra file: "
1383 << it->c_str() << " to submit."
1384 << std::endl;);
1385 return false;
1387 this->AddSubmitFile(it->c_str());
1389 return true;
1392 //----------------------------------------------------------------------
1393 bool cmCTest::SubmitExtraFiles(const char* cfiles)
1395 if ( !cfiles )
1397 return 1;
1400 std::vector<cmStdString> files;
1402 cmCTestLog(this, OUTPUT, "Submit extra files" << std::endl);
1404 files = cmSystemTools::SplitString(cfiles, ';');
1405 if ( files.size() == 0 )
1407 return 1;
1410 return this->SubmitExtraFiles(files);
1414 //-------------------------------------------------------
1415 // for a -D argument convert the next argument into
1416 // the proper list of dashboard steps via SetTest
1417 bool cmCTest::AddTestsForDashboardType(std::string &targ)
1419 if ( targ == "Experimental" )
1421 this->SetTestModel(cmCTest::EXPERIMENTAL);
1422 this->SetTest("Start");
1423 this->SetTest("Configure");
1424 this->SetTest("Build");
1425 this->SetTest("Test");
1426 this->SetTest("Coverage");
1427 this->SetTest("Submit");
1429 else if ( targ == "ExperimentalStart" )
1431 this->SetTestModel(cmCTest::EXPERIMENTAL);
1432 this->SetTest("Start");
1434 else if ( targ == "ExperimentalUpdate" )
1436 this->SetTestModel(cmCTest::EXPERIMENTAL);
1437 this->SetTest("Update");
1439 else if ( targ == "ExperimentalConfigure" )
1441 this->SetTestModel(cmCTest::EXPERIMENTAL);
1442 this->SetTest("Configure");
1444 else if ( targ == "ExperimentalBuild" )
1446 this->SetTestModel(cmCTest::EXPERIMENTAL);
1447 this->SetTest("Build");
1449 else if ( targ == "ExperimentalTest" )
1451 this->SetTestModel(cmCTest::EXPERIMENTAL);
1452 this->SetTest("Test");
1454 else if ( targ == "ExperimentalMemCheck"
1455 || targ == "ExperimentalPurify" )
1457 this->SetTestModel(cmCTest::EXPERIMENTAL);
1458 this->SetTest("MemCheck");
1460 else if ( targ == "ExperimentalCoverage" )
1462 this->SetTestModel(cmCTest::EXPERIMENTAL);
1463 this->SetTest("Coverage");
1465 else if ( targ == "ExperimentalSubmit" )
1467 this->SetTestModel(cmCTest::EXPERIMENTAL);
1468 this->SetTest("Submit");
1470 else if ( targ == "Continuous" )
1472 this->SetTestModel(cmCTest::CONTINUOUS);
1473 this->SetTest("Start");
1474 this->SetTest("Update");
1475 this->SetTest("Configure");
1476 this->SetTest("Build");
1477 this->SetTest("Test");
1478 this->SetTest("Coverage");
1479 this->SetTest("Submit");
1481 else if ( targ == "ContinuousStart" )
1483 this->SetTestModel(cmCTest::CONTINUOUS);
1484 this->SetTest("Start");
1486 else if ( targ == "ContinuousUpdate" )
1488 this->SetTestModel(cmCTest::CONTINUOUS);
1489 this->SetTest("Update");
1491 else if ( targ == "ContinuousConfigure" )
1493 this->SetTestModel(cmCTest::CONTINUOUS);
1494 this->SetTest("Configure");
1496 else if ( targ == "ContinuousBuild" )
1498 this->SetTestModel(cmCTest::CONTINUOUS);
1499 this->SetTest("Build");
1501 else if ( targ == "ContinuousTest" )
1503 this->SetTestModel(cmCTest::CONTINUOUS);
1504 this->SetTest("Test");
1506 else if ( targ == "ContinuousMemCheck"
1507 || targ == "ContinuousPurify" )
1509 this->SetTestModel(cmCTest::CONTINUOUS);
1510 this->SetTest("MemCheck");
1512 else if ( targ == "ContinuousCoverage" )
1514 this->SetTestModel(cmCTest::CONTINUOUS);
1515 this->SetTest("Coverage");
1517 else if ( targ == "ContinuousSubmit" )
1519 this->SetTestModel(cmCTest::CONTINUOUS);
1520 this->SetTest("Submit");
1522 else if ( targ == "Nightly" )
1524 this->SetTestModel(cmCTest::NIGHTLY);
1525 this->SetTest("Start");
1526 this->SetTest("Update");
1527 this->SetTest("Configure");
1528 this->SetTest("Build");
1529 this->SetTest("Test");
1530 this->SetTest("Coverage");
1531 this->SetTest("Submit");
1533 else if ( targ == "NightlyStart" )
1535 this->SetTestModel(cmCTest::NIGHTLY);
1536 this->SetTest("Start");
1538 else if ( targ == "NightlyUpdate" )
1540 this->SetTestModel(cmCTest::NIGHTLY);
1541 this->SetTest("Update");
1543 else if ( targ == "NightlyConfigure" )
1545 this->SetTestModel(cmCTest::NIGHTLY);
1546 this->SetTest("Configure");
1548 else if ( targ == "NightlyBuild" )
1550 this->SetTestModel(cmCTest::NIGHTLY);
1551 this->SetTest("Build");
1553 else if ( targ == "NightlyTest" )
1555 this->SetTestModel(cmCTest::NIGHTLY);
1556 this->SetTest("Test");
1558 else if ( targ == "NightlyMemCheck"
1559 || targ == "NightlyPurify" )
1561 this->SetTestModel(cmCTest::NIGHTLY);
1562 this->SetTest("MemCheck");
1564 else if ( targ == "NightlyCoverage" )
1566 this->SetTestModel(cmCTest::NIGHTLY);
1567 this->SetTest("Coverage");
1569 else if ( targ == "NightlySubmit" )
1571 this->SetTestModel(cmCTest::NIGHTLY);
1572 this->SetTest("Submit");
1574 else if ( targ == "MemoryCheck" )
1576 this->SetTestModel(cmCTest::EXPERIMENTAL);
1577 this->SetTest("Start");
1578 this->SetTest("Configure");
1579 this->SetTest("Build");
1580 this->SetTest("MemCheck");
1581 this->SetTest("Coverage");
1582 this->SetTest("Submit");
1584 else if ( targ == "NightlyMemoryCheck" )
1586 this->SetTestModel(cmCTest::NIGHTLY);
1587 this->SetTest("Start");
1588 this->SetTest("Update");
1589 this->SetTest("Configure");
1590 this->SetTest("Build");
1591 this->SetTest("MemCheck");
1592 this->SetTest("Coverage");
1593 this->SetTest("Submit");
1595 else
1597 cmCTestLog(this, ERROR_MESSAGE,
1598 "CTest -D called with incorrect option: "
1599 << targ << std::endl);
1600 cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
1601 << " " << "ctest" << " -D Continuous" << std::endl
1602 << " " << "ctest"
1603 << " -D Continuous(Start|Update|Configure|Build)" << std::endl
1604 << " " << "ctest"
1605 << " -D Continuous(Test|Coverage|MemCheck|Submit)"
1606 << std::endl
1607 << " " << "ctest" << " -D Experimental" << std::endl
1608 << " " << "ctest"
1609 << " -D Experimental(Start|Update|Configure|Build)"
1610 << std::endl
1611 << " " << "ctest"
1612 << " -D Experimental(Test|Coverage|MemCheck|Submit)"
1613 << std::endl
1614 << " " << "ctest" << " -D Nightly" << std::endl
1615 << " " << "ctest"
1616 << " -D Nightly(Start|Update|Configure|Build)" << std::endl
1617 << " " << "ctest"
1618 << " -D Nightly(Test|Coverage|MemCheck|Submit)" << std::endl
1619 << " " << "ctest" << " -D NightlyMemoryCheck" << std::endl);
1620 return false;
1622 return true;
1626 //----------------------------------------------------------------------
1627 bool cmCTest::CheckArgument(const std::string& arg, const char* varg1,
1628 const char* varg2)
1630 if ( varg1 && arg == varg1 || varg2 && arg == varg2 )
1632 return true;
1634 return false;
1638 //----------------------------------------------------------------------
1639 // Processes one command line argument (and its arguments if any)
1640 // for many simple options and then returns
1641 void cmCTest::HandleCommandLineArguments(size_t &i,
1642 std::vector<std::string> &args)
1644 std::string arg = args[i];
1646 if(this->CheckArgument(arg, "-j", "--parallel") && i < args.size() - 1)
1648 i++;
1649 int plevel = atoi(args[i].c_str());
1650 this->SetParallelLevel(plevel);
1652 else if(arg.find("-j") == 0)
1654 int plevel = atoi(arg.substr(2).c_str());
1655 this->SetParallelLevel(plevel);
1657 if(this->CheckArgument(arg, "--internal-ctest-parallel")
1658 && i < args.size() - 1)
1660 i++;
1661 int pid = atoi(args[i].c_str());
1662 this->SetParallelSubprocessId(pid);
1663 this->SetParallelSubprocess();
1666 if(this->CheckArgument(arg, "--parallel-cache") && i < args.size() - 1)
1668 i++;
1669 this->SetParallelCacheFile(args[i].c_str());
1672 if(this->CheckArgument(arg, "-C", "--build-config") &&
1673 i < args.size() - 1)
1675 i++;
1676 this->SetConfigType(args[i].c_str());
1679 if(this->CheckArgument(arg, "--debug"))
1681 this->Debug = true;
1682 this->ShowLineNumbers = true;
1684 if(this->CheckArgument(arg, "--track") && i < args.size() - 1)
1686 i++;
1687 this->SpecificTrack = args[i];
1689 if(this->CheckArgument(arg, "--show-line-numbers"))
1691 this->ShowLineNumbers = true;
1693 if(this->CheckArgument(arg, "-Q", "--quiet"))
1695 this->Quiet = true;
1697 if(this->CheckArgument(arg, "-V", "--verbose"))
1699 this->Verbose = true;
1701 if(this->CheckArgument(arg, "-VV", "--extra-verbose"))
1703 this->ExtraVerbose = true;
1704 this->Verbose = true;
1707 if(this->CheckArgument(arg, "-N", "--show-only"))
1709 this->ShowOnly = true;
1712 if(this->CheckArgument(arg, "-O", "--output-log") && i < args.size() - 1 )
1714 i++;
1715 this->SetOutputLogFileName(args[i].c_str());
1718 if(this->CheckArgument(arg, "--tomorrow-tag"))
1720 this->TomorrowTag = true;
1722 if(this->CheckArgument(arg, "--force-new-ctest-process"))
1724 this->ForceNewCTestProcess = true;
1726 if(this->CheckArgument(arg, "-W", "--max-width") && i < args.size() - 1)
1728 i++;
1729 this->MaxTestNameWidth = atoi(args[i].c_str());
1731 if(this->CheckArgument(arg, "--interactive-debug-mode") &&
1732 i < args.size() - 1 )
1734 i++;
1735 this->InteractiveDebugMode = cmSystemTools::IsOn(args[i].c_str());
1737 if(this->CheckArgument(arg, "--submit-index") && i < args.size() - 1 )
1739 i++;
1740 this->SubmitIndex = atoi(args[i].c_str());
1741 if ( this->SubmitIndex < 0 )
1743 this->SubmitIndex = 0;
1747 if(this->CheckArgument(arg, "--overwrite") && i < args.size() - 1)
1749 i++;
1750 this->AddCTestConfigurationOverwrite(args[i].c_str());
1752 if(this->CheckArgument(arg, "-A", "--add-notes") && i < args.size() - 1)
1754 this->ProduceXML = true;
1755 this->SetTest("Notes");
1756 i++;
1757 this->SetNotesFiles(args[i].c_str());
1760 // options that control what tests are run
1761 if(this->CheckArgument(arg, "-I", "--tests-information") &&
1762 i < args.size() - 1)
1764 i++;
1765 this->GetHandler("test")->SetPersistentOption("TestsToRunInformation",
1766 args[i].c_str());
1767 this->GetHandler("memcheck")->
1768 SetPersistentOption("TestsToRunInformation",args[i].c_str());
1770 if(this->CheckArgument(arg, "-U", "--union"))
1772 this->GetHandler("test")->SetPersistentOption("UseUnion", "true");
1773 this->GetHandler("memcheck")->SetPersistentOption("UseUnion", "true");
1775 if(this->CheckArgument(arg, "-R", "--tests-regex") && i < args.size() - 1)
1777 i++;
1778 this->GetHandler("test")->
1779 SetPersistentOption("IncludeRegularExpression", args[i].c_str());
1780 this->GetHandler("memcheck")->
1781 SetPersistentOption("IncludeRegularExpression", args[i].c_str());
1784 if(this->CheckArgument(arg, "-E", "--exclude-regex") &&
1785 i < args.size() - 1)
1787 i++;
1788 this->GetHandler("test")->
1789 SetPersistentOption("ExcludeRegularExpression", args[i].c_str());
1790 this->GetHandler("memcheck")->
1791 SetPersistentOption("ExcludeRegularExpression", args[i].c_str());
1795 //----------------------------------------------------------------------
1796 // handle the -S -SR and -SP arguments
1797 void cmCTest::HandleScriptArguments(size_t &i,
1798 std::vector<std::string> &args,
1799 bool &SRArgumentSpecified)
1801 std::string arg = args[i];
1802 if(this->CheckArgument(arg, "-SP", "--script-new-process") &&
1803 i < args.size() - 1 )
1805 this->RunConfigurationScript = true;
1806 i++;
1807 cmCTestScriptHandler* ch
1808 = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
1809 // -SR is an internal argument, -SP should be ignored when it is passed
1810 if (!SRArgumentSpecified)
1812 ch->AddConfigurationScript(args[i].c_str(),false);
1816 if(this->CheckArgument(arg, "-SR", "--script-run") &&
1817 i < args.size() - 1 )
1819 SRArgumentSpecified = true;
1820 this->RunConfigurationScript = true;
1821 i++;
1822 cmCTestScriptHandler* ch
1823 = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
1824 ch->AddConfigurationScript(args[i].c_str(),true);
1827 if(this->CheckArgument(arg, "-S", "--script") && i < args.size() - 1 )
1829 this->RunConfigurationScript = true;
1830 i++;
1831 cmCTestScriptHandler* ch
1832 = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
1833 // -SR is an internal argument, -S should be ignored when it is passed
1834 if (!SRArgumentSpecified)
1836 ch->AddConfigurationScript(args[i].c_str(),true);
1841 //----------------------------------------------------------------------
1842 // the main entry point of ctest, called from main
1843 int cmCTest::Run(std::vector<std::string> &args, std::string* output)
1845 this->FindRunningCMake();
1846 const char* ctestExec = "ctest";
1847 bool cmakeAndTest = false;
1848 bool performSomeTest = true;
1849 bool SRArgumentSpecified = false;
1851 // copy the command line
1852 for(size_t i=0; i < args.size(); ++i)
1854 this->InitialCommandLineArguments.push_back(args[i]);
1857 // process the command line arguments
1858 for(size_t i=1; i < args.size(); ++i)
1860 // handle the simple commandline arguments
1861 this->HandleCommandLineArguments(i,args);
1863 // handle the script arguments -S -SR -SP
1864 this->HandleScriptArguments(i,args,SRArgumentSpecified);
1866 // handle a request for a dashboard
1867 std::string arg = args[i];
1868 if(this->CheckArgument(arg, "-D", "--dashboard") && i < args.size() - 1 )
1870 this->ProduceXML = true;
1871 i++;
1872 std::string targ = args[i];
1873 // AddTestsForDashboard parses the dashborad type and converts it
1874 // into the seperate stages
1875 if (!this->AddTestsForDashboardType(targ))
1877 performSomeTest = false;
1881 if(this->CheckArgument(arg, "-T", "--test-action") &&
1882 (i < args.size() -1) )
1884 this->ProduceXML = true;
1885 i++;
1886 if ( !this->SetTest(args[i].c_str(), false) )
1888 performSomeTest = false;
1889 cmCTestLog(this, ERROR_MESSAGE,
1890 "CTest -T called with incorrect option: "
1891 << args[i].c_str() << std::endl);
1892 cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
1893 << " " << ctestExec << " -T all" << std::endl
1894 << " " << ctestExec << " -T start" << std::endl
1895 << " " << ctestExec << " -T update" << std::endl
1896 << " " << ctestExec << " -T configure" << std::endl
1897 << " " << ctestExec << " -T build" << std::endl
1898 << " " << ctestExec << " -T test" << std::endl
1899 << " " << ctestExec << " -T coverage" << std::endl
1900 << " " << ctestExec << " -T memcheck" << std::endl
1901 << " " << ctestExec << " -T notes" << std::endl
1902 << " " << ctestExec << " -T submit" << std::endl);
1906 // what type of test model
1907 if(this->CheckArgument(arg, "-M", "--test-model") &&
1908 (i < args.size() -1) )
1910 i++;
1911 std::string const& str = args[i];
1912 if ( cmSystemTools::LowerCase(str) == "nightly" )
1914 this->SetTestModel(cmCTest::NIGHTLY);
1916 else if ( cmSystemTools::LowerCase(str) == "continuous" )
1918 this->SetTestModel(cmCTest::CONTINUOUS);
1920 else if ( cmSystemTools::LowerCase(str) == "experimental" )
1922 this->SetTestModel(cmCTest::EXPERIMENTAL);
1924 else
1926 performSomeTest = false;
1927 cmCTestLog(this, ERROR_MESSAGE,
1928 "CTest -M called with incorrect option: " << str.c_str()
1929 << std::endl);
1930 cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
1931 << " " << ctestExec << " -M Continuous" << std::endl
1932 << " " << ctestExec << " -M Experimental" << std::endl
1933 << " " << ctestExec << " -M Nightly" << std::endl);
1937 if(this->CheckArgument(arg, "--extra-submit") && i < args.size() - 1)
1939 this->ProduceXML = true;
1940 this->SetTest("Submit");
1941 i++;
1942 if ( !this->SubmitExtraFiles(args[i].c_str()) )
1944 return 0;
1948 // --build-and-test options
1949 if(this->CheckArgument(arg, "--build-and-test") && i < args.size() - 1)
1951 cmakeAndTest = true;
1954 // pass the argument to all the handlers as well, but i may no longer be
1955 // set to what it was originally so I'm not sure this is working as
1956 // intended
1957 cmCTest::t_TestingHandlers::iterator it;
1958 for ( it = this->TestingHandlers.begin();
1959 it != this->TestingHandlers.end();
1960 ++ it )
1962 if ( !it->second->ProcessCommandLineArguments(arg, i, args) )
1964 cmCTestLog(this, ERROR_MESSAGE,
1965 "Problem parsing command line arguments within a handler");
1966 return 0;
1969 } // the close of the for argument loop
1972 // now what sould cmake do? if --build-and-test was specified then
1973 // we run the build and test handler and return
1974 if(cmakeAndTest)
1976 this->Verbose = true;
1977 cmCTestBuildAndTestHandler* handler =
1978 static_cast<cmCTestBuildAndTestHandler*>(this->GetHandler("buildtest"));
1979 int retv = handler->ProcessHandler();
1980 *output = handler->GetOutput();
1981 #ifdef CMAKE_BUILD_WITH_CMAKE
1982 cmDynamicLoader::FlushCache();
1983 #endif
1984 return retv;
1987 // if some tests must be run
1988 if(performSomeTest)
1990 int res;
1991 // call process directory
1992 if (this->RunConfigurationScript)
1994 if ( this->ExtraVerbose )
1996 cmCTestLog(this, OUTPUT, "* Extra verbosity turned on" << std::endl);
1998 cmCTest::t_TestingHandlers::iterator it;
1999 for ( it = this->TestingHandlers.begin();
2000 it != this->TestingHandlers.end();
2001 ++ it )
2003 it->second->SetVerbose(this->ExtraVerbose);
2004 it->second->SetSubmitIndex(this->SubmitIndex);
2006 this->GetHandler("script")->SetVerbose(this->Verbose);
2007 res = this->GetHandler("script")->ProcessHandler();
2009 else
2011 // What is this? -V seems to be the same as -VV,
2012 // and Verbose is always on in this case
2013 this->ExtraVerbose = this->Verbose;
2014 this->Verbose = true;
2015 cmCTest::t_TestingHandlers::iterator it;
2016 for ( it = this->TestingHandlers.begin();
2017 it != this->TestingHandlers.end();
2018 ++ it )
2020 it->second->SetVerbose(this->Verbose);
2021 it->second->SetSubmitIndex(this->SubmitIndex);
2023 if ( !this->Initialize(
2024 cmSystemTools::GetCurrentWorkingDirectory().c_str()) )
2026 res = 12;
2027 cmCTestLog(this, ERROR_MESSAGE, "Problem initializing the dashboard."
2028 << std::endl);
2030 else
2032 res = this->ProcessTests();
2034 this->Finalize();
2036 return res;
2039 return 1;
2042 //----------------------------------------------------------------------
2043 void cmCTest::FindRunningCMake()
2045 // Find our own executable.
2046 this->CTestSelf = cmSystemTools::GetExecutableDirectory();
2047 this->CTestSelf += "/ctest";
2048 this->CTestSelf += cmSystemTools::GetExecutableExtension();
2049 if(!cmSystemTools::FileExists(this->CTestSelf.c_str()))
2051 cmSystemTools::Error("CTest executable cannot be found at ",
2052 this->CTestSelf.c_str());
2055 this->CMakeSelf = cmSystemTools::GetExecutableDirectory();
2056 this->CMakeSelf += "/cmake";
2057 this->CMakeSelf += cmSystemTools::GetExecutableExtension();
2058 if(!cmSystemTools::FileExists(this->CMakeSelf.c_str()))
2060 cmSystemTools::Error("CMake executable cannot be found at ",
2061 this->CMakeSelf.c_str());
2065 //----------------------------------------------------------------------
2066 void cmCTest::SetNotesFiles(const char* notes)
2068 if ( !notes )
2070 return;
2072 this->NotesFiles = notes;
2075 //----------------------------------------------------------------------
2076 int cmCTest::ReadCustomConfigurationFileTree(const char* dir, cmMakefile* mf)
2078 bool found = false;
2079 VectorOfStrings dirs;
2080 VectorOfStrings ndirs;
2081 cmCTestLog(this, DEBUG, "* Read custom CTest configuration directory: "
2082 << dir << std::endl);
2084 std::string fname = dir;
2085 fname += "/CTestCustom.cmake";
2086 cmCTestLog(this, DEBUG, "* Check for file: "
2087 << fname.c_str() << std::endl);
2088 if ( cmSystemTools::FileExists(fname.c_str()) )
2090 cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: "
2091 << fname.c_str() << std::endl);
2092 bool erroroc = cmSystemTools::GetErrorOccuredFlag();
2093 cmSystemTools::ResetErrorOccuredFlag();
2095 if ( !mf->ReadListFile(0, fname.c_str()) ||
2096 cmSystemTools::GetErrorOccuredFlag() )
2098 cmCTestLog(this, ERROR_MESSAGE,
2099 "Problem reading custom configuration: "
2100 << fname.c_str() << std::endl);
2102 found = true;
2103 if ( erroroc )
2105 cmSystemTools::SetErrorOccured();
2109 std::string rexpr = dir;
2110 rexpr += "/CTestCustom.ctest";
2111 cmCTestLog(this, DEBUG, "* Check for file: "
2112 << rexpr.c_str() << std::endl);
2113 if ( !found && cmSystemTools::FileExists(rexpr.c_str()) )
2115 cmsys::Glob gl;
2116 gl.RecurseOn();
2117 gl.FindFiles(rexpr);
2118 std::vector<std::string>& files = gl.GetFiles();
2119 std::vector<std::string>::iterator fileIt;
2120 for ( fileIt = files.begin(); fileIt != files.end();
2121 ++ fileIt )
2123 cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: "
2124 << fileIt->c_str() << std::endl);
2125 if ( !mf->ReadListFile(0, fileIt->c_str()) ||
2126 cmSystemTools::GetErrorOccuredFlag() )
2128 cmCTestLog(this, ERROR_MESSAGE,
2129 "Problem reading custom configuration: "
2130 << fileIt->c_str() << std::endl);
2133 found = true;
2136 if ( found )
2138 cmCTest::t_TestingHandlers::iterator it;
2139 for ( it = this->TestingHandlers.begin();
2140 it != this->TestingHandlers.end(); ++ it )
2142 cmCTestLog(this, DEBUG,
2143 "* Read custom CTest configuration vectors for handler: "
2144 << it->first.c_str() << " (" << it->second << ")" << std::endl);
2145 it->second->PopulateCustomVectors(mf);
2149 return 1;
2152 //----------------------------------------------------------------------
2153 void cmCTest::PopulateCustomVector(cmMakefile* mf, const char* def,
2154 VectorOfStrings& vec)
2156 if ( !def)
2158 return;
2160 const char* dval = mf->GetDefinition(def);
2161 if ( !dval )
2163 return;
2165 cmCTestLog(this, DEBUG, "PopulateCustomVector: " << def << std::endl);
2166 std::vector<std::string> slist;
2167 cmSystemTools::ExpandListArgument(dval, slist);
2168 std::vector<std::string>::iterator it;
2170 for ( it = slist.begin(); it != slist.end(); ++it )
2172 cmCTestLog(this, DEBUG, " -- " << it->c_str() << std::endl);
2173 vec.push_back(it->c_str());
2177 //----------------------------------------------------------------------
2178 void cmCTest::PopulateCustomInteger(cmMakefile* mf, const char* def, int& val)
2180 if ( !def)
2182 return;
2184 const char* dval = mf->GetDefinition(def);
2185 if ( !dval )
2187 return;
2189 val = atoi(dval);
2192 //----------------------------------------------------------------------
2193 std::string cmCTest::GetShortPathToFile(const char* cfname)
2195 const std::string& sourceDir
2196 = cmSystemTools::CollapseFullPath(
2197 this->GetCTestConfiguration("SourceDirectory").c_str());
2198 const std::string& buildDir
2199 = cmSystemTools::CollapseFullPath(
2200 this->GetCTestConfiguration("BuildDirectory").c_str());
2201 std::string fname = cmSystemTools::CollapseFullPath(cfname);
2203 // Find relative paths to both directories
2204 std::string srcRelpath
2205 = cmSystemTools::RelativePath(sourceDir.c_str(), fname.c_str());
2206 std::string bldRelpath
2207 = cmSystemTools::RelativePath(buildDir.c_str(), fname.c_str());
2209 // If any contains "." it is not parent directory
2210 bool inSrc = srcRelpath.find("..") == srcRelpath.npos;
2211 bool inBld = bldRelpath.find("..") == bldRelpath.npos;
2212 // TODO: Handle files with .. in their name
2214 std::string* res = 0;
2216 if ( inSrc && inBld )
2218 // If both have relative path with no dots, pick the shorter one
2219 if ( srcRelpath.size() < bldRelpath.size() )
2221 res = &srcRelpath;
2223 else
2225 res = &bldRelpath;
2228 else if ( inSrc )
2230 res = &srcRelpath;
2232 else if ( inBld )
2234 res = &bldRelpath;
2237 std::string path;
2239 if ( !res )
2241 path = fname;
2243 else
2245 cmSystemTools::ConvertToUnixSlashes(*res);
2247 path = "./" + *res;
2248 if ( path[path.size()-1] == '/' )
2250 path = path.substr(0, path.size()-1);
2254 cmsys::SystemTools::ReplaceString(path, ":", "_");
2255 cmsys::SystemTools::ReplaceString(path, " ", "_");
2256 return path;
2259 //----------------------------------------------------------------------
2260 std::string cmCTest::GetCTestConfiguration(const char *name)
2262 if ( this->CTestConfigurationOverwrites.find(name) !=
2263 this->CTestConfigurationOverwrites.end() )
2265 return this->CTestConfigurationOverwrites[name];
2267 return this->CTestConfiguration[name];
2270 //----------------------------------------------------------------------
2271 void cmCTest::EmptyCTestConfiguration()
2273 this->CTestConfiguration.clear();
2276 //----------------------------------------------------------------------
2277 void cmCTest::SetCTestConfiguration(const char *name, const char* value)
2279 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "SetCTestConfiguration:"
2280 << name << ":" << value << "\n");
2282 if ( !name )
2284 return;
2286 if ( !value )
2288 this->CTestConfiguration.erase(name);
2289 return;
2291 this->CTestConfiguration[name] = value;
2295 //----------------------------------------------------------------------
2296 std::string cmCTest::GetCurrentTag()
2298 return this->CurrentTag;
2301 //----------------------------------------------------------------------
2302 std::string cmCTest::GetBinaryDir()
2304 return this->BinaryDir;
2307 //----------------------------------------------------------------------
2308 std::string const& cmCTest::GetConfigType()
2310 return this->ConfigType;
2313 //----------------------------------------------------------------------
2314 bool cmCTest::GetShowOnly()
2316 return this->ShowOnly;
2319 //----------------------------------------------------------------------
2320 int cmCTest::GetMaxTestNameWidth() const
2322 return this->MaxTestNameWidth;
2325 //----------------------------------------------------------------------
2326 void cmCTest::SetProduceXML(bool v)
2328 this->ProduceXML = v;
2331 //----------------------------------------------------------------------
2332 bool cmCTest::GetProduceXML()
2334 return this->ProduceXML;
2337 //----------------------------------------------------------------------
2338 const char* cmCTest::GetSpecificTrack()
2340 if ( this->SpecificTrack.empty() )
2342 return 0;
2344 return this->SpecificTrack.c_str();
2347 //----------------------------------------------------------------------
2348 void cmCTest::SetSpecificTrack(const char* track)
2350 if ( !track )
2352 this->SpecificTrack = "";
2353 return;
2355 this->SpecificTrack = track;
2358 //----------------------------------------------------------------------
2359 void cmCTest::AddSubmitFile(const char* name)
2361 this->SubmitFiles.insert(name);
2364 //----------------------------------------------------------------------
2365 void cmCTest::AddCTestConfigurationOverwrite(const char* encstr)
2367 std::string overStr = encstr;
2368 size_t epos = overStr.find("=");
2369 if ( epos == overStr.npos )
2371 cmCTestLog(this, ERROR_MESSAGE,
2372 "CTest configuration overwrite specified in the wrong format."
2373 << std::endl
2374 << "Valid format is: --overwrite key=value" << std::endl
2375 << "The specified was: --overwrite " << overStr.c_str() << std::endl);
2376 return;
2378 std::string key = overStr.substr(0, epos);
2379 std::string value = overStr.substr(epos+1, overStr.npos);
2380 this->CTestConfigurationOverwrites[key] = value;
2383 //----------------------------------------------------------------------
2384 void cmCTest::SetConfigType(const char* ct)
2386 this->ConfigType = ct?ct:"";
2387 cmSystemTools::ReplaceString(this->ConfigType, ".\\", "");
2388 std::string confTypeEnv
2389 = "CMAKE_CONFIG_TYPE=" + this->ConfigType;
2390 cmSystemTools::PutEnv(confTypeEnv.c_str());
2393 //----------------------------------------------------------------------
2394 bool cmCTest::SetCTestConfigurationFromCMakeVariable(cmMakefile* mf,
2395 const char* dconfig, const char* cmake_var)
2397 const char* ctvar;
2398 ctvar = mf->GetDefinition(cmake_var);
2399 if ( !ctvar )
2401 return false;
2403 cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
2404 "SetCTestConfigurationFromCMakeVariable:"
2405 << dconfig << ":" << cmake_var);
2406 this->SetCTestConfiguration(dconfig, ctvar);
2407 return true;
2410 bool cmCTest::RunCommand(
2411 const char* command,
2412 std::string* stdOut,
2413 std::string* stdErr,
2414 int *retVal,
2415 const char* dir,
2416 double timeout)
2418 std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
2420 if(args.size() < 1)
2422 return false;
2425 std::vector<const char*> argv;
2426 for(std::vector<cmStdString>::const_iterator a = args.begin();
2427 a != args.end(); ++a)
2429 argv.push_back(a->c_str());
2431 argv.push_back(0);
2433 *stdOut = "";
2434 *stdErr = "";
2436 cmsysProcess* cp = cmsysProcess_New();
2437 cmsysProcess_SetCommand(cp, &*argv.begin());
2438 cmsysProcess_SetWorkingDirectory(cp, dir);
2439 if(cmSystemTools::GetRunCommandHideConsole())
2441 cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
2443 cmsysProcess_SetTimeout(cp, timeout);
2444 cmsysProcess_Execute(cp);
2446 std::vector<char> tempOutput;
2447 std::vector<char> tempError;
2448 char* data;
2449 int length;
2450 int res;
2451 bool done = false;
2452 while(!done)
2454 res = cmsysProcess_WaitForData(cp, &data, &length, 0);
2455 switch ( res )
2457 case cmsysProcess_Pipe_STDOUT:
2458 tempOutput.insert(tempOutput.end(), data, data+length);
2459 break;
2460 case cmsysProcess_Pipe_STDERR:
2461 tempError.insert(tempError.end(), data, data+length);
2462 break;
2463 default:
2464 done = true;
2466 if ( (res == cmsysProcess_Pipe_STDOUT ||
2467 res == cmsysProcess_Pipe_STDERR) && this->ExtraVerbose )
2469 cmSystemTools::Stdout(data, length);
2473 cmsysProcess_WaitForExit(cp, 0);
2474 if ( tempOutput.size() > 0 )
2476 stdOut->append(&*tempOutput.begin(), tempOutput.size());
2478 if ( tempError.size() > 0 )
2480 stdErr->append(&*tempError.begin(), tempError.size());
2483 bool result = true;
2484 if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
2486 if ( retVal )
2488 *retVal = cmsysProcess_GetExitValue(cp);
2490 else
2492 if ( cmsysProcess_GetExitValue(cp) != 0 )
2494 result = false;
2498 else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
2500 const char* exception_str = cmsysProcess_GetExceptionString(cp);
2501 cmCTestLog(this, ERROR_MESSAGE, exception_str << std::endl);
2502 stdErr->append(exception_str, strlen(exception_str));
2503 result = false;
2505 else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
2507 const char* error_str = cmsysProcess_GetErrorString(cp);
2508 cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
2509 stdErr->append(error_str, strlen(error_str));
2510 result = false;
2512 else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
2514 const char* error_str = "Process terminated due to timeout\n";
2515 cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
2516 stdErr->append(error_str, strlen(error_str));
2517 result = false;
2520 cmsysProcess_Delete(cp);
2521 return result;
2524 //----------------------------------------------------------------------
2525 void cmCTest::SetOutputLogFileName(const char* name)
2527 if ( this->OutputLogFile)
2529 delete this->OutputLogFile;
2530 this->OutputLogFile= 0;
2532 if ( name )
2534 this->OutputLogFile = new cmGeneratedFileStream(name);
2538 //----------------------------------------------------------------------
2539 static const char* cmCTestStringLogType[] =
2541 "DEBUG",
2542 "OUTPUT",
2543 "HANDLER_OUTPUT",
2544 "HANDLER_VERBOSE_OUTPUT",
2545 "WARNING",
2546 "ERROR_MESSAGE",
2550 //----------------------------------------------------------------------
2551 #ifdef cerr
2552 # undef cerr
2553 #endif
2554 #ifdef cout
2555 # undef cout
2556 #endif
2558 #define cmCTestLogOutputFileLine(stream) \
2559 if ( this->ShowLineNumbers ) \
2561 (stream) << std::endl << file << ":" << line << " "; \
2564 void cmCTest::Log(int logType, const char* file, int line, const char* msg)
2566 if ( !msg || !*msg )
2568 return;
2570 if ( this->OutputLogFile )
2572 bool display = true;
2573 if ( logType == cmCTest::DEBUG && !this->Debug ) { display = false; }
2574 if ( logType == cmCTest::HANDLER_VERBOSE_OUTPUT && !this->Debug &&
2575 !this->ExtraVerbose ) { display = false; }
2576 if ( display )
2578 cmCTestLogOutputFileLine(*this->OutputLogFile);
2579 if ( logType != this->OutputLogFileLastTag )
2581 *this->OutputLogFile << "[";
2582 if ( logType >= OTHER || logType < 0 )
2584 *this->OutputLogFile << "OTHER";
2586 else
2588 *this->OutputLogFile << cmCTestStringLogType[logType];
2590 *this->OutputLogFile << "] " << std::endl << std::flush;
2592 *this->OutputLogFile << msg << std::flush;
2593 if ( logType != this->OutputLogFileLastTag )
2595 *this->OutputLogFile << std::endl << std::flush;
2596 this->OutputLogFileLastTag = logType;
2600 if ( !this->Quiet )
2602 switch ( logType )
2604 case DEBUG:
2605 if ( this->Debug )
2607 cmCTestLogOutputFileLine(std::cout);
2608 std::cout << msg;
2609 std::cout.flush();
2611 break;
2612 case OUTPUT: case HANDLER_OUTPUT:
2613 if ( this->Debug || this->Verbose )
2615 cmCTestLogOutputFileLine(std::cout);
2616 std::cout << msg;
2617 std::cout.flush();
2619 break;
2620 case HANDLER_VERBOSE_OUTPUT:
2621 if ( this->Debug || this->ExtraVerbose )
2623 cmCTestLogOutputFileLine(std::cout);
2624 std::cout << msg;
2625 std::cout.flush();
2627 break;
2628 case WARNING:
2629 cmCTestLogOutputFileLine(std::cerr);
2630 std::cerr << msg;
2631 std::cerr.flush();
2632 break;
2633 case ERROR_MESSAGE:
2634 cmCTestLogOutputFileLine(std::cerr);
2635 std::cerr << msg;
2636 std::cerr.flush();
2637 cmSystemTools::SetErrorOccured();
2638 break;
2639 default:
2640 cmCTestLogOutputFileLine(std::cout);
2641 std::cout << msg;
2642 std::cout.flush();
2647 //-------------------------------------------------------------------------
2648 double cmCTest::GetRemainingTimeAllowed()
2650 if (!this->GetHandler("script"))
2652 return 1.0e7;
2655 cmCTestScriptHandler* ch
2656 = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
2658 return ch->GetRemainingTimeAllowed();