cmCTestScriptHandler.cxx 31 KB

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