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