cmCTestScriptHandler.cxx 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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->InitialCache = "";
  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. //*** need to make sure this does not have the current script ***
  207. for(size_t i=1; i < initArgs.size(); ++i)
  208. {
  209. argv.push_back(initArgs[i].c_str());
  210. }
  211. argv.push_back(0);
  212. // Now create process object
  213. cmsysProcess* cp = cmsysProcess_New();
  214. cmsysProcess_SetCommand(cp, &*argv.begin());
  215. //cmsysProcess_SetWorkingDirectory(cp, dir);
  216. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  217. //cmsysProcess_SetTimeout(cp, timeout);
  218. cmsysProcess_Execute(cp);
  219. std::vector<char> out;
  220. std::vector<char> err;
  221. std::string line;
  222. int pipe = cmSystemTools::WaitForLine(cp, line, 100.0, out, err);
  223. while(pipe != cmsysProcess_Pipe_None)
  224. {
  225. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Output: "
  226. << line << "\n");
  227. if(pipe == cmsysProcess_Pipe_STDERR)
  228. {
  229. cmCTestLog(this->CTest, ERROR_MESSAGE, line << "\n");
  230. }
  231. else if(pipe == cmsysProcess_Pipe_STDOUT)
  232. {
  233. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, line << "\n");
  234. }
  235. pipe = cmSystemTools::WaitForLine(cp, line, 100, out, err);
  236. }
  237. // Properly handle output of the build command
  238. cmsysProcess_WaitForExit(cp, 0);
  239. int result = cmsysProcess_GetState(cp);
  240. int retVal = 0;
  241. bool failed = false;
  242. if(result == cmsysProcess_State_Exited)
  243. {
  244. retVal = cmsysProcess_GetExitValue(cp);
  245. }
  246. else if(result == cmsysProcess_State_Exception)
  247. {
  248. retVal = cmsysProcess_GetExitException(cp);
  249. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tThere was an exception: "
  250. << cmsysProcess_GetExceptionString(cp) << " " <<
  251. retVal << std::endl);
  252. failed = true;
  253. }
  254. else if(result == cmsysProcess_State_Expired)
  255. {
  256. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tThere was a timeout"
  257. << std::endl);
  258. failed = true;
  259. }
  260. else if(result == cmsysProcess_State_Error)
  261. {
  262. cmCTestLog(this->CTest, ERROR_MESSAGE, "\tError executing ctest: "
  263. << cmsysProcess_GetErrorString(cp) << std::endl);
  264. failed = true;
  265. }
  266. if(failed)
  267. {
  268. cmOStringStream message;
  269. message << "Error running command: [";
  270. message << result << "] ";
  271. for(std::vector<const char*>::iterator i = argv.begin();
  272. i != argv.end(); ++i)
  273. {
  274. if(*i)
  275. {
  276. message << *i << " ";
  277. }
  278. }
  279. cmCTestLog(this->CTest, ERROR_MESSAGE,
  280. message.str() << argv[0] << std::endl);
  281. return -1;
  282. }
  283. return retVal;
  284. }
  285. void cmCTestScriptHandler::CreateCMake()
  286. {
  287. // create a cmake instance to read the configuration script
  288. if (this->CMake)
  289. {
  290. delete this->CMake;
  291. delete this->GlobalGenerator;
  292. delete this->LocalGenerator;
  293. }
  294. this->CMake = new cmake;
  295. this->CMake->AddCMakePaths();
  296. this->GlobalGenerator = new cmGlobalGenerator;
  297. this->GlobalGenerator->SetCMakeInstance(this->CMake);
  298. this->LocalGenerator = this->GlobalGenerator->CreateLocalGenerator();
  299. this->Makefile = this->LocalGenerator->GetMakefile();
  300. // Set CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_BINARY_DIR.
  301. // Also, some commands need Makefile->GetCurrentDirectory().
  302. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  303. this->Makefile->SetStartDirectory(cwd.c_str());
  304. this->Makefile->SetStartOutputDirectory(cwd.c_str());
  305. // remove all cmake commands which are not scriptable, since they can't be
  306. // used in ctest scripts
  307. this->CMake->RemoveUnscriptableCommands();
  308. // add any ctest specific commands, probably should have common superclass
  309. // for ctest commands to clean this up. If a couple more commands are
  310. // created with the same format lets do that - ken
  311. this->AddCTestCommand(new cmCTestBuildCommand);
  312. this->AddCTestCommand(new cmCTestConfigureCommand);
  313. this->AddCTestCommand(new cmCTestCoverageCommand);
  314. this->AddCTestCommand(new cmCTestEmptyBinaryDirectoryCommand);
  315. this->AddCTestCommand(new cmCTestMemCheckCommand);
  316. this->AddCTestCommand(new cmCTestReadCustomFilesCommand);
  317. this->AddCTestCommand(new cmCTestRunScriptCommand);
  318. this->AddCTestCommand(new cmCTestSleepCommand);
  319. this->AddCTestCommand(new cmCTestStartCommand);
  320. this->AddCTestCommand(new cmCTestSubmitCommand);
  321. this->AddCTestCommand(new cmCTestTestCommand);
  322. this->AddCTestCommand(new cmCTestUpdateCommand);
  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. return 2;
  397. }
  398. return 0;
  399. }
  400. //----------------------------------------------------------------------
  401. // extract variabels from the script to set ivars
  402. int cmCTestScriptHandler::ExtractVariables()
  403. {
  404. // Temporary variables
  405. const char* minInterval;
  406. const char* contDuration;
  407. this->SourceDir
  408. = this->Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
  409. this->BinaryDir
  410. = this->Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");
  411. // add in translations for src and bin
  412. cmSystemTools::AddKeepPath(this->SourceDir.c_str());
  413. cmSystemTools::AddKeepPath(this->BinaryDir.c_str());
  414. this->CTestCmd
  415. = this->Makefile->GetSafeDefinition("CTEST_COMMAND");
  416. this->CVSCheckOut
  417. = this->Makefile->GetSafeDefinition("CTEST_CVS_CHECKOUT");
  418. this->CTestRoot
  419. = this->Makefile->GetSafeDefinition("CTEST_DASHBOARD_ROOT");
  420. this->UpdateCmd
  421. = this->Makefile->GetSafeDefinition("CTEST_UPDATE_COMMAND");
  422. if ( this->UpdateCmd.empty() )
  423. {
  424. this->UpdateCmd
  425. = this->Makefile->GetSafeDefinition("CTEST_CVS_COMMAND");
  426. }
  427. this->CTestEnv
  428. = this->Makefile->GetSafeDefinition("CTEST_ENVIRONMENT");
  429. this->InitialCache
  430. = this->Makefile->GetSafeDefinition("CTEST_INITIAL_CACHE");
  431. this->CMakeCmd
  432. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_COMMAND");
  433. this->CMOutFile
  434. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_OUTPUT_FILE_NAME");
  435. this->Backup
  436. = this->Makefile->IsOn("CTEST_BACKUP_AND_RESTORE");
  437. this->EmptyBinDir
  438. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY");
  439. this->EmptyBinDirOnce
  440. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY_ONCE");
  441. minInterval
  442. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_MINIMUM_INTERVAL");
  443. contDuration
  444. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_DURATION");
  445. char updateVar[40];
  446. int i;
  447. for (i = 1; i < 10; ++i)
  448. {
  449. sprintf(updateVar,"CTEST_EXTRA_UPDATES_%i",i);
  450. const char *updateVal = this->Makefile->GetDefinition(updateVar);
  451. if ( updateVal )
  452. {
  453. if ( this->UpdateCmd.empty() )
  454. {
  455. cmSystemTools::Error(updateVar,
  456. " specified without specifying CTEST_CVS_COMMAND.");
  457. return 12;
  458. }
  459. this->ExtraUpdates.push_back(updateVal);
  460. }
  461. }
  462. // in order to backup and restore we also must have the cvs root
  463. if (this->Backup && this->CVSCheckOut.empty())
  464. {
  465. cmSystemTools::Error(
  466. "Backup was requested without specifying CTEST_CVS_CHECKOUT.");
  467. return 3;
  468. }
  469. // make sure the required info is here
  470. if (this->SourceDir.empty() ||
  471. this->BinaryDir.empty() ||
  472. this->CTestCmd.empty())
  473. {
  474. std::string msg = "CTEST_SOURCE_DIRECTORY = ";
  475. msg += (!this->SourceDir.empty()) ? this->SourceDir.c_str() : "(Null)";
  476. msg += "\nCTEST_BINARY_DIRECTORY = ";
  477. msg += (!this->BinaryDir.empty()) ? this->BinaryDir.c_str() : "(Null)";
  478. msg += "\nCTEST_COMMAND = ";
  479. msg += (!this->CTestCmd.empty()) ? this->CTestCmd.c_str() : "(Null)";
  480. cmSystemTools::Error(
  481. "Some required settings in the configuration file were missing:\n",
  482. msg.c_str());
  483. return 4;
  484. }
  485. // if the dashboard root isn't specified then we can compute it from the
  486. // this->SourceDir
  487. if (this->CTestRoot.empty() )
  488. {
  489. this->CTestRoot = cmSystemTools::GetFilenamePath(this->SourceDir).c_str();
  490. }
  491. // the script may override the minimum continuous interval
  492. if (minInterval)
  493. {
  494. this->MinimumInterval = 60 * atof(minInterval);
  495. }
  496. if (contDuration)
  497. {
  498. this->ContinuousDuration = 60.0 * atof(contDuration);
  499. }
  500. this->UpdateElapsedTime();
  501. return 0;
  502. }
  503. //----------------------------------------------------------------------
  504. void cmCTestScriptHandler::SleepInSeconds(unsigned int secondsToWait)
  505. {
  506. #if defined(_WIN32)
  507. Sleep(1000*secondsToWait);
  508. #else
  509. sleep(secondsToWait);
  510. #endif
  511. }
  512. //----------------------------------------------------------------------
  513. // run a specific script
  514. int cmCTestScriptHandler::RunConfigurationScript
  515. (const std::string& total_script_arg, bool pscope)
  516. {
  517. int result;
  518. this->ScriptStartTime =
  519. cmSystemTools::GetTime();
  520. // read in the script
  521. if (pscope)
  522. {
  523. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  524. "Reading Script: " << total_script_arg << std::endl);
  525. result = this->ReadInScript(total_script_arg);
  526. }
  527. else
  528. {
  529. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  530. "Executing Script: " << total_script_arg << std::endl);
  531. result = this->ExecuteScript(total_script_arg);
  532. }
  533. if (result)
  534. {
  535. return result;
  536. }
  537. // only run the curent script if we should
  538. if (this->Makefile && this->Makefile->IsOn("CTEST_RUN_CURRENT_SCRIPT"))
  539. {
  540. return this->RunCurrentScript();
  541. }
  542. return result;
  543. }
  544. //----------------------------------------------------------------------
  545. int cmCTestScriptHandler::RunCurrentScript()
  546. {
  547. int result;
  548. // do not run twice
  549. this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", false);
  550. // no popup widows
  551. cmSystemTools::SetRunCommandHideConsole(true);
  552. // extract the vars from the cache and store in ivars
  553. result = this->ExtractVariables();
  554. if (result)
  555. {
  556. return result;
  557. }
  558. // set any environment variables
  559. if (!this->CTestEnv.empty())
  560. {
  561. std::vector<std::string> envArgs;
  562. cmSystemTools::ExpandListArgument(this->CTestEnv.c_str(),envArgs);
  563. // for each variable/argument do a putenv
  564. for (unsigned i = 0; i < envArgs.size(); ++i)
  565. {
  566. cmSystemTools::PutEnv(envArgs[i].c_str());
  567. }
  568. }
  569. // now that we have done most of the error checking finally run the
  570. // dashboard, we may be asked to repeatedly run this dashboard, such as
  571. // for a continuous, do we ned to run it more than once?
  572. if ( this->ContinuousDuration >= 0 )
  573. {
  574. this->UpdateElapsedTime();
  575. double ending_time = cmSystemTools::GetTime() + this->ContinuousDuration;
  576. if (this->EmptyBinDirOnce)
  577. {
  578. this->EmptyBinDir = true;
  579. }
  580. do
  581. {
  582. double interval = cmSystemTools::GetTime();
  583. result = this->RunConfigurationDashboard();
  584. interval = cmSystemTools::GetTime() - interval;
  585. if (interval < this->MinimumInterval)
  586. {
  587. this->SleepInSeconds(
  588. static_cast<unsigned int>(this->MinimumInterval - interval));
  589. }
  590. if (this->EmptyBinDirOnce)
  591. {
  592. this->EmptyBinDir = false;
  593. }
  594. }
  595. while (cmSystemTools::GetTime() < ending_time);
  596. }
  597. // otherwise just run it once
  598. else
  599. {
  600. result = this->RunConfigurationDashboard();
  601. }
  602. return result;
  603. }
  604. //----------------------------------------------------------------------
  605. int cmCTestScriptHandler::CheckOutSourceDir()
  606. {
  607. std::string command;
  608. std::string output;
  609. int retVal;
  610. bool res;
  611. if (!cmSystemTools::FileExists(this->SourceDir.c_str()) &&
  612. !this->CVSCheckOut.empty())
  613. {
  614. // we must now checkout the src dir
  615. output = "";
  616. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  617. "Run cvs: " << this->CVSCheckOut << std::endl);
  618. res = cmSystemTools::RunSingleCommand(this->CVSCheckOut.c_str(), &output,
  619. &retVal, this->CTestRoot.c_str(), this->HandlerVerbose,
  620. 0 /*this->TimeOut*/);
  621. if (!res || retVal != 0)
  622. {
  623. cmSystemTools::Error("Unable to perform cvs checkout:\n",
  624. output.c_str());
  625. return 6;
  626. }
  627. }
  628. return 0;
  629. }
  630. //----------------------------------------------------------------------
  631. int cmCTestScriptHandler::BackupDirectories()
  632. {
  633. int retVal;
  634. // compute the backup names
  635. this->BackupSourceDir = this->SourceDir;
  636. this->BackupSourceDir += "_CMakeBackup";
  637. this->BackupBinaryDir = this->BinaryDir;
  638. this->BackupBinaryDir += "_CMakeBackup";
  639. // backup the binary and src directories if requested
  640. if (this->Backup)
  641. {
  642. // if for some reason those directories exist then first delete them
  643. if (cmSystemTools::FileExists(this->BackupSourceDir.c_str()))
  644. {
  645. cmSystemTools::RemoveADirectory(this->BackupSourceDir.c_str());
  646. }
  647. if (cmSystemTools::FileExists(this->BackupBinaryDir.c_str()))
  648. {
  649. cmSystemTools::RemoveADirectory(this->BackupBinaryDir.c_str());
  650. }
  651. // first rename the src and binary directories
  652. rename(this->SourceDir.c_str(), this->BackupSourceDir.c_str());
  653. rename(this->BinaryDir.c_str(), this->BackupBinaryDir.c_str());
  654. // we must now checkout the src dir
  655. retVal = this->CheckOutSourceDir();
  656. if (retVal)
  657. {
  658. this->RestoreBackupDirectories();
  659. return retVal;
  660. }
  661. }
  662. return 0;
  663. }
  664. //----------------------------------------------------------------------
  665. int cmCTestScriptHandler::PerformExtraUpdates()
  666. {
  667. std::string command;
  668. std::string output;
  669. int retVal;
  670. bool res;
  671. // do an initial cvs update as required
  672. command = this->UpdateCmd;
  673. std::vector<cmStdString>::iterator it;
  674. for (it = this->ExtraUpdates.begin();
  675. it != this->ExtraUpdates.end();
  676. ++ it )
  677. {
  678. std::vector<std::string> cvsArgs;
  679. cmSystemTools::ExpandListArgument(it->c_str(),cvsArgs);
  680. if (cvsArgs.size() == 2)
  681. {
  682. std::string fullCommand = command;
  683. fullCommand += " update ";
  684. fullCommand += cvsArgs[1];
  685. output = "";
  686. retVal = 0;
  687. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run Update: "
  688. << fullCommand.c_str() << std::endl);
  689. res = cmSystemTools::RunSingleCommand(fullCommand.c_str(), &output,
  690. &retVal, cvsArgs[0].c_str(),
  691. this->HandlerVerbose, 0 /*this->TimeOut*/);
  692. if (!res || retVal != 0)
  693. {
  694. cmSystemTools::Error("Unable to perform extra updates:\n",
  695. it->c_str(), "\nWith output:\n",
  696. output.c_str());
  697. return 0;
  698. }
  699. }
  700. }
  701. return 0;
  702. }
  703. //----------------------------------------------------------------------
  704. // run a single dashboard entry
  705. int cmCTestScriptHandler::RunConfigurationDashboard()
  706. {
  707. // local variables
  708. std::string command;
  709. std::string output;
  710. int retVal;
  711. bool res;
  712. // make sure the src directory is there, if it isn't then we might be able
  713. // to check it out from cvs
  714. retVal = this->CheckOutSourceDir();
  715. if (retVal)
  716. {
  717. return retVal;
  718. }
  719. // backup the dirs if requested
  720. retVal = this->BackupDirectories();
  721. if (retVal)
  722. {
  723. return retVal;
  724. }
  725. // clear the binary directory?
  726. if (this->EmptyBinDir)
  727. {
  728. if ( !cmCTestScriptHandler::EmptyBinaryDirectory(
  729. this->BinaryDir.c_str()) )
  730. {
  731. cmCTestLog(this->CTest, ERROR_MESSAGE,
  732. "Problem removing the binary directory" << std::endl);
  733. }
  734. }
  735. // make sure the binary directory exists if it isn't the srcdir
  736. if (!cmSystemTools::FileExists(this->BinaryDir.c_str()) &&
  737. this->SourceDir != this->BinaryDir)
  738. {
  739. if (!cmSystemTools::MakeDirectory(this->BinaryDir.c_str()))
  740. {
  741. cmSystemTools::Error("Unable to create the binary directory:\n",
  742. this->BinaryDir.c_str());
  743. this->RestoreBackupDirectories();
  744. return 7;
  745. }
  746. }
  747. // if the binary directory and the source directory are the same,
  748. // and we are starting with an empty binary directory, then that means
  749. // we must check out the source tree
  750. if (this->EmptyBinDir && this->SourceDir == this->BinaryDir)
  751. {
  752. // make sure we have the required info
  753. if (this->CVSCheckOut.empty())
  754. {
  755. cmSystemTools::Error("You have specified the source and binary "
  756. "directories to be the same (an in source build). You have also "
  757. "specified that the binary directory is to be erased. This means "
  758. "that the source will have to be checked out from CVS. But you have "
  759. "not specified CTEST_CVS_CHECKOUT");
  760. return 8;
  761. }
  762. // we must now checkout the src dir
  763. retVal = this->CheckOutSourceDir();
  764. if (retVal)
  765. {
  766. this->RestoreBackupDirectories();
  767. return retVal;
  768. }
  769. }
  770. // backup the dirs if requested
  771. retVal = this->PerformExtraUpdates();
  772. if (retVal)
  773. {
  774. return retVal;
  775. }
  776. // put the initial cache into the bin dir
  777. if (!this->InitialCache.empty())
  778. {
  779. if (!this->WriteInitialCache(this->BinaryDir.c_str(),
  780. this->InitialCache.c_str()))
  781. {
  782. this->RestoreBackupDirectories();
  783. return 9;
  784. }
  785. }
  786. // do an initial cmake to setup the DartConfig file
  787. int cmakeFailed = 0;
  788. std::string cmakeFailedOuput;
  789. if (!this->CMakeCmd.empty())
  790. {
  791. command = this->CMakeCmd;
  792. command += " \"";
  793. command += this->SourceDir;
  794. output = "";
  795. command += "\"";
  796. retVal = 0;
  797. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run cmake command: "
  798. << command.c_str() << std::endl);
  799. res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  800. &retVal, this->BinaryDir.c_str(),
  801. this->HandlerVerbose, 0 /*this->TimeOut*/);
  802. if ( !this->CMOutFile.empty() )
  803. {
  804. std::string cmakeOutputFile = this->CMOutFile;
  805. if ( !cmSystemTools::FileIsFullPath(cmakeOutputFile.c_str()) )
  806. {
  807. cmakeOutputFile = this->BinaryDir + "/" + cmakeOutputFile;
  808. }
  809. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  810. "Write CMake output to file: " << cmakeOutputFile.c_str()
  811. << std::endl);
  812. cmGeneratedFileStream fout(cmakeOutputFile.c_str());
  813. if ( fout )
  814. {
  815. fout << output.c_str();
  816. }
  817. else
  818. {
  819. cmCTestLog(this->CTest, ERROR_MESSAGE,
  820. "Cannot open CMake output file: "
  821. << cmakeOutputFile.c_str() << " for writing" << std::endl);
  822. }
  823. }
  824. if (!res || retVal != 0)
  825. {
  826. // even if this fails continue to the next step
  827. cmakeFailed = 1;
  828. cmakeFailedOuput = output;
  829. }
  830. }
  831. // run ctest, it may be more than one command in here
  832. std::vector<std::string> ctestCommands;
  833. cmSystemTools::ExpandListArgument(this->CTestCmd,ctestCommands);
  834. // for each variable/argument do a putenv
  835. for (unsigned i = 0; i < ctestCommands.size(); ++i)
  836. {
  837. command = ctestCommands[i];
  838. output = "";
  839. retVal = 0;
  840. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run ctest command: "
  841. << command.c_str() << std::endl);
  842. res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  843. &retVal, this->BinaryDir.c_str(), this->HandlerVerbose,
  844. 0 /*this->TimeOut*/);
  845. // did something critical fail in ctest
  846. if (!res || cmakeFailed ||
  847. retVal & cmCTest::BUILD_ERRORS)
  848. {
  849. this->RestoreBackupDirectories();
  850. if (cmakeFailed)
  851. {
  852. cmCTestLog(this->CTest, ERROR_MESSAGE,
  853. "Unable to run cmake:" << std::endl
  854. << cmakeFailedOuput.c_str() << std::endl);
  855. return 10;
  856. }
  857. cmCTestLog(this->CTest, ERROR_MESSAGE,
  858. "Unable to run ctest:" << std::endl
  859. << "command: " << command.c_str() << std::endl
  860. << "output: " << output.c_str() << std::endl);
  861. if (!res)
  862. {
  863. return 11;
  864. }
  865. return retVal * 100;
  866. }
  867. }
  868. // if all was succesful, delete the backup dirs to free up disk space
  869. if (this->Backup)
  870. {
  871. cmSystemTools::RemoveADirectory(this->BackupSourceDir.c_str());
  872. cmSystemTools::RemoveADirectory(this->BackupBinaryDir.c_str());
  873. }
  874. return 0;
  875. }
  876. //-------------------------------------------------------------------------
  877. bool cmCTestScriptHandler::WriteInitialCache(const char* directory,
  878. const char* text)
  879. {
  880. std::string cacheFile = directory;
  881. cacheFile += "/CMakeCache.txt";
  882. cmGeneratedFileStream fout(cacheFile.c_str());
  883. if(!fout)
  884. {
  885. return false;
  886. }
  887. if (text!=0)
  888. {
  889. fout.write(text, strlen(text));
  890. }
  891. // Make sure the operating system has finished writing the file
  892. // before closing it. This will ensure the file is finished before
  893. // the check below.
  894. fout.flush();
  895. fout.close();
  896. return true;
  897. }
  898. //-------------------------------------------------------------------------
  899. void cmCTestScriptHandler::RestoreBackupDirectories()
  900. {
  901. // if we backed up the dirs and the build failed, then restore
  902. // the backed up dirs
  903. if (this->Backup)
  904. {
  905. // if for some reason those directories exist then first delete them
  906. if (cmSystemTools::FileExists(this->SourceDir.c_str()))
  907. {
  908. cmSystemTools::RemoveADirectory(this->SourceDir.c_str());
  909. }
  910. if (cmSystemTools::FileExists(this->BinaryDir.c_str()))
  911. {
  912. cmSystemTools::RemoveADirectory(this->BinaryDir.c_str());
  913. }
  914. // rename the src and binary directories
  915. rename(this->BackupSourceDir.c_str(), this->SourceDir.c_str());
  916. rename(this->BackupBinaryDir.c_str(), this->BinaryDir.c_str());
  917. }
  918. }
  919. bool cmCTestScriptHandler::RunScript(cmCTest* ctest, const char *sname,
  920. bool InProcess)
  921. {
  922. cmCTestScriptHandler* sh = new cmCTestScriptHandler();
  923. sh->SetCTestInstance(ctest);
  924. sh->AddConfigurationScript(sname,InProcess);
  925. sh->ProcessHandler();
  926. delete sh;
  927. return true;
  928. }
  929. bool cmCTestScriptHandler::EmptyBinaryDirectory(const char *sname)
  930. {
  931. // try to avoid deleting root
  932. if (!sname || strlen(sname) < 2)
  933. {
  934. return false;
  935. }
  936. // try to avoid deleting directories that we shouldn't
  937. std::string check = sname;
  938. check += "/CMakeCache.txt";
  939. if(cmSystemTools::FileExists(check.c_str()) &&
  940. !cmSystemTools::RemoveADirectory(sname))
  941. {
  942. return false;
  943. }
  944. return true;
  945. }
  946. //-------------------------------------------------------------------------
  947. double cmCTestScriptHandler::GetRemainingTimeAllowed()
  948. {
  949. if (!this->Makefile)
  950. {
  951. return 1.0e7;
  952. }
  953. const char *timelimitS
  954. = this->Makefile->GetDefinition("CTEST_TIME_LIMIT");
  955. if (!timelimitS)
  956. {
  957. return 1.0e7;
  958. }
  959. double timelimit = atof(timelimitS);
  960. return timelimit - cmSystemTools::GetTime() + this->ScriptStartTime;
  961. }