cmCTestScriptHandler.cxx 32 KB

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