cmCTestScriptHandler.cxx 33 KB

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