PageRenderTime 196ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 1ms

/cmake-2.8.9-rc2/Source/cmCTest.cxx

#
C++ | 3225 lines | 2801 code | 248 blank | 176 comment | 495 complexity | d84bf56181d0ac26d8a59d9659e27288 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, Apache-2.0
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cm_curl.h"
  11. #include "cmCTest.h"
  12. #include "cmake.h"
  13. #include "cmMakefile.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmGlobalGenerator.h"
  16. #include <cmsys/Base64.h>
  17. #include <cmsys/Directory.hxx>
  18. #include <cmsys/SystemInformation.hxx>
  19. #include "cmDynamicLoader.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "cmXMLSafe.h"
  22. #include "cmVersionMacros.h"
  23. #include "cmCTestCommand.h"
  24. #include "cmCTestStartCommand.h"
  25. #include "cmCTestBuildHandler.h"
  26. #include "cmCTestBuildAndTestHandler.h"
  27. #include "cmCTestConfigureHandler.h"
  28. #include "cmCTestCoverageHandler.h"
  29. #include "cmCTestMemCheckHandler.h"
  30. #include "cmCTestScriptHandler.h"
  31. #include "cmCTestSubmitHandler.h"
  32. #include "cmCTestTestHandler.h"
  33. #include "cmCTestUpdateHandler.h"
  34. #include "cmCTestUploadHandler.h"
  35. #include "cmVersion.h"
  36. #include <cmsys/RegularExpression.hxx>
  37. #include <cmsys/Process.h>
  38. #include <cmsys/Glob.hxx>
  39. #include <stdlib.h>
  40. #include <math.h>
  41. #include <float.h>
  42. #include <ctype.h>
  43. #include <cmsys/auto_ptr.hxx>
  44. #include <cm_zlib.h>
  45. #include <cmsys/Base64.h>
  46. #if defined(__BEOS__) && !defined(__HAIKU__)
  47. #include <be/kernel/OS.h> /* disable_debugger() API. */
  48. #endif
  49. #if defined(__HAIKU__)
  50. #include <os/kernel/OS.h> /* disable_debugger() API. */
  51. #endif
  52. #define DEBUGOUT std::cout << __LINE__ << " "; std::cout
  53. #define DEBUGERR std::cerr << __LINE__ << " "; std::cerr
  54. //----------------------------------------------------------------------
  55. struct tm* cmCTest::GetNightlyTime(std::string str,
  56. bool tomorrowtag)
  57. {
  58. struct tm* lctime;
  59. time_t tctime = time(0);
  60. lctime = gmtime(&tctime);
  61. char buf[1024];
  62. // add todays year day and month to the time in str because
  63. // curl_getdate no longer assumes the day is today
  64. sprintf(buf, "%d%02d%02d %s",
  65. lctime->tm_year+1900,
  66. lctime->tm_mon +1,
  67. lctime->tm_mday,
  68. str.c_str());
  69. cmCTestLog(this, OUTPUT, "Determine Nightly Start Time" << std::endl
  70. << " Specified time: " << str.c_str() << std::endl);
  71. //Convert the nightly start time to seconds. Since we are
  72. //providing only a time and a timezone, the current date of
  73. //the local machine is assumed. Consequently, nightlySeconds
  74. //is the time at which the nightly dashboard was opened or
  75. //will be opened on the date of the current client machine.
  76. //As such, this time may be in the past or in the future.
  77. time_t ntime = curl_getdate(buf, &tctime);
  78. cmCTestLog(this, DEBUG, " Get curl time: " << ntime << std::endl);
  79. tctime = time(0);
  80. cmCTestLog(this, DEBUG, " Get the current time: " << tctime << std::endl);
  81. const int dayLength = 24 * 60 * 60;
  82. cmCTestLog(this, DEBUG, "Seconds: " << tctime << std::endl);
  83. while ( ntime > tctime )
  84. {
  85. // If nightlySeconds is in the past, this is the current
  86. // open dashboard, then return nightlySeconds. If
  87. // nightlySeconds is in the future, this is the next
  88. // dashboard to be opened, so subtract 24 hours to get the
  89. // time of the current open dashboard
  90. ntime -= dayLength;
  91. cmCTestLog(this, DEBUG, "Pick yesterday" << std::endl);
  92. cmCTestLog(this, DEBUG, " Future time, subtract day: " << ntime
  93. << std::endl);
  94. }
  95. while ( tctime > (ntime + dayLength) )
  96. {
  97. ntime += dayLength;
  98. cmCTestLog(this, DEBUG, " Past time, add day: " << ntime << std::endl);
  99. }
  100. cmCTestLog(this, DEBUG, "nightlySeconds: " << ntime << std::endl);
  101. cmCTestLog(this, DEBUG, " Current time: " << tctime
  102. << " Nightly time: " << ntime << std::endl);
  103. if ( tomorrowtag )
  104. {
  105. cmCTestLog(this, OUTPUT, " Use future tag, Add a day" << std::endl);
  106. ntime += dayLength;
  107. }
  108. lctime = gmtime(&ntime);
  109. return lctime;
  110. }
  111. //----------------------------------------------------------------------
  112. std::string cmCTest::CleanString(const std::string& str)
  113. {
  114. std::string::size_type spos = str.find_first_not_of(" \n\t\r\f\v");
  115. std::string::size_type epos = str.find_last_not_of(" \n\t\r\f\v");
  116. if ( spos == str.npos )
  117. {
  118. return std::string();
  119. }
  120. if ( epos != str.npos )
  121. {
  122. epos = epos - spos + 1;
  123. }
  124. return str.substr(spos, epos);
  125. }
  126. //----------------------------------------------------------------------
  127. std::string cmCTest::CurrentTime()
  128. {
  129. time_t currenttime = time(0);
  130. struct tm* t = localtime(&currenttime);
  131. //return ::CleanString(ctime(&currenttime));
  132. char current_time[1024];
  133. if ( this->ShortDateFormat )
  134. {
  135. strftime(current_time, 1000, "%b %d %H:%M %Z", t);
  136. }
  137. else
  138. {
  139. strftime(current_time, 1000, "%a %b %d %H:%M:%S %Z %Y", t);
  140. }
  141. cmCTestLog(this, DEBUG, " Current_Time: " << current_time << std::endl);
  142. return cmXMLSafe(cmCTest::CleanString(current_time)).str();
  143. }
  144. //----------------------------------------------------------------------
  145. std::string cmCTest::GetCostDataFile()
  146. {
  147. std::string fname = this->GetCTestConfiguration("CostDataFile");
  148. if(fname == "")
  149. {
  150. fname= this->GetBinaryDir() + "/Testing/Temporary/CTestCostData.txt";
  151. }
  152. return fname;
  153. }
  154. #ifdef CMAKE_BUILD_WITH_CMAKE
  155. //----------------------------------------------------------------------------
  156. static size_t
  157. HTTPResponseCallback(void *ptr, size_t size, size_t nmemb, void *data)
  158. {
  159. register int realsize = (int)(size * nmemb);
  160. std::string *response
  161. = static_cast<std::string*>(data);
  162. const char* chPtr = static_cast<char*>(ptr);
  163. *response += chPtr;
  164. return realsize;
  165. }
  166. //----------------------------------------------------------------------------
  167. int cmCTest::HTTPRequest(std::string url, HTTPMethod method,
  168. std::string& response,
  169. std::string fields,
  170. std::string putFile, int timeout)
  171. {
  172. CURL* curl;
  173. FILE* file;
  174. ::curl_global_init(CURL_GLOBAL_ALL);
  175. curl = ::curl_easy_init();
  176. //set request options based on method
  177. switch(method)
  178. {
  179. case cmCTest::HTTP_POST:
  180. ::curl_easy_setopt(curl, CURLOPT_POST, 1);
  181. ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str());
  182. break;
  183. case cmCTest::HTTP_PUT:
  184. if(!cmSystemTools::FileExists(putFile.c_str()))
  185. {
  186. response = "Error: File ";
  187. response += putFile + " does not exist.\n";
  188. return -1;
  189. }
  190. ::curl_easy_setopt(curl, CURLOPT_PUT, 1);
  191. file = ::fopen(putFile.c_str(), "rb");
  192. ::curl_easy_setopt(curl, CURLOPT_INFILE, file);
  193. //fall through to append GET fields
  194. case cmCTest::HTTP_GET:
  195. if(fields.size())
  196. {
  197. url += "?" + fields;
  198. }
  199. break;
  200. default:
  201. break;
  202. }
  203. ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  204. ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  205. ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  206. //set response options
  207. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HTTPResponseCallback);
  208. ::curl_easy_setopt(curl, CURLOPT_FILE, (void *)&response);
  209. ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  210. CURLcode res = ::curl_easy_perform(curl);
  211. ::curl_easy_cleanup(curl);
  212. ::curl_global_cleanup();
  213. return static_cast<int>(res);
  214. }
  215. #endif
  216. //----------------------------------------------------------------------
  217. std::string cmCTest::MakeURLSafe(const std::string& str)
  218. {
  219. cmOStringStream ost;
  220. char buffer[10];
  221. for ( std::string::size_type pos = 0; pos < str.size(); pos ++ )
  222. {
  223. unsigned char ch = str[pos];
  224. if ( ( ch > 126 || ch < 32 ||
  225. ch == '&' ||
  226. ch == '%' ||
  227. ch == '+' ||
  228. ch == '=' ||
  229. ch == '@'
  230. ) && ch != 9 )
  231. {
  232. sprintf(buffer, "%02x;", (unsigned int)ch);
  233. ost << buffer;
  234. }
  235. else
  236. {
  237. ost << ch;
  238. }
  239. }
  240. return ost.str();
  241. }
  242. //----------------------------------------------------------------------------
  243. std::string cmCTest::DecodeURL(const std::string& in)
  244. {
  245. std::string out;
  246. for(const char* c = in.c_str(); *c; ++c)
  247. {
  248. if(*c == '%' && isxdigit(*(c+1)) && isxdigit(*(c+2)))
  249. {
  250. char buf[3] = {*(c+1), *(c+2), 0};
  251. out.append(1, char(strtoul(buf, 0, 16)));
  252. c += 2;
  253. }
  254. else
  255. {
  256. out.append(1, *c);
  257. }
  258. }
  259. return out;
  260. }
  261. //----------------------------------------------------------------------
  262. cmCTest::cmCTest()
  263. {
  264. this->LabelSummary = true;
  265. this->ParallelLevel = 1;
  266. this->SubmitIndex = 0;
  267. this->Failover = false;
  268. this->BatchJobs = false;
  269. this->ForceNewCTestProcess = false;
  270. this->TomorrowTag = false;
  271. this->Verbose = false;
  272. this->Debug = false;
  273. this->ShowLineNumbers = false;
  274. this->Quiet = false;
  275. this->ExtraVerbose = false;
  276. this->ProduceXML = false;
  277. this->ShowOnly = false;
  278. this->RunConfigurationScript = false;
  279. this->UseHTTP10 = false;
  280. this->PrintLabels = false;
  281. this->CompressTestOutput = true;
  282. this->CompressMemCheckOutput = true;
  283. this->TestModel = cmCTest::EXPERIMENTAL;
  284. this->MaxTestNameWidth = 30;
  285. this->InteractiveDebugMode = true;
  286. this->TimeOut = 0;
  287. this->GlobalTimeout = 0;
  288. this->LastStopTimeout = 24 * 60 * 60;
  289. this->CompressXMLFiles = false;
  290. this->CTestConfigFile = "";
  291. this->ScheduleType = "";
  292. this->StopTime = "";
  293. this->NextDayStopTime = false;
  294. this->OutputLogFile = 0;
  295. this->OutputLogFileLastTag = -1;
  296. this->SuppressUpdatingCTestConfiguration = false;
  297. this->DartVersion = 1;
  298. this->OutputTestOutputOnTestFailure = false;
  299. this->ComputedCompressTestOutput = false;
  300. this->ComputedCompressMemCheckOutput = false;
  301. if(cmSystemTools::GetEnv("CTEST_OUTPUT_ON_FAILURE"))
  302. {
  303. this->OutputTestOutputOnTestFailure = true;
  304. }
  305. this->InitStreams();
  306. this->Parts[PartStart].SetName("Start");
  307. this->Parts[PartUpdate].SetName("Update");
  308. this->Parts[PartConfigure].SetName("Configure");
  309. this->Parts[PartBuild].SetName("Build");
  310. this->Parts[PartTest].SetName("Test");
  311. this->Parts[PartCoverage].SetName("Coverage");
  312. this->Parts[PartMemCheck].SetName("MemCheck");
  313. this->Parts[PartSubmit].SetName("Submit");
  314. this->Parts[PartNotes].SetName("Notes");
  315. this->Parts[PartExtraFiles].SetName("ExtraFiles");
  316. this->Parts[PartUpload].SetName("Upload");
  317. // Fill the part name-to-id map.
  318. for(Part p = PartStart; p != PartCount; p = Part(p+1))
  319. {
  320. this->PartMap[cmSystemTools::LowerCase(this->Parts[p].GetName())] = p;
  321. }
  322. this->ShortDateFormat = true;
  323. this->TestingHandlers["build"] = new cmCTestBuildHandler;
  324. this->TestingHandlers["buildtest"] = new cmCTestBuildAndTestHandler;
  325. this->TestingHandlers["coverage"] = new cmCTestCoverageHandler;
  326. this->TestingHandlers["script"] = new cmCTestScriptHandler;
  327. this->TestingHandlers["test"] = new cmCTestTestHandler;
  328. this->TestingHandlers["update"] = new cmCTestUpdateHandler;
  329. this->TestingHandlers["configure"] = new cmCTestConfigureHandler;
  330. this->TestingHandlers["memcheck"] = new cmCTestMemCheckHandler;
  331. this->TestingHandlers["submit"] = new cmCTestSubmitHandler;
  332. this->TestingHandlers["upload"] = new cmCTestUploadHandler;
  333. cmCTest::t_TestingHandlers::iterator it;
  334. for ( it = this->TestingHandlers.begin();
  335. it != this->TestingHandlers.end(); ++ it )
  336. {
  337. it->second->SetCTestInstance(this);
  338. }
  339. // Make sure we can capture the build tool output.
  340. cmSystemTools::EnableVSConsoleOutput();
  341. }
  342. //----------------------------------------------------------------------
  343. cmCTest::~cmCTest()
  344. {
  345. cmCTest::t_TestingHandlers::iterator it;
  346. for ( it = this->TestingHandlers.begin();
  347. it != this->TestingHandlers.end(); ++ it )
  348. {
  349. delete it->second;
  350. it->second = 0;
  351. }
  352. this->SetOutputLogFileName(0);
  353. }
  354. void cmCTest::SetParallelLevel(int level)
  355. {
  356. this->ParallelLevel = level < 1 ? 1 : level;
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmCTest::ShouldCompressTestOutput()
  360. {
  361. if(!this->ComputedCompressTestOutput)
  362. {
  363. std::string cdashVersion = this->GetCDashVersion();
  364. //version >= 1.6?
  365. bool cdashSupportsGzip = cmSystemTools::VersionCompare(
  366. cmSystemTools::OP_GREATER, cdashVersion.c_str(), "1.6") ||
  367. cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  368. cdashVersion.c_str(), "1.6");
  369. this->CompressTestOutput &= cdashSupportsGzip;
  370. this->ComputedCompressTestOutput = true;
  371. }
  372. return this->CompressTestOutput;
  373. }
  374. //----------------------------------------------------------------------------
  375. bool cmCTest::ShouldCompressMemCheckOutput()
  376. {
  377. if(!this->ComputedCompressMemCheckOutput)
  378. {
  379. std::string cdashVersion = this->GetCDashVersion();
  380. bool compressionSupported = cmSystemTools::VersionCompare(
  381. cmSystemTools::OP_GREATER, cdashVersion.c_str(), "1.9.0");
  382. this->CompressMemCheckOutput &= compressionSupported;
  383. this->ComputedCompressMemCheckOutput = true;
  384. }
  385. return this->CompressMemCheckOutput;
  386. }
  387. //----------------------------------------------------------------------------
  388. std::string cmCTest::GetCDashVersion()
  389. {
  390. #ifdef CMAKE_BUILD_WITH_CMAKE
  391. //First query the server. If that fails, fall back to the local setting
  392. std::string response;
  393. std::string url = "http://";
  394. url += this->GetCTestConfiguration("DropSite");
  395. std::string cdashUri = this->GetCTestConfiguration("DropLocation");
  396. cdashUri = cdashUri.substr(0, cdashUri.find("/submit.php"));
  397. int res = 1;
  398. if ( ! cdashUri.empty() )
  399. {
  400. url += cdashUri + "/api/getversion.php";
  401. res = cmCTest::HTTPRequest(url, cmCTest::HTTP_GET, response, "", "", 3);
  402. }
  403. return res ? this->GetCTestConfiguration("CDashVersion") : response;
  404. #else
  405. return this->GetCTestConfiguration("CDashVersion");
  406. #endif
  407. }
  408. //----------------------------------------------------------------------------
  409. cmCTest::Part cmCTest::GetPartFromName(const char* name)
  410. {
  411. // Look up by lower-case to make names case-insensitive.
  412. std::string lower_name = cmSystemTools::LowerCase(name);
  413. PartMapType::const_iterator i = this->PartMap.find(lower_name);
  414. if(i != this->PartMap.end())
  415. {
  416. return i->second;
  417. }
  418. // The string does not name a valid part.
  419. return PartCount;
  420. }
  421. //----------------------------------------------------------------------
  422. int cmCTest::Initialize(const char* binary_dir, cmCTestStartCommand* command)
  423. {
  424. cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
  425. if(!this->InteractiveDebugMode)
  426. {
  427. this->BlockTestErrorDiagnostics();
  428. }
  429. else
  430. {
  431. cmSystemTools::PutEnv("CTEST_INTERACTIVE_DEBUG_MODE=1");
  432. }
  433. this->BinaryDir = binary_dir;
  434. cmSystemTools::ConvertToUnixSlashes(this->BinaryDir);
  435. this->UpdateCTestConfiguration();
  436. cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
  437. if ( this->ProduceXML )
  438. {
  439. cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
  440. cmCTestLog(this, OUTPUT,
  441. " Site: " << this->GetCTestConfiguration("Site") << std::endl
  442. << " Build name: " << this->GetCTestConfiguration("BuildName")
  443. << std::endl);
  444. cmCTestLog(this, DEBUG, "Produce XML is on" << std::endl);
  445. if ( this->TestModel == cmCTest::NIGHTLY &&
  446. this->GetCTestConfiguration("NightlyStartTime").empty() )
  447. {
  448. cmCTestLog(this, WARNING,
  449. "WARNING: No nightly start time found please set in"
  450. " CTestConfig.cmake or DartConfig.cmake" << std::endl);
  451. cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
  452. return 0;
  453. }
  454. }
  455. cmake cm;
  456. cmGlobalGenerator gg;
  457. gg.SetCMakeInstance(&cm);
  458. cmsys::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  459. cmMakefile *mf = lg->GetMakefile();
  460. if ( !this->ReadCustomConfigurationFileTree(this->BinaryDir.c_str(), mf) )
  461. {
  462. cmCTestLog(this, DEBUG, "Cannot find custom configuration file tree"
  463. << std::endl);
  464. return 0;
  465. }
  466. if ( this->ProduceXML )
  467. {
  468. // Verify "Testing" directory exists:
  469. //
  470. std::string testingDir = this->BinaryDir + "/Testing";
  471. if ( cmSystemTools::FileExists(testingDir.c_str()) )
  472. {
  473. if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
  474. {
  475. cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
  476. << " is in the place of the testing directory" << std::endl);
  477. return 0;
  478. }
  479. }
  480. else
  481. {
  482. if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
  483. {
  484. cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory "
  485. << testingDir << std::endl);
  486. return 0;
  487. }
  488. }
  489. // Create new "TAG" file or read existing one:
  490. //
  491. bool createNewTag = true;
  492. if (command)
  493. {
  494. createNewTag = command->ShouldCreateNewTag();
  495. }
  496. std::string tagfile = testingDir + "/TAG";
  497. std::ifstream tfin(tagfile.c_str());
  498. std::string tag;
  499. if (createNewTag)
  500. {
  501. time_t tctime = time(0);
  502. if ( this->TomorrowTag )
  503. {
  504. tctime += ( 24 * 60 * 60 );
  505. }
  506. struct tm *lctime = gmtime(&tctime);
  507. if ( tfin && cmSystemTools::GetLineFromStream(tfin, tag) )
  508. {
  509. int year = 0;
  510. int mon = 0;
  511. int day = 0;
  512. int hour = 0;
  513. int min = 0;
  514. sscanf(tag.c_str(), "%04d%02d%02d-%02d%02d",
  515. &year, &mon, &day, &hour, &min);
  516. if ( year != lctime->tm_year + 1900 ||
  517. mon != lctime->tm_mon+1 ||
  518. day != lctime->tm_mday )
  519. {
  520. tag = "";
  521. }
  522. std::string tagmode;
  523. if ( cmSystemTools::GetLineFromStream(tfin, tagmode) )
  524. {
  525. if (tagmode.size() > 4 && !this->Parts[PartStart])
  526. {
  527. this->TestModel = cmCTest::GetTestModelFromString(tagmode.c_str());
  528. }
  529. }
  530. tfin.close();
  531. }
  532. if (tag.size() == 0 || (0 != command) || this->Parts[PartStart])
  533. {
  534. cmCTestLog(this, DEBUG, "TestModel: " << this->GetTestModelString()
  535. << std::endl);
  536. cmCTestLog(this, DEBUG, "TestModel: " << this->TestModel << std::endl);
  537. if ( this->TestModel == cmCTest::NIGHTLY )
  538. {
  539. lctime = this->GetNightlyTime(
  540. this->GetCTestConfiguration("NightlyStartTime"),
  541. this->TomorrowTag);
  542. }
  543. char datestring[100];
  544. sprintf(datestring, "%04d%02d%02d-%02d%02d",
  545. lctime->tm_year + 1900,
  546. lctime->tm_mon+1,
  547. lctime->tm_mday,
  548. lctime->tm_hour,
  549. lctime->tm_min);
  550. tag = datestring;
  551. std::ofstream ofs(tagfile.c_str());
  552. if ( ofs )
  553. {
  554. ofs << tag << std::endl;
  555. ofs << this->GetTestModelString() << std::endl;
  556. }
  557. ofs.close();
  558. if ( 0 == command )
  559. {
  560. cmCTestLog(this, OUTPUT, "Create new tag: " << tag << " - "
  561. << this->GetTestModelString() << std::endl);
  562. }
  563. }
  564. }
  565. else
  566. {
  567. if ( tfin )
  568. {
  569. cmSystemTools::GetLineFromStream(tfin, tag);
  570. tfin.close();
  571. }
  572. if ( tag.empty() )
  573. {
  574. cmCTestLog(this, ERROR_MESSAGE,
  575. "Cannot read existing TAG file in " << testingDir
  576. << std::endl);
  577. return 0;
  578. }
  579. cmCTestLog(this, OUTPUT, " Use existing tag: " << tag << " - "
  580. << this->GetTestModelString() << std::endl);
  581. }
  582. this->CurrentTag = tag;
  583. }
  584. return 1;
  585. }
  586. //----------------------------------------------------------------------
  587. bool cmCTest::InitializeFromCommand(cmCTestStartCommand* command)
  588. {
  589. std::string src_dir
  590. = this->GetCTestConfiguration("SourceDirectory").c_str();
  591. std::string bld_dir = this->GetCTestConfiguration("BuildDirectory").c_str();
  592. this->DartVersion = 1;
  593. for(Part p = PartStart; p != PartCount; p = Part(p+1))
  594. {
  595. this->Parts[p].SubmitFiles.clear();
  596. }
  597. cmMakefile* mf = command->GetMakefile();
  598. std::string fname;
  599. std::string src_dir_fname = src_dir;
  600. src_dir_fname += "/CTestConfig.cmake";
  601. cmSystemTools::ConvertToUnixSlashes(src_dir_fname);
  602. std::string bld_dir_fname = bld_dir;
  603. bld_dir_fname += "/CTestConfig.cmake";
  604. cmSystemTools::ConvertToUnixSlashes(bld_dir_fname);
  605. if ( cmSystemTools::FileExists(bld_dir_fname.c_str()) )
  606. {
  607. fname = bld_dir_fname;
  608. }
  609. else if ( cmSystemTools::FileExists(src_dir_fname.c_str()) )
  610. {
  611. fname = src_dir_fname;
  612. }
  613. if ( !fname.empty() )
  614. {
  615. cmCTestLog(this, OUTPUT, " Reading ctest configuration file: "
  616. << fname.c_str() << std::endl);
  617. bool readit = mf->ReadListFile(mf->GetCurrentListFile(),
  618. fname.c_str() );
  619. if(!readit)
  620. {
  621. std::string m = "Could not find include file: ";
  622. m += fname;
  623. command->SetError(m.c_str());
  624. return false;
  625. }
  626. }
  627. else
  628. {
  629. cmCTestLog(this, WARNING,
  630. "Cannot locate CTest configuration: in BuildDirectory: "
  631. << bld_dir_fname.c_str() << std::endl);
  632. cmCTestLog(this, WARNING,
  633. "Cannot locate CTest configuration: in SourceDirectory: "
  634. << src_dir_fname.c_str() << std::endl);
  635. }
  636. this->SetCTestConfigurationFromCMakeVariable(mf, "NightlyStartTime",
  637. "CTEST_NIGHTLY_START_TIME");
  638. this->SetCTestConfigurationFromCMakeVariable(mf, "Site", "CTEST_SITE");
  639. this->SetCTestConfigurationFromCMakeVariable(mf, "BuildName",
  640. "CTEST_BUILD_NAME");
  641. const char* dartVersion = mf->GetDefinition("CTEST_DART_SERVER_VERSION");
  642. if ( dartVersion )
  643. {
  644. this->DartVersion = atoi(dartVersion);
  645. if ( this->DartVersion < 0 )
  646. {
  647. cmCTestLog(this, ERROR_MESSAGE, "Invalid Dart server version: "
  648. << dartVersion << ". Please specify the version number."
  649. << std::endl);
  650. return false;
  651. }
  652. }
  653. if ( !this->Initialize(bld_dir.c_str(), command) )
  654. {
  655. return false;
  656. }
  657. cmCTestLog(this, OUTPUT, " Use " << this->GetTestModelString()
  658. << " tag: " << this->GetCurrentTag() << std::endl);
  659. return true;
  660. }
  661. //----------------------------------------------------------------------
  662. bool cmCTest::UpdateCTestConfiguration()
  663. {
  664. if ( this->SuppressUpdatingCTestConfiguration )
  665. {
  666. return true;
  667. }
  668. std::string fileName = this->CTestConfigFile;
  669. if ( fileName.empty() )
  670. {
  671. fileName = this->BinaryDir + "/CTestConfiguration.ini";
  672. if ( !cmSystemTools::FileExists(fileName.c_str()) )
  673. {
  674. fileName = this->BinaryDir + "/DartConfiguration.tcl";
  675. }
  676. }
  677. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "UpdateCTestConfiguration from :"
  678. << fileName.c_str() << "\n");
  679. if ( !cmSystemTools::FileExists(fileName.c_str()) )
  680. {
  681. // No need to exit if we are not producing XML
  682. if ( this->ProduceXML )
  683. {
  684. cmCTestLog(this, ERROR_MESSAGE, "Cannot find file: " << fileName.c_str()
  685. << std::endl);
  686. return false;
  687. }
  688. }
  689. else
  690. {
  691. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Parse Config file:"
  692. << fileName.c_str() << "\n");
  693. // parse the dart test file
  694. std::ifstream fin(fileName.c_str());
  695. if(!fin)
  696. {
  697. return false;
  698. }
  699. char buffer[1024];
  700. while ( fin )
  701. {
  702. buffer[0] = 0;
  703. fin.getline(buffer, 1023);
  704. buffer[1023] = 0;
  705. std::string line = cmCTest::CleanString(buffer);
  706. if(line.size() == 0)
  707. {
  708. continue;
  709. }
  710. while ( fin && (line[line.size()-1] == '\\') )
  711. {
  712. line = line.substr(0, line.size()-1);
  713. buffer[0] = 0;
  714. fin.getline(buffer, 1023);
  715. buffer[1023] = 0;
  716. line += cmCTest::CleanString(buffer);
  717. }
  718. if ( line[0] == '#' )
  719. {
  720. continue;
  721. }
  722. std::string::size_type cpos = line.find_first_of(":");
  723. if ( cpos == line.npos )
  724. {
  725. continue;
  726. }
  727. std::string key = line.substr(0, cpos);
  728. std::string value
  729. = cmCTest::CleanString(line.substr(cpos+1, line.npos));
  730. this->CTestConfiguration[key] = value;
  731. }
  732. fin.close();
  733. }
  734. if ( !this->GetCTestConfiguration("BuildDirectory").empty() )
  735. {
  736. this->BinaryDir = this->GetCTestConfiguration("BuildDirectory");
  737. cmSystemTools::ChangeDirectory(this->BinaryDir.c_str());
  738. }
  739. this->TimeOut = atoi(this->GetCTestConfiguration("TimeOut").c_str());
  740. if ( this->ProduceXML )
  741. {
  742. this->CompressXMLFiles = cmSystemTools::IsOn(
  743. this->GetCTestConfiguration("CompressSubmission").c_str());
  744. }
  745. return true;
  746. }
  747. //----------------------------------------------------------------------
  748. void cmCTest::BlockTestErrorDiagnostics()
  749. {
  750. cmSystemTools::PutEnv("DART_TEST_FROM_DART=1");
  751. cmSystemTools::PutEnv("DASHBOARD_TEST_FROM_CTEST=" CMake_VERSION);
  752. #if defined(_WIN32)
  753. SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
  754. #elif defined(__BEOS__) || defined(__HAIKU__)
  755. disable_debugger(1);
  756. #endif
  757. }
  758. //----------------------------------------------------------------------
  759. void cmCTest::SetTestModel(int mode)
  760. {
  761. this->InteractiveDebugMode = false;
  762. this->TestModel = mode;
  763. }
  764. //----------------------------------------------------------------------
  765. bool cmCTest::SetTest(const char* ttype, bool report)
  766. {
  767. if ( cmSystemTools::LowerCase(ttype) == "all" )
  768. {
  769. for(Part p = PartStart; p != PartCount; p = Part(p+1))
  770. {
  771. this->Parts[p].Enable();
  772. }
  773. return true;
  774. }
  775. Part p = this->GetPartFromName(ttype);
  776. if(p != PartCount)
  777. {
  778. this->Parts[p].Enable();
  779. return true;
  780. }
  781. else
  782. {
  783. if ( report )
  784. {
  785. cmCTestLog(this, ERROR_MESSAGE, "Don't know about test \"" << ttype
  786. << "\" yet..." << std::endl);
  787. }
  788. return false;
  789. }
  790. }
  791. //----------------------------------------------------------------------
  792. void cmCTest::Finalize()
  793. {
  794. }
  795. //----------------------------------------------------------------------
  796. bool cmCTest::OpenOutputFile(const std::string& path,
  797. const std::string& name, cmGeneratedFileStream& stream,
  798. bool compress)
  799. {
  800. std::string testingDir = this->BinaryDir + "/Testing";
  801. if ( path.size() > 0 )
  802. {
  803. testingDir += "/" + path;
  804. }
  805. if ( cmSystemTools::FileExists(testingDir.c_str()) )
  806. {
  807. if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
  808. {
  809. cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
  810. << " is in the place of the testing directory"
  811. << std::endl);
  812. return false;
  813. }
  814. }
  815. else
  816. {
  817. if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
  818. {
  819. cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory " << testingDir
  820. << std::endl);
  821. return false;
  822. }
  823. }
  824. std::string filename = testingDir + "/" + name;
  825. stream.Open(filename.c_str());
  826. if( !stream )
  827. {
  828. cmCTestLog(this, ERROR_MESSAGE, "Problem opening file: " << filename
  829. << std::endl);
  830. return false;
  831. }
  832. if ( compress )
  833. {
  834. if ( this->CompressXMLFiles )
  835. {
  836. stream.SetCompression(true);
  837. }
  838. }
  839. return true;
  840. }
  841. //----------------------------------------------------------------------
  842. bool cmCTest::AddIfExists(Part part, const char* file)
  843. {
  844. if ( this->CTestFileExists(file) )
  845. {
  846. this->AddSubmitFile(part, file);
  847. }
  848. else
  849. {
  850. std::string name = file;
  851. name += ".gz";
  852. if ( this->CTestFileExists(name.c_str()) )
  853. {
  854. this->AddSubmitFile(part, file);
  855. }
  856. else
  857. {
  858. return false;
  859. }
  860. }
  861. return true;
  862. }
  863. //----------------------------------------------------------------------
  864. bool cmCTest::CTestFileExists(const std::string& filename)
  865. {
  866. std::string testingDir = this->BinaryDir + "/Testing/" +
  867. this->CurrentTag + "/" + filename;
  868. return cmSystemTools::FileExists(testingDir.c_str());
  869. }
  870. //----------------------------------------------------------------------
  871. cmCTestGenericHandler* cmCTest::GetInitializedHandler(const char* handler)
  872. {
  873. cmCTest::t_TestingHandlers::iterator it =
  874. this->TestingHandlers.find(handler);
  875. if ( it == this->TestingHandlers.end() )
  876. {
  877. return 0;
  878. }
  879. it->second->Initialize();
  880. return it->second;
  881. }
  882. //----------------------------------------------------------------------
  883. cmCTestGenericHandler* cmCTest::GetHandler(const char* handler)
  884. {
  885. cmCTest::t_TestingHandlers::iterator it =
  886. this->TestingHandlers.find(handler);
  887. if ( it == this->TestingHandlers.end() )
  888. {
  889. return 0;
  890. }
  891. return it->second;
  892. }
  893. //----------------------------------------------------------------------
  894. int cmCTest::ExecuteHandler(const char* shandler)
  895. {
  896. cmCTestGenericHandler* handler = this->GetHandler(shandler);
  897. if ( !handler )
  898. {
  899. return -1;
  900. }
  901. handler->Initialize();
  902. return handler->ProcessHandler();
  903. }
  904. //----------------------------------------------------------------------
  905. int cmCTest::ProcessTests()
  906. {
  907. int res = 0;
  908. bool notest = true;
  909. int update_count = 0;
  910. for(Part p = PartStart; notest && p != PartCount; p = Part(p+1))
  911. {
  912. notest = !this->Parts[p];
  913. }
  914. if (this->Parts[PartUpdate] &&
  915. (this->GetRemainingTimeAllowed() - 120 > 0))
  916. {
  917. cmCTestGenericHandler* uphandler = this->GetHandler("update");
  918. uphandler->SetPersistentOption("SourceDirectory",
  919. this->GetCTestConfiguration("SourceDirectory").c_str());
  920. update_count = uphandler->ProcessHandler();
  921. if ( update_count < 0 )
  922. {
  923. res |= cmCTest::UPDATE_ERRORS;
  924. }
  925. }
  926. if ( this->TestModel == cmCTest::CONTINUOUS && !update_count )
  927. {
  928. return 0;
  929. }
  930. if (this->Parts[PartConfigure] &&
  931. (this->GetRemainingTimeAllowed() - 120 > 0))
  932. {
  933. if (this->GetHandler("configure")->ProcessHandler() < 0)
  934. {
  935. res |= cmCTest::CONFIGURE_ERRORS;
  936. }
  937. }
  938. if (this->Parts[PartBuild] &&
  939. (this->GetRemainingTimeAllowed() - 120 > 0))
  940. {
  941. this->UpdateCTestConfiguration();
  942. if (this->GetHandler("build")->ProcessHandler() < 0)
  943. {
  944. res |= cmCTest::BUILD_ERRORS;
  945. }
  946. }
  947. if ((this->Parts[PartTest] || notest) &&
  948. (this->GetRemainingTimeAllowed() - 120 > 0))
  949. {
  950. this->UpdateCTestConfiguration();
  951. if (this->GetHandler("test")->ProcessHandler() < 0)
  952. {
  953. res |= cmCTest::TEST_ERRORS;
  954. }
  955. }
  956. if (this->Parts[PartCoverage] &&
  957. (this->GetRemainingTimeAllowed() - 120 > 0))
  958. {
  959. this->UpdateCTestConfiguration();
  960. if (this->GetHandler("coverage")->ProcessHandler() < 0)
  961. {
  962. res |= cmCTest::COVERAGE_ERRORS;
  963. }
  964. }
  965. if (this->Parts[PartMemCheck] &&
  966. (this->GetRemainingTimeAllowed() - 120 > 0))
  967. {
  968. this->UpdateCTestConfiguration();
  969. if (this->GetHandler("memcheck")->ProcessHandler() < 0)
  970. {
  971. res |= cmCTest::MEMORY_ERRORS;
  972. }
  973. }
  974. if ( !notest )
  975. {
  976. std::string notes_dir = this->BinaryDir + "/Testing/Notes";
  977. if ( cmSystemTools::FileIsDirectory(notes_dir.c_str()) )
  978. {
  979. cmsys::Directory d;
  980. d.Load(notes_dir.c_str());
  981. unsigned long kk;
  982. for ( kk = 0; kk < d.GetNumberOfFiles(); kk ++ )
  983. {
  984. const char* file = d.GetFile(kk);
  985. std::string fullname = notes_dir + "/" + file;
  986. if ( cmSystemTools::FileExists(fullname.c_str()) &&
  987. !cmSystemTools::FileIsDirectory(fullname.c_str()) )
  988. {
  989. if ( this->NotesFiles.size() > 0 )
  990. {
  991. this->NotesFiles += ";";
  992. }
  993. this->NotesFiles += fullname;
  994. this->Parts[PartNotes].Enable();
  995. }
  996. }
  997. }
  998. }
  999. if (this->Parts[PartNotes])
  1000. {
  1001. this->UpdateCTestConfiguration();
  1002. if ( this->NotesFiles.size() )
  1003. {
  1004. this->GenerateNotesFile(this->NotesFiles.c_str());
  1005. }
  1006. }
  1007. if (this->Parts[PartSubmit])
  1008. {
  1009. this->UpdateCTestConfiguration();
  1010. if (this->GetHandler("submit")->ProcessHandler() < 0)
  1011. {
  1012. res |= cmCTest::SUBMIT_ERRORS;
  1013. }
  1014. }
  1015. if ( res != 0 )
  1016. {
  1017. cmCTestLog(this, ERROR_MESSAGE, "Errors while running CTest"
  1018. << std::endl);
  1019. }
  1020. return res;
  1021. }
  1022. //----------------------------------------------------------------------
  1023. std::string cmCTest::GetTestModelString()
  1024. {
  1025. if ( !this->SpecificTrack.empty() )
  1026. {
  1027. return this->SpecificTrack;
  1028. }
  1029. switch ( this->TestModel )
  1030. {
  1031. case cmCTest::NIGHTLY:
  1032. return "Nightly";
  1033. case cmCTest::CONTINUOUS:
  1034. return "Continuous";
  1035. }
  1036. return "Experimental";
  1037. }
  1038. //----------------------------------------------------------------------
  1039. int cmCTest::GetTestModelFromString(const char* str)
  1040. {
  1041. if ( !str )
  1042. {
  1043. return cmCTest::EXPERIMENTAL;
  1044. }
  1045. std::string rstr = cmSystemTools::LowerCase(str);
  1046. if ( strncmp(rstr.c_str(), "cont", 4) == 0 )
  1047. {
  1048. return cmCTest::CONTINUOUS;
  1049. }
  1050. if ( strncmp(rstr.c_str(), "nigh", 4) == 0 )
  1051. {
  1052. return cmCTest::NIGHTLY;
  1053. }
  1054. return cmCTest::EXPERIMENTAL;
  1055. }
  1056. //######################################################################
  1057. //######################################################################
  1058. //######################################################################
  1059. //######################################################################
  1060. //----------------------------------------------------------------------
  1061. int cmCTest::RunMakeCommand(const char* command, std::string* output,
  1062. int* retVal, const char* dir, int timeout, std::ofstream& ofs)
  1063. {
  1064. // First generate the command and arguments
  1065. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  1066. if(args.size() < 1)
  1067. {
  1068. return false;
  1069. }
  1070. std::vector<const char*> argv;
  1071. for(std::vector<cmStdString>::const_iterator a = args.begin();
  1072. a != args.end(); ++a)
  1073. {
  1074. argv.push_back(a->c_str());
  1075. }
  1076. argv.push_back(0);
  1077. if ( output )
  1078. {
  1079. *output = "";
  1080. }
  1081. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Run command:");
  1082. std::vector<const char*>::iterator ait;
  1083. for ( ait = argv.begin(); ait != argv.end() && *ait; ++ ait )
  1084. {
  1085. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, " \"" << *ait << "\"");
  1086. }
  1087. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, std::endl);
  1088. // Now create process object
  1089. cmsysProcess* cp = cmsysProcess_New();
  1090. cmsysProcess_SetCommand(cp, &*argv.begin());
  1091. cmsysProcess_SetWorkingDirectory(cp, dir);
  1092. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  1093. cmsysProcess_SetTimeout(cp, timeout);
  1094. cmsysProcess_Execute(cp);
  1095. // Initialize tick's
  1096. std::string::size_type tick = 0;
  1097. std::string::size_type tick_len = 1024;
  1098. std::string::size_type tick_line_len = 50;
  1099. char* data;
  1100. int length;
  1101. cmCTestLog(this, HANDLER_OUTPUT,
  1102. " Each . represents " << tick_len << " bytes of output" << std::endl
  1103. << " " << std::flush);
  1104. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  1105. {
  1106. if ( output )
  1107. {
  1108. for(int cc =0; cc < length; ++cc)
  1109. {
  1110. if(data[cc] == 0)
  1111. {
  1112. data[cc] = '\n';
  1113. }
  1114. }
  1115. output->append(data, length);
  1116. while ( output->size() > (tick * tick_len) )
  1117. {
  1118. tick ++;
  1119. cmCTestLog(this, HANDLER_OUTPUT, "." << std::flush);
  1120. if ( tick % tick_line_len == 0 && tick > 0 )
  1121. {
  1122. cmCTestLog(this, HANDLER_OUTPUT, " Size: "
  1123. << int((double(output->size()) / 1024.0) + 1) << "K" << std::endl
  1124. << " " << std::flush);
  1125. }
  1126. }
  1127. }
  1128. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
  1129. if ( ofs )
  1130. {
  1131. ofs << cmCTestLogWrite(data, length);
  1132. }
  1133. }
  1134. cmCTestLog(this, OUTPUT, " Size of output: "
  1135. << int(double(output->size()) / 1024.0) << "K" << std::endl);
  1136. cmsysProcess_WaitForExit(cp, 0);
  1137. int result = cmsysProcess_GetState(cp);
  1138. if(result == cmsysProcess_State_Exited)
  1139. {
  1140. *retVal = cmsysProcess_GetExitValue(cp);
  1141. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Command exited with the value: "
  1142. << *retVal << std::endl);
  1143. }
  1144. else if(result == cmsysProcess_State_Exception)
  1145. {
  1146. *retVal = cmsysProcess_GetExitException(cp);
  1147. cmCTestLog(this, WARNING, "There was an exception: " << *retVal
  1148. << std::endl);
  1149. }
  1150. else if(result == cmsysProcess_State_Expired)
  1151. {
  1152. cmCTestLog(this, WARNING, "There was a timeout" << std::endl);
  1153. }
  1154. else if(result == cmsysProcess_State_Error)
  1155. {
  1156. *output += "\n*** ERROR executing: ";
  1157. *output += cmsysProcess_GetErrorString(cp);
  1158. *output += "\n***The build process failed.";
  1159. cmCTestLog(this, ERROR_MESSAGE, "There was an error: "
  1160. << cmsysProcess_GetErrorString(cp) << std::endl);
  1161. }
  1162. cmsysProcess_Delete(cp);
  1163. return result;
  1164. }
  1165. //######################################################################
  1166. //######################################################################
  1167. //######################################################################
  1168. //######################################################################
  1169. //----------------------------------------------------------------------
  1170. int cmCTest::RunTest(std::vector<const char*> argv,
  1171. std::string* output, int *retVal,
  1172. std::ostream* log, double testTimeOut,
  1173. std::vector<std::string>* environment)
  1174. {
  1175. bool modifyEnv = (environment && environment->size()>0);
  1176. // determine how much time we have
  1177. double timeout = this->GetRemainingTimeAllowed() - 120;
  1178. if (this->TimeOut > 0 && this->TimeOut < timeout)
  1179. {
  1180. timeout = this->TimeOut;
  1181. }
  1182. if (testTimeOut > 0
  1183. && testTimeOut < this->GetRemainingTimeAllowed())
  1184. {
  1185. timeout = testTimeOut;
  1186. }
  1187. // always have at least 1 second if we got to here
  1188. if (timeout <= 0)
  1189. {
  1190. timeout = 1;
  1191. }
  1192. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
  1193. "Test timeout computed to be: " << timeout << "\n");
  1194. if(cmSystemTools::SameFile(argv[0], this->CTestSelf.c_str()) &&
  1195. !this->ForceNewCTestProcess)
  1196. {
  1197. cmCTest inst;
  1198. inst.ConfigType = this->ConfigType;
  1199. inst.TimeOut = timeout;
  1200. // Capture output of the child ctest.
  1201. cmOStringStream oss;
  1202. inst.SetStreams(&oss, &oss);
  1203. std::vector<std::string> args;
  1204. for(unsigned int i =0; i < argv.size(); ++i)
  1205. {
  1206. if(argv[i])
  1207. {
  1208. // make sure we pass the timeout in for any build and test
  1209. // invocations. Since --build-generator is required this is a
  1210. // good place to check for it, and to add the arguments in
  1211. if (strcmp(argv[i],"--build-generator") == 0 && timeout > 0)
  1212. {
  1213. args.push_back("--test-timeout");
  1214. cmOStringStream msg;
  1215. msg << timeout;
  1216. args.push_back(msg.str());
  1217. }
  1218. args.push_back(argv[i]);
  1219. }
  1220. }
  1221. if ( log )
  1222. {
  1223. *log << "* Run internal CTest" << std::endl;
  1224. }
  1225. std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory();
  1226. cmsys::auto_ptr<cmSystemTools::SaveRestoreEnvironment> saveEnv;
  1227. if (modifyEnv)
  1228. {
  1229. saveEnv.reset(new cmSystemTools::SaveRestoreEnvironment);
  1230. cmSystemTools::AppendEnv(*environment);
  1231. }
  1232. *retVal = inst.Run(args, output);
  1233. *output += oss.str();
  1234. if ( log )
  1235. {
  1236. *log << output->c_str();
  1237. }
  1238. cmSystemTools::ChangeDirectory(oldpath.c_str());
  1239. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
  1240. "Internal cmCTest object used to run test." << std::endl
  1241. << *output << std::endl);
  1242. return cmsysProcess_State_Exited;
  1243. }
  1244. std::vector<char> tempOutput;
  1245. if ( output )
  1246. {
  1247. *output = "";
  1248. }
  1249. cmsys::auto_ptr<cmSystemTools::SaveRestoreEnvironment> saveEnv;
  1250. if (modifyEnv)
  1251. {
  1252. saveEnv.reset(new cmSystemTools::SaveRestoreEnvironment);
  1253. cmSystemTools::AppendEnv(*environment);
  1254. }
  1255. cmsysProcess* cp = cmsysProcess_New();
  1256. cmsysProcess_SetCommand(cp, &*argv.begin());
  1257. cmCTestLog(this, DEBUG, "Command is: " << argv[0] << std::endl);
  1258. if(cmSystemTools::GetRunCommandHideConsole())
  1259. {
  1260. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  1261. }
  1262. cmsysProcess_SetTimeout(cp, timeout);
  1263. cmsysProcess_Execute(cp);
  1264. char* data;
  1265. int length;
  1266. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  1267. {
  1268. if ( output )
  1269. {
  1270. tempOutput.insert(tempOutput.end(), data, data+length);
  1271. }
  1272. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
  1273. if ( log )
  1274. {
  1275. log->write(data, length);
  1276. }
  1277. }
  1278. cmsysProcess_WaitForExit(cp, 0);
  1279. if(output && tempOutput.begin() != tempOutput.end())
  1280. {
  1281. output->append(&*tempOutput.begin(), tempOutput.size());
  1282. }
  1283. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "-- Process completed"
  1284. << std::endl);
  1285. int result = cmsysProcess_GetState(cp);
  1286. if(result == cmsysProcess_State_Exited)
  1287. {
  1288. *retVal = cmsysProcess_GetExitValue(cp);
  1289. if(*retVal != 0 && this->OutputTestOutputOnTestFailure)
  1290. {
  1291. OutputTestErrors(tempOutput);
  1292. }
  1293. }
  1294. else if(result == cmsysProcess_State_Exception)
  1295. {
  1296. if(this->OutputTestOutputOnTestFailure)
  1297. {
  1298. OutputTestErrors(tempOutput);
  1299. }
  1300. *retVal = cmsysProcess_GetExitException(cp);
  1301. std::string outerr = "\n*** Exception executing: ";
  1302. outerr += cmsysProcess_GetExceptionString(cp);
  1303. *output += outerr;
  1304. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl
  1305. << std::flush);
  1306. }
  1307. else if(result == cmsysProcess_State_Error)
  1308. {
  1309. std::string outerr = "\n*** ERROR executing: ";
  1310. outerr += cmsysProcess_GetErrorString(cp);
  1311. *output += outerr;
  1312. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl
  1313. << std::flush);
  1314. }
  1315. cmsysProcess_Delete(cp);
  1316. return result;
  1317. }
  1318. //----------------------------------------------------------------------
  1319. std::string cmCTest::SafeBuildIdField(const std::string& value)
  1320. {
  1321. std::string safevalue(value);
  1322. if (safevalue != "")
  1323. {
  1324. // Disallow non-filename and non-space whitespace characters.
  1325. // If they occur, replace them with ""
  1326. //
  1327. const char *disallowed = "\\/:*?\"<>|\n\r\t\f\v";
  1328. if (safevalue.find_first_of(disallowed) != value.npos)
  1329. {
  1330. std::string::size_type i = 0;
  1331. std::string::size_type n = strlen(disallowed);
  1332. char replace[2];
  1333. replace[1] = 0;
  1334. for (i= 0; i<n; ++i)
  1335. {
  1336. replace[0] = disallowed[i];
  1337. cmSystemTools::ReplaceString(safevalue, replace, "");
  1338. }
  1339. }
  1340. safevalue = cmXMLSafe(safevalue).str();
  1341. }
  1342. if (safevalue == "")
  1343. {
  1344. safevalue = "(empty)";
  1345. }
  1346. return safevalue;
  1347. }
  1348. //----------------------------------------------------------------------
  1349. void cmCTest::StartXML(std::ostream& ostr, bool append)
  1350. {
  1351. if(this->CurrentTag.empty())
  1352. {
  1353. cmCTestLog(this, ERROR_MESSAGE,
  1354. "Current Tag empty, this may mean"
  1355. " NightlStartTime was not set correctly." << std::endl);
  1356. cmSystemTools::SetFatalErrorOccured();
  1357. }
  1358. // find out about the system
  1359. cmsys::SystemInformation info;
  1360. info.RunCPUCheck();
  1361. info.RunOSCheck();
  1362. info.RunMemoryCheck();
  1363. std::string buildname = cmCTest::SafeBuildIdField(
  1364. this->GetCTestConfiguration("BuildName"));
  1365. std::string stamp = cmCTest::SafeBuildIdField(
  1366. this->CurrentTag + "-" + this->GetTestModelString());
  1367. std::string site = cmCTest::SafeBuildIdField(
  1368. this->GetCTestConfiguration("Site"));
  1369. ostr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1370. << "<Site BuildName=\"" << buildname << "\"\n"
  1371. << "\tBuildStamp=\"" << stamp << "\"\n"
  1372. << "\tName=\"" << site << "\"\n"
  1373. << "\tGenerator=\"ctest-" << cmVersion::GetCMakeVersion() << "\"\n"
  1374. << (append? "\tAppend=\"true\"\n":"")
  1375. << "\tCompilerName=\"" << this->GetCTestConfiguration("Compiler")
  1376. << "\"\n"
  1377. #ifdef _COMPILER_VERSION
  1378. << "\tCompilerVersion=\"_COMPILER_VERSION\"\n"
  1379. #endif
  1380. << "\tOSName=\"" << info.GetOSName() << "\"\n"
  1381. << "\tHostname=\"" << info.GetHostname() << "\"\n"
  1382. << "\tOSRelease=\"" << info.GetOSRelease() << "\"\n"
  1383. << "\tOSVersion=\"" << info.GetOSVersion() << "\"\n"
  1384. << "\tOSPlatform=\"" << info.GetOSPlatform() << "\"\n"
  1385. << "\tIs64Bits=\"" << info.Is64Bits() << "\"\n"
  1386. << "\tVendorString=\"" << info.GetVendorString() << "\"\n"
  1387. << "\tVendorID=\"" << info.GetVendorID() << "\"\n"
  1388. << "\tFamilyID=\"" << info.GetFamilyID() << "\"\n"
  1389. << "\tModelID=\"" << info.GetModelID() << "\"\n"
  1390. << "\tProcessorCacheSize=\"" << info.GetProcessorCacheSize() << "\"\n"
  1391. << "\tNumberOfLogicalCPU=\"" << info.GetNumberOfLogicalCPU() << "\"\n"
  1392. << "\tNumberOfPhysicalCPU=\""<< info.GetNumberOfPhysicalCPU() << "\"\n"
  1393. << "\tTotalVirtualMemory=\"" << info.GetTotalVirtualMemory() << "\"\n"
  1394. << "\tTotalPhysicalMemory=\""<< info.GetTotalPhysicalMemory() << "\"\n"
  1395. << "\tLogicalProcessorsPerPhysical=\""
  1396. << info.GetLogicalProcessorsPerPhysical() << "\"\n"
  1397. << "\tProcessorClockFrequency=\""
  1398. << info.GetProcessorClockFrequency() << "\"\n"
  1399. << ">" << std::endl;
  1400. this->AddSiteProperties(ostr);
  1401. }
  1402. //----------------------------------------------------------------------
  1403. void cmCTest::AddSiteProperties(std::ostream& ostr)
  1404. {
  1405. cmCTestScriptHandler* ch =
  1406. static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  1407. cmake* cm = ch->GetCMake();
  1408. // if no CMake then this is the old style script and props like
  1409. // this will not work anyway.
  1410. if(!cm)
  1411. {
  1412. return;
  1413. }
  1414. // This code should go when cdash is changed to use labels only
  1415. const char* subproject = cm->GetProperty("SubProject", cmProperty::GLOBAL);
  1416. if(subproject)
  1417. {
  1418. ostr << "<Subproject name=\"" << subproject << "\">\n";
  1419. const char* labels =
  1420. ch->GetCMake()->GetProperty("SubProjectLabels", cmProperty::GLOBAL);
  1421. if(labels)
  1422. {
  1423. ostr << " <Labels>\n";
  1424. std::string l = labels;
  1425. std::vector<std::string> args;
  1426. cmSystemTools::ExpandListArgument(l, args);
  1427. for(std::vector<std::string>::iterator i = args.begin();
  1428. i != args.end(); ++i)
  1429. {
  1430. ostr << " <Label>" << i->c_str() << "</Label>\n";
  1431. }
  1432. ostr << " </Labels>\n";
  1433. }
  1434. ostr << "</Subproject>\n";
  1435. }
  1436. // This code should stay when cdash only does label based sub-projects
  1437. const char* label = cm->GetProperty("Label", cmProperty::GLOBAL);
  1438. if(label)
  1439. {
  1440. ostr << "<Labels>\n";
  1441. ostr << " <Label>" << label << "</Label>\n";
  1442. ostr << "</Labels>\n";
  1443. }
  1444. }
  1445. //----------------------------------------------------------------------
  1446. void cmCTest::EndXML(std::ostream& ostr)
  1447. {
  1448. ostr << "</Site>" << std::endl;
  1449. }
  1450. //----------------------------------------------------------------------
  1451. int cmCTest::GenerateCTestNotesOutput(std::ostream& os,
  1452. const cmCTest::VectorOfStrings& files)
  1453. {
  1454. cmCTest::VectorOfStrings::const_iterator it;
  1455. os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1456. << "<?xml-stylesheet type=\"text/xsl\" "
  1457. "href=\"Dart/Source/Server/XSL/Build.xsl "
  1458. "<file:///Dart/Source/Server/XSL/Build.xsl> \"?>\n"
  1459. << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName")
  1460. << "\" BuildStamp=\""
  1461. << this->CurrentTag << "-" << this->GetTestModelString() << "\" Name=\""
  1462. << this->GetCTestConfiguration("Site") << "\" Generator=\"ctest"
  1463. << cmVersion::GetCMakeVersion()
  1464. << "\">\n";
  1465. this->AddSiteProperties(os);
  1466. os << "<Notes>" << std::endl;
  1467. for ( it = files.begin(); it != files.end(); it ++ )
  1468. {
  1469. cmCTestLog(this, OUTPUT, "\tAdd file: " << it->c_str() << std::endl);
  1470. std::string note_time = this->CurrentTime();
  1471. os << "<Note Name=\"" << cmXMLSafe(*it) << "\">\n"
  1472. << "<Time>" << cmSystemTools::GetTime() << "</Time>\n"
  1473. << "<DateTime>" << note_time << "</DateTime>\n"
  1474. << "<Text>" << std::endl;
  1475. std::ifstream ifs(it->c_str());
  1476. if ( ifs )
  1477. {
  1478. std::string line;
  1479. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1480. {
  1481. os << cmXMLSafe(line) << std::endl;
  1482. }
  1483. ifs.close();
  1484. }
  1485. else
  1486. {
  1487. os << "Problem reading file: " << it->c_str() << std::endl;
  1488. cmCTestLog(this, ERROR_MESSAGE, "Problem reading file: " << it->c_str()
  1489. << " while creating notes" << std::endl);
  1490. }
  1491. os << "</Text>\n"
  1492. << "</Note>" << std::endl;
  1493. }
  1494. os << "</Notes>\n"
  1495. << "</Site>" << std::endl;
  1496. return 1;
  1497. }
  1498. //----------------------------------------------------------------------
  1499. int cmCTest::GenerateNotesFile(const std::vector<cmStdString> &files)
  1500. {
  1501. cmGeneratedFileStream ofs;
  1502. if ( !this->OpenOutputFile(this->CurrentTag, "Notes.xml", ofs) )
  1503. {
  1504. cmCTestLog(this, ERROR_MESSAGE, "Cannot open notes file" << std::endl);
  1505. return 1;
  1506. }
  1507. this->GenerateCTestNotesOutput(ofs, files);
  1508. return 0;
  1509. }
  1510. //----------------------------------------------------------------------
  1511. int cmCTest::GenerateNotesFile(const char* cfiles)
  1512. {
  1513. if ( !cfiles )
  1514. {
  1515. return 1;
  1516. }
  1517. std::vector<cmStdString> files;
  1518. cmCTestLog(this, OUTPUT, "Create notes file" << std::endl);
  1519. files = cmSystemTools::SplitString(cfiles, ';');
  1520. if ( files.size() == 0 )
  1521. {
  1522. return 1;
  1523. }
  1524. return this->GenerateNotesFile(files);
  1525. }
  1526. //----------------------------------------------------------------------
  1527. std::string cmCTest::Base64GzipEncodeFile(std::string file)
  1528. {
  1529. std::string tarFile = file + "_temp.tar.gz";
  1530. std::vector<cmStdString> files;
  1531. files.push_back(file);
  1532. if(!cmSystemTools::CreateTar(tarFile.c_str(), files, true, false, false))
  1533. {
  1534. cmCTestLog(this, ERROR_MESSAGE, "Error creating tar while "
  1535. "encoding file: " << file << std::endl);
  1536. return "";
  1537. }
  1538. std::string base64 = this->Base64EncodeFile(tarFile);
  1539. cmSystemTools::RemoveFile(tarFile.c_str());
  1540. return base64;
  1541. }
  1542. //----------------------------------------------------------------------
  1543. std::string cmCTest::Base64EncodeFile(std::string file)
  1544. {
  1545. long len = cmSystemTools::FileLength(file.c_str());
  1546. std::ifstream ifs(file.c_str(), std::ios::in
  1547. #ifdef _WIN32
  1548. | std::ios::binary
  1549. #endif
  1550. );
  1551. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1552. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1553. ifs.close();
  1554. unsigned char *encoded_buffer
  1555. = new unsigned char [ static_cast<int>(
  1556. static_cast<double>(len) * 1.5 + 5.0) ];
  1557. unsigned long rlen
  1558. = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1559. std::string base64 = "";
  1560. for(unsigned long i = 0; i < rlen; i++)
  1561. {
  1562. base64 += encoded_buffer[i];
  1563. }
  1564. delete [] file_buffer;
  1565. delete [] encoded_buffer;
  1566. return base64;
  1567. }
  1568. //----------------------------------------------------------------------
  1569. bool cmCTest::SubmitExtraFiles(const std::vector<cmStdString> &files)
  1570. {
  1571. std::vector<cmStdString>::const_iterator it;
  1572. for ( it = files.begin();
  1573. it != files.end();
  1574. ++ it )
  1575. {
  1576. if ( !cmSystemTools::FileExists(it->c_str()) )
  1577. {
  1578. cmCTestLog(this, ERROR_MESSAGE, "Cannot find extra file: "
  1579. << it->c_str() << " to submit."
  1580. << std::endl;);
  1581. return false;
  1582. }
  1583. this->AddSubmitFile(PartExtraFiles, it->c_str());
  1584. }
  1585. return true;
  1586. }
  1587. //----------------------------------------------------------------------
  1588. bool cmCTest::SubmitExtraFiles(const char* cfiles)
  1589. {
  1590. if ( !cfiles )
  1591. {
  1592. return 1;
  1593. }
  1594. std::vector<cmStdString> files;
  1595. cmCTestLog(this, OUTPUT, "Submit extra files" << std::endl);
  1596. files = cmSystemTools::SplitString(cfiles, ';');
  1597. if ( files.size() == 0 )
  1598. {
  1599. return 1;
  1600. }
  1601. return this->SubmitExtraFiles(files);
  1602. }
  1603. //-------------------------------------------------------
  1604. // for a -D argument convert the next argument into
  1605. // the proper list of dashboard steps via SetTest
  1606. bool cmCTest::AddTestsForDashboardType(std::string &targ)
  1607. {
  1608. if ( targ == "Experimental" )
  1609. {
  1610. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1611. this->SetTest("Start");
  1612. this->SetTest("Configure");
  1613. this->SetTest("Build");
  1614. this->SetTest("Test");
  1615. this->SetTest("Coverage");
  1616. this->SetTest("Submit");
  1617. }
  1618. else if ( targ == "ExperimentalStart" )
  1619. {
  1620. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1621. this->SetTest("Start");
  1622. }
  1623. else if ( targ == "ExperimentalUpdate" )
  1624. {
  1625. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1626. this->SetTest("Update");
  1627. }
  1628. else if ( targ == "ExperimentalConfigure" )
  1629. {
  1630. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1631. this->SetTest("Configure");
  1632. }
  1633. else if ( targ == "ExperimentalBuild" )
  1634. {
  1635. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1636. this->SetTest("Build");
  1637. }
  1638. else if ( targ == "ExperimentalTest" )
  1639. {
  1640. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1641. this->SetTest("Test");
  1642. }
  1643. else if ( targ == "ExperimentalMemCheck"
  1644. || targ == "ExperimentalPurify" )
  1645. {
  1646. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1647. this->SetTest("MemCheck");
  1648. }
  1649. else if ( targ == "ExperimentalCoverage" )
  1650. {
  1651. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1652. this->SetTest("Coverage");
  1653. }
  1654. else if ( targ == "ExperimentalSubmit" )
  1655. {
  1656. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1657. this->SetTest("Submit");
  1658. }
  1659. else if ( targ == "Continuous" )
  1660. {
  1661. this->SetTestModel(cmCTest::CONTINUOUS);
  1662. this->SetTest("Start");
  1663. this->SetTest("Update");
  1664. this->SetTest("Configure");
  1665. this->SetTest("Build");
  1666. this->SetTest("Test");
  1667. this->SetTest("Coverage");
  1668. this->SetTest("Submit");
  1669. }
  1670. else if ( targ == "ContinuousStart" )
  1671. {
  1672. this->SetTestModel(cmCTest::CONTINUOUS);
  1673. this->SetTest("Start");
  1674. }
  1675. else if ( targ == "ContinuousUpdate" )
  1676. {
  1677. this->SetTestModel(cmCTest::CONTINUOUS);
  1678. this->SetTest("Update");
  1679. }
  1680. else if ( targ == "ContinuousConfigure" )
  1681. {
  1682. this->SetTestModel(cmCTest::CONTINUOUS);
  1683. this->SetTest("Configure");
  1684. }
  1685. else if ( targ == "ContinuousBuild" )
  1686. {
  1687. this->SetTestModel(cmCTest::CONTINUOUS);
  1688. this->SetTest("Build");
  1689. }
  1690. else if ( targ == "ContinuousTest" )
  1691. {
  1692. this->SetTestModel(cmCTest::CONTINUOUS);
  1693. this->SetTest("Test");
  1694. }
  1695. else if ( targ == "ContinuousMemCheck"
  1696. || targ == "ContinuousPurify" )
  1697. {
  1698. this->SetTestModel(cmCTest::CONTINUOUS);
  1699. this->SetTest("MemCheck");
  1700. }
  1701. else if ( targ == "ContinuousCoverage" )
  1702. {
  1703. this->SetTestModel(cmCTest::CONTINUOUS);
  1704. this->SetTest("Coverage");
  1705. }
  1706. else if ( targ == "ContinuousSubmit" )
  1707. {
  1708. this->SetTestModel(cmCTest::CONTINUOUS);
  1709. this->SetTest("Submit");
  1710. }
  1711. else if ( targ == "Nightly" )
  1712. {
  1713. this->SetTestModel(cmCTest::NIGHTLY);
  1714. this->SetTest("Start");
  1715. this->SetTest("Update");
  1716. this->SetTest("Configure");
  1717. this->SetTest("Build");
  1718. this->SetTest("Test");
  1719. this->SetTest("Coverage");
  1720. this->SetTest("Submit");
  1721. }
  1722. else if ( targ == "NightlyStart" )
  1723. {
  1724. this->SetTestModel(cmCTest::NIGHTLY);
  1725. this->SetTest("Start");
  1726. }
  1727. else if ( targ == "NightlyUpdate" )
  1728. {
  1729. this->SetTestModel(cmCTest::NIGHTLY);
  1730. this->SetTest("Update");
  1731. }
  1732. else if ( targ == "NightlyConfigure" )
  1733. {
  1734. this->SetTestModel(cmCTest::NIGHTLY);
  1735. this->SetTest("Configure");
  1736. }
  1737. else if ( targ == "NightlyBuild" )
  1738. {
  1739. this->SetTestModel(cmCTest::NIGHTLY);
  1740. this->SetTest("Build");
  1741. }
  1742. else if ( targ == "NightlyTest" )
  1743. {
  1744. this->SetTestModel(cmCTest::NIGHTLY);
  1745. this->SetTest("Test");
  1746. }
  1747. else if ( targ == "NightlyMemCheck"
  1748. || targ == "NightlyPurify" )
  1749. {
  1750. this->SetTestModel(cmCTest::NIGHTLY);
  1751. this->SetTest("MemCheck");
  1752. }
  1753. else if ( targ == "NightlyCoverage" )
  1754. {
  1755. this->SetTestModel(cmCTest::NIGHTLY);
  1756. this->SetTest("Coverage");
  1757. }
  1758. else if ( targ == "NightlySubmit" )
  1759. {
  1760. this->SetTestModel(cmCTest::NIGHTLY);
  1761. this->SetTest("Submit");
  1762. }
  1763. else if ( targ == "MemoryCheck" )
  1764. {
  1765. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1766. this->SetTest("Start");
  1767. this->SetTest("Configure");
  1768. this->SetTest("Build");
  1769. this->SetTest("MemCheck");
  1770. this->SetTest("Coverage");
  1771. this->SetTest("Submit");
  1772. }
  1773. else if ( targ == "NightlyMemoryCheck" )
  1774. {
  1775. this->SetTestModel(cmCTest::NIGHTLY);
  1776. this->SetTest("Start");
  1777. this->SetTest("Update");
  1778. this->SetTest("Configure");
  1779. this->SetTest("Build");
  1780. this->SetTest("MemCheck");
  1781. this->SetTest("Coverage");
  1782. this->SetTest("Submit");
  1783. }
  1784. else
  1785. {
  1786. return false;
  1787. }
  1788. return true;
  1789. }
  1790. //----------------------------------------------------------------------
  1791. void cmCTest::ErrorMessageUnknownDashDValue(std::string &val)
  1792. {
  1793. cmCTestLog(this, ERROR_MESSAGE,
  1794. "CTest -D called with incorrect option: " << val << std::endl);
  1795. cmCTestLog(this, ERROR_MESSAGE,
  1796. "Available options are:" << std::endl
  1797. << " ctest -D Continuous" << std::endl
  1798. << " ctest -D Continuous(Start|Update|Configure|Build)" << std::endl
  1799. << " ctest -D Continuous(Test|Coverage|MemCheck|Submit)" << std::endl
  1800. << " ctest -D Experimental" << std::endl
  1801. << " ctest -D Experimental(Start|Update|Configure|Build)" << std::endl
  1802. << " ctest -D Experimental(Test|Coverage|MemCheck|Submit)" << std::endl
  1803. << " ctest -D Nightly" << std::endl
  1804. << " ctest -D Nightly(Start|Update|Configure|Build)" << std::endl
  1805. << " ctest -D Nightly(Test|Coverage|MemCheck|Submit)" << std::endl
  1806. << " ctest -D NightlyMemoryCheck" << std::endl);
  1807. }
  1808. //----------------------------------------------------------------------
  1809. bool cmCTest::CheckArgument(const std::string& arg, const char* varg1,
  1810. const char* varg2)
  1811. {
  1812. return (varg1 && arg == varg1) || (varg2 && arg == varg2);
  1813. }
  1814. //----------------------------------------------------------------------
  1815. // Processes one command line argument (and its arguments if any)
  1816. // for many simple options and then returns
  1817. void cmCTest::HandleCommandLineArguments(size_t &i,
  1818. std::vector<std::string> &args)
  1819. {
  1820. std::string arg = args[i];
  1821. if(this->CheckArgument(arg, "-F"))
  1822. {
  1823. this->Failover = true;
  1824. }
  1825. if(this->CheckArgument(arg, "-j", "--parallel") && i < args.size() - 1)
  1826. {
  1827. i++;
  1828. int plevel = atoi(args[i].c_str());
  1829. this->SetParallelLevel(plevel);
  1830. }
  1831. else if(arg.find("-j") == 0)
  1832. {
  1833. int plevel = atoi(arg.substr(2).c_str());
  1834. this->SetParallelLevel(plevel);
  1835. }
  1836. if(this->CheckArgument(arg, "--no-compress-output"))
  1837. {
  1838. this->CompressTestOutput = false;
  1839. this->CompressMemCheckOutput = false;
  1840. }
  1841. if(this->CheckArgument(arg, "--print-labels"))
  1842. {
  1843. this->PrintLabels = true;
  1844. }
  1845. if(this->CheckArgument(arg, "--http1.0"))
  1846. {
  1847. this->UseHTTP10 = true;
  1848. }
  1849. if(this->CheckArgument(arg, "--timeout") && i < args.size() - 1)
  1850. {
  1851. i++;
  1852. double timeout = (double)atof(args[i].c_str());
  1853. this->GlobalTimeout = timeout;
  1854. }
  1855. if(this->CheckArgument(arg, "--stop-time") && i < args.size() - 1)
  1856. {
  1857. i++;
  1858. this->SetStopTime(args[i]);
  1859. }
  1860. if(this->CheckArgument(arg, "-C", "--build-config") &&
  1861. i < args.size() - 1)
  1862. {
  1863. i++;
  1864. this->SetConfigType(args[i].c_str());
  1865. }
  1866. if(this->CheckArgument(arg, "--debug"))
  1867. {
  1868. this->Debug = true;
  1869. this->ShowLineNumbers = true;
  1870. }
  1871. if(this->CheckArgument(arg, "--track") && i < args.size() - 1)
  1872. {
  1873. i++;
  1874. this->SpecificTrack = args[i];
  1875. }
  1876. if(this->CheckArgument(arg, "--show-line-numbers"))
  1877. {
  1878. this->ShowLineNumbers = true;
  1879. }
  1880. if(this->CheckArgument(arg, "--no-label-summary"))
  1881. {
  1882. this->LabelSummary = false;
  1883. }
  1884. if(this->CheckArgument(arg, "-Q", "--quiet"))
  1885. {
  1886. this->Quiet = true;
  1887. }
  1888. if(this->CheckArgument(arg, "-V", "--verbose"))
  1889. {
  1890. this->Verbose = true;
  1891. }
  1892. if(this->CheckArgument(arg, "-B"))
  1893. {
  1894. this->BatchJobs = true;
  1895. }
  1896. if(this->CheckArgument(arg, "-VV", "--extra-verbose"))
  1897. {
  1898. this->ExtraVerbose = true;
  1899. this->Verbose = true;
  1900. }
  1901. if(this->CheckArgument(arg, "--output-on-failure"))
  1902. {
  1903. this->OutputTestOutputOnTestFailure = true;
  1904. }
  1905. if(this->CheckArgument(arg, "-N", "--show-only"))
  1906. {
  1907. this->ShowOnly = true;
  1908. }
  1909. if(this->CheckArgument(arg, "-O", "--output-log") && i < args.size() - 1 )
  1910. {
  1911. i++;
  1912. this->SetOutputLogFileName(args[i].c_str());
  1913. }
  1914. if(this->CheckArgument(arg, "--tomorrow-tag"))
  1915. {
  1916. this->TomorrowTag = true;
  1917. }
  1918. if(this->CheckArgument(arg, "--force-new-ctest-process"))
  1919. {
  1920. this->ForceNewCTestProcess = true;
  1921. }
  1922. if(this->CheckArgument(arg, "-W", "--max-width") && i < args.size() - 1)
  1923. {
  1924. i++;
  1925. this->MaxTestNameWidth = atoi(args[i].c_str());
  1926. }
  1927. if(this->CheckArgument(arg, "--interactive-debug-mode") &&
  1928. i < args.size() - 1 )
  1929. {
  1930. i++;
  1931. this->InteractiveDebugMode = cmSystemTools::IsOn(args[i].c_str());
  1932. }
  1933. if(this->CheckArgument(arg, "--submit-index") && i < args.size() - 1 )
  1934. {
  1935. i++;
  1936. this->SubmitIndex = atoi(args[i].c_str());
  1937. if ( this->SubmitIndex < 0 )
  1938. {
  1939. this->SubmitIndex = 0;
  1940. }
  1941. }
  1942. if(this->CheckArgument(arg, "--overwrite") && i < args.size() - 1)
  1943. {
  1944. i++;
  1945. this->AddCTestConfigurationOverwrite(args[i].c_str());
  1946. }
  1947. if(this->CheckArgument(arg, "-A", "--add-notes") && i < args.size() - 1)
  1948. {
  1949. this->ProduceXML = true;
  1950. this->SetTest("Notes");
  1951. i++;
  1952. this->SetNotesFiles(args[i].c_str());
  1953. }
  1954. // options that control what tests are run
  1955. if(this->CheckArgument(arg, "-I", "--tests-information") &&
  1956. i < args.size() - 1)
  1957. {
  1958. i++;
  1959. this->GetHandler("test")->SetPersistentOption("TestsToRunInformation",
  1960. args[i].c_str());
  1961. this->GetHandler("memcheck")->
  1962. SetPersistentOption("TestsToRunInformation",args[i].c_str());
  1963. }
  1964. if(this->CheckArgument(arg, "-U", "--union"))
  1965. {
  1966. this->GetHandler("test")->SetPersistentOption("UseUnion", "true");
  1967. this->GetHandler("memcheck")->SetPersistentOption("UseUnion", "true");
  1968. }
  1969. if(this->CheckArgument(arg, "-R", "--tests-regex") && i < args.size() - 1)
  1970. {
  1971. i++;
  1972. this->GetHandler("test")->
  1973. SetPersistentOption("IncludeRegularExpression", args[i].c_str());
  1974. this->GetHandler("memcheck")->
  1975. SetPersistentOption("IncludeRegularExpression", args[i].c_str());
  1976. }
  1977. if(this->CheckArgument(arg, "-L", "--label-regex") && i < args.size() - 1)
  1978. {
  1979. i++;
  1980. this->GetHandler("test")->
  1981. SetPersistentOption("LabelRegularExpression", args[i].c_str());
  1982. this->GetHandler("memcheck")->
  1983. SetPersistentOption("LabelRegularExpression", args[i].c_str());
  1984. }
  1985. if(this->CheckArgument(arg, "-LE", "--label-exclude") && i < args.size() - 1)
  1986. {
  1987. i++;
  1988. this->GetHandler("test")->
  1989. SetPersistentOption("ExcludeLabelRegularExpression", args[i].c_str());
  1990. this->GetHandler("memcheck")->
  1991. SetPersistentOption("ExcludeLabelRegularExpression", args[i].c_str());
  1992. }
  1993. if(this->CheckArgument(arg, "-E", "--exclude-regex") &&
  1994. i < args.size() - 1)
  1995. {
  1996. i++;
  1997. this->GetHandler("test")->
  1998. SetPersistentOption("ExcludeRegularExpression", args[i].c_str());
  1999. this->GetHandler("memcheck")->
  2000. SetPersistentOption("ExcludeRegularExpression", args[i].c_str());
  2001. }
  2002. }
  2003. //----------------------------------------------------------------------
  2004. // handle the -S -SR and -SP arguments
  2005. void cmCTest::HandleScriptArguments(size_t &i,
  2006. std::vector<std::string> &args,
  2007. bool &SRArgumentSpecified)
  2008. {
  2009. std::string arg = args[i];
  2010. if(this->CheckArgument(arg, "-SP", "--script-new-process") &&
  2011. i < args.size() - 1 )
  2012. {
  2013. this->RunConfigurationScript = true;
  2014. i++;
  2015. cmCTestScriptHandler* ch
  2016. = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  2017. // -SR is an internal argument, -SP should be ignored when it is passed
  2018. if (!SRArgumentSpecified)
  2019. {
  2020. ch->AddConfigurationScript(args[i].c_str(),false);
  2021. }
  2022. }
  2023. if(this->CheckArgument(arg, "-SR", "--script-run") &&
  2024. i < args.size() - 1 )
  2025. {
  2026. SRArgumentSpecified = true;
  2027. this->RunConfigurationScript = true;
  2028. i++;
  2029. cmCTestScriptHandler* ch
  2030. = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  2031. ch->AddConfigurationScript(args[i].c_str(),true);
  2032. }
  2033. if(this->CheckArgument(arg, "-S", "--script") && i < args.size() - 1 )
  2034. {
  2035. this->RunConfigurationScript = true;
  2036. i++;
  2037. cmCTestScriptHandler* ch
  2038. = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  2039. // -SR is an internal argument, -S should be ignored when it is passed
  2040. if (!SRArgumentSpecified)
  2041. {
  2042. ch->AddConfigurationScript(args[i].c_str(),true);
  2043. }
  2044. }
  2045. }
  2046. //----------------------------------------------------------------------
  2047. bool cmCTest::AddVariableDefinition(const std::string &arg)
  2048. {
  2049. std::string name;
  2050. std::string value;
  2051. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  2052. if (cmCacheManager::ParseEntry(arg.c_str(), name, value, type))
  2053. {
  2054. this->Definitions[name] = value;
  2055. return true;
  2056. }
  2057. return false;
  2058. }
  2059. //----------------------------------------------------------------------
  2060. // the main entry point of ctest, called from main
  2061. int cmCTest::Run(std::vector<std::string> &args, std::string* output)
  2062. {
  2063. this->FindRunningCMake();
  2064. const char* ctestExec = "ctest";
  2065. bool cmakeAndTest = false;
  2066. bool executeTests = true;
  2067. bool SRArgumentSpecified = false;
  2068. // copy the command line
  2069. for(size_t i=0; i < args.size(); ++i)
  2070. {
  2071. this->InitialCommandLineArguments.push_back(args[i]);
  2072. }
  2073. // process the command line arguments
  2074. for(size_t i=1; i < args.size(); ++i)
  2075. {
  2076. // handle the simple commandline arguments
  2077. this->HandleCommandLineArguments(i,args);
  2078. // handle the script arguments -S -SR -SP
  2079. this->HandleScriptArguments(i,args,SRArgumentSpecified);
  2080. // handle a request for a dashboard
  2081. std::string arg = args[i];
  2082. if(this->CheckArgument(arg, "-D", "--dashboard") && i < args.size() - 1 )
  2083. {
  2084. this->ProduceXML = true;
  2085. i++;
  2086. std::string targ = args[i];
  2087. // AddTestsForDashboard parses the dashboard type and converts it
  2088. // into the separate stages
  2089. if (!this->AddTestsForDashboardType(targ))
  2090. {
  2091. if (!this->AddVariableDefinition(targ))
  2092. {
  2093. this->ErrorMessageUnknownDashDValue(targ);
  2094. executeTests = false;
  2095. }
  2096. }
  2097. }
  2098. // If it's not exactly -D, but it starts with -D, then try to parse out
  2099. // a variable definition from it, same as CMake does. Unsuccessful
  2100. // attempts are simply ignored since previous ctest versions ignore
  2101. // this too. (As well as many other unknown command line args.)
  2102. //
  2103. if(arg != "-D" && cmSystemTools::StringStartsWith(arg.c_str(), "-D"))
  2104. {
  2105. std::string input = arg.substr(2);
  2106. this->AddVariableDefinition(input);
  2107. }
  2108. if(this->CheckArgument(arg, "-T", "--test-action") &&
  2109. (i < args.size() -1) )
  2110. {
  2111. this->ProduceXML = true;
  2112. i++;
  2113. if ( !this->SetTest(args[i].c_str(), false) )
  2114. {
  2115. executeTests = false;
  2116. cmCTestLog(this, ERROR_MESSAGE,
  2117. "CTest -T called with incorrect option: "
  2118. << args[i].c_str() << std::endl);
  2119. cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
  2120. << " " << ctestExec << " -T all" << std::endl
  2121. << " " << ctestExec << " -T start" << std::endl
  2122. << " " << ctestExec << " -T update" << std::endl
  2123. << " " << ctestExec << " -T configure" << std::endl
  2124. << " " << ctestExec << " -T build" << std::endl
  2125. << " " << ctestExec << " -T test" << std::endl
  2126. << " " << ctestExec << " -T coverage" << std::endl
  2127. << " " << ctestExec << " -T memcheck" << std::endl
  2128. << " " << ctestExec << " -T notes" << std::endl
  2129. << " " << ctestExec << " -T submit" << std::endl);
  2130. }
  2131. }
  2132. // what type of test model
  2133. if(this->CheckArgument(arg, "-M", "--test-model") &&
  2134. (i < args.size() -1) )
  2135. {
  2136. i++;
  2137. std::string const& str = args[i];
  2138. if ( cmSystemTools::LowerCase(str) == "nightly" )
  2139. {
  2140. this->SetTestModel(cmCTest::NIGHTLY);
  2141. }
  2142. else if ( cmSystemTools::LowerCase(str) == "continuous" )
  2143. {
  2144. this->SetTestModel(cmCTest::CONTINUOUS);
  2145. }
  2146. else if ( cmSystemTools::LowerCase(str) == "experimental" )
  2147. {
  2148. this->SetTestModel(cmCTest::EXPERIMENTAL);
  2149. }
  2150. else
  2151. {
  2152. executeTests = false;
  2153. cmCTestLog(this, ERROR_MESSAGE,
  2154. "CTest -M called with incorrect option: " << str.c_str()
  2155. << std::endl);
  2156. cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
  2157. << " " << ctestExec << " -M Continuous" << std::endl
  2158. << " " << ctestExec << " -M Experimental" << std::endl
  2159. << " " << ctestExec << " -M Nightly" << std::endl);
  2160. }
  2161. }
  2162. if(this->CheckArgument(arg, "--extra-submit") && i < args.size() - 1)
  2163. {
  2164. this->ProduceXML = true;
  2165. this->SetTest("Submit");
  2166. i++;
  2167. if ( !this->SubmitExtraFiles(args[i].c_str()) )
  2168. {
  2169. return 0;
  2170. }
  2171. }
  2172. // --build-and-test options
  2173. if(this->CheckArgument(arg, "--build-and-test") && i < args.size() - 1)
  2174. {
  2175. cmakeAndTest = true;
  2176. }
  2177. if(this->CheckArgument(arg, "--schedule-random"))
  2178. {
  2179. this->ScheduleType = "Random";
  2180. }
  2181. // pass the argument to all the handlers as well, but i may no longer be
  2182. // set to what it was originally so I'm not sure this is working as
  2183. // intended
  2184. cmCTest::t_TestingHandlers::iterator it;
  2185. for ( it = this->TestingHandlers.begin();
  2186. it != this->TestingHandlers.end();
  2187. ++ it )
  2188. {
  2189. if ( !it->second->ProcessCommandLineArguments(arg, i, args) )
  2190. {
  2191. cmCTestLog(this, ERROR_MESSAGE,
  2192. "Problem parsing command line arguments within a handler");
  2193. return 0;
  2194. }
  2195. }
  2196. } // the close of the for argument loop
  2197. // now what sould cmake do? if --build-and-test was specified then
  2198. // we run the build and test handler and return
  2199. if(cmakeAndTest)
  2200. {
  2201. this->Verbose = true;
  2202. cmCTestBuildAndTestHandler* handler =
  2203. static_cast<cmCTestBuildAndTestHandler*>(this->GetHandler("buildtest"));
  2204. int retv = handler->ProcessHandler();
  2205. *output = handler->GetOutput();
  2206. #ifdef CMAKE_BUILD_WITH_CMAKE
  2207. cmDynamicLoader::FlushCache();
  2208. #endif
  2209. if(retv != 0)
  2210. {
  2211. cmCTestLog(this, DEBUG, "build and test failing returing: " << retv
  2212. << std::endl);
  2213. }
  2214. return retv;
  2215. }
  2216. if(executeTests)
  2217. {
  2218. int res;
  2219. // call process directory
  2220. if (this->RunConfigurationScript)
  2221. {
  2222. if ( this->ExtraVerbose )
  2223. {
  2224. cmCTestLog(this, OUTPUT, "* Extra verbosity turned on" << std::endl);
  2225. }
  2226. cmCTest::t_TestingHandlers::iterator it;
  2227. for ( it = this->TestingHandlers.begin();
  2228. it != this->TestingHandlers.end();
  2229. ++ it )
  2230. {
  2231. it->second->SetVerbose(this->ExtraVerbose);
  2232. it->second->SetSubmitIndex(this->SubmitIndex);
  2233. }
  2234. this->GetHandler("script")->SetVerbose(this->Verbose);
  2235. res = this->GetHandler("script")->ProcessHandler();
  2236. if(res != 0)
  2237. {
  2238. cmCTestLog(this, DEBUG, "running script failing returning: " << res
  2239. << std::endl);
  2240. }
  2241. }
  2242. else
  2243. {
  2244. // What is this? -V seems to be the same as -VV,
  2245. // and Verbose is always on in this case
  2246. this->ExtraVerbose = this->Verbose;
  2247. this->Verbose = true;
  2248. cmCTest::t_TestingHandlers::iterator it;
  2249. for ( it = this->TestingHandlers.begin();
  2250. it != this->TestingHandlers.end();
  2251. ++ it )
  2252. {
  2253. it->second->SetVerbose(this->Verbose);
  2254. it->second->SetSubmitIndex(this->SubmitIndex);
  2255. }
  2256. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2257. if(!this->Initialize(cwd.c_str(), 0))
  2258. {
  2259. res = 12;
  2260. cmCTestLog(this, ERROR_MESSAGE, "Problem initializing the dashboard."
  2261. << std::endl);
  2262. }
  2263. else
  2264. {
  2265. res = this->ProcessTests();
  2266. }
  2267. this->Finalize();
  2268. }
  2269. if(res != 0)
  2270. {
  2271. cmCTestLog(this, DEBUG, "Running a test(s) failed returning : " << res
  2272. << std::endl);
  2273. }
  2274. return res;
  2275. }
  2276. return 1;
  2277. }
  2278. //----------------------------------------------------------------------
  2279. void cmCTest::FindRunningCMake()
  2280. {
  2281. // Find our own executable.
  2282. this->CTestSelf = cmSystemTools::GetExecutableDirectory();
  2283. this->CTestSelf += "/ctest";
  2284. this->CTestSelf += cmSystemTools::GetExecutableExtension();
  2285. if(!cmSystemTools::FileExists(this->CTestSelf.c_str()))
  2286. {
  2287. cmSystemTools::Error("CTest executable cannot be found at ",
  2288. this->CTestSelf.c_str());
  2289. }
  2290. this->CMakeSelf = cmSystemTools::GetExecutableDirectory();
  2291. this->CMakeSelf += "/cmake";
  2292. this->CMakeSelf += cmSystemTools::GetExecutableExtension();
  2293. if(!cmSystemTools::FileExists(this->CMakeSelf.c_str()))
  2294. {
  2295. cmSystemTools::Error("CMake executable cannot be found at ",
  2296. this->CMakeSelf.c_str());
  2297. }
  2298. }
  2299. //----------------------------------------------------------------------
  2300. void cmCTest::SetNotesFiles(const char* notes)
  2301. {
  2302. if ( !notes )
  2303. {
  2304. return;
  2305. }
  2306. this->NotesFiles = notes;
  2307. }
  2308. //----------------------------------------------------------------------
  2309. void cmCTest::SetStopTime(std::string time)
  2310. {
  2311. this->StopTime = time;
  2312. this->DetermineNextDayStop();
  2313. }
  2314. //----------------------------------------------------------------------
  2315. int cmCTest::ReadCustomConfigurationFileTree(const char* dir, cmMakefile* mf)
  2316. {
  2317. bool found = false;
  2318. VectorOfStrings dirs;
  2319. VectorOfStrings ndirs;
  2320. cmCTestLog(this, DEBUG, "* Read custom CTest configuration directory: "
  2321. << dir << std::endl);
  2322. std::string fname = dir;
  2323. fname += "/CTestCustom.cmake";
  2324. cmCTestLog(this, DEBUG, "* Check for file: "
  2325. << fname.c_str() << std::endl);
  2326. if ( cmSystemTools::FileExists(fname.c_str()) )
  2327. {
  2328. cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: "
  2329. << fname.c_str() << std::endl);
  2330. bool erroroc = cmSystemTools::GetErrorOccuredFlag();
  2331. cmSystemTools::ResetErrorOccuredFlag();
  2332. if ( !mf->ReadListFile(0, fname.c_str()) ||
  2333. cmSystemTools::GetErrorOccuredFlag() )
  2334. {
  2335. cmCTestLog(this, ERROR_MESSAGE,
  2336. "Problem reading custom configuration: "
  2337. << fname.c_str() << std::endl);
  2338. }
  2339. found = true;
  2340. if ( erroroc )
  2341. {
  2342. cmSystemTools::SetErrorOccured();
  2343. }
  2344. }
  2345. std::string rexpr = dir;
  2346. rexpr += "/CTestCustom.ctest";
  2347. cmCTestLog(this, DEBUG, "* Check for file: "
  2348. << rexpr.c_str() << std::endl);
  2349. if ( !found && cmSystemTools::FileExists(rexpr.c_str()) )
  2350. {
  2351. cmsys::Glob gl;
  2352. gl.RecurseOn();
  2353. gl.FindFiles(rexpr);
  2354. std::vector<std::string>& files = gl.GetFiles();
  2355. std::vector<std::string>::iterator fileIt;
  2356. for ( fileIt = files.begin(); fileIt != files.end();
  2357. ++ fileIt )
  2358. {
  2359. cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: "
  2360. << fileIt->c_str() << std::endl);
  2361. if ( !mf->ReadListFile(0, fileIt->c_str()) ||
  2362. cmSystemTools::GetErrorOccuredFlag() )
  2363. {
  2364. cmCTestLog(this, ERROR_MESSAGE,
  2365. "Problem reading custom configuration: "
  2366. << fileIt->c_str() << std::endl);
  2367. }
  2368. }
  2369. found = true;
  2370. }
  2371. if ( found )
  2372. {
  2373. cmCTest::t_TestingHandlers::iterator it;
  2374. for ( it = this->TestingHandlers.begin();
  2375. it != this->TestingHandlers.end(); ++ it )
  2376. {
  2377. cmCTestLog(this, DEBUG,
  2378. "* Read custom CTest configuration vectors for handler: "
  2379. << it->first.c_str() << " (" << it->second << ")" << std::endl);
  2380. it->second->PopulateCustomVectors(mf);
  2381. }
  2382. }
  2383. return 1;
  2384. }
  2385. //----------------------------------------------------------------------
  2386. void cmCTest::PopulateCustomVector(cmMakefile* mf, const char* def,
  2387. VectorOfStrings& vec)
  2388. {
  2389. if ( !def)
  2390. {
  2391. return;
  2392. }
  2393. const char* dval = mf->GetDefinition(def);
  2394. if ( !dval )
  2395. {
  2396. return;
  2397. }
  2398. cmCTestLog(this, DEBUG, "PopulateCustomVector: " << def << std::endl);
  2399. std::vector<std::string> slist;
  2400. cmSystemTools::ExpandListArgument(dval, slist);
  2401. std::vector<std::string>::iterator it;
  2402. vec.clear();
  2403. for ( it = slist.begin(); it != slist.end(); ++it )
  2404. {
  2405. cmCTestLog(this, DEBUG, " -- " << it->c_str() << std::endl);
  2406. vec.push_back(it->c_str());
  2407. }
  2408. }
  2409. //----------------------------------------------------------------------
  2410. void cmCTest::PopulateCustomInteger(cmMakefile* mf, const char* def, int& val)
  2411. {
  2412. if ( !def)
  2413. {
  2414. return;
  2415. }
  2416. const char* dval = mf->GetDefinition(def);
  2417. if ( !dval )
  2418. {
  2419. return;
  2420. }
  2421. val = atoi(dval);
  2422. }
  2423. //----------------------------------------------------------------------
  2424. std::string cmCTest::GetShortPathToFile(const char* cfname)
  2425. {
  2426. const std::string& sourceDir
  2427. = cmSystemTools::CollapseFullPath(
  2428. this->GetCTestConfiguration("SourceDirectory").c_str());
  2429. const std::string& buildDir
  2430. = cmSystemTools::CollapseFullPath(
  2431. this->GetCTestConfiguration("BuildDirectory").c_str());
  2432. std::string fname = cmSystemTools::CollapseFullPath(cfname);
  2433. // Find relative paths to both directories
  2434. std::string srcRelpath
  2435. = cmSystemTools::RelativePath(sourceDir.c_str(), fname.c_str());
  2436. std::string bldRelpath
  2437. = cmSystemTools::RelativePath(buildDir.c_str(), fname.c_str());
  2438. // If any contains "." it is not parent directory
  2439. bool inSrc = srcRelpath.find("..") == srcRelpath.npos;
  2440. bool inBld = bldRelpath.find("..") == bldRelpath.npos;
  2441. // TODO: Handle files with .. in their name
  2442. std::string* res = 0;
  2443. if ( inSrc && inBld )
  2444. {
  2445. // If both have relative path with no dots, pick the shorter one
  2446. if ( srcRelpath.size() < bldRelpath.size() )
  2447. {
  2448. res = &srcRelpath;
  2449. }
  2450. else
  2451. {
  2452. res = &bldRelpath;
  2453. }
  2454. }
  2455. else if ( inSrc )
  2456. {
  2457. res = &srcRelpath;
  2458. }
  2459. else if ( inBld )
  2460. {
  2461. res = &bldRelpath;
  2462. }
  2463. std::string path;
  2464. if ( !res )
  2465. {
  2466. path = fname;
  2467. }
  2468. else
  2469. {
  2470. cmSystemTools::ConvertToUnixSlashes(*res);
  2471. path = "./" + *res;
  2472. if ( path[path.size()-1] == '/' )
  2473. {
  2474. path = path.substr(0, path.size()-1);
  2475. }
  2476. }
  2477. cmsys::SystemTools::ReplaceString(path, ":", "_");
  2478. cmsys::SystemTools::ReplaceString(path, " ", "_");
  2479. return path;
  2480. }
  2481. //----------------------------------------------------------------------
  2482. std::string cmCTest::GetCTestConfiguration(const char *name)
  2483. {
  2484. if ( this->CTestConfigurationOverwrites.find(name) !=
  2485. this->CTestConfigurationOverwrites.end() )
  2486. {
  2487. return this->CTestConfigurationOverwrites[name];
  2488. }
  2489. return this->CTestConfiguration[name];
  2490. }
  2491. //----------------------------------------------------------------------
  2492. void cmCTest::EmptyCTestConfiguration()
  2493. {
  2494. this->CTestConfiguration.clear();
  2495. }
  2496. //----------------------------------------------------------------------
  2497. void cmCTest::DetermineNextDayStop()
  2498. {
  2499. struct tm* lctime;
  2500. time_t current_time = time(0);
  2501. lctime = gmtime(&current_time);
  2502. int gm_hour = lctime->tm_hour;
  2503. time_t gm_time = mktime(lctime);
  2504. lctime = localtime(&current_time);
  2505. int local_hour = lctime->tm_hour;
  2506. int tzone_offset = local_hour - gm_hour;
  2507. if(gm_time > current_time && gm_hour < local_hour)
  2508. {
  2509. // this means gm_time is on the next day
  2510. tzone_offset -= 24;
  2511. }
  2512. else if(gm_time < current_time && gm_hour > local_hour)
  2513. {
  2514. // this means gm_time is on the previous day
  2515. tzone_offset += 24;
  2516. }
  2517. tzone_offset *= 100;
  2518. char buf[1024];
  2519. sprintf(buf, "%d%02d%02d %s %+05i",
  2520. lctime->tm_year + 1900,
  2521. lctime->tm_mon + 1,
  2522. lctime->tm_mday,
  2523. this->StopTime.c_str(),
  2524. tzone_offset);
  2525. time_t stop_time = curl_getdate(buf, &current_time);
  2526. if(stop_time < current_time)
  2527. {
  2528. this->NextDayStopTime = true;
  2529. }
  2530. }
  2531. //----------------------------------------------------------------------
  2532. void cmCTest::SetCTestConfiguration(const char *name, const char* value)
  2533. {
  2534. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "SetCTestConfiguration:"
  2535. << name << ":" << (value ? value : "(null)") << "\n");
  2536. if ( !name )
  2537. {
  2538. return;
  2539. }
  2540. if ( !value )
  2541. {
  2542. this->CTestConfiguration.erase(name);
  2543. return;
  2544. }
  2545. this->CTestConfiguration[name] = value;
  2546. }
  2547. //----------------------------------------------------------------------
  2548. std::string cmCTest::GetCurrentTag()
  2549. {
  2550. return this->CurrentTag;
  2551. }
  2552. //----------------------------------------------------------------------
  2553. std::string cmCTest::GetBinaryDir()
  2554. {
  2555. return this->BinaryDir;
  2556. }
  2557. //----------------------------------------------------------------------
  2558. std::string const& cmCTest::GetConfigType()
  2559. {
  2560. return this->ConfigType;
  2561. }
  2562. //----------------------------------------------------------------------
  2563. bool cmCTest::GetShowOnly()
  2564. {
  2565. return this->ShowOnly;
  2566. }
  2567. //----------------------------------------------------------------------
  2568. int cmCTest::GetMaxTestNameWidth() const
  2569. {
  2570. return this->MaxTestNameWidth;
  2571. }
  2572. //----------------------------------------------------------------------
  2573. void cmCTest::SetProduceXML(bool v)
  2574. {
  2575. this->ProduceXML = v;
  2576. }
  2577. //----------------------------------------------------------------------
  2578. bool cmCTest::GetProduceXML()
  2579. {
  2580. return this->ProduceXML;
  2581. }
  2582. //----------------------------------------------------------------------
  2583. const char* cmCTest::GetSpecificTrack()
  2584. {
  2585. if ( this->SpecificTrack.empty() )
  2586. {
  2587. return 0;
  2588. }
  2589. return this->SpecificTrack.c_str();
  2590. }
  2591. //----------------------------------------------------------------------
  2592. void cmCTest::SetSpecificTrack(const char* track)
  2593. {
  2594. if ( !track )
  2595. {
  2596. this->SpecificTrack = "";
  2597. return;
  2598. }
  2599. this->SpecificTrack = track;
  2600. }
  2601. //----------------------------------------------------------------------
  2602. void cmCTest::AddSubmitFile(Part part, const char* name)
  2603. {
  2604. this->Parts[part].SubmitFiles.push_back(name);
  2605. }
  2606. //----------------------------------------------------------------------
  2607. void cmCTest::AddCTestConfigurationOverwrite(const char* encstr)
  2608. {
  2609. std::string overStr = encstr;
  2610. size_t epos = overStr.find("=");
  2611. if ( epos == overStr.npos )
  2612. {
  2613. cmCTestLog(this, ERROR_MESSAGE,
  2614. "CTest configuration overwrite specified in the wrong format."
  2615. << std::endl
  2616. << "Valid format is: --overwrite key=value" << std::endl
  2617. << "The specified was: --overwrite " << overStr.c_str() << std::endl);
  2618. return;
  2619. }
  2620. std::string key = overStr.substr(0, epos);
  2621. std::string value = overStr.substr(epos+1, overStr.npos);
  2622. this->CTestConfigurationOverwrites[key] = value;
  2623. }
  2624. //----------------------------------------------------------------------
  2625. void cmCTest::SetConfigType(const char* ct)
  2626. {
  2627. this->ConfigType = ct?ct:"";
  2628. cmSystemTools::ReplaceString(this->ConfigType, ".\\", "");
  2629. std::string confTypeEnv
  2630. = "CMAKE_CONFIG_TYPE=" + this->ConfigType;
  2631. cmSystemTools::PutEnv(confTypeEnv.c_str());
  2632. }
  2633. //----------------------------------------------------------------------
  2634. bool cmCTest::SetCTestConfigurationFromCMakeVariable(cmMakefile* mf,
  2635. const char* dconfig, const char* cmake_var)
  2636. {
  2637. const char* ctvar;
  2638. ctvar = mf->GetDefinition(cmake_var);
  2639. if ( !ctvar )
  2640. {
  2641. return false;
  2642. }
  2643. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
  2644. "SetCTestConfigurationFromCMakeVariable:"
  2645. << dconfig << ":" << cmake_var << std::endl);
  2646. this->SetCTestConfiguration(dconfig, ctvar);
  2647. return true;
  2648. }
  2649. bool cmCTest::RunCommand(
  2650. const char* command,
  2651. std::string* stdOut,
  2652. std::string* stdErr,
  2653. int *retVal,
  2654. const char* dir,
  2655. double timeout)
  2656. {
  2657. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  2658. if(args.size() < 1)
  2659. {
  2660. return false;
  2661. }
  2662. std::vector<const char*> argv;
  2663. for(std::vector<cmStdString>::const_iterator a = args.begin();
  2664. a != args.end(); ++a)
  2665. {
  2666. argv.push_back(a->c_str());
  2667. }
  2668. argv.push_back(0);
  2669. *stdOut = "";
  2670. *stdErr = "";
  2671. cmsysProcess* cp = cmsysProcess_New();
  2672. cmsysProcess_SetCommand(cp, &*argv.begin());
  2673. cmsysProcess_SetWorkingDirectory(cp, dir);
  2674. if(cmSystemTools::GetRunCommandHideConsole())
  2675. {
  2676. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  2677. }
  2678. cmsysProcess_SetTimeout(cp, timeout);
  2679. cmsysProcess_Execute(cp);
  2680. std::vector<char> tempOutput;
  2681. std::vector<char> tempError;
  2682. char* data;
  2683. int length;
  2684. int res;
  2685. bool done = false;
  2686. while(!done)
  2687. {
  2688. res = cmsysProcess_WaitForData(cp, &data, &length, 0);
  2689. switch ( res )
  2690. {
  2691. case cmsysProcess_Pipe_STDOUT:
  2692. tempOutput.insert(tempOutput.end(), data, data+length);
  2693. break;
  2694. case cmsysProcess_Pipe_STDERR:
  2695. tempError.insert(tempError.end(), data, data+length);
  2696. break;
  2697. default:
  2698. done = true;
  2699. }
  2700. if ( (res == cmsysProcess_Pipe_STDOUT ||
  2701. res == cmsysProcess_Pipe_STDERR) && this->ExtraVerbose )
  2702. {
  2703. cmSystemTools::Stdout(data, length);
  2704. }
  2705. }
  2706. cmsysProcess_WaitForExit(cp, 0);
  2707. if ( tempOutput.size() > 0 )
  2708. {
  2709. stdOut->append(&*tempOutput.begin(), tempOutput.size());
  2710. }
  2711. if ( tempError.size() > 0 )
  2712. {
  2713. stdErr->append(&*tempError.begin(), tempError.size());
  2714. }
  2715. bool result = true;
  2716. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  2717. {
  2718. if ( retVal )
  2719. {
  2720. *retVal = cmsysProcess_GetExitValue(cp);
  2721. }
  2722. else
  2723. {
  2724. if ( cmsysProcess_GetExitValue(cp) != 0 )
  2725. {
  2726. result = false;
  2727. }
  2728. }
  2729. }
  2730. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
  2731. {
  2732. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  2733. cmCTestLog(this, ERROR_MESSAGE, exception_str << std::endl);
  2734. stdErr->append(exception_str, strlen(exception_str));
  2735. result = false;
  2736. }
  2737. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  2738. {
  2739. const char* error_str = cmsysProcess_GetErrorString(cp);
  2740. cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
  2741. stdErr->append(error_str, strlen(error_str));
  2742. result = false;
  2743. }
  2744. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  2745. {
  2746. const char* error_str = "Process terminated due to timeout\n";
  2747. cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
  2748. stdErr->append(error_str, strlen(error_str));
  2749. result = false;
  2750. }
  2751. cmsysProcess_Delete(cp);
  2752. return result;
  2753. }
  2754. //----------------------------------------------------------------------
  2755. void cmCTest::SetOutputLogFileName(const char* name)
  2756. {
  2757. if ( this->OutputLogFile)
  2758. {
  2759. delete this->OutputLogFile;
  2760. this->OutputLogFile= 0;
  2761. }
  2762. if ( name )
  2763. {
  2764. this->OutputLogFile = new cmGeneratedFileStream(name);
  2765. }
  2766. }
  2767. //----------------------------------------------------------------------
  2768. static const char* cmCTestStringLogType[] =
  2769. {
  2770. "DEBUG",
  2771. "OUTPUT",
  2772. "HANDLER_OUTPUT",
  2773. "HANDLER_VERBOSE_OUTPUT",
  2774. "WARNING",
  2775. "ERROR_MESSAGE",
  2776. 0
  2777. };
  2778. //----------------------------------------------------------------------
  2779. #ifdef cerr
  2780. # undef cerr
  2781. #endif
  2782. #ifdef cout
  2783. # undef cout
  2784. #endif
  2785. #define cmCTestLogOutputFileLine(stream) \
  2786. if ( this->ShowLineNumbers ) \
  2787. { \
  2788. (stream) << std::endl << file << ":" << line << " "; \
  2789. }
  2790. void cmCTest::InitStreams()
  2791. {
  2792. // By default we write output to the process output streams.
  2793. this->StreamOut = &std::cout;
  2794. this->StreamErr = &std::cerr;
  2795. }
  2796. void cmCTest::Log(int logType, const char* file, int line, const char* msg)
  2797. {
  2798. if ( !msg || !*msg )
  2799. {
  2800. return;
  2801. }
  2802. if ( this->OutputLogFile )
  2803. {
  2804. bool display = true;
  2805. if ( logType == cmCTest::DEBUG && !this->Debug ) { display = false; }
  2806. if ( logType == cmCTest::HANDLER_VERBOSE_OUTPUT && !this->Debug &&
  2807. !this->ExtraVerbose ) { display = false; }
  2808. if ( display )
  2809. {
  2810. cmCTestLogOutputFileLine(*this->OutputLogFile);
  2811. if ( logType != this->OutputLogFileLastTag )
  2812. {
  2813. *this->OutputLogFile << "[";
  2814. if ( logType >= OTHER || logType < 0 )
  2815. {
  2816. *this->OutputLogFile << "OTHER";
  2817. }
  2818. else
  2819. {
  2820. *this->OutputLogFile << cmCTestStringLogType[logType];
  2821. }
  2822. *this->OutputLogFile << "] " << std::endl << std::flush;
  2823. }
  2824. *this->OutputLogFile << msg << std::flush;
  2825. if ( logType != this->OutputLogFileLastTag )
  2826. {
  2827. *this->OutputLogFile << std::endl << std::flush;
  2828. this->OutputLogFileLastTag = logType;
  2829. }
  2830. }
  2831. }
  2832. if ( !this->Quiet )
  2833. {
  2834. std::ostream& out = *this->StreamOut;
  2835. std::ostream& err = *this->StreamErr;
  2836. switch ( logType )
  2837. {
  2838. case DEBUG:
  2839. if ( this->Debug )
  2840. {
  2841. cmCTestLogOutputFileLine(out);
  2842. out << msg;
  2843. out.flush();
  2844. }
  2845. break;
  2846. case OUTPUT: case HANDLER_OUTPUT:
  2847. if ( this->Debug || this->Verbose )
  2848. {
  2849. cmCTestLogOutputFileLine(out);
  2850. out << msg;
  2851. out.flush();
  2852. }
  2853. break;
  2854. case HANDLER_VERBOSE_OUTPUT:
  2855. if ( this->Debug || this->ExtraVerbose )
  2856. {
  2857. cmCTestLogOutputFileLine(out);
  2858. out << msg;
  2859. out.flush();
  2860. }
  2861. break;
  2862. case WARNING:
  2863. cmCTestLogOutputFileLine(err);
  2864. err << msg;
  2865. err.flush();
  2866. break;
  2867. case ERROR_MESSAGE:
  2868. cmCTestLogOutputFileLine(err);
  2869. err << msg;
  2870. err.flush();
  2871. cmSystemTools::SetErrorOccured();
  2872. break;
  2873. default:
  2874. cmCTestLogOutputFileLine(out);
  2875. out << msg;
  2876. out.flush();
  2877. }
  2878. }
  2879. }
  2880. //-------------------------------------------------------------------------
  2881. double cmCTest::GetRemainingTimeAllowed()
  2882. {
  2883. if (!this->GetHandler("script"))
  2884. {
  2885. return 1.0e7;
  2886. }
  2887. cmCTestScriptHandler* ch
  2888. = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  2889. return ch->GetRemainingTimeAllowed();
  2890. }
  2891. //----------------------------------------------------------------------
  2892. void cmCTest::OutputTestErrors(std::vector<char> const &process_output)
  2893. {
  2894. std::string test_outputs("\n*** Test Failed:\n");
  2895. if(process_output.size())
  2896. {
  2897. test_outputs.append(&*process_output.begin(), process_output.size());
  2898. }
  2899. cmCTestLog(this, HANDLER_OUTPUT, test_outputs << std::endl << std::flush);
  2900. }
  2901. //----------------------------------------------------------------------
  2902. bool cmCTest::CompressString(std::string& str)
  2903. {
  2904. int ret;
  2905. z_stream strm;
  2906. unsigned char* in = reinterpret_cast<unsigned char*>(
  2907. const_cast<char*>(str.c_str()));
  2908. //zlib makes the guarantee that this is the maximum output size
  2909. int outSize = static_cast<int>(
  2910. static_cast<double>(str.size()) * 1.001 + 13.0);
  2911. unsigned char* out = new unsigned char[outSize];
  2912. strm.zalloc = Z_NULL;
  2913. strm.zfree = Z_NULL;
  2914. strm.opaque = Z_NULL;
  2915. ret = deflateInit(&strm, -1); //default compression level
  2916. if (ret != Z_OK)
  2917. {
  2918. delete[] out;
  2919. return false;
  2920. }
  2921. strm.avail_in = static_cast<uInt>(str.size());
  2922. strm.next_in = in;
  2923. strm.avail_out = outSize;
  2924. strm.next_out = out;
  2925. ret = deflate(&strm, Z_FINISH);
  2926. if(ret == Z_STREAM_ERROR || ret != Z_STREAM_END)
  2927. {
  2928. cmCTestLog(this, ERROR_MESSAGE, "Error during gzip compression."
  2929. << std::endl);
  2930. delete[] out;
  2931. return false;
  2932. }
  2933. (void)deflateEnd(&strm);
  2934. // Now base64 encode the resulting binary string
  2935. unsigned char* base64EncodedBuffer
  2936. = new unsigned char[static_cast<int>(outSize * 1.5)];
  2937. unsigned long rlen
  2938. = cmsysBase64_Encode(out, strm.total_out, base64EncodedBuffer, 1);
  2939. str = "";
  2940. str.append(reinterpret_cast<char*>(base64EncodedBuffer), rlen);
  2941. delete [] base64EncodedBuffer;
  2942. delete [] out;
  2943. return true;
  2944. }