cmCTestScriptHandler.cxx 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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;
  307. this->GlobalGenerator->SetCMakeInstance(this->CMake);
  308. this->LocalGenerator = this->GlobalGenerator->MakeLocalGenerator();
  309. this->Makefile = this->LocalGenerator->GetMakefile();
  310. this->CMake->SetProgressCallback(ctestScriptProgressCallback, this->CTest);
  311. // Set CMAKE_CURRENT_SOURCE_DIR and CMAKE_CURRENT_BINARY_DIR.
  312. // Also, some commands need Makefile->GetCurrentSourceDirectory().
  313. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  314. this->Makefile->SetCurrentSourceDirectory(cwd);
  315. this->Makefile->SetCurrentBinaryDirectory(cwd);
  316. // remove all cmake commands which are not scriptable, since they can't be
  317. // used in ctest scripts
  318. this->CMake->GetState()->RemoveUnscriptableCommands();
  319. // add any ctest specific commands, probably should have common superclass
  320. // for ctest commands to clean this up. If a couple more commands are
  321. // created with the same format lets do that - ken
  322. this->AddCTestCommand(new cmCTestBuildCommand);
  323. this->AddCTestCommand(new cmCTestConfigureCommand);
  324. this->AddCTestCommand(new cmCTestCoverageCommand);
  325. this->AddCTestCommand(new cmCTestEmptyBinaryDirectoryCommand);
  326. this->AddCTestCommand(new cmCTestMemCheckCommand);
  327. this->AddCTestCommand(new cmCTestReadCustomFilesCommand);
  328. this->AddCTestCommand(new cmCTestRunScriptCommand);
  329. this->AddCTestCommand(new cmCTestSleepCommand);
  330. this->AddCTestCommand(new cmCTestStartCommand);
  331. this->AddCTestCommand(new cmCTestSubmitCommand);
  332. this->AddCTestCommand(new cmCTestTestCommand);
  333. this->AddCTestCommand(new cmCTestUpdateCommand);
  334. this->AddCTestCommand(new cmCTestUploadCommand);
  335. }
  336. //----------------------------------------------------------------------
  337. // this sets up some variables for the script to use, creates the required
  338. // cmake instance and generators, and then reads in the script
  339. int cmCTestScriptHandler::ReadInScript(const std::string& total_script_arg)
  340. {
  341. // Reset the error flag so that the script is read in no matter what
  342. cmSystemTools::ResetErrorOccuredFlag();
  343. // if the argument has a , in it then it needs to be broken into the fist
  344. // argument (which is the script) and the second argument which will be
  345. // passed into the scripts as S_ARG
  346. std::string script = total_script_arg;
  347. std::string script_arg;
  348. if (total_script_arg.find(",") != std::string::npos)
  349. {
  350. script = total_script_arg.substr(0,total_script_arg.find(","));
  351. script_arg = total_script_arg.substr(total_script_arg.find(",")+1);
  352. }
  353. // make sure the file exists
  354. if (!cmSystemTools::FileExists(script.c_str()))
  355. {
  356. cmSystemTools::Error("Cannot find file: ", script.c_str());
  357. return 1;
  358. }
  359. // read in the list file to fill the cache
  360. // create a cmake instance to read the configuration script
  361. this->CreateCMake();
  362. // set a variable with the path to the current script
  363. this->Makefile->AddDefinition("CTEST_SCRIPT_DIRECTORY",
  364. cmSystemTools::GetFilenamePath(script).c_str());
  365. this->Makefile->AddDefinition("CTEST_SCRIPT_NAME",
  366. cmSystemTools::GetFilenameName(script).c_str());
  367. this->Makefile->AddDefinition("CTEST_EXECUTABLE_NAME",
  368. cmSystemTools::GetCTestCommand().c_str());
  369. this->Makefile->AddDefinition("CMAKE_EXECUTABLE_NAME",
  370. cmSystemTools::GetCMakeCommand().c_str());
  371. this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", true);
  372. this->UpdateElapsedTime();
  373. // add the script arg if defined
  374. if (!script_arg.empty())
  375. {
  376. this->Makefile->AddDefinition("CTEST_SCRIPT_ARG", script_arg.c_str());
  377. }
  378. // always add a function blocker to update the elapsed time
  379. cmCTestScriptFunctionBlocker *f = new cmCTestScriptFunctionBlocker();
  380. f->CTestScriptHandler = this;
  381. this->Makefile->AddFunctionBlocker(f);
  382. /* Execute CTestScriptMode.cmake, which loads CMakeDetermineSystem and
  383. CMakeSystemSpecificInformation, so
  384. that variables like CMAKE_SYSTEM and also the search paths for libraries,
  385. header and executables are set correctly and can be used. Makes new-style
  386. ctest scripting easier. */
  387. std::string systemFile =
  388. this->Makefile->GetModulesFile("CTestScriptMode.cmake");
  389. if (!this->Makefile->ReadListFile(systemFile.c_str()) ||
  390. cmSystemTools::GetErrorOccuredFlag())
  391. {
  392. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error in read:"
  393. << systemFile << "\n");
  394. return 2;
  395. }
  396. // Add definitions of variables passed in on the command line:
  397. const std::map<std::string, std::string> &defs =
  398. this->CTest->GetDefinitions();
  399. for (std::map<std::string, std::string>::const_iterator it = defs.begin();
  400. it != defs.end(); ++it)
  401. {
  402. this->Makefile->AddDefinition(it->first, it->second.c_str());
  403. }
  404. // finally read in the script
  405. if (!this->Makefile->ReadListFile(script.c_str()) ||
  406. cmSystemTools::GetErrorOccuredFlag())
  407. {
  408. // Reset the error flag so that it can run more than
  409. // one script with an error when you use ctest_run_script.
  410. cmSystemTools::ResetErrorOccuredFlag();
  411. return 2;
  412. }
  413. return 0;
  414. }
  415. //----------------------------------------------------------------------
  416. // extract variabels from the script to set ivars
  417. int cmCTestScriptHandler::ExtractVariables()
  418. {
  419. // Temporary variables
  420. const char* minInterval;
  421. const char* contDuration;
  422. this->SourceDir
  423. = this->Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
  424. this->BinaryDir
  425. = this->Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");
  426. // add in translations for src and bin
  427. cmSystemTools::AddKeepPath(this->SourceDir);
  428. cmSystemTools::AddKeepPath(this->BinaryDir);
  429. this->CTestCmd
  430. = this->Makefile->GetSafeDefinition("CTEST_COMMAND");
  431. this->CVSCheckOut
  432. = this->Makefile->GetSafeDefinition("CTEST_CVS_CHECKOUT");
  433. this->CTestRoot
  434. = this->Makefile->GetSafeDefinition("CTEST_DASHBOARD_ROOT");
  435. this->UpdateCmd
  436. = this->Makefile->GetSafeDefinition("CTEST_UPDATE_COMMAND");
  437. if ( this->UpdateCmd.empty() )
  438. {
  439. this->UpdateCmd
  440. = this->Makefile->GetSafeDefinition("CTEST_CVS_COMMAND");
  441. }
  442. this->CTestEnv
  443. = this->Makefile->GetSafeDefinition("CTEST_ENVIRONMENT");
  444. this->InitialCache
  445. = this->Makefile->GetSafeDefinition("CTEST_INITIAL_CACHE");
  446. this->CMakeCmd
  447. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_COMMAND");
  448. this->CMOutFile
  449. = this->Makefile->GetSafeDefinition("CTEST_CMAKE_OUTPUT_FILE_NAME");
  450. this->Backup
  451. = this->Makefile->IsOn("CTEST_BACKUP_AND_RESTORE");
  452. this->EmptyBinDir
  453. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY");
  454. this->EmptyBinDirOnce
  455. = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY_ONCE");
  456. minInterval
  457. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_MINIMUM_INTERVAL");
  458. contDuration
  459. = this->Makefile->GetDefinition("CTEST_CONTINUOUS_DURATION");
  460. char updateVar[40];
  461. int i;
  462. for (i = 1; i < 10; ++i)
  463. {
  464. sprintf(updateVar,"CTEST_EXTRA_UPDATES_%i",i);
  465. const char *updateVal = this->Makefile->GetDefinition(updateVar);
  466. if ( updateVal )
  467. {
  468. if ( this->UpdateCmd.empty() )
  469. {
  470. cmSystemTools::Error(updateVar,
  471. " specified without specifying CTEST_CVS_COMMAND.");
  472. return 12;
  473. }
  474. this->ExtraUpdates.push_back(updateVal);
  475. }
  476. }
  477. // in order to backup and restore we also must have the cvs root
  478. if (this->Backup && this->CVSCheckOut.empty())
  479. {
  480. cmSystemTools::Error(
  481. "Backup was requested without specifying CTEST_CVS_CHECKOUT.");
  482. return 3;
  483. }
  484. // make sure the required info is here
  485. if (this->SourceDir.empty() ||
  486. this->BinaryDir.empty() ||
  487. this->CTestCmd.empty())
  488. {
  489. std::string msg = "CTEST_SOURCE_DIRECTORY = ";
  490. msg += (!this->SourceDir.empty()) ? this->SourceDir.c_str() : "(Null)";
  491. msg += "\nCTEST_BINARY_DIRECTORY = ";
  492. msg += (!this->BinaryDir.empty()) ? this->BinaryDir.c_str() : "(Null)";
  493. msg += "\nCTEST_COMMAND = ";
  494. msg += (!this->CTestCmd.empty()) ? this->CTestCmd.c_str() : "(Null)";
  495. cmSystemTools::Error(
  496. "Some required settings in the configuration file were missing:\n",
  497. msg.c_str());
  498. return 4;
  499. }
  500. // if the dashboard root isn't specified then we can compute it from the
  501. // this->SourceDir
  502. if (this->CTestRoot.empty() )
  503. {
  504. this->CTestRoot = cmSystemTools::GetFilenamePath(this->SourceDir).c_str();
  505. }
  506. // the script may override the minimum continuous interval
  507. if (minInterval)
  508. {
  509. this->MinimumInterval = 60 * atof(minInterval);
  510. }
  511. if (contDuration)
  512. {
  513. this->ContinuousDuration = 60.0 * atof(contDuration);
  514. }
  515. this->UpdateElapsedTime();
  516. return 0;
  517. }
  518. //----------------------------------------------------------------------
  519. void cmCTestScriptHandler::SleepInSeconds(unsigned int secondsToWait)
  520. {
  521. #if defined(_WIN32)
  522. Sleep(1000*secondsToWait);
  523. #else
  524. sleep(secondsToWait);
  525. #endif
  526. }
  527. //----------------------------------------------------------------------
  528. // run a specific script
  529. int cmCTestScriptHandler::RunConfigurationScript
  530. (const std::string& total_script_arg, bool pscope)
  531. {
  532. #ifdef CMAKE_BUILD_WITH_CMAKE
  533. cmSystemTools::SaveRestoreEnvironment sre;
  534. #endif
  535. int result;
  536. this->ScriptStartTime =
  537. cmSystemTools::GetTime();
  538. // read in the script
  539. if (pscope)
  540. {
  541. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  542. "Reading Script: " << total_script_arg << std::endl);
  543. result = this->ReadInScript(total_script_arg);
  544. }
  545. else
  546. {
  547. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  548. "Executing Script: " << total_script_arg << std::endl);
  549. result = this->ExecuteScript(total_script_arg);
  550. }
  551. if (result)
  552. {
  553. return result;
  554. }
  555. // only run the curent script if we should
  556. if (this->Makefile && this->Makefile->IsOn("CTEST_RUN_CURRENT_SCRIPT"))
  557. {
  558. return this->RunCurrentScript();
  559. }
  560. return result;
  561. }
  562. //----------------------------------------------------------------------
  563. int cmCTestScriptHandler::RunCurrentScript()
  564. {
  565. int result;
  566. // do not run twice
  567. this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", false);
  568. // no popup widows
  569. cmSystemTools::SetRunCommandHideConsole(true);
  570. // extract the vars from the cache and store in ivars
  571. result = this->ExtractVariables();
  572. if (result)
  573. {
  574. return result;
  575. }
  576. // set any environment variables
  577. if (!this->CTestEnv.empty())
  578. {
  579. std::vector<std::string> envArgs;
  580. cmSystemTools::ExpandListArgument(this->CTestEnv,envArgs);
  581. cmSystemTools::AppendEnv(envArgs);
  582. }
  583. // now that we have done most of the error checking finally run the
  584. // dashboard, we may be asked to repeatedly run this dashboard, such as
  585. // for a continuous, do we ned to run it more than once?
  586. if ( this->ContinuousDuration >= 0 )
  587. {
  588. this->UpdateElapsedTime();
  589. double ending_time = cmSystemTools::GetTime() + this->ContinuousDuration;
  590. if (this->EmptyBinDirOnce)
  591. {
  592. this->EmptyBinDir = true;
  593. }
  594. do
  595. {
  596. double interval = cmSystemTools::GetTime();
  597. result = this->RunConfigurationDashboard();
  598. interval = cmSystemTools::GetTime() - interval;
  599. if (interval < this->MinimumInterval)
  600. {
  601. this->SleepInSeconds(
  602. static_cast<unsigned int>(this->MinimumInterval - interval));
  603. }
  604. if (this->EmptyBinDirOnce)
  605. {
  606. this->EmptyBinDir = false;
  607. }
  608. }
  609. while (cmSystemTools::GetTime() < ending_time);
  610. }
  611. // otherwise just run it once
  612. else
  613. {
  614. result = this->RunConfigurationDashboard();
  615. }
  616. return result;
  617. }
  618. //----------------------------------------------------------------------
  619. int cmCTestScriptHandler::CheckOutSourceDir()
  620. {
  621. std::string command;
  622. std::string output;
  623. int retVal;
  624. bool res;
  625. if (!cmSystemTools::FileExists(this->SourceDir.c_str()) &&
  626. !this->CVSCheckOut.empty())
  627. {
  628. // we must now checkout the src dir
  629. output = "";
  630. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  631. "Run cvs: " << this->CVSCheckOut << std::endl);
  632. res = cmSystemTools::RunSingleCommand(
  633. this->CVSCheckOut.c_str(), &output, &output,
  634. &retVal, this->CTestRoot.c_str(), this->HandlerVerbose,
  635. 0 /*this->TimeOut*/);
  636. if (!res || retVal != 0)
  637. {
  638. cmSystemTools::Error("Unable to perform cvs checkout:\n",
  639. output.c_str());
  640. return 6;
  641. }
  642. }
  643. return 0;
  644. }
  645. //----------------------------------------------------------------------
  646. int cmCTestScriptHandler::BackupDirectories()
  647. {
  648. int retVal;
  649. // compute the backup names
  650. this->BackupSourceDir = this->SourceDir;
  651. this->BackupSourceDir += "_CMakeBackup";
  652. this->BackupBinaryDir = this->BinaryDir;
  653. this->BackupBinaryDir += "_CMakeBackup";
  654. // backup the binary and src directories if requested
  655. if (this->Backup)
  656. {
  657. // if for some reason those directories exist then first delete them
  658. if (cmSystemTools::FileExists(this->BackupSourceDir.c_str()))
  659. {
  660. cmSystemTools::RemoveADirectory(this->BackupSourceDir);
  661. }
  662. if (cmSystemTools::FileExists(this->BackupBinaryDir.c_str()))
  663. {
  664. cmSystemTools::RemoveADirectory(this->BackupBinaryDir);
  665. }
  666. // first rename the src and binary directories
  667. rename(this->SourceDir.c_str(), this->BackupSourceDir.c_str());
  668. rename(this->BinaryDir.c_str(), this->BackupBinaryDir.c_str());
  669. // we must now checkout the src dir
  670. retVal = this->CheckOutSourceDir();
  671. if (retVal)
  672. {
  673. this->RestoreBackupDirectories();
  674. return retVal;
  675. }
  676. }
  677. return 0;
  678. }
  679. //----------------------------------------------------------------------
  680. int cmCTestScriptHandler::PerformExtraUpdates()
  681. {
  682. std::string command;
  683. std::string output;
  684. int retVal;
  685. bool res;
  686. // do an initial cvs update as required
  687. command = this->UpdateCmd;
  688. std::vector<std::string>::iterator it;
  689. for (it = this->ExtraUpdates.begin();
  690. it != this->ExtraUpdates.end();
  691. ++ it )
  692. {
  693. std::vector<std::string> cvsArgs;
  694. cmSystemTools::ExpandListArgument(*it,cvsArgs);
  695. if (cvsArgs.size() == 2)
  696. {
  697. std::string fullCommand = command;
  698. fullCommand += " update ";
  699. fullCommand += cvsArgs[1];
  700. output = "";
  701. retVal = 0;
  702. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run Update: "
  703. << fullCommand << std::endl);
  704. res = cmSystemTools::RunSingleCommand(
  705. fullCommand.c_str(), &output, &output,
  706. &retVal, cvsArgs[0].c_str(),
  707. this->HandlerVerbose, 0 /*this->TimeOut*/);
  708. if (!res || retVal != 0)
  709. {
  710. cmSystemTools::Error("Unable to perform extra updates:\n",
  711. it->c_str(), "\nWith output:\n",
  712. output.c_str());
  713. return 0;
  714. }
  715. }
  716. }
  717. return 0;
  718. }
  719. //----------------------------------------------------------------------
  720. // run a single dashboard entry
  721. int cmCTestScriptHandler::RunConfigurationDashboard()
  722. {
  723. // local variables
  724. std::string command;
  725. std::string output;
  726. int retVal;
  727. bool res;
  728. // make sure the src directory is there, if it isn't then we might be able
  729. // to check it out from cvs
  730. retVal = this->CheckOutSourceDir();
  731. if (retVal)
  732. {
  733. return retVal;
  734. }
  735. // backup the dirs if requested
  736. retVal = this->BackupDirectories();
  737. if (retVal)
  738. {
  739. return retVal;
  740. }
  741. // clear the binary directory?
  742. if (this->EmptyBinDir)
  743. {
  744. if ( !cmCTestScriptHandler::EmptyBinaryDirectory(
  745. this->BinaryDir.c_str()) )
  746. {
  747. cmCTestLog(this->CTest, ERROR_MESSAGE,
  748. "Problem removing the binary directory" << std::endl);
  749. }
  750. }
  751. // make sure the binary directory exists if it isn't the srcdir
  752. if (!cmSystemTools::FileExists(this->BinaryDir.c_str()) &&
  753. this->SourceDir != this->BinaryDir)
  754. {
  755. if (!cmSystemTools::MakeDirectory(this->BinaryDir.c_str()))
  756. {
  757. cmSystemTools::Error("Unable to create the binary directory:\n",
  758. this->BinaryDir.c_str());
  759. this->RestoreBackupDirectories();
  760. return 7;
  761. }
  762. }
  763. // if the binary directory and the source directory are the same,
  764. // and we are starting with an empty binary directory, then that means
  765. // we must check out the source tree
  766. if (this->EmptyBinDir && this->SourceDir == this->BinaryDir)
  767. {
  768. // make sure we have the required info
  769. if (this->CVSCheckOut.empty())
  770. {
  771. cmSystemTools::Error("You have specified the source and binary "
  772. "directories to be the same (an in source build). You have also "
  773. "specified that the binary directory is to be erased. This means "
  774. "that the source will have to be checked out from CVS. But you have "
  775. "not specified CTEST_CVS_CHECKOUT");
  776. return 8;
  777. }
  778. // we must now checkout the src dir
  779. retVal = this->CheckOutSourceDir();
  780. if (retVal)
  781. {
  782. this->RestoreBackupDirectories();
  783. return retVal;
  784. }
  785. }
  786. // backup the dirs if requested
  787. retVal = this->PerformExtraUpdates();
  788. if (retVal)
  789. {
  790. return retVal;
  791. }
  792. // put the initial cache into the bin dir
  793. if (!this->InitialCache.empty())
  794. {
  795. if (!this->WriteInitialCache(this->BinaryDir.c_str(),
  796. this->InitialCache.c_str()))
  797. {
  798. this->RestoreBackupDirectories();
  799. return 9;
  800. }
  801. }
  802. // do an initial cmake to setup the DartConfig file
  803. int cmakeFailed = 0;
  804. std::string cmakeFailedOuput;
  805. if (!this->CMakeCmd.empty())
  806. {
  807. command = this->CMakeCmd;
  808. command += " \"";
  809. command += this->SourceDir;
  810. output = "";
  811. command += "\"";
  812. retVal = 0;
  813. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run cmake command: "
  814. << command << std::endl);
  815. res = cmSystemTools::RunSingleCommand(
  816. command.c_str(), &output, &output,
  817. &retVal, this->BinaryDir.c_str(),
  818. this->HandlerVerbose, 0 /*this->TimeOut*/);
  819. if ( !this->CMOutFile.empty() )
  820. {
  821. std::string cmakeOutputFile = this->CMOutFile;
  822. if ( !cmSystemTools::FileIsFullPath(cmakeOutputFile.c_str()) )
  823. {
  824. cmakeOutputFile = this->BinaryDir + "/" + cmakeOutputFile;
  825. }
  826. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  827. "Write CMake output to file: " << cmakeOutputFile
  828. << std::endl);
  829. cmGeneratedFileStream fout(cmakeOutputFile.c_str());
  830. if ( fout )
  831. {
  832. fout << output.c_str();
  833. }
  834. else
  835. {
  836. cmCTestLog(this->CTest, ERROR_MESSAGE,
  837. "Cannot open CMake output file: "
  838. << cmakeOutputFile << " for writing" << std::endl);
  839. }
  840. }
  841. if (!res || retVal != 0)
  842. {
  843. // even if this fails continue to the next step
  844. cmakeFailed = 1;
  845. cmakeFailedOuput = output;
  846. }
  847. }
  848. // run ctest, it may be more than one command in here
  849. std::vector<std::string> ctestCommands;
  850. cmSystemTools::ExpandListArgument(this->CTestCmd,ctestCommands);
  851. // for each variable/argument do a putenv
  852. for (unsigned i = 0; i < ctestCommands.size(); ++i)
  853. {
  854. command = ctestCommands[i];
  855. output = "";
  856. retVal = 0;
  857. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run ctest command: "
  858. << command << std::endl);
  859. res = cmSystemTools::RunSingleCommand(
  860. command.c_str(), &output, &output,
  861. &retVal, this->BinaryDir.c_str(), this->HandlerVerbose,
  862. 0 /*this->TimeOut*/);
  863. // did something critical fail in ctest
  864. if (!res || cmakeFailed ||
  865. retVal & cmCTest::BUILD_ERRORS)
  866. {
  867. this->RestoreBackupDirectories();
  868. if (cmakeFailed)
  869. {
  870. cmCTestLog(this->CTest, ERROR_MESSAGE,
  871. "Unable to run cmake:" << std::endl
  872. << cmakeFailedOuput << std::endl);
  873. return 10;
  874. }
  875. cmCTestLog(this->CTest, ERROR_MESSAGE,
  876. "Unable to run ctest:" << std::endl
  877. << "command: " << command << std::endl
  878. << "output: " << output << std::endl);
  879. if (!res)
  880. {
  881. return 11;
  882. }
  883. return retVal * 100;
  884. }
  885. }
  886. // if all was succesful, delete the backup dirs to free up disk space
  887. if (this->Backup)
  888. {
  889. cmSystemTools::RemoveADirectory(this->BackupSourceDir);
  890. cmSystemTools::RemoveADirectory(this->BackupBinaryDir);
  891. }
  892. return 0;
  893. }
  894. //-------------------------------------------------------------------------
  895. bool cmCTestScriptHandler::WriteInitialCache(const char* directory,
  896. const char* text)
  897. {
  898. std::string cacheFile = directory;
  899. cacheFile += "/CMakeCache.txt";
  900. cmGeneratedFileStream fout(cacheFile.c_str());
  901. if(!fout)
  902. {
  903. return false;
  904. }
  905. if (text!=0)
  906. {
  907. fout.write(text, strlen(text));
  908. }
  909. // Make sure the operating system has finished writing the file
  910. // before closing it. This will ensure the file is finished before
  911. // the check below.
  912. fout.flush();
  913. fout.close();
  914. return true;
  915. }
  916. //-------------------------------------------------------------------------
  917. void cmCTestScriptHandler::RestoreBackupDirectories()
  918. {
  919. // if we backed up the dirs and the build failed, then restore
  920. // the backed up dirs
  921. if (this->Backup)
  922. {
  923. // if for some reason those directories exist then first delete them
  924. if (cmSystemTools::FileExists(this->SourceDir.c_str()))
  925. {
  926. cmSystemTools::RemoveADirectory(this->SourceDir);
  927. }
  928. if (cmSystemTools::FileExists(this->BinaryDir.c_str()))
  929. {
  930. cmSystemTools::RemoveADirectory(this->BinaryDir);
  931. }
  932. // rename the src and binary directories
  933. rename(this->BackupSourceDir.c_str(), this->SourceDir.c_str());
  934. rename(this->BackupBinaryDir.c_str(), this->BinaryDir.c_str());
  935. }
  936. }
  937. bool cmCTestScriptHandler::RunScript(cmCTest* ctest, const char *sname,
  938. bool InProcess, int* returnValue)
  939. {
  940. cmCTestScriptHandler* sh = new cmCTestScriptHandler();
  941. sh->SetCTestInstance(ctest);
  942. sh->AddConfigurationScript(sname,InProcess);
  943. int res = sh->ProcessHandler();
  944. if(returnValue)
  945. {
  946. *returnValue = res;
  947. }
  948. delete sh;
  949. return true;
  950. }
  951. bool cmCTestScriptHandler::EmptyBinaryDirectory(const char *sname)
  952. {
  953. // try to avoid deleting root
  954. if (!sname || strlen(sname) < 2)
  955. {
  956. return false;
  957. }
  958. // consider non existing target directory a success
  959. if(!cmSystemTools::FileExists(sname))
  960. {
  961. return true;
  962. }
  963. // try to avoid deleting directories that we shouldn't
  964. std::string check = sname;
  965. check += "/CMakeCache.txt";
  966. if(!cmSystemTools::FileExists(check.c_str()))
  967. {
  968. return false;
  969. }
  970. for(int i = 0; i < 5; ++i)
  971. {
  972. if(TryToRemoveBinaryDirectoryOnce(sname))
  973. {
  974. return true;
  975. }
  976. cmSystemTools::Delay(100);
  977. }
  978. return false;
  979. }
  980. //-------------------------------------------------------------------------
  981. bool cmCTestScriptHandler::TryToRemoveBinaryDirectoryOnce(
  982. const std::string& directoryPath)
  983. {
  984. cmsys::Directory directory;
  985. directory.Load(directoryPath);
  986. for(unsigned long i = 0; i < directory.GetNumberOfFiles(); ++i)
  987. {
  988. std::string path = directory.GetFile(i);
  989. if(path == "." || path == ".." || path == "CMakeCache.txt")
  990. {
  991. continue;
  992. }
  993. std::string fullPath = directoryPath + std::string("/") + path;
  994. bool isDirectory = cmSystemTools::FileIsDirectory(fullPath) &&
  995. !cmSystemTools::FileIsSymlink(fullPath);
  996. if(isDirectory)
  997. {
  998. if(!cmSystemTools::RemoveADirectory(fullPath))
  999. {
  1000. return false;
  1001. }
  1002. }
  1003. else
  1004. {
  1005. if(!cmSystemTools::RemoveFile(fullPath))
  1006. {
  1007. return false;
  1008. }
  1009. }
  1010. }
  1011. return cmSystemTools::RemoveADirectory(directoryPath);
  1012. }
  1013. //-------------------------------------------------------------------------
  1014. double cmCTestScriptHandler::GetRemainingTimeAllowed()
  1015. {
  1016. if (!this->Makefile)
  1017. {
  1018. return 1.0e7;
  1019. }
  1020. const char *timelimitS
  1021. = this->Makefile->GetDefinition("CTEST_TIME_LIMIT");
  1022. if (!timelimitS)
  1023. {
  1024. return 1.0e7;
  1025. }
  1026. double timelimit = atof(timelimitS);
  1027. return timelimit - cmSystemTools::GetTime() + this->ScriptStartTime;
  1028. }