cmCTestScriptHandler.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestScriptHandler.h"
  14. #include "cmCTest.h"
  15. #include "cmake.h"
  16. #include "cmFunctionBlocker.h"
  17. #include "cmMakefile.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmGeneratedFileStream.h"
  21. //#include <cmsys/RegularExpression.hxx>
  22. #include <cmsys/Process.h>
  23. // used for sleep
  24. #ifdef _WIN32
  25. #include "windows.h"
  26. #endif
  27. #include <stdlib.h>
  28. #include <time.h>
  29. #include <math.h>
  30. #include <float.h>
  31. // needed for sleep
  32. #if !defined(_WIN32)
  33. # include <unistd.h>
  34. #endif
  35. #include "cmCTestBuildCommand.h"
  36. #include "cmCTestConfigureCommand.h"
  37. #include "cmCTestCoverageCommand.h"
  38. #include "cmCTestEmptyBinaryDirectoryCommand.h"
  39. #include "cmCTestMemCheckCommand.h"
  40. #include "cmCTestReadCustomFilesCommand.h"
  41. #include "cmCTestRunScriptCommand.h"
  42. #include "cmCTestSleepCommand.h"
  43. #include "cmCTestStartCommand.h"
  44. #include "cmCTestSubmitCommand.h"
  45. #include "cmCTestTestCommand.h"
  46. #include "cmCTestUpdateCommand.h"
  47. #define CTEST_INITIAL_CMAKE_OUTPUT_FILE_NAME "CTestInitialCMakeOutput.log"
  48. // used to keep elapsed time up to date
  49. class cmCTestScriptFunctionBlocker : public cmFunctionBlocker
  50. {
  51. public:
  52. cmCTestScriptFunctionBlocker() {}
  53. virtual ~cmCTestScriptFunctionBlocker() {}
  54. virtual bool IsFunctionBlocked(const cmListFileFunction& lff,
  55. cmMakefile &mf);
  56. //virtual bool ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf);
  57. //virtual void ScopeEnded(cmMakefile &mf);
  58. cmCTestScriptHandler* CTestScriptHandler;
  59. };
  60. // simply update the time and don't block anything
  61. bool cmCTestScriptFunctionBlocker::
  62. IsFunctionBlocked(const cmListFileFunction& , cmMakefile &)
  63. {
  64. this->CTestScriptHandler->UpdateElapsedTime();
  65. return false;
  66. }
  67. //----------------------------------------------------------------------
  68. cmCTestScriptHandler::cmCTestScriptHandler()
  69. {
  70. this->Backup = false;
  71. this->EmptyBinDir = false;
  72. this->EmptyBinDirOnce = false;
  73. this->Makefile = 0;
  74. this->LocalGenerator = 0;
  75. this->CMake = 0;
  76. this->GlobalGenerator = 0;
  77. this->ScriptStartTime = 0;
  78. // the *60 is becuase the settings are in minutes but GetTime is seconds
  79. this->MinimumInterval = 30*60;
  80. this->ContinuousDuration = -1;
  81. }
  82. //----------------------------------------------------------------------
  83. void cmCTestScriptHandler::Initialize()
  84. {
  85. this->Superclass::Initialize();
  86. this->Backup = false;
  87. this->EmptyBinDir = false;
  88. this->EmptyBinDirOnce = false;
  89. this->SourceDir = "";
  90. this->BinaryDir = "";
  91. this->BackupSourceDir = "";
  92. this->BackupBinaryDir = "";
  93. this->CTestRoot = "";
  94. this->CVSCheckOut = "";
  95. this->CTestCmd = "";
  96. this->UpdateCmd = "";
  97. this->CTestEnv = "";
  98. this->InitCache = "";
  99. this->CMakeCmd = "";
  100. this->CMOutFile = "";
  101. this->ExtraUpdates.clear();
  102. this->MinimumInterval = 20*60;
  103. this->ContinuousDuration = -1;
  104. // what time in seconds did this script start running
  105. this->ScriptStartTime = 0;
  106. this->Makefile = 0;
  107. if (this->LocalGenerator)
  108. {
  109. delete this->LocalGenerator;
  110. }
  111. this->LocalGenerator = 0;
  112. if (this->GlobalGenerator)
  113. {
  114. delete this->GlobalGenerator;
  115. }
  116. this->GlobalGenerator = 0;
  117. if (this->CMake)
  118. {
  119. delete this->CMake;
  120. }
  121. }
  122. //----------------------------------------------------------------------
  123. cmCTestScriptHandler::~cmCTestScriptHandler()
  124. {
  125. // local generator owns the makefile
  126. this->Makefile = 0;
  127. if (this->LocalGenerator)
  128. {
  129. delete this->LocalGenerator;
  130. }
  131. this->LocalGenerator = 0;
  132. if (this->GlobalGenerator)
  133. {
  134. delete this->GlobalGenerator;
  135. }
  136. this->GlobalGenerator = 0;
  137. if (this->CMake)
  138. {
  139. delete this->CMake;
  140. }
  141. }
  142. //----------------------------------------------------------------------
  143. // just adds an argument to the vector
  144. void cmCTestScriptHandler::AddConfigurationScript(const char *script,
  145. bool pscope)
  146. {
  147. this->ConfigurationScripts.push_back(script);
  148. this->ScriptProcessScope.push_back(pscope);
  149. }
  150. //----------------------------------------------------------------------
  151. // the generic entry point for handling scripts, this routine will run all
  152. // the scripts provides a -S arguments
  153. int cmCTestScriptHandler::ProcessHandler()
  154. {
  155. int res = 0;
  156. for (size_t i=0; i < this->ConfigurationScripts.size(); ++i)
  157. {
  158. // for each script run it
  159. res += this->RunConfigurationScript
  160. (cmSystemTools::CollapseFullPath(this->ConfigurationScripts[i].c_str()),
  161. this->ScriptProcessScope[i]);
  162. }
  163. if ( res )
  164. {
  165. return -1;
  166. }
  167. return 0;
  168. }
  169. void cmCTestScriptHandler::UpdateElapsedTime()
  170. {
  171. if (this->LocalGenerator)
  172. {
  173. // set the current elapsed time
  174. char timeString[20];
  175. int itime = static_cast<unsigned int>(cmSystemTools::GetTime()
  176. - this->ScriptStartTime);
  177. sprintf(timeString,"%i",itime);
  178. this->LocalGenerator->GetMakefile()->AddDefinition("CTEST_ELAPSED_TIME",
  179. timeString);
  180. }
  181. }
  182. //----------------------------------------------------------------------
  183. void cmCTestScriptHandler::AddCTestCommand(cmCTestCommand* command)
  184. {
  185. cmCTestCommand* newCom = command;
  186. newCom->CTest = this->CTest;
  187. newCom->CTestScriptHandler = this;
  188. this->CMake->AddCommand(newCom);
  189. }
  190. int cmCTestScriptHandler::ExecuteScript(const std::string& total_script_arg)
  191. {
  192. // execute the script passing in the arguments to the script as well as the
  193. // arguments from this invocation of cmake
  194. std::vector<const char*> argv;
  195. argv.push_back(this->CTest->GetCTestExecutable());
  196. argv.push_back("-SR");
  197. argv.push_back(total_script_arg.c_str());
  198. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  199. "Executable for CTest is: " <<
  200. this->CTest->GetCTestExecutable() << "\n");
  201. // now pass through all the other arguments
  202. std::vector<cmStdString> &initArgs =
  203. this->CTest->GetInitialCommandLineArguments();
  204. for(size_t i=1; i < initArgs.size(); ++i)
  205. {
  206. argv.push_back(initArgs[i].c_str());
  207. }
  208. argv.push_back(0);
  209. // Now create process object
  210. cmsysProcess* cp = cmsysProcess_New();
  211. cmsysProcess_SetCommand(cp, &*argv.begin());
  212. //cmsysProcess_SetWorkingDirectory(cp, dir);
  213. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  214. //cmsysProcess_SetTimeout(cp, timeout);
  215. cmsysProcess_Execute(cp);
  216. std::vector<char> out;
  217. std::vector<char> err;
  218. std::string line;
  219. int pipe = cmSystemTools::WaitForLine(cp, line, 100.0, out, err);
  220. while(pipe != cmsysProcess_Pipe_None)
  221. {
  222. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Output: "
  223. << line << "\n");
  224. if(pipe == cmsysProcess_Pipe_STDERR)
  225. {
  226. cmCTestLog(this->CTest, ERROR_MESSAGE, line << "\n");
  227. }
  228. else if(pipe == cmsysProcess_Pipe_STDOUT)
  229. {
  230. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, line << "\n");
  231. }
  232. pipe = cmSystemTools::WaitForLine(cp, line, 100, out, err);
  233. }
  234. // Properly handle output of the build command
  235. cmsysProcess_WaitForExit(cp, 0);
  236. int result = cmsysProcess_GetState(cp);
  237. int retVal = 0;
  238. if(result == cmsysProcess_State_Exited)
  239. {
  240. retVal = cmsysProcess_GetExitValue(cp);
  241. }
  242. else
  243. {
  244. abort();
  245. }
  246. return retVal;
  247. }
  248. void cmCTestScriptHandler::CreateCMake()
  249. {
  250. // create a cmake instance to read the configuration script
  251. if (this->CMake)
  252. {
  253. delete this->CMake;
  254. delete this->GlobalGenerator;
  255. delete this->LocalGenerator;
  256. }
  257. this->CMake = new cmake;
  258. this->CMake->AddCMakePaths(this->CTest->GetCTestExecutable());
  259. this->GlobalGenerator = new cmGlobalGenerator;
  260. this->GlobalGenerator->SetCMakeInstance(this->CMake);
  261. this->LocalGenerator = this->GlobalGenerator->CreateLocalGenerator();
  262. this->LocalGenerator->SetGlobalGenerator(this->GlobalGenerator);
  263. this->Makefile = this->LocalGenerator->GetMakefile();
  264. // add any ctest specific commands, probably should have common superclass
  265. // for ctest commands to clean this up. If a couple more commands are
  266. // created with the same format lets do that - ken
  267. this->AddCTestCommand(new cmCTestBuildCommand);
  268. this->AddCTestCommand(new cmCTestConfigureCommand);
  269. this->AddCTestCommand(new cmCTestCoverageCommand);
  270. this->AddCTestCommand(new cmCTestEmptyBinaryDirectoryCommand);
  271. this->AddCTestCommand(new cmCTestMemCheckCommand);
  272. this->AddCTestCommand(new cmCTestReadCustomFilesCommand);
  273. this->AddCTestCommand(new cmCTestRunScriptCommand);
  274. this->AddCTestCommand(new cmCTestSleepCommand);
  275. this->AddCTestCommand(new cmCTestStartCommand);
  276. this->AddCTestCommand(new cmCTestSubmitCommand);
  277. this->AddCTestCommand(new cmCTestTestCommand);
  278. this->AddCTestCommand(new cmCTestUpdateCommand);
  279. }
  280. void cmCTestScriptHandler::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  281. {
  282. this->CMake->GetCommandDocumentation(v);
  283. }
  284. //----------------------------------------------------------------------
  285. // this sets up some variables for the script to use, creates the required
  286. // cmake instance and generators, and then reads in the script
  287. int cmCTestScriptHandler::ReadInScript(const std::string& total_script_arg)
  288. {
  289. // if the argument has a , in it then it needs to be broken into the fist
  290. // argument (which is the script) and the second argument which will be
  291. // passed into the scripts as S_ARG
  292. std::string script = total_script_arg;
  293. std::string script_arg;
  294. if (total_script_arg.find(",") != std::string::npos)
  295. {
  296. script = total_script_arg.substr(0,total_script_arg.find(","));
  297. script_arg = total_script_arg.substr(total_script_arg.find(",")+1);
  298. }
  299. // make sure the file exists
  300. if (!cmSystemTools::FileExists(script.c_str()))
  301. {
  302. cmSystemTools::Error("Cannot find file: ", script.c_str());
  303. return 1;
  304. }
  305. // read in the list file to fill the cache
  306. // create a cmake instance to read the configuration script
  307. this->CreateCMake();
  308. // set a variable with the path to the current script
  309. this->Makefile->AddDefinition("CTEST_SCRIPT_DIRECTORY",
  310. cmSystemTools::GetFilenamePath(script).c_str());
  311. this->Makefile->AddDefinition("CTEST_SCRIPT_NAME",
  312. cmSystemTools::GetFilenameName(script).c_str());
  313. this->Makefile->AddDefinition("CTEST_EXECUTABLE_NAME",
  314. this->CTest->GetCTestExecutable());
  315. this->Makefile->AddDefinition("CMAKE_EXECUTABLE_NAME",
  316. this->CTest->GetCMakeExecutable());
  317. this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", true);
  318. this->UpdateElapsedTime();
  319. // add the script arg if defined
  320. if (script_arg.size())
  321. {
  322. this->Makefile->AddDefinition("CTEST_SCRIPT_ARG", script_arg.c_str());
  323. }
  324. // always add a function blocker to update the elapsed time
  325. cmCTestScriptFunctionBlocker *f = new cmCTestScriptFunctionBlocker();
  326. f->CTestScriptHandler = this;
  327. this->Makefile->AddFunctionBlocker(f);
  328. // finally read in the script
  329. if (!this->Makefile->ReadListFile(0, script.c_str()) ||
  330. cmSystemTools::GetErrorOccuredFlag())
  331. {
  332. return 2;
  333. }
  334. return 0;
  335. }
  336. //----------------------------------------------------------------------
  337. // extract variabels from the script to set ivars
  338. int cmCTestScriptHandler::ExtractVariables()
  339. {
  340. // Temporary variables
  341. const char* minInterval;
  342. const char* contDuration;
  343. this->SourceDir
  344. = this->Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
  345. this->BinaryDir
  346. = this->Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");
  347. // add in translations for src and bin
  348. cmSystemTools::AddKeepPath(this->SourceDir.c_str());
  349. cmSystemTools::AddKeepPath(this->BinaryDir.c_str());
  350. this->CTestCmd
  351. = this->Makefile->GetSafeDefinition("CTEST_COMMAND");
  352. this->CVSCheckOut
  353. = this->Makefile->GetSafeDefinition("CTEST_CVS_CHECKOUT");
  354. this->CTestRoot
  355. = this->Makefile->GetSafeDefinition("CTEST_DASHBOARD_ROOT");
  356. this->UpdateCmd
  357. = this->Makefile->GetSafeDefinition("CTEST_UPDATE_COMMAND");
  358. if ( this->UpdateCmd.empty() )
  359. {
  360. this->UpdateCmd
  361. = this->Makefile->GetSafeDefinition("CTEST_CVS_COMMAND");
  362. }
  363. this->CTestEnv
  364. = this->Makefile->GetSafeDefinition("CTEST_ENVIRONMENT");
  365. this->InitCache
  366. = this->Makefile->GetSafeDefinition("CTEST_INITIAL_CACHE");
  367. this->CMakeCmd
  368. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_COMMAND");
  369. this->CMOutFile
  370. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_OUTPUT_FILE_NAME");
  371. this->Backup
  372. = this->Makefile->IsOn("CTEST_BACKUP_AND_RESTORE");
  373. this->EmptyBinDir
  374. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY");
  375. this->EmptyBinDirOnce
  376. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY_ONCE");
  377. minInterval
  378. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_MINIMUM_INTERVAL");
  379. contDuration
  380. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_DURATION");
  381. char updateVar[40];
  382. int i;
  383. for (i = 1; i < 10; ++i)
  384. {
  385. sprintf(updateVar,"CTEST_EXTRA_UPDATES_%i",i);
  386. const char *updateVal = this->Makefile->GetDefinition(updateVar);
  387. if ( updateVal )
  388. {
  389. if ( this->UpdateCmd.empty() )
  390. {
  391. cmSystemTools::Error(updateVar,
  392. " specified without specifying CTEST_CVS_COMMAND.");
  393. return 12;
  394. }
  395. this->ExtraUpdates.push_back(updateVal);
  396. }
  397. }
  398. // in order to backup and restore we also must have the cvs root
  399. if (this->Backup && this->CVSCheckOut.empty())
  400. {
  401. cmSystemTools::Error(
  402. "Backup was requested without specifying CTEST_CVS_CHECKOUT.");
  403. return 3;
  404. }
  405. // make sure the required info is here
  406. if (this->SourceDir.empty() ||
  407. this->BinaryDir.empty() ||
  408. this->CTestCmd.empty())
  409. {
  410. std::string msg = "CTEST_SOURCE_DIRECTORY = ";
  411. msg += (!this->SourceDir.empty()) ? this->SourceDir.c_str() : "(Null)";
  412. msg += "\nCTEST_BINARY_DIRECTORY = ";
  413. msg += (!this->BinaryDir.empty()) ? this->BinaryDir.c_str() : "(Null)";
  414. msg += "\nCTEST_COMMAND = ";
  415. msg += (!this->CTestCmd.empty()) ? this->CTestCmd.c_str() : "(Null)";
  416. cmSystemTools::Error(
  417. "Some required settings in the configuration file were missing:\n",
  418. msg.c_str());
  419. return 4;
  420. }
  421. // if the dashboard root isn't specified then we can compute it from the
  422. // this->SourceDir
  423. if (this->CTestRoot.empty() )
  424. {
  425. this->CTestRoot = cmSystemTools::GetFilenamePath(this->SourceDir).c_str();
  426. }
  427. // the script may override the minimum continuous interval
  428. if (minInterval)
  429. {
  430. this->MinimumInterval = 60 * atof(minInterval);
  431. }
  432. if (contDuration)
  433. {
  434. this->ContinuousDuration = 60.0 * atof(contDuration);
  435. }
  436. this->UpdateElapsedTime();
  437. return 0;
  438. }
  439. //----------------------------------------------------------------------
  440. void cmCTestScriptHandler::SleepInSeconds(unsigned int secondsToWait)
  441. {
  442. #if defined(_WIN32)
  443. Sleep(1000*secondsToWait);
  444. #else
  445. sleep(secondsToWait);
  446. #endif
  447. }
  448. //----------------------------------------------------------------------
  449. // run a specific script
  450. int cmCTestScriptHandler::RunConfigurationScript
  451. (const std::string& total_script_arg, bool pscope)
  452. {
  453. int result;
  454. this->ScriptStartTime =
  455. cmSystemTools::GetTime();
  456. // read in the script
  457. if (pscope)
  458. {
  459. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  460. "Reading Script: " << total_script_arg << std::endl);
  461. result = this->ReadInScript(total_script_arg);
  462. }
  463. else
  464. {
  465. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  466. "Executing Script: " << total_script_arg << std::endl);
  467. result = this->ExecuteScript(total_script_arg);
  468. }
  469. if (result)
  470. {
  471. return result;
  472. }
  473. // only run the curent script if we should
  474. if (this->Makefile && this->Makefile->IsOn("CTEST_RUN_CURRENT_SCRIPT"))
  475. {
  476. return this->RunCurrentScript();
  477. }
  478. return result;
  479. }
  480. //----------------------------------------------------------------------
  481. int cmCTestScriptHandler::RunCurrentScript()
  482. {
  483. int result;
  484. // do not run twice
  485. this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", false);
  486. // no popup widows
  487. cmSystemTools::SetRunCommandHideConsole(true);
  488. // extract the vars from the cache and store in ivars
  489. result = this->ExtractVariables();
  490. if (result)
  491. {
  492. return result;
  493. }
  494. // set any environment variables
  495. if (!this->CTestEnv.empty())
  496. {
  497. std::vector<std::string> envArgs;
  498. cmSystemTools::ExpandListArgument(this->CTestEnv.c_str(),envArgs);
  499. // for each variable/argument do a putenv
  500. for (unsigned i = 0; i < envArgs.size(); ++i)
  501. {
  502. cmSystemTools::PutEnv(envArgs[i].c_str());
  503. }
  504. }
  505. // now that we have done most of the error checking finally run the
  506. // dashboard, we may be asked to repeatedly run this dashboard, such as
  507. // for a continuous, do we ned to run it more than once?
  508. if ( this->ContinuousDuration >= 0 )
  509. {
  510. this->UpdateElapsedTime();
  511. double ending_time = cmSystemTools::GetTime() + this->ContinuousDuration;
  512. if (this->EmptyBinDirOnce)
  513. {
  514. this->EmptyBinDir = true;
  515. }
  516. do
  517. {
  518. double interval = cmSystemTools::GetTime();
  519. result = this->RunConfigurationDashboard();
  520. interval = cmSystemTools::GetTime() - interval;
  521. if (interval < this->MinimumInterval)
  522. {
  523. this->SleepInSeconds(
  524. static_cast<unsigned int>(this->MinimumInterval - interval));
  525. }
  526. if (this->EmptyBinDirOnce)
  527. {
  528. this->EmptyBinDir = false;
  529. }
  530. }
  531. while (cmSystemTools::GetTime() < ending_time);
  532. }
  533. // otherwise just run it once
  534. else
  535. {
  536. result = this->RunConfigurationDashboard();
  537. }
  538. return result;
  539. }
  540. //----------------------------------------------------------------------
  541. int cmCTestScriptHandler::CheckOutSourceDir()
  542. {
  543. std::string command;
  544. std::string output;
  545. int retVal;
  546. bool res;
  547. if (!cmSystemTools::FileExists(this->SourceDir.c_str()) &&
  548. !this->CVSCheckOut.empty())
  549. {
  550. // we must now checkout the src dir
  551. output = "";
  552. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  553. "Run cvs: " << this->CVSCheckOut << std::endl);
  554. res = cmSystemTools::RunSingleCommand(this->CVSCheckOut.c_str(), &output,
  555. &retVal, this->CTestRoot.c_str(), this->HandlerVerbose,
  556. 0 /*this->TimeOut*/);
  557. if (!res || retVal != 0)
  558. {
  559. cmSystemTools::Error("Unable to perform cvs checkout:\n",
  560. output.c_str());
  561. return 6;
  562. }
  563. }
  564. return 0;
  565. }
  566. //----------------------------------------------------------------------
  567. int cmCTestScriptHandler::BackupDirectories()
  568. {
  569. int retVal;
  570. // compute the backup names
  571. this->BackupSourceDir = this->SourceDir;
  572. this->BackupSourceDir += "_CMakeBackup";
  573. this->BackupBinaryDir = this->BinaryDir;
  574. this->BackupBinaryDir += "_CMakeBackup";
  575. // backup the binary and src directories if requested
  576. if (this->Backup)
  577. {
  578. // if for some reason those directories exist then first delete them
  579. if (cmSystemTools::FileExists(this->BackupSourceDir.c_str()))
  580. {
  581. cmSystemTools::RemoveADirectory(this->BackupSourceDir.c_str());
  582. }
  583. if (cmSystemTools::FileExists(this->BackupBinaryDir.c_str()))
  584. {
  585. cmSystemTools::RemoveADirectory(this->BackupBinaryDir.c_str());
  586. }
  587. // first rename the src and binary directories
  588. rename(this->SourceDir.c_str(), this->BackupSourceDir.c_str());
  589. rename(this->BinaryDir.c_str(), this->BackupBinaryDir.c_str());
  590. // we must now checkout the src dir
  591. retVal = this->CheckOutSourceDir();
  592. if (retVal)
  593. {
  594. this->RestoreBackupDirectories();
  595. return retVal;
  596. }
  597. }
  598. return 0;
  599. }
  600. //----------------------------------------------------------------------
  601. int cmCTestScriptHandler::PerformExtraUpdates()
  602. {
  603. std::string command;
  604. std::string output;
  605. int retVal;
  606. bool res;
  607. // do an initial cvs update as required
  608. command = this->UpdateCmd;
  609. std::vector<cmStdString>::iterator it;
  610. for (it = this->ExtraUpdates.begin();
  611. it != this->ExtraUpdates.end();
  612. ++ it )
  613. {
  614. std::vector<std::string> cvsArgs;
  615. cmSystemTools::ExpandListArgument(it->c_str(),cvsArgs);
  616. if (cvsArgs.size() == 2)
  617. {
  618. std::string fullCommand = command;
  619. fullCommand += " update ";
  620. fullCommand += cvsArgs[1];
  621. output = "";
  622. retVal = 0;
  623. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run Update: "
  624. << fullCommand.c_str() << std::endl);
  625. res = cmSystemTools::RunSingleCommand(fullCommand.c_str(), &output,
  626. &retVal, cvsArgs[0].c_str(),
  627. this->HandlerVerbose, 0 /*this->TimeOut*/);
  628. if (!res || retVal != 0)
  629. {
  630. cmSystemTools::Error("Unable to perform extra updates:\n",
  631. it->c_str(), "\nWith output:\n",
  632. output.c_str());
  633. return 0;
  634. }
  635. }
  636. }
  637. return 0;
  638. }
  639. //----------------------------------------------------------------------
  640. // run a single dashboard entry
  641. int cmCTestScriptHandler::RunConfigurationDashboard()
  642. {
  643. // local variables
  644. std::string command;
  645. std::string output;
  646. int retVal;
  647. bool res;
  648. // make sure the src directory is there, if it isn't then we might be able
  649. // to check it out from cvs
  650. retVal = this->CheckOutSourceDir();
  651. if (retVal)
  652. {
  653. return retVal;
  654. }
  655. // backup the dirs if requested
  656. retVal = this->BackupDirectories();
  657. if (retVal)
  658. {
  659. return retVal;
  660. }
  661. // clear the binary directory?
  662. if (this->EmptyBinDir)
  663. {
  664. if ( !cmCTestScriptHandler::EmptyBinaryDirectory(
  665. this->BinaryDir.c_str()) )
  666. {
  667. cmCTestLog(this->CTest, ERROR_MESSAGE,
  668. "Problem removing the binary directory" << std::endl);
  669. }
  670. }
  671. // make sure the binary directory exists if it isn't the srcdir
  672. if (!cmSystemTools::FileExists(this->BinaryDir.c_str()) &&
  673. this->SourceDir != this->BinaryDir)
  674. {
  675. if (!cmSystemTools::MakeDirectory(this->BinaryDir.c_str()))
  676. {
  677. cmSystemTools::Error("Unable to create the binary directory:\n",
  678. this->BinaryDir.c_str());
  679. this->RestoreBackupDirectories();
  680. return 7;
  681. }
  682. }
  683. // if the binary directory and the source directory are the same,
  684. // and we are starting with an empty binary directory, then that means
  685. // we must check out the source tree
  686. if (this->EmptyBinDir && this->SourceDir == this->BinaryDir)
  687. {
  688. // make sure we have the required info
  689. if (this->CVSCheckOut.empty())
  690. {
  691. cmSystemTools::Error("You have specified the source and binary "
  692. "directories to be the same (an in source build). You have also "
  693. "specified that the binary directory is to be erased. This means "
  694. "that the source will have to be checked out from CVS. But you have "
  695. "not specified CTEST_CVS_CHECKOUT");
  696. return 8;
  697. }
  698. // we must now checkout the src dir
  699. retVal = this->CheckOutSourceDir();
  700. if (retVal)
  701. {
  702. this->RestoreBackupDirectories();
  703. return retVal;
  704. }
  705. }
  706. // backup the dirs if requested
  707. retVal = this->PerformExtraUpdates();
  708. if (retVal)
  709. {
  710. return retVal;
  711. }
  712. // put the initial cache into the bin dir
  713. if (!this->InitCache.empty())
  714. {
  715. std::string cacheFile = this->BinaryDir;
  716. cacheFile += "/CMakeCache.txt";
  717. cmGeneratedFileStream fout(cacheFile.c_str());
  718. if(!fout)
  719. {
  720. this->RestoreBackupDirectories();
  721. return 9;
  722. }
  723. fout.write(this->InitCache.c_str(), this->InitCache.size());
  724. // Make sure the operating system has finished writing the file
  725. // before closing it. This will ensure the file is finished before
  726. // the check below.
  727. fout.flush();
  728. fout.close();
  729. }
  730. // do an initial cmake to setup the DartConfig file
  731. int cmakeFailed = 0;
  732. std::string cmakeFailedOuput;
  733. if (!this->CMakeCmd.empty())
  734. {
  735. command = this->CMakeCmd;
  736. command += " \"";
  737. command += this->SourceDir;
  738. output = "";
  739. command += "\"";
  740. retVal = 0;
  741. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run cmake command: "
  742. << command.c_str() << std::endl);
  743. res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  744. &retVal, this->BinaryDir.c_str(),
  745. this->HandlerVerbose, 0 /*this->TimeOut*/);
  746. if ( !this->CMOutFile.empty() )
  747. {
  748. std::string cmakeOutputFile = this->CMOutFile;
  749. if ( !cmSystemTools::FileIsFullPath(cmakeOutputFile.c_str()) )
  750. {
  751. cmakeOutputFile = this->BinaryDir + "/" + cmakeOutputFile;
  752. }
  753. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  754. "Write CMake output to file: " << cmakeOutputFile.c_str()
  755. << std::endl);
  756. cmGeneratedFileStream fout(cmakeOutputFile.c_str());
  757. if ( fout )
  758. {
  759. fout << output.c_str();
  760. }
  761. else
  762. {
  763. cmCTestLog(this->CTest, ERROR_MESSAGE,
  764. "Cannot open CMake output file: "
  765. << cmakeOutputFile.c_str() << " for writing" << std::endl);
  766. }
  767. }
  768. if (!res || retVal != 0)
  769. {
  770. // even if this fails continue to the next step
  771. cmakeFailed = 1;
  772. cmakeFailedOuput = output;
  773. }
  774. }
  775. // run ctest, it may be more than one command in here
  776. std::vector<std::string> ctestCommands;
  777. cmSystemTools::ExpandListArgument(this->CTestCmd,ctestCommands);
  778. // for each variable/argument do a putenv
  779. for (unsigned i = 0; i < ctestCommands.size(); ++i)
  780. {
  781. command = ctestCommands[i];
  782. output = "";
  783. retVal = 0;
  784. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run ctest command: "
  785. << command.c_str() << std::endl);
  786. res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  787. &retVal, this->BinaryDir.c_str(), this->HandlerVerbose,
  788. 0 /*this->TimeOut*/);
  789. // did something critical fail in ctest
  790. if (!res || cmakeFailed ||
  791. retVal & cmCTest::BUILD_ERRORS)
  792. {
  793. this->RestoreBackupDirectories();
  794. if (cmakeFailed)
  795. {
  796. cmCTestLog(this->CTest, ERROR_MESSAGE,
  797. "Unable to run cmake:" << std::endl
  798. << cmakeFailedOuput.c_str() << std::endl);
  799. return 10;
  800. }
  801. cmCTestLog(this->CTest, ERROR_MESSAGE,
  802. "Unable to run ctest:" << std::endl
  803. << output.c_str() << std::endl);
  804. if (!res)
  805. {
  806. return 11;
  807. }
  808. return retVal * 100;
  809. }
  810. }
  811. // if all was succesful, delete the backup dirs to free up disk space
  812. if (this->Backup)
  813. {
  814. cmSystemTools::RemoveADirectory(this->BackupSourceDir.c_str());
  815. cmSystemTools::RemoveADirectory(this->BackupBinaryDir.c_str());
  816. }
  817. return 0;
  818. }
  819. //-------------------------------------------------------------------------
  820. void cmCTestScriptHandler::RestoreBackupDirectories()
  821. {
  822. // if we backed up the dirs and the build failed, then restore
  823. // the backed up dirs
  824. if (this->Backup)
  825. {
  826. // if for some reason those directories exist then first delete them
  827. if (cmSystemTools::FileExists(this->SourceDir.c_str()))
  828. {
  829. cmSystemTools::RemoveADirectory(this->SourceDir.c_str());
  830. }
  831. if (cmSystemTools::FileExists(this->BinaryDir.c_str()))
  832. {
  833. cmSystemTools::RemoveADirectory(this->BinaryDir.c_str());
  834. }
  835. // rename the src and binary directories
  836. rename(this->BackupSourceDir.c_str(), this->SourceDir.c_str());
  837. rename(this->BackupBinaryDir.c_str(), this->BinaryDir.c_str());
  838. }
  839. }
  840. bool cmCTestScriptHandler::RunScript(cmCTest* ctest, const char *sname,
  841. bool InProcess)
  842. {
  843. cmCTestScriptHandler* sh = new cmCTestScriptHandler();
  844. sh->SetCTestInstance(ctest);
  845. sh->AddConfigurationScript(sname,InProcess);
  846. sh->ProcessHandler();
  847. delete sh;
  848. return true;
  849. }
  850. bool cmCTestScriptHandler::EmptyBinaryDirectory(const char *sname)
  851. {
  852. // try to avoid deleting root
  853. if (!sname || strlen(sname) < 2)
  854. {
  855. return false;
  856. }
  857. // try to avoid deleting directories that we shouldn't
  858. std::string check = sname;
  859. check += "/CMakeCache.txt";
  860. if(cmSystemTools::FileExists(check.c_str()) &&
  861. !cmSystemTools::RemoveADirectory(sname))
  862. {
  863. return false;
  864. }
  865. return true;
  866. }
  867. //-------------------------------------------------------------------------
  868. double cmCTestScriptHandler::GetRemainingTimeAllowed()
  869. {
  870. if (!this->Makefile)
  871. {
  872. return 1.0e7;
  873. }
  874. const char *timelimitS
  875. = this->Makefile->GetDefinition("CTEST_TIME_LIMIT");
  876. if (!timelimitS)
  877. {
  878. return 1.0e7;
  879. }
  880. double timelimit = atof(timelimitS);
  881. return timelimit - cmSystemTools::GetTime() + this->ScriptStartTime;
  882. }