cmCTestScriptHandler.cxx 31 KB

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