cmCTestScriptHandler.cxx 32 KB

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