cmCTestScriptHandler.cxx 32 KB

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