cmCTestScriptHandler.cxx 23 KB

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