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