cmCTestScriptHandler.cxx 22 KB

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