cmCTestScriptHandler.cxx 31 KB

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