cmCTestScriptHandler.cxx 22 KB

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