cmCTestScriptHandler.cxx 33 KB

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