cmCTestScriptHandler.cxx 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. // used for sleep
  21. #ifdef _WIN32
  22. #include "windows.h"
  23. #endif
  24. #include <stdlib.h>
  25. #include <time.h>
  26. #include <math.h>
  27. #include <float.h>
  28. // needed for sleep
  29. #if !defined(_WIN32)
  30. # include <unistd.h>
  31. #endif
  32. #include "cmCTestBuildCommand.h"
  33. #include "cmCTestConfigureCommand.h"
  34. #include "cmCTestCoverageCommand.h"
  35. #include "cmCTestEmptyBinaryDirectoryCommand.h"
  36. #include "cmCTestMemCheckCommand.h"
  37. #include "cmCTestReadCustomFilesCommand.h"
  38. #include "cmCTestRunScriptCommand.h"
  39. #include "cmCTestSleepCommand.h"
  40. #include "cmCTestStartCommand.h"
  41. #include "cmCTestSubmitCommand.h"
  42. #include "cmCTestTestCommand.h"
  43. #include "cmCTestUpdateCommand.h"
  44. #define CTEST_INITIAL_CMAKE_OUTPUT_FILE_NAME "CTestInitialCMakeOutput.log"
  45. // used to keep elapsed time up to date
  46. class cmCTestScriptFunctionBlocker : public cmFunctionBlocker
  47. {
  48. public:
  49. cmCTestScriptFunctionBlocker() {}
  50. virtual ~cmCTestScriptFunctionBlocker() {}
  51. virtual bool IsFunctionBlocked(const cmListFileFunction& lff,
  52. cmMakefile &mf,
  53. cmExecutionStatus &);
  54. //virtual bool ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf);
  55. //virtual void ScopeEnded(cmMakefile &mf);
  56. cmCTestScriptHandler* CTestScriptHandler;
  57. };
  58. // simply update the time and don't block anything
  59. bool cmCTestScriptFunctionBlocker::
  60. IsFunctionBlocked(const cmListFileFunction& , cmMakefile &,
  61. cmExecutionStatus &)
  62. {
  63. this->CTestScriptHandler->UpdateElapsedTime();
  64. return false;
  65. }
  66. //----------------------------------------------------------------------
  67. cmCTestScriptHandler::cmCTestScriptHandler()
  68. {
  69. this->Backup = false;
  70. this->EmptyBinDir = false;
  71. this->EmptyBinDirOnce = false;
  72. this->Makefile = 0;
  73. this->LocalGenerator = 0;
  74. this->CMake = 0;
  75. this->GlobalGenerator = 0;
  76. this->ScriptStartTime = 0;
  77. // the *60 is becuase the settings are in minutes but GetTime is seconds
  78. this->MinimumInterval = 30*60;
  79. this->ContinuousDuration = -1;
  80. }
  81. //----------------------------------------------------------------------
  82. void cmCTestScriptHandler::Initialize()
  83. {
  84. this->Superclass::Initialize();
  85. this->Backup = false;
  86. this->EmptyBinDir = false;
  87. this->EmptyBinDirOnce = false;
  88. this->SourceDir = "";
  89. this->BinaryDir = "";
  90. this->BackupSourceDir = "";
  91. this->BackupBinaryDir = "";
  92. this->CTestRoot = "";
  93. this->CVSCheckOut = "";
  94. this->CTestCmd = "";
  95. this->UpdateCmd = "";
  96. this->CTestEnv = "";
  97. this->InitialCache = "";
  98. this->CMakeCmd = "";
  99. this->CMOutFile = "";
  100. this->ExtraUpdates.clear();
  101. this->MinimumInterval = 20*60;
  102. this->ContinuousDuration = -1;
  103. // what time in seconds did this script start running
  104. this->ScriptStartTime = 0;
  105. this->Makefile = 0;
  106. if (this->LocalGenerator)
  107. {
  108. delete this->LocalGenerator;
  109. }
  110. this->LocalGenerator = 0;
  111. if (this->GlobalGenerator)
  112. {
  113. delete this->GlobalGenerator;
  114. }
  115. this->GlobalGenerator = 0;
  116. if (this->CMake)
  117. {
  118. delete this->CMake;
  119. }
  120. }
  121. //----------------------------------------------------------------------
  122. cmCTestScriptHandler::~cmCTestScriptHandler()
  123. {
  124. // local generator owns the makefile
  125. this->Makefile = 0;
  126. if (this->LocalGenerator)
  127. {
  128. delete this->LocalGenerator;
  129. }
  130. this->LocalGenerator = 0;
  131. if (this->GlobalGenerator)
  132. {
  133. delete this->GlobalGenerator;
  134. }
  135. this->GlobalGenerator = 0;
  136. if (this->CMake)
  137. {
  138. delete this->CMake;
  139. }
  140. }
  141. //----------------------------------------------------------------------
  142. // just adds an argument to the vector
  143. void cmCTestScriptHandler::AddConfigurationScript(const char *script,
  144. bool pscope)
  145. {
  146. this->ConfigurationScripts.push_back(script);
  147. this->ScriptProcessScope.push_back(pscope);
  148. }
  149. //----------------------------------------------------------------------
  150. // the generic entry point for handling scripts, this routine will run all
  151. // the scripts provides a -S arguments
  152. int cmCTestScriptHandler::ProcessHandler()
  153. {
  154. int res = 0;
  155. for (size_t i=0; i < this->ConfigurationScripts.size(); ++i)
  156. {
  157. // for each script run it
  158. res += this->RunConfigurationScript
  159. (cmSystemTools::CollapseFullPath(this->ConfigurationScripts[i].c_str()),
  160. this->ScriptProcessScope[i]);
  161. }
  162. if ( res )
  163. {
  164. return -1;
  165. }
  166. return 0;
  167. }
  168. void cmCTestScriptHandler::UpdateElapsedTime()
  169. {
  170. if (this->LocalGenerator)
  171. {
  172. // set the current elapsed time
  173. char timeString[20];
  174. int itime = static_cast<unsigned int>(cmSystemTools::GetTime()
  175. - this->ScriptStartTime);
  176. sprintf(timeString,"%i",itime);
  177. this->LocalGenerator->GetMakefile()->AddDefinition("CTEST_ELAPSED_TIME",
  178. timeString);
  179. }
  180. }
  181. //----------------------------------------------------------------------
  182. void cmCTestScriptHandler::AddCTestCommand(cmCTestCommand* command)
  183. {
  184. cmCTestCommand* newCom = command;
  185. newCom->CTest = this->CTest;
  186. newCom->CTestScriptHandler = this;
  187. this->CMake->AddCommand(newCom);
  188. }
  189. int cmCTestScriptHandler::ExecuteScript(const std::string& total_script_arg)
  190. {
  191. // execute the script passing in the arguments to the script as well as the
  192. // arguments from this invocation of cmake
  193. std::vector<const char*> argv;
  194. argv.push_back(this->CTest->GetCTestExecutable());
  195. argv.push_back("-SR");
  196. argv.push_back(total_script_arg.c_str());
  197. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  198. "Executable for CTest is: " <<
  199. this->CTest->GetCTestExecutable() << "\n");
  200. // now pass through all the other arguments
  201. std::vector<cmStdString> &initArgs =
  202. this->CTest->GetInitialCommandLineArguments();
  203. //*** need to make sure this does not have the current script ***
  204. for(size_t i=1; i < initArgs.size(); ++i)
  205. {
  206. argv.push_back(initArgs[i].c_str());
  207. }
  208. argv.push_back(0);
  209. // Now create process object
  210. cmsysProcess* cp = cmsysProcess_New();
  211. cmsysProcess_SetCommand(cp, &*argv.begin());
  212. //cmsysProcess_SetWorkingDirectory(cp, dir);
  213. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  214. //cmsysProcess_SetTimeout(cp, timeout);
  215. cmsysProcess_Execute(cp);
  216. std::vector<char> out;
  217. std::vector<char> err;
  218. std::string line;
  219. int pipe = cmSystemTools::WaitForLine(cp, line, 100.0, out, err);
  220. while(pipe != cmsysProcess_Pipe_None)
  221. {
  222. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Output: "
  223. << line << "\n");
  224. if(pipe == cmsysProcess_Pipe_STDERR)
  225. {
  226. cmCTestLog(this->CTest, ERROR_MESSAGE, line << "\n");
  227. }
  228. else if(pipe == cmsysProcess_Pipe_STDOUT)
  229. {
  230. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, line << "\n");
  231. }
  232. pipe = cmSystemTools::WaitForLine(cp, line, 100, out, err);
  233. }
  234. // Properly handle output of the build command
  235. cmsysProcess_WaitForExit(cp, 0);
  236. int result = cmsysProcess_GetState(cp);
  237. int retVal = 0;
  238. bool failed = false;
  239. if(result == cmsysProcess_State_Exited)
  240. {
  241. retVal = cmsysProcess_GetExitValue(cp);
  242. }
  243. else if(result == cmsysProcess_State_Exception)
  244. {
  245. retVal = cmsysProcess_GetExitException(cp);
  246. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tThere was an exception: "
  247. << cmsysProcess_GetExceptionString(cp) << " " <<
  248. retVal << std::endl);
  249. failed = true;
  250. }
  251. else if(result == cmsysProcess_State_Expired)
  252. {
  253. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tThere was a timeout"
  254. << std::endl);
  255. failed = true;
  256. }
  257. else if(result == cmsysProcess_State_Error)
  258. {
  259. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tError executing ctest: "
  260. << cmsysProcess_GetErrorString(cp) << std::endl);
  261. failed = true;
  262. }
  263. cmsysProcess_Delete(cp);
  264. if(failed)
  265. {
  266. cmOStringStream message;
  267. message << "Error running command: [";
  268. message << result << "] ";
  269. for(std::vector<const char*>::iterator i = argv.begin();
  270. i != argv.end(); ++i)
  271. {
  272. if(*i)
  273. {
  274. message << *i << " ";
  275. }
  276. }
  277. cmCTestLog(this->CTest, ERROR_MESSAGE,
  278. message.str() << argv[0] << std::endl);
  279. return -1;
  280. }
  281. return retVal;
  282. }
  283. void cmCTestScriptHandler::CreateCMake()
  284. {
  285. // create a cmake instance to read the configuration script
  286. if (this->CMake)
  287. {
  288. delete this->CMake;
  289. delete this->GlobalGenerator;
  290. delete this->LocalGenerator;
  291. }
  292. this->CMake = new cmake;
  293. this->CMake->AddCMakePaths();
  294. this->GlobalGenerator = new cmGlobalGenerator;
  295. this->GlobalGenerator->SetCMakeInstance(this->CMake);
  296. this->LocalGenerator = this->GlobalGenerator->CreateLocalGenerator();
  297. this->Makefile = this->LocalGenerator->GetMakefile();
  298. // Set CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_BINARY_DIR.
  299. // Also, some commands need Makefile->GetCurrentDirectory().
  300. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  301. this->Makefile->SetStartDirectory(cwd.c_str());
  302. this->Makefile->SetStartOutputDirectory(cwd.c_str());
  303. // remove all cmake commands which are not scriptable, since they can't be
  304. // used in ctest scripts
  305. this->CMake->RemoveUnscriptableCommands();
  306. // add any ctest specific commands, probably should have common superclass
  307. // for ctest commands to clean this up. If a couple more commands are
  308. // created with the same format lets do that - ken
  309. this->AddCTestCommand(new cmCTestBuildCommand);
  310. this->AddCTestCommand(new cmCTestConfigureCommand);
  311. this->AddCTestCommand(new cmCTestCoverageCommand);
  312. this->AddCTestCommand(new cmCTestEmptyBinaryDirectoryCommand);
  313. this->AddCTestCommand(new cmCTestMemCheckCommand);
  314. this->AddCTestCommand(new cmCTestReadCustomFilesCommand);
  315. this->AddCTestCommand(new cmCTestRunScriptCommand);
  316. this->AddCTestCommand(new cmCTestSleepCommand);
  317. this->AddCTestCommand(new cmCTestStartCommand);
  318. this->AddCTestCommand(new cmCTestSubmitCommand);
  319. this->AddCTestCommand(new cmCTestTestCommand);
  320. this->AddCTestCommand(new cmCTestUpdateCommand);
  321. }
  322. void cmCTestScriptHandler::GetCommandDocumentation(
  323. std::vector<cmDocumentationEntry>& v) const
  324. {
  325. this->CMake->GetCommandDocumentation(v);
  326. }
  327. //----------------------------------------------------------------------
  328. // this sets up some variables for the script to use, creates the required
  329. // cmake instance and generators, and then reads in the script
  330. int cmCTestScriptHandler::ReadInScript(const std::string& total_script_arg)
  331. {
  332. // Reset the error flag so that the script is read in no matter what
  333. cmSystemTools::ResetErrorOccuredFlag();
  334. // if the argument has a , in it then it needs to be broken into the fist
  335. // argument (which is the script) and the second argument which will be
  336. // passed into the scripts as S_ARG
  337. std::string script = total_script_arg;
  338. std::string script_arg;
  339. if (total_script_arg.find(",") != std::string::npos)
  340. {
  341. script = total_script_arg.substr(0,total_script_arg.find(","));
  342. script_arg = total_script_arg.substr(total_script_arg.find(",")+1);
  343. }
  344. // make sure the file exists
  345. if (!cmSystemTools::FileExists(script.c_str()))
  346. {
  347. cmSystemTools::Error("Cannot find file: ", script.c_str());
  348. return 1;
  349. }
  350. // read in the list file to fill the cache
  351. // create a cmake instance to read the configuration script
  352. this->CreateCMake();
  353. // set a variable with the path to the current script
  354. this->Makefile->AddDefinition("CTEST_SCRIPT_DIRECTORY",
  355. cmSystemTools::GetFilenamePath(script).c_str());
  356. this->Makefile->AddDefinition("CTEST_SCRIPT_NAME",
  357. cmSystemTools::GetFilenameName(script).c_str());
  358. this->Makefile->AddDefinition("CTEST_EXECUTABLE_NAME",
  359. this->CTest->GetCTestExecutable());
  360. this->Makefile->AddDefinition("CMAKE_EXECUTABLE_NAME",
  361. this->CTest->GetCMakeExecutable());
  362. this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", true);
  363. this->UpdateElapsedTime();
  364. // add the script arg if defined
  365. if (script_arg.size())
  366. {
  367. this->Makefile->AddDefinition("CTEST_SCRIPT_ARG", script_arg.c_str());
  368. }
  369. // always add a function blocker to update the elapsed time
  370. cmCTestScriptFunctionBlocker *f = new cmCTestScriptFunctionBlocker();
  371. f->CTestScriptHandler = this;
  372. this->Makefile->AddFunctionBlocker(f);
  373. /* Execute CTestScriptMode.cmake, which loads CMakeDetermineSystem and
  374. CMakeSystemSpecificInformation, so
  375. that variables like CMAKE_SYSTEM and also the search paths for libraries,
  376. header and executables are set correctly and can be used. Makes new-style
  377. ctest scripting easier. */
  378. std::string systemFile =
  379. this->Makefile->GetModulesFile("CTestScriptMode.cmake");
  380. if (!this->Makefile->ReadListFile(0, systemFile.c_str()) ||
  381. cmSystemTools::GetErrorOccuredFlag())
  382. {
  383. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error in read:"
  384. << systemFile.c_str() << "\n");
  385. return 2;
  386. }
  387. // finally read in the script
  388. if (!this->Makefile->ReadListFile(0, script.c_str()) ||
  389. cmSystemTools::GetErrorOccuredFlag())
  390. {
  391. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error in read script: "
  392. << script.c_str()
  393. << std::endl);
  394. // Reset the error flag so that it can run more than
  395. // one script with an error when you
  396. // use ctest_run_script
  397. cmSystemTools::ResetErrorOccuredFlag();
  398. return 2;
  399. }
  400. return 0;
  401. }
  402. //----------------------------------------------------------------------
  403. // extract variabels from the script to set ivars
  404. int cmCTestScriptHandler::ExtractVariables()
  405. {
  406. // Temporary variables
  407. const char* minInterval;
  408. const char* contDuration;
  409. this->SourceDir
  410. = this->Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
  411. this->BinaryDir
  412. = this->Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");
  413. // add in translations for src and bin
  414. cmSystemTools::AddKeepPath(this->SourceDir.c_str());
  415. cmSystemTools::AddKeepPath(this->BinaryDir.c_str());
  416. this->CTestCmd
  417. = this->Makefile->GetSafeDefinition("CTEST_COMMAND");
  418. this->CVSCheckOut
  419. = this->Makefile->GetSafeDefinition("CTEST_CVS_CHECKOUT");
  420. this->CTestRoot
  421. = this->Makefile->GetSafeDefinition("CTEST_DASHBOARD_ROOT");
  422. this->UpdateCmd
  423. = this->Makefile->GetSafeDefinition("CTEST_UPDATE_COMMAND");
  424. if ( this->UpdateCmd.empty() )
  425. {
  426. this->UpdateCmd
  427. = this->Makefile->GetSafeDefinition("CTEST_CVS_COMMAND");
  428. }
  429. this->CTestEnv
  430. = this->Makefile->GetSafeDefinition("CTEST_ENVIRONMENT");
  431. this->InitialCache
  432. = this->Makefile->GetSafeDefinition("CTEST_INITIAL_CACHE");
  433. this->CMakeCmd
  434. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_COMMAND");
  435. this->CMOutFile
  436. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_OUTPUT_FILE_NAME");
  437. this->Backup
  438. = this->Makefile->IsOn("CTEST_BACKUP_AND_RESTORE");
  439. this->EmptyBinDir
  440. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY");
  441. this->EmptyBinDirOnce
  442. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY_ONCE");
  443. minInterval
  444. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_MINIMUM_INTERVAL");
  445. contDuration
  446. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_DURATION");
  447. char updateVar[40];
  448. int i;
  449. for (i = 1; i < 10; ++i)
  450. {
  451. sprintf(updateVar,"CTEST_EXTRA_UPDATES_%i",i);
  452. const char *updateVal = this->Makefile->GetDefinition(updateVar);
  453. if ( updateVal )
  454. {
  455. if ( this->UpdateCmd.empty() )
  456. {
  457. cmSystemTools::Error(updateVar,
  458. " specified without specifying CTEST_CVS_COMMAND.");
  459. return 12;
  460. }
  461. this->ExtraUpdates.push_back(updateVal);
  462. }
  463. }
  464. // in order to backup and restore we also must have the cvs root
  465. if (this->Backup && this->CVSCheckOut.empty())
  466. {
  467. cmSystemTools::Error(
  468. "Backup was requested without specifying CTEST_CVS_CHECKOUT.");
  469. return 3;
  470. }
  471. // make sure the required info is here
  472. if (this->SourceDir.empty() ||
  473. this->BinaryDir.empty() ||
  474. this->CTestCmd.empty())
  475. {
  476. std::string msg = "CTEST_SOURCE_DIRECTORY = ";
  477. msg += (!this->SourceDir.empty()) ? this->SourceDir.c_str() : "(Null)";
  478. msg += "\nCTEST_BINARY_DIRECTORY = ";
  479. msg += (!this->BinaryDir.empty()) ? this->BinaryDir.c_str() : "(Null)";
  480. msg += "\nCTEST_COMMAND = ";
  481. msg += (!this->CTestCmd.empty()) ? this->CTestCmd.c_str() : "(Null)";
  482. cmSystemTools::Error(
  483. "Some required settings in the configuration file were missing:\n",
  484. msg.c_str());
  485. return 4;
  486. }
  487. // if the dashboard root isn't specified then we can compute it from the
  488. // this->SourceDir
  489. if (this->CTestRoot.empty() )
  490. {
  491. this->CTestRoot = cmSystemTools::GetFilenamePath(this->SourceDir).c_str();
  492. }
  493. // the script may override the minimum continuous interval
  494. if (minInterval)
  495. {
  496. this->MinimumInterval = 60 * atof(minInterval);
  497. }
  498. if (contDuration)
  499. {
  500. this->ContinuousDuration = 60.0 * atof(contDuration);
  501. }
  502. this->UpdateElapsedTime();
  503. return 0;
  504. }
  505. //----------------------------------------------------------------------
  506. void cmCTestScriptHandler::SleepInSeconds(unsigned int secondsToWait)
  507. {
  508. #if defined(_WIN32)
  509. Sleep(1000*secondsToWait);
  510. #else
  511. sleep(secondsToWait);
  512. #endif
  513. }
  514. //----------------------------------------------------------------------
  515. // run a specific script
  516. int cmCTestScriptHandler::RunConfigurationScript
  517. (const std::string& total_script_arg, bool pscope)
  518. {
  519. #ifdef CMAKE_BUILD_WITH_CMAKE
  520. cmSystemTools::SaveRestoreEnvironment sre;
  521. #endif
  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. }