cmCTestScriptHandler.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestScriptHandler.h"
  14. #include "cmCTest.h"
  15. #include "cmake.h"
  16. #include "cmFunctionBlocker.h"
  17. #include "cmMakefile.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmGlobalGenerator.h"
  20. //#include <cmsys/RegularExpression.hxx>
  21. #include <cmsys/Process.h>
  22. // used for sleep
  23. #ifdef _WIN32
  24. #include "windows.h"
  25. #endif
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <math.h>
  29. #include <float.h>
  30. // needed for sleep
  31. #if !defined(_WIN32)
  32. # include <unistd.h>
  33. #endif
  34. #include "cmCTestBuildCommand.h"
  35. #include "cmCTestConfigureCommand.h"
  36. #include "cmCTestEmptyBinaryDirectoryCommand.h"
  37. #include "cmCTestRunScriptCommand.h"
  38. #include "cmCTestSleepCommand.h"
  39. #include "cmCTestStartCommand.h"
  40. #include "cmCTestUpdateCommand.h"
  41. #include "cmCTestTestCommand.h"
  42. #include "cmCTestSubmitCommand.h"
  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. virtual ~cmCTestScriptFunctionBlocker() {}
  50. virtual bool IsFunctionBlocked(const cmListFileFunction& lff,
  51. cmMakefile &mf);
  52. //virtual bool ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf);
  53. //virtual void ScopeEnded(cmMakefile &mf);
  54. cmCTestScriptHandler* m_CTestScriptHandler;
  55. };
  56. // simply update the time and don't block anything
  57. bool cmCTestScriptFunctionBlocker::
  58. IsFunctionBlocked(const cmListFileFunction& , cmMakefile &)
  59. {
  60. m_CTestScriptHandler->UpdateElapsedTime();
  61. return false;
  62. }
  63. //----------------------------------------------------------------------
  64. cmCTestScriptHandler::cmCTestScriptHandler()
  65. {
  66. m_Verbose = false;
  67. m_Backup = false;
  68. m_EmptyBinDir = false;
  69. m_EmptyBinDirOnce = false;
  70. m_Makefile = 0;
  71. m_LocalGenerator = 0;
  72. m_CMake = 0;
  73. m_GlobalGenerator = 0;
  74. m_ScriptStartTime = 0;
  75. // the *60 is becuase the settings are in minutes but GetTime is seconds
  76. m_MinimumInterval = 30*60;
  77. m_ContinuousDuration = -1;
  78. }
  79. //----------------------------------------------------------------------
  80. cmCTestScriptHandler::~cmCTestScriptHandler()
  81. {
  82. // local generator owns the makefile
  83. m_Makefile = 0;
  84. if (m_LocalGenerator)
  85. {
  86. delete m_LocalGenerator;
  87. }
  88. m_LocalGenerator = 0;
  89. if (m_GlobalGenerator)
  90. {
  91. delete m_GlobalGenerator;
  92. }
  93. m_GlobalGenerator = 0;
  94. if (m_CMake)
  95. {
  96. delete m_CMake;
  97. }
  98. m_CMake = 0;
  99. }
  100. //----------------------------------------------------------------------
  101. // just adds an argument to the vector
  102. void cmCTestScriptHandler::AddConfigurationScript(const char *script)
  103. {
  104. m_ConfigurationScripts.push_back(script);
  105. }
  106. //----------------------------------------------------------------------
  107. // the generic entry point for handling scripts, this routine will run all
  108. // the scripts provides a -S arguments
  109. int cmCTestScriptHandler::ProcessHandler()
  110. {
  111. int res = 0;
  112. std::vector<cmStdString>::iterator it;
  113. for ( it = m_ConfigurationScripts.begin();
  114. it != m_ConfigurationScripts.end();
  115. it ++ )
  116. {
  117. // for each script run it
  118. res += this->RunConfigurationScript(
  119. cmSystemTools::CollapseFullPath(it->c_str()));
  120. }
  121. if ( res )
  122. {
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. void cmCTestScriptHandler::UpdateElapsedTime()
  128. {
  129. if (m_LocalGenerator)
  130. {
  131. // set the current elapsed time
  132. char timeString[20];
  133. int itime = static_cast<unsigned int>(cmSystemTools::GetTime()
  134. - m_ScriptStartTime);
  135. sprintf(timeString,"%i",itime);
  136. m_LocalGenerator->GetMakefile()->AddDefinition("CTEST_ELAPSED_TIME",
  137. timeString);
  138. }
  139. }
  140. //----------------------------------------------------------------------
  141. void cmCTestScriptHandler::AddCTestCommand(cmCTestCommand* command)
  142. {
  143. cmCTestCommand* newCom = command;
  144. newCom->m_CTest = m_CTest;
  145. newCom->m_CTestScriptHandler = this;
  146. m_CMake->AddCommand(newCom);
  147. }
  148. //----------------------------------------------------------------------
  149. // this sets up some variables for thew script to use, creates the required
  150. // cmake instance and generators, and then reads in the script
  151. int cmCTestScriptHandler::ReadInScript(const std::string& total_script_arg)
  152. {
  153. // if the argument has a , in it then it needs to be broken into the fist
  154. // argument (which is the script) and the second argument which will be
  155. // passed into the scripts as S_ARG
  156. std::string script = total_script_arg;
  157. std::string script_arg;
  158. if (total_script_arg.find(",") != std::string::npos)
  159. {
  160. script = total_script_arg.substr(0,total_script_arg.find(","));
  161. script_arg = total_script_arg.substr(total_script_arg.find(",")+1);
  162. }
  163. // make sure the file exists
  164. if (!cmSystemTools::FileExists(script.c_str()))
  165. {
  166. cmSystemTools::Error("Cannot find file: ", script.c_str());
  167. return 1;
  168. }
  169. // create a cmake instance to read the configuration script
  170. // read in the list file to fill the cache
  171. if (m_CMake)
  172. {
  173. delete m_CMake;
  174. delete m_GlobalGenerator;
  175. delete m_LocalGenerator;
  176. }
  177. m_CMake = new cmake;
  178. m_CMake->AddCMakePaths(m_CTest->GetCTestExecutable());
  179. m_GlobalGenerator = new cmGlobalGenerator;
  180. m_GlobalGenerator->SetCMakeInstance(m_CMake);
  181. m_LocalGenerator = m_GlobalGenerator->CreateLocalGenerator();
  182. m_LocalGenerator->SetGlobalGenerator(m_GlobalGenerator);
  183. m_Makefile = m_LocalGenerator->GetMakefile();
  184. // set a variable with the path to the current script
  185. m_Makefile->AddDefinition("CTEST_SCRIPT_DIRECTORY",
  186. cmSystemTools::GetFilenamePath(script).c_str());
  187. m_Makefile->AddDefinition("CTEST_SCRIPT_NAME",
  188. cmSystemTools::GetFilenameName(script).c_str());
  189. m_Makefile->AddDefinition("CTEST_EXECUTABLE_NAME",
  190. m_CTest->GetCTestExecutable());
  191. m_Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", true);
  192. this->UpdateElapsedTime();
  193. // add any ctest specific commands, probably should have common superclass
  194. // for ctest commands to clean this up. If a couple more commands are
  195. // created with the same format lets do that - ken
  196. this->AddCTestCommand(new cmCTestBuildCommand);
  197. this->AddCTestCommand(new cmCTestConfigureCommand);
  198. this->AddCTestCommand(new cmCTestEmptyBinaryDirectoryCommand);
  199. this->AddCTestCommand(new cmCTestRunScriptCommand);
  200. this->AddCTestCommand(new cmCTestSleepCommand);
  201. this->AddCTestCommand(new cmCTestStartCommand);
  202. this->AddCTestCommand(new cmCTestSubmitCommand);
  203. this->AddCTestCommand(new cmCTestTestCommand);
  204. this->AddCTestCommand(new cmCTestUpdateCommand);
  205. // add the script arg if defined
  206. if (script_arg.size())
  207. {
  208. m_Makefile->AddDefinition("CTEST_SCRIPT_ARG", script_arg.c_str());
  209. }
  210. // always add a function blocker to update the elapsed time
  211. cmCTestScriptFunctionBlocker *f = new cmCTestScriptFunctionBlocker();
  212. f->m_CTestScriptHandler = this;
  213. m_Makefile->AddFunctionBlocker(f);
  214. // finally read in the script
  215. if (!m_Makefile->ReadListFile(0, script.c_str()))
  216. {
  217. return 2;
  218. }
  219. return 0;
  220. }
  221. //----------------------------------------------------------------------
  222. // extract variabels from the script to set ivars
  223. int cmCTestScriptHandler::ExtractVariables()
  224. {
  225. // Temporary variables
  226. const char* minInterval;
  227. const char* contDuration;
  228. m_SourceDir = m_Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
  229. m_BinaryDir = m_Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");
  230. m_CTestCmd = m_Makefile->GetSafeDefinition("CTEST_COMMAND");
  231. m_CVSCheckOut = m_Makefile->GetSafeDefinition("CTEST_CVS_CHECKOUT");
  232. m_CTestRoot = m_Makefile->GetSafeDefinition("CTEST_DASHBOARD_ROOT");
  233. m_CVSCmd = m_Makefile->GetSafeDefinition("CTEST_CVS_COMMAND");
  234. m_CTestEnv = m_Makefile->GetSafeDefinition("CTEST_ENVIRONMENT");
  235. m_InitCache = m_Makefile->GetSafeDefinition("CTEST_INITIAL_CACHE");
  236. m_CMakeCmd = m_Makefile->GetSafeDefinition("CTEST_CMAKE_COMMAND");
  237. m_CMOutFile = m_Makefile->GetSafeDefinition("CTEST_CMAKE_OUTPUT_FILE_NAME");
  238. m_Backup = m_Makefile->IsOn("CTEST_BACKUP_AND_RESTORE");
  239. m_EmptyBinDir = m_Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY");
  240. m_EmptyBinDirOnce = m_Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY_ONCE");
  241. minInterval = m_Makefile->GetDefinition("CTEST_CONTINUOUS_MINIMUM_INTERVAL");
  242. contDuration = m_Makefile->GetDefinition("CTEST_CONTINUOUS_DURATION");
  243. char updateVar[40];
  244. int i;
  245. for (i = 1; i < 10; ++i)
  246. {
  247. sprintf(updateVar,"CTEST_EXTRA_UPDATES_%i",i);
  248. const char *updateVal = m_Makefile->GetDefinition(updateVar);
  249. if ( updateVal )
  250. {
  251. if ( m_CVSCmd.empty() )
  252. {
  253. cmSystemTools::Error(updateVar, " specified without specifying CTEST_CVS_COMMAND.");
  254. return 12;
  255. }
  256. m_ExtraUpdates.push_back(updateVal);
  257. }
  258. }
  259. // in order to backup and restore we also must have the cvs root
  260. if (m_Backup && m_CVSCheckOut.empty())
  261. {
  262. cmSystemTools::Error(
  263. "Backup was requested without specifying CTEST_CVS_CHECKOUT.");
  264. return 3;
  265. }
  266. // make sure the required info is here
  267. if (this->m_SourceDir.empty() ||
  268. this->m_BinaryDir.empty() ||
  269. this->m_CTestCmd.empty())
  270. {
  271. std::string message = "CTEST_SOURCE_DIRECTORY = ";
  272. message += (!m_SourceDir.empty()) ? m_SourceDir.c_str() : "(Null)";
  273. message += "\nCTEST_BINARY_DIRECTORY = ";
  274. message += (!m_BinaryDir.empty()) ? m_BinaryDir.c_str() : "(Null)";
  275. message += "\nCTEST_COMMAND = ";
  276. message += (!m_CTestCmd.empty()) ? m_CTestCmd.c_str() : "(Null)";
  277. cmSystemTools::Error(
  278. "Some required settings in the configuration file were missing:\n",
  279. message.c_str());
  280. return 4;
  281. }
  282. // if the dashboard root isn't specified then we can compute it from the
  283. // m_SourceDir
  284. if (m_CTestRoot.empty() )
  285. {
  286. m_CTestRoot = cmSystemTools::GetFilenamePath(m_SourceDir).c_str();
  287. }
  288. // the script may override the minimum continuous interval
  289. if (minInterval)
  290. {
  291. m_MinimumInterval = 60 * atof(minInterval);
  292. }
  293. if (contDuration)
  294. {
  295. m_ContinuousDuration = 60.0 * atof(contDuration);
  296. }
  297. this->UpdateElapsedTime();
  298. return 0;
  299. }
  300. //----------------------------------------------------------------------
  301. void cmCTestScriptHandler::SleepInSeconds(unsigned int secondsToWait)
  302. {
  303. #if defined(_WIN32)
  304. Sleep(1000*secondsToWait);
  305. #else
  306. sleep(secondsToWait);
  307. #endif
  308. }
  309. //----------------------------------------------------------------------
  310. // run a specific script
  311. int cmCTestScriptHandler::RunConfigurationScript(const std::string& total_script_arg)
  312. {
  313. int result;
  314. m_ScriptStartTime =
  315. cmSystemTools::GetTime();
  316. // read in the script
  317. result = this->ReadInScript(total_script_arg);
  318. if (result)
  319. {
  320. return result;
  321. }
  322. // only run the curent script if we should
  323. if (m_Makefile && m_Makefile->IsOn("CTEST_RUN_CURRENT_SCRIPT"))
  324. {
  325. return this->RunCurrentScript();
  326. }
  327. return result;
  328. }
  329. //----------------------------------------------------------------------
  330. int cmCTestScriptHandler::RunCurrentScript()
  331. {
  332. int result;
  333. // do not run twice
  334. m_Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", false);
  335. // no popup widows
  336. cmSystemTools::SetRunCommandHideConsole(true);
  337. // extract the vars from the cache and store in ivars
  338. result = this->ExtractVariables();
  339. if (result)
  340. {
  341. return result;
  342. }
  343. // set any environment variables
  344. if (!m_CTestEnv.empty())
  345. {
  346. std::vector<std::string> envArgs;
  347. cmSystemTools::ExpandListArgument(m_CTestEnv.c_str(),envArgs);
  348. // for each variable/argument do a putenv
  349. for (unsigned i = 0; i < envArgs.size(); ++i)
  350. {
  351. cmSystemTools::PutEnv(envArgs[i].c_str());
  352. }
  353. }
  354. // now that we have done most of the error checking finally run the
  355. // dashboard, we may be asked to repeatedly run this dashboard, such as
  356. // for a continuous, do we ned to run it more than once?
  357. if ( m_ContinuousDuration >= 0 )
  358. {
  359. this->UpdateElapsedTime();
  360. double ending_time = cmSystemTools::GetTime() + m_ContinuousDuration;
  361. if (m_EmptyBinDirOnce)
  362. {
  363. m_EmptyBinDir = true;
  364. }
  365. do
  366. {
  367. double interval = cmSystemTools::GetTime();
  368. result = this->RunConfigurationDashboard();
  369. interval = cmSystemTools::GetTime() - interval;
  370. if (interval < m_MinimumInterval)
  371. {
  372. this->SleepInSeconds(
  373. static_cast<unsigned int>(m_MinimumInterval - interval));
  374. }
  375. if (m_EmptyBinDirOnce)
  376. {
  377. m_EmptyBinDir = false;
  378. }
  379. }
  380. while (cmSystemTools::GetTime() < ending_time);
  381. }
  382. // otherwise just run it once
  383. else
  384. {
  385. result = this->RunConfigurationDashboard();
  386. }
  387. return result;
  388. }
  389. //----------------------------------------------------------------------
  390. int cmCTestScriptHandler::CheckOutSourceDir()
  391. {
  392. std::string command;
  393. std::string output;
  394. int retVal;
  395. bool res;
  396. if (!cmSystemTools::FileExists(m_SourceDir.c_str()) &&
  397. !m_CVSCheckOut.empty())
  398. {
  399. // we must now checkout the src dir
  400. output = "";
  401. if ( m_Verbose )
  402. {
  403. std::cerr << "Run cvs: " << m_CVSCheckOut << std::endl;
  404. }
  405. res = cmSystemTools::RunSingleCommand(m_CVSCheckOut.c_str(), &output,
  406. &retVal, m_CTestRoot.c_str(),
  407. m_Verbose, 0 /*m_TimeOut*/);
  408. if (!res || retVal != 0)
  409. {
  410. cmSystemTools::Error("Unable to perform cvs checkout:\n",
  411. output.c_str());
  412. return 6;
  413. }
  414. }
  415. return 0;
  416. }
  417. //----------------------------------------------------------------------
  418. int cmCTestScriptHandler::BackupDirectories()
  419. {
  420. int retVal;
  421. // compute the backup names
  422. m_BackupSourceDir = m_SourceDir;
  423. m_BackupSourceDir += "_CMakeBackup";
  424. m_BackupBinaryDir = m_BinaryDir;
  425. m_BackupBinaryDir += "_CMakeBackup";
  426. // backup the binary and src directories if requested
  427. if (m_Backup)
  428. {
  429. // if for some reason those directories exist then first delete them
  430. if (cmSystemTools::FileExists(m_BackupSourceDir.c_str()))
  431. {
  432. cmSystemTools::RemoveADirectory(m_BackupSourceDir.c_str());
  433. }
  434. if (cmSystemTools::FileExists(m_BackupBinaryDir.c_str()))
  435. {
  436. cmSystemTools::RemoveADirectory(m_BackupBinaryDir.c_str());
  437. }
  438. // first rename the src and binary directories
  439. rename(m_SourceDir.c_str(), m_BackupSourceDir.c_str());
  440. rename(m_BinaryDir.c_str(), m_BackupBinaryDir.c_str());
  441. // we must now checkout the src dir
  442. retVal = this->CheckOutSourceDir();
  443. if (retVal)
  444. {
  445. this->RestoreBackupDirectories();
  446. return retVal;
  447. }
  448. }
  449. return 0;
  450. }
  451. //----------------------------------------------------------------------
  452. int cmCTestScriptHandler::PerformExtraUpdates()
  453. {
  454. std::string command;
  455. std::string output;
  456. int retVal;
  457. bool res;
  458. // do an initial cvs update as required
  459. command = m_CVSCmd;
  460. std::vector<cmStdString>::iterator it;
  461. for (it = m_ExtraUpdates.begin(); it != m_ExtraUpdates.end(); ++ it )
  462. {
  463. std::vector<std::string> cvsArgs;
  464. cmSystemTools::ExpandListArgument(it->c_str(),cvsArgs);
  465. if (cvsArgs.size() == 2)
  466. {
  467. std::string fullCommand = command;
  468. fullCommand += " update ";
  469. fullCommand += cvsArgs[1];
  470. output = "";
  471. retVal = 0;
  472. if ( m_Verbose )
  473. {
  474. std::cerr << "Run CVS: " << fullCommand.c_str() << std::endl;
  475. }
  476. res = cmSystemTools::RunSingleCommand(fullCommand.c_str(), &output,
  477. &retVal, cvsArgs[0].c_str(),
  478. m_Verbose, 0 /*m_TimeOut*/);
  479. if (!res || retVal != 0)
  480. {
  481. cmSystemTools::Error("Unable to perform extra cvs updates:\n",
  482. output.c_str());
  483. return 0;
  484. }
  485. }
  486. }
  487. return 0;
  488. }
  489. //----------------------------------------------------------------------
  490. // run a single dashboard entry
  491. int cmCTestScriptHandler::RunConfigurationDashboard()
  492. {
  493. // local variables
  494. std::string command;
  495. std::string output;
  496. int retVal;
  497. bool res;
  498. // make sure the src directory is there, if it isn't then we might be able
  499. // to check it out from cvs
  500. retVal = this->CheckOutSourceDir();
  501. if (retVal)
  502. {
  503. return retVal;
  504. }
  505. // backup the dirs if requested
  506. retVal = this->BackupDirectories();
  507. if (retVal)
  508. {
  509. return retVal;
  510. }
  511. // clear the binary directory?
  512. if (m_EmptyBinDir)
  513. {
  514. if ( !cmCTestScriptHandler::EmptyBinaryDirectory(m_BinaryDir.c_str()) )
  515. {
  516. std::cerr << "Problem removing the binary directory" << std::endl;
  517. }
  518. }
  519. // make sure the binary directory exists if it isn't the srcdir
  520. if (!cmSystemTools::FileExists(m_BinaryDir.c_str()) &&
  521. m_SourceDir != m_BinaryDir)
  522. {
  523. if (!cmSystemTools::MakeDirectory(m_BinaryDir.c_str()))
  524. {
  525. cmSystemTools::Error("Unable to create the binary directory:\n",
  526. m_BinaryDir.c_str());
  527. this->RestoreBackupDirectories();
  528. return 7;
  529. }
  530. }
  531. // if the binary directory and the source directory are the same,
  532. // and we are starting with an empty binary directory, then that means
  533. // we must check out the source tree
  534. if (m_EmptyBinDir && m_SourceDir == m_BinaryDir)
  535. {
  536. // make sure we have the required info
  537. if (m_CVSCheckOut.empty())
  538. {
  539. cmSystemTools::Error("You have specified the source and binary directories to be the same (an in source build). You have also specified that the binary directory is to be erased. This means that the source will have to be checked out from CVS. But you have not specified CTEST_CVS_CHECKOUT");
  540. return 8;
  541. }
  542. // we must now checkout the src dir
  543. retVal = this->CheckOutSourceDir();
  544. if (retVal)
  545. {
  546. this->RestoreBackupDirectories();
  547. return retVal;
  548. }
  549. }
  550. // backup the dirs if requested
  551. retVal = this->PerformExtraUpdates();
  552. if (retVal)
  553. {
  554. return retVal;
  555. }
  556. // put the initial cache into the bin dir
  557. if (!m_InitCache.empty())
  558. {
  559. std::string cacheFile = m_BinaryDir;
  560. cacheFile += "/CMakeCache.txt";
  561. std::ofstream fout(cacheFile.c_str());
  562. if(!fout)
  563. {
  564. this->RestoreBackupDirectories();
  565. return 9;
  566. }
  567. fout.write(m_InitCache.c_str(), m_InitCache.size());
  568. // Make sure the operating system has finished writing the file
  569. // before closing it. This will ensure the file is finished before
  570. // the check below.
  571. fout.flush();
  572. fout.close();
  573. }
  574. // do an initial cmake to setup the DartConfig file
  575. int cmakeFailed = 0;
  576. std::string cmakeFailedOuput;
  577. if (!m_CMakeCmd.empty())
  578. {
  579. command = m_CMakeCmd;
  580. command += " \"";
  581. command += m_SourceDir;
  582. output = "";
  583. command += "\"";
  584. retVal = 0;
  585. if ( m_Verbose )
  586. {
  587. std::cerr << "Run cmake command: " << command.c_str() << std::endl;
  588. }
  589. res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  590. &retVal, m_BinaryDir.c_str(),
  591. m_Verbose, 0 /*m_TimeOut*/);
  592. if ( !m_CMOutFile.empty() )
  593. {
  594. std::string cmakeOutputFile = m_CMOutFile;
  595. if ( !cmSystemTools::FileIsFullPath(cmakeOutputFile.c_str()) )
  596. {
  597. cmakeOutputFile = m_BinaryDir + "/" + cmakeOutputFile;
  598. }
  599. if ( m_Verbose )
  600. {
  601. std::cerr << "Write CMake output to file: " << cmakeOutputFile.c_str()
  602. << std::endl;
  603. }
  604. std::ofstream fout(cmakeOutputFile.c_str());
  605. if ( fout )
  606. {
  607. fout << output.c_str();
  608. }
  609. else
  610. {
  611. cmSystemTools::Error("Cannot open CMake output file: ",
  612. cmakeOutputFile.c_str(), " for writing");
  613. }
  614. }
  615. if (!res || retVal != 0)
  616. {
  617. // even if this fails continue to the next step
  618. cmakeFailed = 1;
  619. cmakeFailedOuput = output;
  620. }
  621. }
  622. // run ctest, it may be more than one command in here
  623. std::vector<std::string> ctestCommands;
  624. cmSystemTools::ExpandListArgument(m_CTestCmd,ctestCommands);
  625. // for each variable/argument do a putenv
  626. for (unsigned i = 0; i < ctestCommands.size(); ++i)
  627. {
  628. command = ctestCommands[i];
  629. output = "";
  630. retVal = 0;
  631. if ( m_Verbose )
  632. {
  633. std::cerr << "Run ctest command: " << command.c_str() << std::endl;
  634. }
  635. res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
  636. &retVal, m_BinaryDir.c_str(),
  637. m_Verbose, 0 /*m_TimeOut*/);
  638. // did something critical fail in ctest
  639. if (!res || cmakeFailed ||
  640. retVal & cmCTest::BUILD_ERRORS)
  641. {
  642. this->RestoreBackupDirectories();
  643. if (cmakeFailed)
  644. {
  645. cmSystemTools::Error("Unable to run cmake:\n",
  646. cmakeFailedOuput.c_str());
  647. return 10;
  648. }
  649. cmSystemTools::Error("Unable to run ctest:\n", output.c_str());
  650. if (!res)
  651. {
  652. return 11;
  653. }
  654. return retVal * 100;
  655. }
  656. }
  657. // if all was succesful, delete the backup dirs to free up disk space
  658. if (m_Backup)
  659. {
  660. cmSystemTools::RemoveADirectory(m_BackupSourceDir.c_str());
  661. cmSystemTools::RemoveADirectory(m_BackupBinaryDir.c_str());
  662. }
  663. return 0;
  664. }
  665. //-------------------------------------------------------------------------
  666. void cmCTestScriptHandler::RestoreBackupDirectories()
  667. {
  668. // if we backed up the dirs and the build failed, then restore
  669. // the backed up dirs
  670. if (m_Backup)
  671. {
  672. // if for some reason those directories exist then first delete them
  673. if (cmSystemTools::FileExists(m_SourceDir.c_str()))
  674. {
  675. cmSystemTools::RemoveADirectory(m_SourceDir.c_str());
  676. }
  677. if (cmSystemTools::FileExists(m_BinaryDir.c_str()))
  678. {
  679. cmSystemTools::RemoveADirectory(m_BinaryDir.c_str());
  680. }
  681. // rename the src and binary directories
  682. rename(m_BackupSourceDir.c_str(), m_SourceDir.c_str());
  683. rename(m_BackupBinaryDir.c_str(), m_BinaryDir.c_str());
  684. }
  685. }
  686. bool cmCTestScriptHandler::RunScript(cmCTest* ctest, const char *sname)
  687. {
  688. cmCTestScriptHandler* sh = new cmCTestScriptHandler();
  689. sh->SetCTestInstance(ctest);
  690. sh->AddConfigurationScript(sname);
  691. sh->ProcessHandler();
  692. delete sh;
  693. return true;
  694. }
  695. bool cmCTestScriptHandler::EmptyBinaryDirectory(const char *sname)
  696. {
  697. // try to avoid deleting root
  698. if (!sname || strlen(sname) < 2)
  699. {
  700. return false;
  701. }
  702. // try to avoid deleting directories that we shouldn't
  703. std::string check = sname;
  704. check += "/CMakeCache.txt";
  705. if(cmSystemTools::FileExists(check.c_str()) &&
  706. !cmSystemTools::RemoveADirectory(sname))
  707. {
  708. return false;
  709. }
  710. return true;
  711. }