cmCTestScriptHandler.cxx 33 KB

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