cmCTestScriptHandler.cxx 33 KB

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