cmCTestScriptHandler.cxx 31 KB

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