cmCTestScriptHandler.cxx 33 KB

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