cmCTestScriptHandler.cxx 33 KB

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