cmCTestScriptHandler.cxx 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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();
  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. // remove all cmake commands which are not scriptable, since they can't be
  265. // used in ctest scripts
  266. this->CMake->RemoveUnscriptableCommands();
  267. // add any ctest specific commands, probably should have common superclass
  268. // for ctest commands to clean this up. If a couple more commands are
  269. // created with the same format lets do that - ken
  270. this->AddCTestCommand(new cmCTestBuildCommand);
  271. this->AddCTestCommand(new cmCTestConfigureCommand);
  272. this->AddCTestCommand(new cmCTestCoverageCommand);
  273. this->AddCTestCommand(new cmCTestEmptyBinaryDirectoryCommand);
  274. this->AddCTestCommand(new cmCTestMemCheckCommand);
  275. this->AddCTestCommand(new cmCTestReadCustomFilesCommand);
  276. this->AddCTestCommand(new cmCTestRunScriptCommand);
  277. this->AddCTestCommand(new cmCTestSleepCommand);
  278. this->AddCTestCommand(new cmCTestStartCommand);
  279. this->AddCTestCommand(new cmCTestSubmitCommand);
  280. this->AddCTestCommand(new cmCTestTestCommand);
  281. this->AddCTestCommand(new cmCTestUpdateCommand);
  282. }
  283. void cmCTestScriptHandler::GetCommandDocumentation(
  284. std::vector<cmDocumentationEntry>& v) const
  285. {
  286. this->CMake->GetCommandDocumentation(v);
  287. }
  288. //----------------------------------------------------------------------
  289. // this sets up some variables for the script to use, creates the required
  290. // cmake instance and generators, and then reads in the script
  291. int cmCTestScriptHandler::ReadInScript(const std::string& total_script_arg)
  292. {
  293. // if the argument has a , in it then it needs to be broken into the fist
  294. // argument (which is the script) and the second argument which will be
  295. // passed into the scripts as S_ARG
  296. std::string script = total_script_arg;
  297. std::string script_arg;
  298. if (total_script_arg.find(",") != std::string::npos)
  299. {
  300. script = total_script_arg.substr(0,total_script_arg.find(","));
  301. script_arg = total_script_arg.substr(total_script_arg.find(",")+1);
  302. }
  303. // make sure the file exists
  304. if (!cmSystemTools::FileExists(script.c_str()))
  305. {
  306. cmSystemTools::Error("Cannot find file: ", script.c_str());
  307. return 1;
  308. }
  309. // read in the list file to fill the cache
  310. // create a cmake instance to read the configuration script
  311. this->CreateCMake();
  312. // set a variable with the path to the current script
  313. this->Makefile->AddDefinition("CTEST_SCRIPT_DIRECTORY",
  314. cmSystemTools::GetFilenamePath(script).c_str());
  315. this->Makefile->AddDefinition("CTEST_SCRIPT_NAME",
  316. cmSystemTools::GetFilenameName(script).c_str());
  317. this->Makefile->AddDefinition("CTEST_EXECUTABLE_NAME",
  318. this->CTest->GetCTestExecutable());
  319. this->Makefile->AddDefinition("CMAKE_EXECUTABLE_NAME",
  320. this->CTest->GetCMakeExecutable());
  321. this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", true);
  322. this->UpdateElapsedTime();
  323. // add the script arg if defined
  324. if (script_arg.size())
  325. {
  326. this->Makefile->AddDefinition("CTEST_SCRIPT_ARG", script_arg.c_str());
  327. }
  328. // always add a function blocker to update the elapsed time
  329. cmCTestScriptFunctionBlocker *f = new cmCTestScriptFunctionBlocker();
  330. f->CTestScriptHandler = this;
  331. this->Makefile->AddFunctionBlocker(f);
  332. // finally read in the script
  333. if (!this->Makefile->ReadListFile(0, script.c_str()) ||
  334. cmSystemTools::GetErrorOccuredFlag())
  335. {
  336. return 2;
  337. }
  338. return 0;
  339. }
  340. //----------------------------------------------------------------------
  341. // extract variabels from the script to set ivars
  342. int cmCTestScriptHandler::ExtractVariables()
  343. {
  344. // Temporary variables
  345. const char* minInterval;
  346. const char* contDuration;
  347. this->SourceDir
  348. = this->Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
  349. this->BinaryDir
  350. = this->Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");
  351. // add in translations for src and bin
  352. cmSystemTools::AddKeepPath(this->SourceDir.c_str());
  353. cmSystemTools::AddKeepPath(this->BinaryDir.c_str());
  354. this->CTestCmd
  355. = this->Makefile->GetSafeDefinition("CTEST_COMMAND");
  356. this->CVSCheckOut
  357. = this->Makefile->GetSafeDefinition("CTEST_CVS_CHECKOUT");
  358. this->CTestRoot
  359. = this->Makefile->GetSafeDefinition("CTEST_DASHBOARD_ROOT");
  360. this->UpdateCmd
  361. = this->Makefile->GetSafeDefinition("CTEST_UPDATE_COMMAND");
  362. if ( this->UpdateCmd.empty() )
  363. {
  364. this->UpdateCmd
  365. = this->Makefile->GetSafeDefinition("CTEST_CVS_COMMAND");
  366. }
  367. this->CTestEnv
  368. = this->Makefile->GetSafeDefinition("CTEST_ENVIRONMENT");
  369. this->InitCache
  370. = this->Makefile->GetSafeDefinition("CTEST_INITIAL_CACHE");
  371. this->CMakeCmd
  372. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_COMMAND");
  373. this->CMOutFile
  374. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_OUTPUT_FILE_NAME");
  375. this->Backup
  376. = this->Makefile->IsOn("CTEST_BACKUP_AND_RESTORE");
  377. this->EmptyBinDir
  378. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY");
  379. this->EmptyBinDirOnce
  380. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY_ONCE");
  381. minInterval
  382. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_MINIMUM_INTERVAL");
  383. contDuration
  384. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_DURATION");
  385. char updateVar[40];
  386. int i;
  387. for (i = 1; i < 10; ++i)
  388. {
  389. sprintf(updateVar,"CTEST_EXTRA_UPDATES_%i",i);
  390. const char *updateVal = this->Makefile->GetDefinition(updateVar);
  391. if ( updateVal )
  392. {
  393. if ( this->UpdateCmd.empty() )
  394. {
  395. cmSystemTools::Error(updateVar,
  396. " specified without specifying CTEST_CVS_COMMAND.");
  397. return 12;
  398. }
  399. this->ExtraUpdates.push_back(updateVal);
  400. }
  401. }
  402. // in order to backup and restore we also must have the cvs root
  403. if (this->Backup && this->CVSCheckOut.empty())
  404. {
  405. cmSystemTools::Error(
  406. "Backup was requested without specifying CTEST_CVS_CHECKOUT.");
  407. return 3;
  408. }
  409. // make sure the required info is here
  410. if (this->SourceDir.empty() ||
  411. this->BinaryDir.empty() ||
  412. this->CTestCmd.empty())
  413. {
  414. std::string msg = "CTEST_SOURCE_DIRECTORY = ";
  415. msg += (!this->SourceDir.empty()) ? this->SourceDir.c_str() : "(Null)";
  416. msg += "\nCTEST_BINARY_DIRECTORY = ";
  417. msg += (!this->BinaryDir.empty()) ? this->BinaryDir.c_str() : "(Null)";
  418. msg += "\nCTEST_COMMAND = ";
  419. msg += (!this->CTestCmd.empty()) ? this->CTestCmd.c_str() : "(Null)";
  420. cmSystemTools::Error(
  421. "Some required settings in the configuration file were missing:\n",
  422. msg.c_str());
  423. return 4;
  424. }
  425. // if the dashboard root isn't specified then we can compute it from the
  426. // this->SourceDir
  427. if (this->CTestRoot.empty() )
  428. {
  429. this->CTestRoot = cmSystemTools::GetFilenamePath(this->SourceDir).c_str();
  430. }
  431. // the script may override the minimum continuous interval
  432. if (minInterval)
  433. {
  434. this->MinimumInterval = 60 * atof(minInterval);
  435. }
  436. if (contDuration)
  437. {
  438. this->ContinuousDuration = 60.0 * atof(contDuration);
  439. }
  440. this->UpdateElapsedTime();
  441. return 0;
  442. }
  443. //----------------------------------------------------------------------
  444. void cmCTestScriptHandler::SleepInSeconds(unsigned int secondsToWait)
  445. {
  446. #if defined(_WIN32)
  447. Sleep(1000*secondsToWait);
  448. #else
  449. sleep(secondsToWait);
  450. #endif
  451. }
  452. //----------------------------------------------------------------------
  453. // run a specific script
  454. int cmCTestScriptHandler::RunConfigurationScript
  455. (const std::string& total_script_arg, bool pscope)
  456. {
  457. int result;
  458. this->ScriptStartTime =
  459. cmSystemTools::GetTime();
  460. // read in the script
  461. if (pscope)
  462. {
  463. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  464. "Reading Script: " << total_script_arg << std::endl);
  465. result = this->ReadInScript(total_script_arg);
  466. }
  467. else
  468. {
  469. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  470. "Executing Script: " << total_script_arg << std::endl);
  471. result = this->ExecuteScript(total_script_arg);
  472. }
  473. if (result)
  474. {
  475. return result;
  476. }
  477. // only run the curent script if we should
  478. if (this->Makefile && this->Makefile->IsOn("CTEST_RUN_CURRENT_SCRIPT"))
  479. {
  480. return this->RunCurrentScript();
  481. }
  482. return result;
  483. }
  484. //----------------------------------------------------------------------
  485. int cmCTestScriptHandler::RunCurrentScript()
  486. {
  487. int result;
  488. // do not run twice
  489. this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", false);
  490. // no popup widows
  491. cmSystemTools::SetRunCommandHideConsole(true);
  492. // extract the vars from the cache and store in ivars
  493. result = this->ExtractVariables();
  494. if (result)
  495. {
  496. return result;
  497. }
  498. // set any environment variables
  499. if (!this->CTestEnv.empty())
  500. {
  501. std::vector<std::string> envArgs;
  502. cmSystemTools::ExpandListArgument(this->CTestEnv.c_str(),envArgs);
  503. // for each variable/argument do a putenv
  504. for (unsigned i = 0; i < envArgs.size(); ++i)
  505. {
  506. cmSystemTools::PutEnv(envArgs[i].c_str());
  507. }
  508. }
  509. // now that we have done most of the error checking finally run the
  510. // dashboard, we may be asked to repeatedly run this dashboard, such as
  511. // for a continuous, do we ned to run it more than once?
  512. if ( this->ContinuousDuration >= 0 )
  513. {
  514. this->UpdateElapsedTime();
  515. double ending_time = cmSystemTools::GetTime() + this->ContinuousDuration;
  516. if (this->EmptyBinDirOnce)
  517. {
  518. this->EmptyBinDir = true;
  519. }
  520. do
  521. {
  522. double interval = cmSystemTools::GetTime();
  523. result = this->RunConfigurationDashboard();
  524. interval = cmSystemTools::GetTime() - interval;
  525. if (interval < this->MinimumInterval)
  526. {
  527. this->SleepInSeconds(
  528. static_cast<unsigned int>(this->MinimumInterval - interval));
  529. }
  530. if (this->EmptyBinDirOnce)
  531. {
  532. this->EmptyBinDir = false;
  533. }
  534. }
  535. while (cmSystemTools::GetTime() < ending_time);
  536. }
  537. // otherwise just run it once
  538. else
  539. {
  540. result = this->RunConfigurationDashboard();
  541. }
  542. return result;
  543. }
  544. //----------------------------------------------------------------------
  545. int cmCTestScriptHandler::CheckOutSourceDir()
  546. {
  547. std::string command;
  548. std::string output;
  549. int retVal;
  550. bool res;
  551. if (!cmSystemTools::FileExists(this->SourceDir.c_str()) &&
  552. !this->CVSCheckOut.empty())
  553. {
  554. // we must now checkout the src dir
  555. output = "";
  556. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  557. "Run cvs: " << this->CVSCheckOut << std::endl);
  558. res = cmSystemTools::RunSingleCommand(this->CVSCheckOut.c_str(), &output,
  559. &retVal, this->CTestRoot.c_str(), this->HandlerVerbose,
  560. 0 /*this->TimeOut*/);
  561. if (!res || retVal != 0)
  562. {
  563. cmSystemTools::Error("Unable to perform cvs checkout:\n",
  564. output.c_str());
  565. return 6;
  566. }
  567. }
  568. return 0;
  569. }
  570. //----------------------------------------------------------------------
  571. int cmCTestScriptHandler::BackupDirectories()
  572. {
  573. int retVal;
  574. // compute the backup names
  575. this->BackupSourceDir = this->SourceDir;
  576. this->BackupSourceDir += "_CMakeBackup";
  577. this->BackupBinaryDir = this->BinaryDir;
  578. this->BackupBinaryDir += "_CMakeBackup";
  579. // backup the binary and src directories if requested
  580. if (this->Backup)
  581. {
  582. // if for some reason those directories exist then first delete them
  583. if (cmSystemTools::FileExists(this->BackupSourceDir.c_str()))
  584. {
  585. cmSystemTools::RemoveADirectory(this->BackupSourceDir.c_str());
  586. }
  587. if (cmSystemTools::FileExists(this->BackupBinaryDir.c_str()))
  588. {
  589. cmSystemTools::RemoveADirectory(this->BackupBinaryDir.c_str());
  590. }
  591. // first rename the src and binary directories
  592. rename(this->SourceDir.c_str(), this->BackupSourceDir.c_str());
  593. rename(this->BinaryDir.c_str(), this->BackupBinaryDir.c_str());
  594. // we must now checkout the src dir
  595. retVal = this->CheckOutSourceDir();
  596. if (retVal)
  597. {
  598. this->RestoreBackupDirectories();
  599. return retVal;
  600. }
  601. }
  602. return 0;
  603. }
  604. //----------------------------------------------------------------------
  605. int cmCTestScriptHandler::PerformExtraUpdates()
  606. {
  607. std::string command;
  608. std::string output;
  609. int retVal;
  610. bool res;
  611. // do an initial cvs update as required
  612. command = this->UpdateCmd;
  613. std::vector<cmStdString>::iterator it;
  614. for (it = this->ExtraUpdates.begin();
  615. it != this->ExtraUpdates.end();
  616. ++ it )
  617. {
  618. std::vector<std::string> cvsArgs;
  619. cmSystemTools::ExpandListArgument(it->c_str(),cvsArgs);
  620. if (cvsArgs.size() == 2)
  621. {
  622. std::string fullCommand = command;
  623. fullCommand += " update ";
  624. fullCommand += cvsArgs[1];
  625. output = "";
  626. retVal = 0;
  627. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run Update: "
  628. << fullCommand.c_str() << std::endl);
  629. res = cmSystemTools::RunSingleCommand(fullCommand.c_str(), &output,
  630. &retVal, cvsArgs[0].c_str(),
  631. this->HandlerVerbose, 0 /*this->TimeOut*/);
  632. if (!res || retVal != 0)
  633. {
  634. cmSystemTools::Error("Unable to perform extra updates:\n",
  635. it->c_str(), "\nWith output:\n",
  636. output.c_str());
  637. return 0;
  638. }
  639. }
  640. }
  641. return 0;
  642. }
  643. //----------------------------------------------------------------------
  644. // run a single dashboard entry
  645. int cmCTestScriptHandler::RunConfigurationDashboard()
  646. {
  647. // local variables
  648. std::string command;
  649. std::string output;
  650. int retVal;
  651. bool res;
  652. // make sure the src directory is there, if it isn't then we might be able
  653. // to check it out from cvs
  654. retVal = this->CheckOutSourceDir();
  655. if (retVal)
  656. {
  657. return retVal;
  658. }
  659. // backup the dirs if requested
  660. retVal = this->BackupDirectories();
  661. if (retVal)
  662. {
  663. return retVal;
  664. }
  665. // clear the binary directory?
  666. if (this->EmptyBinDir)
  667. {
  668. if ( !cmCTestScriptHandler::EmptyBinaryDirectory(
  669. this->BinaryDir.c_str()) )
  670. {
  671. cmCTestLog(this->CTest, ERROR_MESSAGE,
  672. "Problem removing the binary directory" << std::endl);
  673. }
  674. }
  675. // make sure the binary directory exists if it isn't the srcdir
  676. if (!cmSystemTools::FileExists(this->BinaryDir.c_str()) &&
  677. this->SourceDir != this->BinaryDir)
  678. {
  679. if (!cmSystemTools::MakeDirectory(this->BinaryDir.c_str()))
  680. {
  681. cmSystemTools::Error("Unable to create the binary directory:\n",
  682. this->BinaryDir.c_str());
  683. this->RestoreBackupDirectories();
  684. return 7;
  685. }
  686. }
  687. // if the binary directory and the source directory are the same,
  688. // and we are starting with an empty binary directory, then that means
  689. // we must check out the source tree
  690. if (this->EmptyBinDir && this->SourceDir == this->BinaryDir)
  691. {
  692. // make sure we have the required info
  693. if (this->CVSCheckOut.empty())
  694. {
  695. cmSystemTools::Error("You have specified the source and binary "
  696. "directories to be the same (an in source build). You have also "
  697. "specified that the binary directory is to be erased. This means "
  698. "that the source will have to be checked out from CVS. But you have "
  699. "not specified CTEST_CVS_CHECKOUT");
  700. return 8;
  701. }
  702. // we must now checkout the src dir
  703. retVal = this->CheckOutSourceDir();
  704. if (retVal)
  705. {
  706. this->RestoreBackupDirectories();
  707. return retVal;
  708. }
  709. }
  710. // backup the dirs if requested
  711. retVal = this->PerformExtraUpdates();
  712. if (retVal)
  713. {
  714. return retVal;
  715. }
  716. // put the initial cache into the bin dir
  717. if (!this->InitCache.empty())
  718. {
  719. std::string cacheFile = this->BinaryDir;
  720. cacheFile += "/CMakeCache.txt";
  721. cmGeneratedFileStream fout(cacheFile.c_str());
  722. if(!fout)
  723. {
  724. this->RestoreBackupDirectories();
  725. return 9;
  726. }
  727. fout.write(this->InitCache.c_str(), this->InitCache.size());
  728. // Make sure the operating system has finished writing the file
  729. // before closing it. This will ensure the file is finished before
  730. // the check below.
  731. fout.flush();
  732. fout.close();
  733. }
  734. // do an initial cmake to setup the DartConfig file
  735. int cmakeFailed = 0;
  736. std::string cmakeFailedOuput;
  737. if (!this->CMakeCmd.empty())
  738. {
  739. command = this->CMakeCmd;
  740. command += " \"";
  741. command += this->SourceDir;
  742. output = "";
  743. command += "\"";
  744. retVal = 0;
  745. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run cmake command: "
  746. << command.c_str() << std::endl);
  747. res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  748. &retVal, this->BinaryDir.c_str(),
  749. this->HandlerVerbose, 0 /*this->TimeOut*/);
  750. if ( !this->CMOutFile.empty() )
  751. {
  752. std::string cmakeOutputFile = this->CMOutFile;
  753. if ( !cmSystemTools::FileIsFullPath(cmakeOutputFile.c_str()) )
  754. {
  755. cmakeOutputFile = this->BinaryDir + "/" + cmakeOutputFile;
  756. }
  757. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  758. "Write CMake output to file: " << cmakeOutputFile.c_str()
  759. << std::endl);
  760. cmGeneratedFileStream fout(cmakeOutputFile.c_str());
  761. if ( fout )
  762. {
  763. fout << output.c_str();
  764. }
  765. else
  766. {
  767. cmCTestLog(this->CTest, ERROR_MESSAGE,
  768. "Cannot open CMake output file: "
  769. << cmakeOutputFile.c_str() << " for writing" << std::endl);
  770. }
  771. }
  772. if (!res || retVal != 0)
  773. {
  774. // even if this fails continue to the next step
  775. cmakeFailed = 1;
  776. cmakeFailedOuput = output;
  777. }
  778. }
  779. // run ctest, it may be more than one command in here
  780. std::vector<std::string> ctestCommands;
  781. cmSystemTools::ExpandListArgument(this->CTestCmd,ctestCommands);
  782. // for each variable/argument do a putenv
  783. for (unsigned i = 0; i < ctestCommands.size(); ++i)
  784. {
  785. command = ctestCommands[i];
  786. output = "";
  787. retVal = 0;
  788. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run ctest command: "
  789. << command.c_str() << std::endl);
  790. res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  791. &retVal, this->BinaryDir.c_str(), this->HandlerVerbose,
  792. 0 /*this->TimeOut*/);
  793. // did something critical fail in ctest
  794. if (!res || cmakeFailed ||
  795. retVal & cmCTest::BUILD_ERRORS)
  796. {
  797. this->RestoreBackupDirectories();
  798. if (cmakeFailed)
  799. {
  800. cmCTestLog(this->CTest, ERROR_MESSAGE,
  801. "Unable to run cmake:" << std::endl
  802. << cmakeFailedOuput.c_str() << std::endl);
  803. return 10;
  804. }
  805. cmCTestLog(this->CTest, ERROR_MESSAGE,
  806. "Unable to run ctest:" << std::endl
  807. << output.c_str() << std::endl);
  808. if (!res)
  809. {
  810. return 11;
  811. }
  812. return retVal * 100;
  813. }
  814. }
  815. // if all was succesful, delete the backup dirs to free up disk space
  816. if (this->Backup)
  817. {
  818. cmSystemTools::RemoveADirectory(this->BackupSourceDir.c_str());
  819. cmSystemTools::RemoveADirectory(this->BackupBinaryDir.c_str());
  820. }
  821. return 0;
  822. }
  823. //-------------------------------------------------------------------------
  824. void cmCTestScriptHandler::RestoreBackupDirectories()
  825. {
  826. // if we backed up the dirs and the build failed, then restore
  827. // the backed up dirs
  828. if (this->Backup)
  829. {
  830. // if for some reason those directories exist then first delete them
  831. if (cmSystemTools::FileExists(this->SourceDir.c_str()))
  832. {
  833. cmSystemTools::RemoveADirectory(this->SourceDir.c_str());
  834. }
  835. if (cmSystemTools::FileExists(this->BinaryDir.c_str()))
  836. {
  837. cmSystemTools::RemoveADirectory(this->BinaryDir.c_str());
  838. }
  839. // rename the src and binary directories
  840. rename(this->BackupSourceDir.c_str(), this->SourceDir.c_str());
  841. rename(this->BackupBinaryDir.c_str(), this->BinaryDir.c_str());
  842. }
  843. }
  844. bool cmCTestScriptHandler::RunScript(cmCTest* ctest, const char *sname,
  845. bool InProcess)
  846. {
  847. cmCTestScriptHandler* sh = new cmCTestScriptHandler();
  848. sh->SetCTestInstance(ctest);
  849. sh->AddConfigurationScript(sname,InProcess);
  850. sh->ProcessHandler();
  851. delete sh;
  852. return true;
  853. }
  854. bool cmCTestScriptHandler::EmptyBinaryDirectory(const char *sname)
  855. {
  856. // try to avoid deleting root
  857. if (!sname || strlen(sname) < 2)
  858. {
  859. return false;
  860. }
  861. // try to avoid deleting directories that we shouldn't
  862. std::string check = sname;
  863. check += "/CMakeCache.txt";
  864. if(cmSystemTools::FileExists(check.c_str()) &&
  865. !cmSystemTools::RemoveADirectory(sname))
  866. {
  867. return false;
  868. }
  869. return true;
  870. }
  871. //-------------------------------------------------------------------------
  872. double cmCTestScriptHandler::GetRemainingTimeAllowed()
  873. {
  874. if (!this->Makefile)
  875. {
  876. return 1.0e7;
  877. }
  878. const char *timelimitS
  879. = this->Makefile->GetDefinition("CTEST_TIME_LIMIT");
  880. if (!timelimitS)
  881. {
  882. return 1.0e7;
  883. }
  884. double timelimit = atof(timelimitS);
  885. return timelimit - cmSystemTools::GetTime() + this->ScriptStartTime;
  886. }