cmCTestScriptHandler.cxx 19 KB

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