cmCTestScriptHandler.cxx 33 KB

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