cmCTestScriptHandler.cxx 27 KB

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