cmCTest.cxx 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  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 "cmcurl/curl/curl.h"
  14. #include "cmCTest.h"
  15. #include "cmake.h"
  16. #include "cmMakefile.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmGlobalGenerator.h"
  19. #include <cmsys/Directory.hxx>
  20. #include "cmGlob.h"
  21. #include "cmDynamicLoader.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include "cmCTestCommand.h"
  24. #include "cmCTestBuildHandler.h"
  25. #include "cmCTestBuildAndTestHandler.h"
  26. #include "cmCTestConfigureHandler.h"
  27. #include "cmCTestCoverageHandler.h"
  28. #include "cmCTestMemCheckHandler.h"
  29. #include "cmCTestScriptHandler.h"
  30. #include "cmCTestTestHandler.h"
  31. #include "cmCTestUpdateHandler.h"
  32. #include "cmCTestSubmitHandler.h"
  33. #include "cmVersion.h"
  34. #include <cmsys/RegularExpression.hxx>
  35. #include <cmsys/Process.h>
  36. #include <stdlib.h>
  37. #include <math.h>
  38. #include <float.h>
  39. #include <memory> // auto_ptr
  40. #define DEBUGOUT std::cout << __LINE__ << " "; std::cout
  41. #define DEBUGERR std::cerr << __LINE__ << " "; std::cerr
  42. //----------------------------------------------------------------------
  43. struct tm* cmCTest::GetNightlyTime(std::string str,
  44. bool tomorrowtag)
  45. {
  46. struct tm* lctime;
  47. time_t tctime = time(0);
  48. cmCTestLog(this, OUTPUT, "Determine Nightly Start Time" << std::endl
  49. << " Specified time: " << str.c_str() << std::endl);
  50. //Convert the nightly start time to seconds. Since we are
  51. //providing only a time and a timezone, the current date of
  52. //the local machine is assumed. Consequently, nightlySeconds
  53. //is the time at which the nightly dashboard was opened or
  54. //will be opened on the date of the current client machine.
  55. //As such, this time may be in the past or in the future.
  56. time_t ntime = curl_getdate(str.c_str(), &tctime);
  57. cmCTestLog(this, DEBUG, " Get curl time: " << ntime << std::endl);
  58. tctime = time(0);
  59. cmCTestLog(this, DEBUG, " Get the current time: " << tctime << std::endl);
  60. const int dayLength = 24 * 60 * 60;
  61. cmCTestLog(this, DEBUG, "Seconds: " << tctime << std::endl);
  62. while ( ntime > tctime )
  63. {
  64. // If nightlySeconds is in the past, this is the current
  65. // open dashboard, then return nightlySeconds. If
  66. // nightlySeconds is in the future, this is the next
  67. // dashboard to be opened, so subtract 24 hours to get the
  68. // time of the current open dashboard
  69. ntime -= dayLength;
  70. cmCTestLog(this, DEBUG, "Pick yesterday" << std::endl);
  71. cmCTestLog(this, DEBUG, " Future time, subtract day: " << ntime << std::endl);
  72. }
  73. while ( tctime > (ntime + dayLength) )
  74. {
  75. ntime += dayLength;
  76. cmCTestLog(this, DEBUG, " Past time, add day: " << ntime << std::endl);
  77. }
  78. cmCTestLog(this, DEBUG, "nightlySeconds: " << ntime << std::endl);
  79. cmCTestLog(this, DEBUG, " Current time: " << tctime
  80. << " Nightly time: " << ntime << std::endl);
  81. if ( tomorrowtag )
  82. {
  83. cmCTestLog(this, OUTPUT, " Use future tag, Add a day" << std::endl);
  84. ntime += dayLength;
  85. }
  86. lctime = gmtime(&ntime);
  87. return lctime;
  88. }
  89. //----------------------------------------------------------------------
  90. std::string cmCTest::CleanString(const std::string& str)
  91. {
  92. std::string::size_type spos = str.find_first_not_of(" \n\t\r\f\v");
  93. std::string::size_type epos = str.find_last_not_of(" \n\t\r\f\v");
  94. if ( spos == str.npos )
  95. {
  96. return std::string();
  97. }
  98. if ( epos != str.npos )
  99. {
  100. epos = epos - spos + 1;
  101. }
  102. return str.substr(spos, epos);
  103. }
  104. //----------------------------------------------------------------------
  105. std::string cmCTest::CurrentTime()
  106. {
  107. time_t currenttime = time(0);
  108. struct tm* t = localtime(&currenttime);
  109. //return ::CleanString(ctime(&currenttime));
  110. char current_time[1024];
  111. if ( m_ShortDateFormat )
  112. {
  113. strftime(current_time, 1000, "%b %d %H:%M %Z", t);
  114. }
  115. else
  116. {
  117. strftime(current_time, 1000, "%a %b %d %H:%M:%S %Z %Y", t);
  118. }
  119. cmCTestLog(this, DEBUG, " Current_Time: " << current_time << std::endl);
  120. return cmCTest::MakeXMLSafe(cmCTest::CleanString(current_time));
  121. }
  122. //----------------------------------------------------------------------
  123. std::string cmCTest::MakeXMLSafe(const std::string& str)
  124. {
  125. std::vector<char> result;
  126. result.reserve(500);
  127. const char* pos = str.c_str();
  128. for ( ;*pos; ++pos)
  129. {
  130. char ch = *pos;
  131. if ( (ch > 126 || ch < 32) && ch != 9 && ch != 10 && ch != 13 && ch != '\r' )
  132. {
  133. char buffer[33];
  134. sprintf(buffer, "&lt;%d&gt;", (int)ch);
  135. //sprintf(buffer, "&#x%0x;", (unsigned int)ch);
  136. result.insert(result.end(), buffer, buffer+strlen(buffer));
  137. }
  138. else
  139. {
  140. const char* const encodedChars[] = {
  141. "&amp;",
  142. "&lt;",
  143. "&gt;"
  144. };
  145. switch ( ch )
  146. {
  147. case '&':
  148. result.insert(result.end(), encodedChars[0], encodedChars[0]+5);
  149. break;
  150. case '<':
  151. result.insert(result.end(), encodedChars[1], encodedChars[1]+4);
  152. break;
  153. case '>':
  154. result.insert(result.end(), encodedChars[2], encodedChars[2]+4);
  155. break;
  156. case '\n':
  157. result.push_back('\n');
  158. break;
  159. case '\r': break; // Ignore \r
  160. default:
  161. result.push_back(ch);
  162. }
  163. }
  164. }
  165. return std::string(&*result.begin(), result.size());
  166. }
  167. //----------------------------------------------------------------------
  168. std::string cmCTest::MakeURLSafe(const std::string& str)
  169. {
  170. cmOStringStream ost;
  171. char buffer[10];
  172. for ( std::string::size_type pos = 0; pos < str.size(); pos ++ )
  173. {
  174. unsigned char ch = str[pos];
  175. if ( ( ch > 126 || ch < 32 ||
  176. ch == '&' ||
  177. ch == '%' ||
  178. ch == '+' ||
  179. ch == '=' ||
  180. ch == '@'
  181. ) && ch != 9 )
  182. {
  183. sprintf(buffer, "%02x;", (unsigned int)ch);
  184. ost << buffer;
  185. }
  186. else
  187. {
  188. ost << ch;
  189. }
  190. }
  191. return ost.str();
  192. }
  193. //----------------------------------------------------------------------
  194. cmCTest::cmCTest()
  195. {
  196. m_SubmitIndex = 0;
  197. m_ForceNewCTestProcess = false;
  198. m_TomorrowTag = false;
  199. m_Verbose = false;
  200. m_Debug = false;
  201. m_ShowLineNumbers = false;
  202. m_Quiet = false;
  203. m_ExtraVerbose = false;
  204. m_ProduceXML = false;
  205. m_ShowOnly = false;
  206. m_RunConfigurationScript = false;
  207. m_TestModel = cmCTest::EXPERIMENTAL;
  208. m_InteractiveDebugMode = true;
  209. m_TimeOut = 0;
  210. m_CompressXMLFiles = false;
  211. m_CTestConfigFile = "";
  212. m_OutputLogFile = 0;
  213. m_OutputLogFileLastTag = -1;
  214. m_SuppressUpdatingCTestConfiguration = false;
  215. m_DartVersion = 1;
  216. int cc;
  217. for ( cc=0; cc < cmCTest::LAST_TEST; cc ++ )
  218. {
  219. m_Tests[cc] = 0;
  220. }
  221. m_ShortDateFormat = true;
  222. m_TestingHandlers["build"] = new cmCTestBuildHandler;
  223. m_TestingHandlers["buildtest"] = new cmCTestBuildAndTestHandler;
  224. m_TestingHandlers["coverage"] = new cmCTestCoverageHandler;
  225. m_TestingHandlers["script"] = new cmCTestScriptHandler;
  226. m_TestingHandlers["test"] = new cmCTestTestHandler;
  227. m_TestingHandlers["update"] = new cmCTestUpdateHandler;
  228. m_TestingHandlers["configure"] = new cmCTestConfigureHandler;
  229. m_TestingHandlers["memcheck"] = new cmCTestMemCheckHandler;
  230. m_TestingHandlers["submit"] = new cmCTestSubmitHandler;
  231. cmCTest::t_TestingHandlers::iterator it;
  232. for ( it = m_TestingHandlers.begin(); it != m_TestingHandlers.end(); ++ it )
  233. {
  234. it->second->SetCTestInstance(this);
  235. }
  236. }
  237. //----------------------------------------------------------------------
  238. cmCTest::~cmCTest()
  239. {
  240. cmCTest::t_TestingHandlers::iterator it;
  241. for ( it = m_TestingHandlers.begin(); it != m_TestingHandlers.end(); ++ it )
  242. {
  243. delete it->second;
  244. it->second = 0;
  245. }
  246. this->SetOutputLogFileName(0);
  247. }
  248. //----------------------------------------------------------------------
  249. int cmCTest::Initialize(const char* binary_dir, bool new_tag, bool verbose_tag)
  250. {
  251. if(!m_InteractiveDebugMode)
  252. {
  253. this->BlockTestErrorDiagnostics();
  254. }
  255. m_BinaryDir = binary_dir;
  256. cmSystemTools::ConvertToUnixSlashes(m_BinaryDir);
  257. this->UpdateCTestConfiguration();
  258. if ( m_ProduceXML )
  259. {
  260. cmCTestLog(this, OUTPUT,
  261. " Site: " << this->GetCTestConfiguration("Site") << std::endl
  262. << " Build name: " << this->GetCTestConfiguration("BuildName") << std::endl
  263. );
  264. cmCTestLog(this, DEBUG, "Produce XML is on" << std::endl);
  265. if ( this->GetCTestConfiguration("NightlyStartTime").empty() )
  266. {
  267. cmCTestLog(this, DEBUG, "No nightly start time" << std::endl);
  268. return 0;
  269. }
  270. }
  271. if ( !this->ReadCustomConfigurationFileTree(m_BinaryDir.c_str()) )
  272. {
  273. cmCTestLog(this, DEBUG, "Cannot find custom configuration file tree" << std::endl);
  274. return 0;
  275. }
  276. if ( m_ProduceXML )
  277. {
  278. std::string testingDir = m_BinaryDir + "/Testing";
  279. if ( cmSystemTools::FileExists(testingDir.c_str()) )
  280. {
  281. if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
  282. {
  283. cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir << " is in the place of the testing directory"
  284. << std::endl);
  285. return 0;
  286. }
  287. }
  288. else
  289. {
  290. if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
  291. {
  292. cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory " << testingDir
  293. << std::endl);
  294. return 0;
  295. }
  296. }
  297. std::string tagfile = testingDir + "/TAG";
  298. std::ifstream tfin(tagfile.c_str());
  299. std::string tag;
  300. time_t tctime = time(0);
  301. if ( m_TomorrowTag )
  302. {
  303. tctime += ( 24 * 60 * 60 );
  304. }
  305. struct tm *lctime = gmtime(&tctime);
  306. if ( tfin && cmSystemTools::GetLineFromStream(tfin, tag) )
  307. {
  308. int year = 0;
  309. int mon = 0;
  310. int day = 0;
  311. int hour = 0;
  312. int min = 0;
  313. sscanf(tag.c_str(), "%04d%02d%02d-%02d%02d",
  314. &year, &mon, &day, &hour, &min);
  315. if ( year != lctime->tm_year + 1900 ||
  316. mon != lctime->tm_mon+1 ||
  317. day != lctime->tm_mday )
  318. {
  319. tag = "";
  320. }
  321. std::string tagmode;
  322. if ( cmSystemTools::GetLineFromStream(tfin, tagmode) )
  323. {
  324. if ( tagmode.size() > 4 && !( m_Tests[cmCTest::START_TEST] || m_Tests[ALL_TEST] ))
  325. {
  326. m_TestModel = cmCTest::GetTestModelFromString(tagmode.c_str());
  327. }
  328. }
  329. tfin.close();
  330. }
  331. if ( tag.size() == 0 || new_tag || m_Tests[cmCTest::START_TEST] || m_Tests[ALL_TEST])
  332. {
  333. cmCTestLog(this, DEBUG, "TestModel: " << this->GetTestModelString() << std::endl);
  334. cmCTestLog(this, DEBUG, "TestModel: " << m_TestModel << std::endl);
  335. if ( m_TestModel == cmCTest::NIGHTLY )
  336. {
  337. lctime = this->GetNightlyTime(this->GetCTestConfiguration("NightlyStartTime"), m_TomorrowTag);
  338. }
  339. char datestring[100];
  340. sprintf(datestring, "%04d%02d%02d-%02d%02d",
  341. lctime->tm_year + 1900,
  342. lctime->tm_mon+1,
  343. lctime->tm_mday,
  344. lctime->tm_hour,
  345. lctime->tm_min);
  346. tag = datestring;
  347. std::ofstream ofs(tagfile.c_str());
  348. if ( ofs )
  349. {
  350. ofs << tag << std::endl;
  351. ofs << this->GetTestModelString() << std::endl;
  352. }
  353. ofs.close();
  354. if ( verbose_tag )
  355. {
  356. cmCTestLog(this, OUTPUT, "Create new tag: " << tag << " - "
  357. << this->GetTestModelString() << std::endl);
  358. }
  359. }
  360. m_CurrentTag = tag;
  361. }
  362. return 1;
  363. }
  364. //----------------------------------------------------------------------
  365. bool cmCTest::InitializeFromCommand(cmCTestCommand* command, bool first)
  366. {
  367. if ( !first && !m_CurrentTag.empty() )
  368. {
  369. return true;
  370. }
  371. std::string src_dir = this->GetCTestConfiguration("SourceDirectory").c_str();
  372. std::string bld_dir = this->GetCTestConfiguration("BuildDirectory").c_str();
  373. m_DartVersion = 1;
  374. m_SubmitFiles.clear();
  375. cmMakefile* mf = command->GetMakefile();
  376. std::string fname = src_dir;
  377. fname += "/CTestConfig.cmake";
  378. cmSystemTools::ConvertToUnixSlashes(fname);
  379. if ( cmSystemTools::FileExists(fname.c_str()) )
  380. {
  381. cmCTestLog(this, OUTPUT, " Reading ctest configuration file: " << fname.c_str() << std::endl);
  382. bool readit = mf->ReadListFile(mf->GetCurrentListFile(),
  383. fname.c_str() );
  384. if(!readit)
  385. {
  386. std::string m = "Could not find include file: ";
  387. m += fname;
  388. command->SetError(m.c_str());
  389. return false;
  390. }
  391. }
  392. else if ( !first )
  393. {
  394. cmCTestLog(this, WARNING, "Cannot locate CTest configuration: " << fname.c_str() << std::endl);
  395. }
  396. else
  397. {
  398. cmCTestLog(this, HANDLER_OUTPUT, " Cannot locate CTest configuration: " << fname.c_str() << std::endl
  399. << " Delay the initialization of CTest" << std::endl);
  400. }
  401. this->SetCTestConfigurationFromCMakeVariable(mf, "NightlyStartTime", "CTEST_NIGHTLY_START_TIME");
  402. this->SetCTestConfigurationFromCMakeVariable(mf, "Site", "CTEST_SITE");
  403. this->SetCTestConfigurationFromCMakeVariable(mf, "BuildName", "CTEST_BUILD_NAME");
  404. const char* dartVersion = mf->GetDefinition("CTEST_DART_SERVER_VERSION");
  405. if ( dartVersion )
  406. {
  407. m_DartVersion = atoi(dartVersion);
  408. if ( m_DartVersion < 0 )
  409. {
  410. cmCTestLog(this, ERROR_MESSAGE, "Invalid Dart server version: " << dartVersion << ". Please specify the version number." << std::endl);
  411. return false;
  412. }
  413. }
  414. if ( !this->Initialize(bld_dir.c_str(), true, false) )
  415. {
  416. if ( this->GetCTestConfiguration("NightlyStartTime").empty() && first)
  417. {
  418. return true;
  419. }
  420. return false;
  421. }
  422. cmCTestLog(this, OUTPUT, " Use " << this->GetTestModelString() << " tag: " << this->GetCurrentTag() << std::endl);
  423. return true;
  424. }
  425. //----------------------------------------------------------------------
  426. bool cmCTest::UpdateCTestConfiguration()
  427. {
  428. if ( m_SuppressUpdatingCTestConfiguration )
  429. {
  430. return true;
  431. }
  432. std::string fileName = m_CTestConfigFile;
  433. if ( fileName.empty() )
  434. {
  435. fileName = m_BinaryDir + "/DartConfiguration.tcl";
  436. if ( !cmSystemTools::FileExists(fileName.c_str()) )
  437. {
  438. fileName = m_BinaryDir + "/CTestConfiguration.ini";
  439. }
  440. }
  441. if ( !cmSystemTools::FileExists(fileName.c_str()) )
  442. {
  443. // No need to exit if we are not producing XML
  444. if ( m_ProduceXML )
  445. {
  446. cmCTestLog(this, ERROR_MESSAGE, "Cannot find file: " << fileName.c_str() << std::endl);
  447. return false;
  448. }
  449. }
  450. else
  451. {
  452. // parse the dart test file
  453. std::ifstream fin(fileName.c_str());
  454. if(!fin)
  455. {
  456. return false;
  457. }
  458. char buffer[1024];
  459. while ( fin )
  460. {
  461. buffer[0] = 0;
  462. fin.getline(buffer, 1023);
  463. buffer[1023] = 0;
  464. std::string line = cmCTest::CleanString(buffer);
  465. if(line.size() == 0)
  466. {
  467. continue;
  468. }
  469. while ( fin && (line[line.size()-1] == '\\') )
  470. {
  471. line = line.substr(0, line.size()-1);
  472. buffer[0] = 0;
  473. fin.getline(buffer, 1023);
  474. buffer[1023] = 0;
  475. line += cmCTest::CleanString(buffer);
  476. }
  477. if ( line[0] == '#' )
  478. {
  479. continue;
  480. }
  481. std::string::size_type cpos = line.find_first_of(":");
  482. if ( cpos == line.npos )
  483. {
  484. continue;
  485. }
  486. std::string key = line.substr(0, cpos);
  487. std::string value = cmCTest::CleanString(line.substr(cpos+1, line.npos));
  488. m_CTestConfiguration[key] = value;
  489. }
  490. fin.close();
  491. }
  492. m_TimeOut = atoi(this->GetCTestConfiguration("TimeOut").c_str());
  493. if ( m_ProduceXML )
  494. {
  495. m_CompressXMLFiles = cmSystemTools::IsOn(this->GetCTestConfiguration("CompressSubmission").c_str());
  496. }
  497. return true;
  498. }
  499. //----------------------------------------------------------------------
  500. void cmCTest::BlockTestErrorDiagnostics()
  501. {
  502. cmSystemTools::PutEnv("DART_TEST_FROM_DART=1");
  503. cmSystemTools::PutEnv("DASHBOARD_TEST_FROM_CTEST=1");
  504. #if defined(_WIN32)
  505. SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
  506. #endif
  507. }
  508. //----------------------------------------------------------------------
  509. void cmCTest::SetTestModel(int mode)
  510. {
  511. m_InteractiveDebugMode = false;
  512. m_TestModel = mode;
  513. }
  514. //----------------------------------------------------------------------
  515. bool cmCTest::SetTest(const char* ttype, bool report)
  516. {
  517. if ( cmSystemTools::LowerCase(ttype) == "all" )
  518. {
  519. m_Tests[cmCTest::ALL_TEST] = 1;
  520. }
  521. else if ( cmSystemTools::LowerCase(ttype) == "start" )
  522. {
  523. m_Tests[cmCTest::START_TEST] = 1;
  524. }
  525. else if ( cmSystemTools::LowerCase(ttype) == "update" )
  526. {
  527. m_Tests[cmCTest::UPDATE_TEST] = 1;
  528. }
  529. else if ( cmSystemTools::LowerCase(ttype) == "configure" )
  530. {
  531. m_Tests[cmCTest::CONFIGURE_TEST] = 1;
  532. }
  533. else if ( cmSystemTools::LowerCase(ttype) == "build" )
  534. {
  535. m_Tests[cmCTest::BUILD_TEST] = 1;
  536. }
  537. else if ( cmSystemTools::LowerCase(ttype) == "test" )
  538. {
  539. m_Tests[cmCTest::TEST_TEST] = 1;
  540. }
  541. else if ( cmSystemTools::LowerCase(ttype) == "coverage" )
  542. {
  543. m_Tests[cmCTest::COVERAGE_TEST] = 1;
  544. }
  545. else if ( cmSystemTools::LowerCase(ttype) == "memcheck" )
  546. {
  547. m_Tests[cmCTest::MEMCHECK_TEST] = 1;
  548. }
  549. else if ( cmSystemTools::LowerCase(ttype) == "notes" )
  550. {
  551. m_Tests[cmCTest::NOTES_TEST] = 1;
  552. }
  553. else if ( cmSystemTools::LowerCase(ttype) == "submit" )
  554. {
  555. m_Tests[cmCTest::SUBMIT_TEST] = 1;
  556. }
  557. else
  558. {
  559. if ( report )
  560. {
  561. cmCTestLog(this, ERROR_MESSAGE, "Don't know about test \"" << ttype << "\" yet..." << std::endl);
  562. }
  563. return false;
  564. }
  565. return true;
  566. }
  567. //----------------------------------------------------------------------
  568. void cmCTest::Finalize()
  569. {
  570. }
  571. //----------------------------------------------------------------------
  572. bool cmCTest::OpenOutputFile(const std::string& path,
  573. const std::string& name, cmGeneratedFileStream& stream,
  574. bool compress)
  575. {
  576. std::string testingDir = m_BinaryDir + "/Testing";
  577. if ( path.size() > 0 )
  578. {
  579. testingDir += "/" + path;
  580. }
  581. if ( cmSystemTools::FileExists(testingDir.c_str()) )
  582. {
  583. if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
  584. {
  585. cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
  586. << " is in the place of the testing directory"
  587. << std::endl);
  588. return false;
  589. }
  590. }
  591. else
  592. {
  593. if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
  594. {
  595. cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory " << testingDir
  596. << std::endl);
  597. return false;
  598. }
  599. }
  600. std::string filename = testingDir + "/" + name;
  601. stream.Open(filename.c_str());
  602. if( !stream )
  603. {
  604. cmCTestLog(this, ERROR_MESSAGE, "Problem opening file: " << filename << std::endl);
  605. return false;
  606. }
  607. if ( compress )
  608. {
  609. if ( m_CompressXMLFiles )
  610. {
  611. stream.SetCompression(true);
  612. }
  613. }
  614. return true;
  615. }
  616. //----------------------------------------------------------------------
  617. bool cmCTest::AddIfExists(tm_SetOfStrings& files, const char* file)
  618. {
  619. if ( this->CTestFileExists(file) )
  620. {
  621. files.insert(file);
  622. }
  623. else
  624. {
  625. std::string name = file;
  626. name += ".gz";
  627. if ( this->CTestFileExists(name.c_str()) )
  628. {
  629. files.insert(name.c_str());
  630. }
  631. else
  632. {
  633. return false;
  634. }
  635. }
  636. return true;
  637. }
  638. //----------------------------------------------------------------------
  639. bool cmCTest::CTestFileExists(const std::string& filename)
  640. {
  641. std::string testingDir = m_BinaryDir + "/Testing/" + m_CurrentTag + "/" +
  642. filename;
  643. return cmSystemTools::FileExists(testingDir.c_str());
  644. }
  645. //----------------------------------------------------------------------
  646. cmCTestGenericHandler* cmCTest::GetInitializedHandler(const char* handler)
  647. {
  648. cmCTest::t_TestingHandlers::iterator it = m_TestingHandlers.find(handler);
  649. if ( it == m_TestingHandlers.end() )
  650. {
  651. return 0;
  652. }
  653. it->second->Initialize();
  654. return it->second;
  655. }
  656. //----------------------------------------------------------------------
  657. cmCTestGenericHandler* cmCTest::GetHandler(const char* handler)
  658. {
  659. cmCTest::t_TestingHandlers::iterator it = m_TestingHandlers.find(handler);
  660. if ( it == m_TestingHandlers.end() )
  661. {
  662. return 0;
  663. }
  664. return it->second;
  665. }
  666. //----------------------------------------------------------------------
  667. int cmCTest::ExecuteHandler(const char* shandler)
  668. {
  669. cmCTestGenericHandler* handler = this->GetHandler(shandler);
  670. if ( !handler )
  671. {
  672. return -1;
  673. }
  674. handler->Initialize();
  675. return handler->ProcessHandler();
  676. }
  677. //----------------------------------------------------------------------
  678. int cmCTest::ProcessTests()
  679. {
  680. int res = 0;
  681. bool notest = true;
  682. int cc;
  683. int update_count = 0;
  684. for ( cc = 0; cc < LAST_TEST; cc ++ )
  685. {
  686. if ( m_Tests[cc] )
  687. {
  688. notest = false;
  689. break;
  690. }
  691. }
  692. if ( m_Tests[UPDATE_TEST] || m_Tests[ALL_TEST] )
  693. {
  694. cmCTestGenericHandler* uphandler = this->GetHandler("update");
  695. uphandler->SetOption("SourceDirectory", this->GetCTestConfiguration("SourceDirectory").c_str());
  696. update_count = uphandler->ProcessHandler();
  697. if ( update_count < 0 )
  698. {
  699. res |= cmCTest::UPDATE_ERRORS;
  700. }
  701. }
  702. if ( m_TestModel == cmCTest::CONTINUOUS && !update_count )
  703. {
  704. return 0;
  705. }
  706. if ( m_Tests[CONFIGURE_TEST] || m_Tests[ALL_TEST] )
  707. {
  708. if (this->GetHandler("configure")->ProcessHandler() < 0)
  709. {
  710. res |= cmCTest::CONFIGURE_ERRORS;
  711. }
  712. }
  713. if ( m_Tests[BUILD_TEST] || m_Tests[ALL_TEST] )
  714. {
  715. this->UpdateCTestConfiguration();
  716. if (this->GetHandler("build")->ProcessHandler() < 0)
  717. {
  718. res |= cmCTest::BUILD_ERRORS;
  719. }
  720. }
  721. if ( m_Tests[TEST_TEST] || m_Tests[ALL_TEST] || notest )
  722. {
  723. this->UpdateCTestConfiguration();
  724. if (this->GetHandler("test")->ProcessHandler() < 0)
  725. {
  726. res |= cmCTest::TEST_ERRORS;
  727. }
  728. }
  729. if ( m_Tests[COVERAGE_TEST] || m_Tests[ALL_TEST] )
  730. {
  731. this->UpdateCTestConfiguration();
  732. if (this->GetHandler("coverage")->ProcessHandler() < 0)
  733. {
  734. res |= cmCTest::COVERAGE_ERRORS;
  735. }
  736. }
  737. if ( m_Tests[MEMCHECK_TEST] || m_Tests[ALL_TEST] )
  738. {
  739. this->UpdateCTestConfiguration();
  740. if (this->GetHandler("memcheck")->ProcessHandler() < 0)
  741. {
  742. res |= cmCTest::MEMORY_ERRORS;
  743. }
  744. }
  745. if ( !notest )
  746. {
  747. std::string notes_dir = m_BinaryDir + "/Testing/Notes";
  748. if ( cmSystemTools::FileIsDirectory(notes_dir.c_str()) )
  749. {
  750. cmsys::Directory d;
  751. d.Load(notes_dir.c_str());
  752. unsigned long kk;
  753. for ( kk = 0; kk < d.GetNumberOfFiles(); kk ++ )
  754. {
  755. const char* file = d.GetFile(kk);
  756. std::string fullname = notes_dir + "/" + file;
  757. if ( cmSystemTools::FileExists(fullname.c_str()) &&
  758. !cmSystemTools::FileIsDirectory(fullname.c_str()) )
  759. {
  760. if ( m_NotesFiles.size() > 0 )
  761. {
  762. m_NotesFiles += ";";
  763. }
  764. m_NotesFiles += fullname;
  765. m_Tests[NOTES_TEST] = 1;
  766. }
  767. }
  768. }
  769. }
  770. if ( m_Tests[NOTES_TEST] || m_Tests[ALL_TEST] )
  771. {
  772. this->UpdateCTestConfiguration();
  773. if ( m_NotesFiles.size() )
  774. {
  775. this->GenerateNotesFile(m_NotesFiles.c_str());
  776. }
  777. }
  778. if ( m_Tests[SUBMIT_TEST] || m_Tests[ALL_TEST] )
  779. {
  780. this->UpdateCTestConfiguration();
  781. if (this->GetHandler("submit")->ProcessHandler() < 0)
  782. {
  783. res |= cmCTest::SUBMIT_ERRORS;
  784. }
  785. }
  786. return res;
  787. }
  788. //----------------------------------------------------------------------
  789. std::string cmCTest::GetTestModelString()
  790. {
  791. switch ( m_TestModel )
  792. {
  793. case cmCTest::NIGHTLY:
  794. return "Nightly";
  795. case cmCTest::CONTINUOUS:
  796. return "Continuous";
  797. }
  798. return "Experimental";
  799. }
  800. //----------------------------------------------------------------------
  801. int cmCTest::GetTestModelFromString(const char* str)
  802. {
  803. if ( !str )
  804. {
  805. return cmCTest::EXPERIMENTAL;
  806. }
  807. std::string rstr = cmSystemTools::LowerCase(str);
  808. if ( strncmp(rstr.c_str(), "cont", 4) == 0 )
  809. {
  810. return cmCTest::CONTINUOUS;
  811. }
  812. if ( strncmp(rstr.c_str(), "nigh", 4) == 0 )
  813. {
  814. return cmCTest::NIGHTLY;
  815. }
  816. return cmCTest::EXPERIMENTAL;
  817. }
  818. //######################################################################
  819. //######################################################################
  820. //######################################################################
  821. //######################################################################
  822. //----------------------------------------------------------------------
  823. int cmCTest::RunMakeCommand(const char* command, std::string* output,
  824. int* retVal, const char* dir, int timeout, std::ofstream& ofs)
  825. {
  826. // First generate the command and arguments
  827. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  828. if(args.size() < 1)
  829. {
  830. return false;
  831. }
  832. std::vector<const char*> argv;
  833. for(std::vector<cmStdString>::const_iterator a = args.begin();
  834. a != args.end(); ++a)
  835. {
  836. argv.push_back(a->c_str());
  837. }
  838. argv.push_back(0);
  839. if ( output )
  840. {
  841. *output = "";
  842. }
  843. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Run command:");
  844. std::vector<const char*>::iterator ait;
  845. for ( ait = argv.begin(); ait != argv.end() && *ait; ++ ait )
  846. {
  847. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, " \"" << *ait << "\"");
  848. }
  849. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, std::endl);
  850. // Now create process object
  851. cmsysProcess* cp = cmsysProcess_New();
  852. cmsysProcess_SetCommand(cp, &*argv.begin());
  853. cmsysProcess_SetWorkingDirectory(cp, dir);
  854. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  855. cmsysProcess_SetTimeout(cp, timeout);
  856. cmsysProcess_Execute(cp);
  857. // Initialize tick's
  858. std::string::size_type tick = 0;
  859. std::string::size_type tick_len = 1024;
  860. std::string::size_type tick_line_len = 50;
  861. char* data;
  862. int length;
  863. cmCTestLog(this, HANDLER_OUTPUT,
  864. " Each . represents " << tick_len << " bytes of output" << std::endl
  865. << " " << std::flush);
  866. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  867. {
  868. if ( output )
  869. {
  870. for(int cc =0; cc < length; ++cc)
  871. {
  872. if(data[cc] == 0)
  873. {
  874. data[cc] = '\n';
  875. }
  876. }
  877. output->append(data, length);
  878. while ( output->size() > (tick * tick_len) )
  879. {
  880. tick ++;
  881. cmCTestLog(this, HANDLER_OUTPUT, "." << std::flush);
  882. if ( tick % tick_line_len == 0 && tick > 0 )
  883. {
  884. cmCTestLog(this, HANDLER_OUTPUT, " Size: "
  885. << int((output->size() / 1024.0) + 1) << "K" << std::endl
  886. << " " << std::flush);
  887. }
  888. }
  889. }
  890. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
  891. if ( ofs )
  892. {
  893. ofs << cmCTestLogWrite(data, length);
  894. }
  895. }
  896. cmCTestLog(this, OUTPUT, " Size of output: "
  897. << int(output->size() / 1024.0) << "K" << std::endl);
  898. cmsysProcess_WaitForExit(cp, 0);
  899. int result = cmsysProcess_GetState(cp);
  900. if(result == cmsysProcess_State_Exited)
  901. {
  902. *retVal = cmsysProcess_GetExitValue(cp);
  903. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Command exited with the value: " << *retVal << std::endl);
  904. }
  905. else if(result == cmsysProcess_State_Exception)
  906. {
  907. *retVal = cmsysProcess_GetExitException(cp);
  908. cmCTestLog(this, WARNING, "There was an exception: " << *retVal << std::endl);
  909. }
  910. else if(result == cmsysProcess_State_Expired)
  911. {
  912. cmCTestLog(this, WARNING, "There was a timeout" << std::endl);
  913. }
  914. else if(result == cmsysProcess_State_Error)
  915. {
  916. *output += "\n*** ERROR executing: ";
  917. *output += cmsysProcess_GetErrorString(cp);
  918. cmCTestLog(this, ERROR_MESSAGE, "There was an error: " << cmsysProcess_GetErrorString(cp) << std::endl);
  919. }
  920. cmsysProcess_Delete(cp);
  921. return result;
  922. }
  923. //######################################################################
  924. //######################################################################
  925. //######################################################################
  926. //######################################################################
  927. //----------------------------------------------------------------------
  928. int cmCTest::RunTest(std::vector<const char*> argv,
  929. std::string* output, int *retVal,
  930. std::ostream* log)
  931. {
  932. if(cmSystemTools::SameFile(argv[0], m_CTestSelf.c_str()) &&
  933. !m_ForceNewCTestProcess)
  934. {
  935. cmCTest inst;
  936. inst.m_ConfigType = m_ConfigType;
  937. inst.m_TimeOut = m_TimeOut;
  938. std::vector<std::string> args;
  939. for(unsigned int i =0; i < argv.size(); ++i)
  940. {
  941. if(argv[i])
  942. {
  943. args.push_back(argv[i]);
  944. }
  945. }
  946. if ( *log )
  947. {
  948. *log << "* Run internal CTest" << std::endl;
  949. }
  950. std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory();
  951. *retVal = inst.Run(args, output);
  952. if ( *log )
  953. {
  954. *log << output->c_str();
  955. }
  956. cmSystemTools::ChangeDirectory(oldpath.c_str());
  957. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Internal cmCTest object used to run test." << std::endl
  958. << *output << std::endl);
  959. return cmsysProcess_State_Exited;
  960. }
  961. std::vector<char> tempOutput;
  962. if ( output )
  963. {
  964. *output = "";
  965. }
  966. cmsysProcess* cp = cmsysProcess_New();
  967. cmsysProcess_SetCommand(cp, &*argv.begin());
  968. cmCTestLog(this, DEBUG, "Command is: " << argv[0] << std::endl);
  969. if(cmSystemTools::GetRunCommandHideConsole())
  970. {
  971. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  972. }
  973. cmsysProcess_SetTimeout(cp, m_TimeOut);
  974. cmsysProcess_Execute(cp);
  975. char* data;
  976. int length;
  977. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  978. {
  979. if ( output )
  980. {
  981. tempOutput.insert(tempOutput.end(), data, data+length);
  982. }
  983. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
  984. if ( log )
  985. {
  986. log->write(data, length);
  987. }
  988. }
  989. cmsysProcess_WaitForExit(cp, 0);
  990. if(output)
  991. {
  992. output->append(&*tempOutput.begin(), tempOutput.size());
  993. }
  994. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "-- Process completed" << std::endl);
  995. int result = cmsysProcess_GetState(cp);
  996. if(result == cmsysProcess_State_Exited)
  997. {
  998. *retVal = cmsysProcess_GetExitValue(cp);
  999. }
  1000. else if(result == cmsysProcess_State_Exception)
  1001. {
  1002. *retVal = cmsysProcess_GetExitException(cp);
  1003. std::string outerr = "\n*** Exception executing: ";
  1004. outerr += cmsysProcess_GetExceptionString(cp);
  1005. *output += outerr;
  1006. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl << std::flush);
  1007. }
  1008. else if(result == cmsysProcess_State_Error)
  1009. {
  1010. std::string outerr = "\n*** ERROR executing: ";
  1011. outerr += cmsysProcess_GetErrorString(cp);
  1012. *output += outerr;
  1013. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl << std::flush);
  1014. }
  1015. cmsysProcess_Delete(cp);
  1016. return result;
  1017. }
  1018. //----------------------------------------------------------------------
  1019. void cmCTest::StartXML(std::ostream& ostr)
  1020. {
  1021. ostr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1022. << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName")
  1023. << "\" BuildStamp=\"" << m_CurrentTag << "-"
  1024. << this->GetTestModelString() << "\" Name=\""
  1025. << this->GetCTestConfiguration("Site") << "\" Generator=\"ctest"
  1026. << cmVersion::GetCMakeVersion()
  1027. << "\">" << std::endl;
  1028. }
  1029. //----------------------------------------------------------------------
  1030. void cmCTest::EndXML(std::ostream& ostr)
  1031. {
  1032. ostr << "</Site>" << std::endl;
  1033. }
  1034. //----------------------------------------------------------------------
  1035. int cmCTest::GenerateCTestNotesOutput(std::ostream& os, const cmCTest::tm_VectorOfStrings& files)
  1036. {
  1037. cmCTest::tm_VectorOfStrings::const_iterator it;
  1038. os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1039. << "<?xml-stylesheet type=\"text/xsl\" href=\"Dart/Source/Server/XSL/Build.xsl <file:///Dart/Source/Server/XSL/Build.xsl> \"?>\n"
  1040. << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName") << "\" BuildStamp=\""
  1041. << m_CurrentTag << "-" << this->GetTestModelString() << "\" Name=\""
  1042. << this->GetCTestConfiguration("Site") << "\" Generator=\"ctest"
  1043. << cmVersion::GetCMakeVersion()
  1044. << "\">\n"
  1045. << "<Notes>" << std::endl;
  1046. for ( it = files.begin(); it != files.end(); it ++ )
  1047. {
  1048. cmCTestLog(this, OUTPUT, "\tAdd file: " << it->c_str() << std::endl);
  1049. std::string note_time = this->CurrentTime();
  1050. os << "<Note Name=\"" << this->MakeXMLSafe(it->c_str()) << "\">\n"
  1051. << "<DateTime>" << note_time << "</DateTime>\n"
  1052. << "<Text>" << std::endl;
  1053. std::ifstream ifs(it->c_str());
  1054. if ( ifs )
  1055. {
  1056. std::string line;
  1057. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1058. {
  1059. os << this->MakeXMLSafe(line) << std::endl;
  1060. }
  1061. ifs.close();
  1062. }
  1063. else
  1064. {
  1065. os << "Problem reading file: " << it->c_str() << std::endl;
  1066. cmCTestLog(this, ERROR_MESSAGE, "Problem reading file: " << it->c_str() << " while creating notes" << std::endl);
  1067. }
  1068. os << "</Text>\n"
  1069. << "</Note>" << std::endl;
  1070. }
  1071. os << "</Notes>\n"
  1072. << "</Site>" << std::endl;
  1073. return 1;
  1074. }
  1075. //----------------------------------------------------------------------
  1076. int cmCTest::GenerateNotesFile(const std::vector<cmStdString> &files)
  1077. {
  1078. cmGeneratedFileStream ofs;
  1079. if ( !this->OpenOutputFile(m_CurrentTag, "Notes.xml", ofs) )
  1080. {
  1081. cmCTestLog(this, ERROR_MESSAGE, "Cannot open notes file" << std::endl);
  1082. return 1;
  1083. }
  1084. this->GenerateCTestNotesOutput(ofs, files);
  1085. return 0;
  1086. }
  1087. //----------------------------------------------------------------------
  1088. int cmCTest::GenerateNotesFile(const char* cfiles)
  1089. {
  1090. if ( !cfiles )
  1091. {
  1092. return 1;
  1093. }
  1094. std::vector<cmStdString> files;
  1095. cmCTestLog(this, OUTPUT, "Create notes file" << std::endl);
  1096. files = cmSystemTools::SplitString(cfiles, ';');
  1097. if ( files.size() == 0 )
  1098. {
  1099. return 1;
  1100. }
  1101. return this->GenerateNotesFile(files);
  1102. }
  1103. //----------------------------------------------------------------------
  1104. bool cmCTest::SubmitExtraFiles(const std::vector<cmStdString> &files)
  1105. {
  1106. std::vector<cmStdString>::const_iterator it;
  1107. for ( it = files.begin();
  1108. it != files.end();
  1109. ++ it )
  1110. {
  1111. if ( !cmSystemTools::FileExists(it->c_str()) )
  1112. {
  1113. cmCTestLog(this, ERROR_MESSAGE, "Cannot find extra file: " << it->c_str() << " to submit."
  1114. << std::endl;);
  1115. return false;
  1116. }
  1117. this->AddSubmitFile(it->c_str());
  1118. }
  1119. return true;
  1120. }
  1121. //----------------------------------------------------------------------
  1122. bool cmCTest::SubmitExtraFiles(const char* cfiles)
  1123. {
  1124. if ( !cfiles )
  1125. {
  1126. return 1;
  1127. }
  1128. std::vector<cmStdString> files;
  1129. cmCTestLog(this, OUTPUT, "Submit extra files" << std::endl);
  1130. files = cmSystemTools::SplitString(cfiles, ';');
  1131. if ( files.size() == 0 )
  1132. {
  1133. return 1;
  1134. }
  1135. return this->SubmitExtraFiles(files);
  1136. }
  1137. //----------------------------------------------------------------------
  1138. bool cmCTest::CheckArgument(const std::string& arg, const char* varg1, const char* varg2)
  1139. {
  1140. cmOStringStream ostr;
  1141. ostr << varg1;
  1142. if ( varg2 )
  1143. {
  1144. ostr << ", " << varg2;
  1145. }
  1146. size_t minlen = arg.size();
  1147. size_t lenvarg = strlen(varg1);
  1148. if ( lenvarg < minlen )
  1149. {
  1150. minlen = lenvarg;
  1151. }
  1152. if ( strncmp(arg.c_str(), varg1, minlen) == 0 )
  1153. {
  1154. return true;
  1155. }
  1156. if ( ! varg2 )
  1157. {
  1158. return false;
  1159. }
  1160. minlen = arg.size();
  1161. lenvarg = strlen(varg2);
  1162. if ( lenvarg < minlen )
  1163. {
  1164. minlen = lenvarg;
  1165. }
  1166. if ( strncmp(arg.c_str(), varg2, minlen) == 0 )
  1167. {
  1168. return true;
  1169. }
  1170. return false;
  1171. }
  1172. //----------------------------------------------------------------------
  1173. int cmCTest::Run(std::vector<std::string>const& args, std::string* output)
  1174. {
  1175. this->FindRunningCMake(args[0].c_str());
  1176. const char* ctestExec = "ctest";
  1177. bool cmakeAndTest = false;
  1178. bool performSomeTest = true;
  1179. for(size_t i=1; i < args.size(); ++i)
  1180. {
  1181. std::string arg = args[i];
  1182. if(this->CheckArgument(arg, "--ctest-config") && i < args.size() - 1)
  1183. {
  1184. i++;
  1185. this->m_CTestConfigFile= args[i];
  1186. }
  1187. if(this->CheckArgument(arg, "-C", "--build-config") && i < args.size() - 1)
  1188. {
  1189. i++;
  1190. this->m_ConfigType = args[i];
  1191. cmSystemTools::ReplaceString(this->m_ConfigType, ".\\", "");
  1192. }
  1193. if(this->CheckArgument(arg, "--debug"))
  1194. {
  1195. this->m_Debug = true;
  1196. }
  1197. if(this->CheckArgument(arg, "--show-line-numbers"))
  1198. {
  1199. this->m_ShowLineNumbers = true;
  1200. }
  1201. if(this->CheckArgument(arg, "-Q", "--quiet"))
  1202. {
  1203. this->m_Quiet = true;
  1204. }
  1205. if(this->CheckArgument(arg, "-V", "--verbose"))
  1206. {
  1207. this->m_Verbose = true;
  1208. }
  1209. if(this->CheckArgument(arg, "-VV", "--extra-verbose"))
  1210. {
  1211. this->m_ExtraVerbose = true;
  1212. this->m_Verbose = true;
  1213. }
  1214. if(this->CheckArgument(arg, "-N", "--show-only"))
  1215. {
  1216. this->m_ShowOnly = true;
  1217. }
  1218. if(this->CheckArgument(arg, "-S", "--script") && i < args.size() - 1 )
  1219. {
  1220. this->m_RunConfigurationScript = true;
  1221. i++;
  1222. cmCTestScriptHandler* ch = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  1223. ch->AddConfigurationScript(args[i].c_str());
  1224. }
  1225. if(this->CheckArgument(arg, "-O", "--output-log") && i < args.size() - 1 )
  1226. {
  1227. i++;
  1228. this->SetOutputLogFileName(args[i].c_str());
  1229. }
  1230. if(this->CheckArgument(arg, "--tomorrow-tag"))
  1231. {
  1232. m_TomorrowTag = true;
  1233. }
  1234. if(this->CheckArgument(arg, "--force-new-ctest-process"))
  1235. {
  1236. m_ForceNewCTestProcess = true;
  1237. }
  1238. if(this->CheckArgument(arg, "--interactive-debug-mode") && i < args.size() - 1 )
  1239. {
  1240. i++;
  1241. m_InteractiveDebugMode = cmSystemTools::IsOn(args[i].c_str());
  1242. }
  1243. if(this->CheckArgument(arg, "--submit-index") && i < args.size() - 1 )
  1244. {
  1245. i++;
  1246. m_SubmitIndex = atoi(args[i].c_str());
  1247. if ( m_SubmitIndex < 0 )
  1248. {
  1249. m_SubmitIndex = 0;
  1250. }
  1251. }
  1252. if(this->CheckArgument(arg, "-D", "--dashboard") && i < args.size() - 1 )
  1253. {
  1254. this->m_ProduceXML = true;
  1255. i++;
  1256. std::string targ = args[i];
  1257. if ( targ == "Experimental" )
  1258. {
  1259. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1260. this->SetTest("Start");
  1261. this->SetTest("Configure");
  1262. this->SetTest("Build");
  1263. this->SetTest("Test");
  1264. this->SetTest("Coverage");
  1265. this->SetTest("Submit");
  1266. }
  1267. else if ( targ == "ExperimentalStart" )
  1268. {
  1269. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1270. this->SetTest("Start");
  1271. }
  1272. else if ( targ == "ExperimentalUpdate" )
  1273. {
  1274. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1275. this->SetTest("Update");
  1276. }
  1277. else if ( targ == "ExperimentalConfigure" )
  1278. {
  1279. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1280. this->SetTest("Configure");
  1281. }
  1282. else if ( targ == "ExperimentalBuild" )
  1283. {
  1284. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1285. this->SetTest("Build");
  1286. }
  1287. else if ( targ == "ExperimentalTest" )
  1288. {
  1289. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1290. this->SetTest("Test");
  1291. }
  1292. else if ( targ == "ExperimentalMemCheck"
  1293. || targ == "ExperimentalPurify" )
  1294. {
  1295. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1296. this->SetTest("MemCheck");
  1297. }
  1298. else if ( targ == "ExperimentalCoverage" )
  1299. {
  1300. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1301. this->SetTest("Coverage");
  1302. }
  1303. else if ( targ == "ExperimentalSubmit" )
  1304. {
  1305. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1306. this->SetTest("Submit");
  1307. }
  1308. else if ( targ == "Continuous" )
  1309. {
  1310. this->SetTestModel(cmCTest::CONTINUOUS);
  1311. this->SetTest("Start");
  1312. this->SetTest("Update");
  1313. this->SetTest("Configure");
  1314. this->SetTest("Build");
  1315. this->SetTest("Test");
  1316. this->SetTest("Coverage");
  1317. this->SetTest("Submit");
  1318. }
  1319. else if ( targ == "ContinuousStart" )
  1320. {
  1321. this->SetTestModel(cmCTest::CONTINUOUS);
  1322. this->SetTest("Start");
  1323. }
  1324. else if ( targ == "ContinuousUpdate" )
  1325. {
  1326. this->SetTestModel(cmCTest::CONTINUOUS);
  1327. this->SetTest("Update");
  1328. }
  1329. else if ( targ == "ContinuousConfigure" )
  1330. {
  1331. this->SetTestModel(cmCTest::CONTINUOUS);
  1332. this->SetTest("Configure");
  1333. }
  1334. else if ( targ == "ContinuousBuild" )
  1335. {
  1336. this->SetTestModel(cmCTest::CONTINUOUS);
  1337. this->SetTest("Build");
  1338. }
  1339. else if ( targ == "ContinuousTest" )
  1340. {
  1341. this->SetTestModel(cmCTest::CONTINUOUS);
  1342. this->SetTest("Test");
  1343. }
  1344. else if ( targ == "ContinuousMemCheck"
  1345. || targ == "ContinuousPurify" )
  1346. {
  1347. this->SetTestModel(cmCTest::CONTINUOUS);
  1348. this->SetTest("MemCheck");
  1349. }
  1350. else if ( targ == "ContinuousCoverage" )
  1351. {
  1352. this->SetTestModel(cmCTest::CONTINUOUS);
  1353. this->SetTest("Coverage");
  1354. }
  1355. else if ( targ == "ContinuousSubmit" )
  1356. {
  1357. this->SetTestModel(cmCTest::CONTINUOUS);
  1358. this->SetTest("Submit");
  1359. }
  1360. else if ( targ == "Nightly" )
  1361. {
  1362. this->SetTestModel(cmCTest::NIGHTLY);
  1363. this->SetTest("Start");
  1364. this->SetTest("Update");
  1365. this->SetTest("Configure");
  1366. this->SetTest("Build");
  1367. this->SetTest("Test");
  1368. this->SetTest("Coverage");
  1369. this->SetTest("Submit");
  1370. }
  1371. else if ( targ == "NightlyStart" )
  1372. {
  1373. this->SetTestModel(cmCTest::NIGHTLY);
  1374. this->SetTest("Start");
  1375. }
  1376. else if ( targ == "NightlyUpdate" )
  1377. {
  1378. this->SetTestModel(cmCTest::NIGHTLY);
  1379. this->SetTest("Update");
  1380. }
  1381. else if ( targ == "NightlyConfigure" )
  1382. {
  1383. this->SetTestModel(cmCTest::NIGHTLY);
  1384. this->SetTest("Configure");
  1385. }
  1386. else if ( targ == "NightlyBuild" )
  1387. {
  1388. this->SetTestModel(cmCTest::NIGHTLY);
  1389. this->SetTest("Build");
  1390. }
  1391. else if ( targ == "NightlyTest" )
  1392. {
  1393. this->SetTestModel(cmCTest::NIGHTLY);
  1394. this->SetTest("Test");
  1395. }
  1396. else if ( targ == "NightlyMemCheck"
  1397. || targ == "NightlyPurify" )
  1398. {
  1399. this->SetTestModel(cmCTest::NIGHTLY);
  1400. this->SetTest("MemCheck");
  1401. }
  1402. else if ( targ == "NightlyCoverage" )
  1403. {
  1404. this->SetTestModel(cmCTest::NIGHTLY);
  1405. this->SetTest("Coverage");
  1406. }
  1407. else if ( targ == "NightlySubmit" )
  1408. {
  1409. this->SetTestModel(cmCTest::NIGHTLY);
  1410. this->SetTest("Submit");
  1411. }
  1412. else if ( targ == "MemoryCheck" )
  1413. {
  1414. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1415. this->SetTest("Start");
  1416. this->SetTest("Configure");
  1417. this->SetTest("Build");
  1418. this->SetTest("MemCheck");
  1419. this->SetTest("Coverage");
  1420. this->SetTest("Submit");
  1421. }
  1422. else if ( targ == "NightlyMemoryCheck" )
  1423. {
  1424. this->SetTestModel(cmCTest::NIGHTLY);
  1425. this->SetTest("Start");
  1426. this->SetTest("Update");
  1427. this->SetTest("Configure");
  1428. this->SetTest("Build");
  1429. this->SetTest("MemCheck");
  1430. this->SetTest("Coverage");
  1431. this->SetTest("Submit");
  1432. }
  1433. else
  1434. {
  1435. performSomeTest = false;
  1436. cmCTestLog(this, ERROR_MESSAGE, "CTest -D called with incorrect option: " << targ << std::endl);
  1437. cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
  1438. << " " << ctestExec << " -D Continuous" << std::endl
  1439. << " " << ctestExec << " -D Continuous(Start|Update|Configure|Build)" << std::endl
  1440. << " " << ctestExec << " -D Continuous(Test|Coverage|MemCheck|Submit)" << std::endl
  1441. << " " << ctestExec << " -D Experimental" << std::endl
  1442. << " " << ctestExec << " -D Experimental(Start|Update|Configure|Build)" << std::endl
  1443. << " " << ctestExec << " -D Experimental(Test|Coverage|MemCheck|Submit)" << std::endl
  1444. << " " << ctestExec << " -D Nightly" << std::endl
  1445. << " " << ctestExec << " -D Nightly(Start|Update|Configure|Build)" << std::endl
  1446. << " " << ctestExec << " -D Nightly(Test|Coverage|MemCheck|Submit)" << std::endl
  1447. << " " << ctestExec << " -D NightlyMemoryCheck" << std::endl);
  1448. }
  1449. }
  1450. if(this->CheckArgument(arg, "-T", "--test-action") && (i < args.size() -1) )
  1451. {
  1452. this->m_ProduceXML = true;
  1453. i++;
  1454. if ( !this->SetTest(args[i].c_str(), false) )
  1455. {
  1456. performSomeTest = false;
  1457. cmCTestLog(this, ERROR_MESSAGE, "CTest -T called with incorrect option: "
  1458. << args[i].c_str() << std::endl);
  1459. cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
  1460. << " " << ctestExec << " -T all" << std::endl
  1461. << " " << ctestExec << " -T start" << std::endl
  1462. << " " << ctestExec << " -T update" << std::endl
  1463. << " " << ctestExec << " -T configure" << std::endl
  1464. << " " << ctestExec << " -T build" << std::endl
  1465. << " " << ctestExec << " -T test" << std::endl
  1466. << " " << ctestExec << " -T coverage" << std::endl
  1467. << " " << ctestExec << " -T memcheck" << std::endl
  1468. << " " << ctestExec << " -T notes" << std::endl
  1469. << " " << ctestExec << " -T submit" << std::endl);
  1470. }
  1471. }
  1472. if(this->CheckArgument(arg, "-M", "--test-model") && (i < args.size() -1) )
  1473. {
  1474. i++;
  1475. std::string const& str = args[i];
  1476. if ( cmSystemTools::LowerCase(str) == "nightly" )
  1477. {
  1478. this->SetTestModel(cmCTest::NIGHTLY);
  1479. }
  1480. else if ( cmSystemTools::LowerCase(str) == "continuous" )
  1481. {
  1482. this->SetTestModel(cmCTest::CONTINUOUS);
  1483. }
  1484. else if ( cmSystemTools::LowerCase(str) == "experimental" )
  1485. {
  1486. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1487. }
  1488. else
  1489. {
  1490. performSomeTest = false;
  1491. cmCTestLog(this, ERROR_MESSAGE, "CTest -M called with incorrect option: " << str.c_str() << std::endl);
  1492. cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
  1493. << " " << ctestExec << " -M Continuous" << std::endl
  1494. << " " << ctestExec << " -M Experimental" << std::endl
  1495. << " " << ctestExec << " -M Nightly" << std::endl);
  1496. }
  1497. }
  1498. if(this->CheckArgument(arg, "-I", "--tests-information") && i < args.size() - 1)
  1499. {
  1500. i++;
  1501. this->GetHandler("test")->SetOption("TestsToRunInformation", args[i].c_str());
  1502. }
  1503. if(this->CheckArgument(arg, "-U", "--union"))
  1504. {
  1505. this->GetHandler("test")->SetOption("UseUnion", "true");
  1506. }
  1507. if(this->CheckArgument(arg, "-R", "--tests-regex") && i < args.size() - 1)
  1508. {
  1509. i++;
  1510. this->GetHandler("test")->SetOption("IncludeRegularExpression", args[i].c_str());
  1511. }
  1512. if(this->CheckArgument(arg, "-E", "--exclude-regex") && i < args.size() - 1)
  1513. {
  1514. i++;
  1515. this->GetHandler("test")->SetOption("ExcludeRegularExpression", args[i].c_str());
  1516. }
  1517. if(this->CheckArgument(arg, "--overwrite") && i < args.size() - 1)
  1518. {
  1519. i++;
  1520. this->AddCTestConfigurationOverwrite(args[i].c_str());
  1521. }
  1522. if(this->CheckArgument(arg, "-A", "--add-notes") && i < args.size() - 1)
  1523. {
  1524. this->m_ProduceXML = true;
  1525. this->SetTest("Notes");
  1526. i++;
  1527. this->SetNotesFiles(args[i].c_str());
  1528. }
  1529. if(this->CheckArgument(arg, "--extra-submit") && i < args.size() - 1)
  1530. {
  1531. this->m_ProduceXML = true;
  1532. this->SetTest("Submit");
  1533. i++;
  1534. if ( !this->SubmitExtraFiles(args[i].c_str()) )
  1535. {
  1536. return 0;
  1537. }
  1538. }
  1539. // --build-and-test options
  1540. if(this->CheckArgument(arg, "--build-and-test") && i < args.size() - 1)
  1541. {
  1542. cmakeAndTest = true;
  1543. }
  1544. cmCTest::t_TestingHandlers::iterator it;
  1545. for ( it = m_TestingHandlers.begin(); it != m_TestingHandlers.end(); ++ it )
  1546. {
  1547. if ( !it->second->ProcessCommandLineArguments(arg, i, args) )
  1548. {
  1549. cmCTestLog(this, ERROR_MESSAGE, "Problem parsing command line arguments within a handler");
  1550. return 0;
  1551. }
  1552. }
  1553. }
  1554. // default to the build type of ctest itself
  1555. if(m_ConfigType.size() == 0)
  1556. {
  1557. #ifdef CMAKE_INTDIR
  1558. m_ConfigType = CMAKE_INTDIR;
  1559. #endif
  1560. }
  1561. if(cmakeAndTest)
  1562. {
  1563. m_Verbose = true;
  1564. cmCTestBuildAndTestHandler* handler =
  1565. static_cast<cmCTestBuildAndTestHandler*>(this->GetHandler("buildtest"));
  1566. int retv = handler->ProcessHandler();
  1567. /*
  1568. cmSystemTools::ResetErrorOccuredFlag();
  1569. cmListFileCache::ClearCache();
  1570. int retv = this->RunCMakeAndTest(output);
  1571. cmSystemTools::ResetErrorOccuredFlag();
  1572. cmListFileCache::ClearCache();
  1573. */
  1574. *output = handler->GetOutput();
  1575. #ifdef CMAKE_BUILD_WITH_CMAKE
  1576. cmDynamicLoader::FlushCache();
  1577. #endif
  1578. return retv;
  1579. }
  1580. if(performSomeTest )
  1581. {
  1582. int res;
  1583. // call process directory
  1584. if (this->m_RunConfigurationScript)
  1585. {
  1586. cmCTest::t_TestingHandlers::iterator it;
  1587. for ( it = m_TestingHandlers.begin(); it != m_TestingHandlers.end(); ++ it )
  1588. {
  1589. it->second->SetVerbose(this->m_ExtraVerbose);
  1590. it->second->SetSubmitIndex(m_SubmitIndex);
  1591. }
  1592. this->GetHandler("script")->SetVerbose(m_Verbose);
  1593. res = this->GetHandler("script")->ProcessHandler();
  1594. }
  1595. else
  1596. {
  1597. m_ExtraVerbose = m_Verbose;
  1598. m_Verbose = true;
  1599. cmCTest::t_TestingHandlers::iterator it;
  1600. for ( it = m_TestingHandlers.begin(); it != m_TestingHandlers.end(); ++ it )
  1601. {
  1602. it->second->SetVerbose(this->m_Verbose);
  1603. it->second->SetSubmitIndex(m_SubmitIndex);
  1604. }
  1605. if ( !this->Initialize(cmSystemTools::GetCurrentWorkingDirectory().c_str()) )
  1606. {
  1607. res = 12;
  1608. }
  1609. else
  1610. {
  1611. res = this->ProcessTests();
  1612. }
  1613. this->Finalize();
  1614. }
  1615. return res;
  1616. }
  1617. return 1;
  1618. }
  1619. //----------------------------------------------------------------------
  1620. void cmCTest::FindRunningCMake(const char* arg0)
  1621. {
  1622. // Find our own executable.
  1623. std::vector<cmStdString> failures;
  1624. m_CTestSelf = arg0;
  1625. cmSystemTools::ConvertToUnixSlashes(m_CTestSelf);
  1626. failures.push_back(m_CTestSelf);
  1627. m_CTestSelf = cmSystemTools::FindProgram(m_CTestSelf.c_str());
  1628. if(!cmSystemTools::FileExists(m_CTestSelf.c_str()))
  1629. {
  1630. failures.push_back(m_CTestSelf);
  1631. m_CTestSelf = "/usr/local/bin/ctest";
  1632. }
  1633. if(!cmSystemTools::FileExists(m_CTestSelf.c_str()))
  1634. {
  1635. failures.push_back(m_CTestSelf);
  1636. cmOStringStream msg;
  1637. msg << "CTEST can not find the command line program ctest.\n";
  1638. msg << " argv[0] = \"" << arg0 << "\"\n";
  1639. msg << " Attempted paths:\n";
  1640. std::vector<cmStdString>::iterator i;
  1641. for(i=failures.begin(); i != failures.end(); ++i)
  1642. {
  1643. msg << " \"" << i->c_str() << "\"\n";
  1644. }
  1645. cmSystemTools::Error(msg.str().c_str());
  1646. }
  1647. std::string dir;
  1648. std::string file;
  1649. if(cmSystemTools::SplitProgramPath(m_CTestSelf.c_str(),
  1650. dir, file, true))
  1651. {
  1652. m_CMakeSelf = dir += "/cmake";
  1653. m_CMakeSelf += cmSystemTools::GetExecutableExtension();
  1654. if(cmSystemTools::FileExists(m_CMakeSelf.c_str()))
  1655. {
  1656. return;
  1657. }
  1658. }
  1659. failures.push_back(m_CMakeSelf);
  1660. #ifdef CMAKE_BUILD_DIR
  1661. std::string intdir = ".";
  1662. #ifdef CMAKE_INTDIR
  1663. intdir = CMAKE_INTDIR;
  1664. #endif
  1665. m_CMakeSelf = CMAKE_BUILD_DIR;
  1666. m_CMakeSelf += "/bin/";
  1667. m_CMakeSelf += intdir;
  1668. m_CMakeSelf += "/cmake";
  1669. m_CMakeSelf += cmSystemTools::GetExecutableExtension();
  1670. #endif
  1671. if(!cmSystemTools::FileExists(m_CMakeSelf.c_str()))
  1672. {
  1673. failures.push_back(m_CMakeSelf);
  1674. cmOStringStream msg;
  1675. msg << "CTEST can not find the command line program cmake.\n";
  1676. msg << " argv[0] = \"" << arg0 << "\"\n";
  1677. msg << " Attempted paths:\n";
  1678. std::vector<cmStdString>::iterator i;
  1679. for(i=failures.begin(); i != failures.end(); ++i)
  1680. {
  1681. msg << " \"" << i->c_str() << "\"\n";
  1682. }
  1683. cmSystemTools::Error(msg.str().c_str());
  1684. }
  1685. }
  1686. //----------------------------------------------------------------------
  1687. void cmCTest::SetNotesFiles(const char* notes)
  1688. {
  1689. if ( !notes )
  1690. {
  1691. return;
  1692. }
  1693. m_NotesFiles = notes;
  1694. }
  1695. //----------------------------------------------------------------------
  1696. int cmCTest::ReadCustomConfigurationFileTree(const char* dir)
  1697. {
  1698. tm_VectorOfStrings dirs;
  1699. tm_VectorOfStrings ndirs;
  1700. cmake cm;
  1701. cmGlobalGenerator gg;
  1702. gg.SetCMakeInstance(&cm);
  1703. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1704. lg->SetGlobalGenerator(&gg);
  1705. cmMakefile *mf = lg->GetMakefile();
  1706. std::string rexpr = dir;
  1707. rexpr += "/CTestCustom.ctest";
  1708. cmGlob gl;
  1709. gl.RecurseOn();
  1710. gl.FindFiles(rexpr);
  1711. std::vector<std::string>& files = gl.GetFiles();
  1712. std::vector<std::string>::iterator fileIt;
  1713. for ( fileIt = files.begin(); fileIt != files.end();
  1714. ++ fileIt )
  1715. {
  1716. cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: " << fileIt->c_str() << std::endl);
  1717. if ( !lg->GetMakefile()->ReadListFile(0, fileIt->c_str()) ||
  1718. cmSystemTools::GetErrorOccuredFlag() )
  1719. {
  1720. cmCTestLog(this, ERROR_MESSAGE, "Problem reading custom configuration: " << fileIt->c_str() << std::endl);
  1721. }
  1722. }
  1723. cmCTest::t_TestingHandlers::iterator it;
  1724. for ( it = m_TestingHandlers.begin(); it != m_TestingHandlers.end(); ++ it )
  1725. {
  1726. it->second->PopulateCustomVectors(mf);
  1727. }
  1728. return 1;
  1729. }
  1730. //----------------------------------------------------------------------
  1731. void cmCTest::PopulateCustomVector(cmMakefile* mf, const char* def, tm_VectorOfStrings& vec)
  1732. {
  1733. if ( !def)
  1734. {
  1735. return;
  1736. }
  1737. const char* dval = mf->GetDefinition(def);
  1738. if ( !dval )
  1739. {
  1740. return;
  1741. }
  1742. std::vector<std::string> slist;
  1743. cmSystemTools::ExpandListArgument(dval, slist);
  1744. std::vector<std::string>::iterator it;
  1745. for ( it = slist.begin(); it != slist.end(); ++it )
  1746. {
  1747. vec.push_back(it->c_str());
  1748. }
  1749. }
  1750. //----------------------------------------------------------------------
  1751. void cmCTest::PopulateCustomInteger(cmMakefile* mf, const char* def, int& val)
  1752. {
  1753. if ( !def)
  1754. {
  1755. return;
  1756. }
  1757. const char* dval = mf->GetDefinition(def);
  1758. if ( !dval )
  1759. {
  1760. return;
  1761. }
  1762. val = atoi(dval);
  1763. }
  1764. //----------------------------------------------------------------------
  1765. std::string cmCTest::GetShortPathToFile(const char* cfname)
  1766. {
  1767. const std::string& sourceDir = this->GetCTestConfiguration("SourceDirectory");
  1768. const std::string& buildDir = this->GetCTestConfiguration("BuildDirectory");
  1769. std::string fname = cmSystemTools::CollapseFullPath(cfname);
  1770. // Find relative paths to both directories
  1771. std::string srcRelpath
  1772. = cmSystemTools::RelativePath(sourceDir.c_str(), fname.c_str());
  1773. std::string bldRelpath
  1774. = cmSystemTools::RelativePath(buildDir.c_str(), fname.c_str());
  1775. // If any contains "." it is not parent directory
  1776. bool inSrc = srcRelpath.find("..") == srcRelpath.npos;
  1777. bool inBld = bldRelpath.find("..") == bldRelpath.npos;
  1778. // TODO: Handle files with .. in their name
  1779. std::string* res = 0;
  1780. if ( inSrc && inBld )
  1781. {
  1782. // If both have relative path with no dots, pick the shorter one
  1783. if ( srcRelpath.size() < bldRelpath.size() )
  1784. {
  1785. res = &srcRelpath;
  1786. }
  1787. else
  1788. {
  1789. res = &bldRelpath;
  1790. }
  1791. }
  1792. else if ( inSrc )
  1793. {
  1794. res = &srcRelpath;
  1795. }
  1796. else if ( inBld )
  1797. {
  1798. res = &bldRelpath;
  1799. }
  1800. if ( !res )
  1801. {
  1802. return fname;
  1803. }
  1804. cmSystemTools::ConvertToUnixSlashes(*res);
  1805. std::string path = "./" + *res;
  1806. if ( path[path.size()-1] == '/' )
  1807. {
  1808. path = path.substr(0, path.size()-1);
  1809. }
  1810. return path;
  1811. }
  1812. //----------------------------------------------------------------------
  1813. std::string cmCTest::GetCTestConfiguration(const char *name)
  1814. {
  1815. if ( m_CTestConfigurationOverwrites.find(name) != m_CTestConfigurationOverwrites.end() )
  1816. {
  1817. return m_CTestConfigurationOverwrites[name];
  1818. }
  1819. return m_CTestConfiguration[name];
  1820. }
  1821. //----------------------------------------------------------------------
  1822. void cmCTest::EmptyCTestConfiguration()
  1823. {
  1824. m_CTestConfiguration.clear();
  1825. }
  1826. //----------------------------------------------------------------------
  1827. void cmCTest::SetCTestConfiguration(const char *name, const char* value)
  1828. {
  1829. if ( !name )
  1830. {
  1831. return;
  1832. }
  1833. if ( !value )
  1834. {
  1835. m_CTestConfiguration.erase(name);
  1836. return;
  1837. }
  1838. m_CTestConfiguration[name] = value;
  1839. }
  1840. //----------------------------------------------------------------------
  1841. std::string cmCTest::GetCurrentTag()
  1842. {
  1843. return m_CurrentTag;
  1844. }
  1845. //----------------------------------------------------------------------
  1846. std::string cmCTest::GetBinaryDir()
  1847. {
  1848. return m_BinaryDir;
  1849. }
  1850. //----------------------------------------------------------------------
  1851. std::string cmCTest::GetConfigType()
  1852. {
  1853. return m_ConfigType;
  1854. }
  1855. //----------------------------------------------------------------------
  1856. bool cmCTest::GetShowOnly()
  1857. {
  1858. return m_ShowOnly;
  1859. }
  1860. //----------------------------------------------------------------------
  1861. void cmCTest::SetProduceXML(bool v)
  1862. {
  1863. m_ProduceXML = v;
  1864. }
  1865. //----------------------------------------------------------------------
  1866. bool cmCTest::GetProduceXML()
  1867. {
  1868. return m_ProduceXML;
  1869. }
  1870. //----------------------------------------------------------------------
  1871. void cmCTest::AddSubmitFile(const char* name)
  1872. {
  1873. m_SubmitFiles.insert(name);
  1874. }
  1875. //----------------------------------------------------------------------
  1876. void cmCTest::AddCTestConfigurationOverwrite(const char* encstr)
  1877. {
  1878. std::string overStr = encstr;
  1879. size_t epos = overStr.find("=");
  1880. if ( epos == overStr.npos )
  1881. {
  1882. cmCTestLog(this, ERROR_MESSAGE,
  1883. "CTest configuration overwrite specified in the wrong format." << std::endl
  1884. << "Valid format is: --overwrite key=value" << std::endl
  1885. << "The specified was: --overwrite " << overStr.c_str() << std::endl);
  1886. return;
  1887. }
  1888. std::string key = overStr.substr(0, epos);
  1889. std::string value = overStr.substr(epos+1, overStr.npos);
  1890. m_CTestConfigurationOverwrites[key] = value;
  1891. }
  1892. //----------------------------------------------------------------------
  1893. bool cmCTest::SetCTestConfigurationFromCMakeVariable(cmMakefile* mf, const char* dconfig, const char* cmake_var)
  1894. {
  1895. const char* ctvar;
  1896. ctvar = mf->GetDefinition(cmake_var);
  1897. if ( !ctvar )
  1898. {
  1899. return false;
  1900. }
  1901. this->SetCTestConfiguration(dconfig, ctvar);
  1902. return true;
  1903. }
  1904. bool cmCTest::RunCommand(
  1905. const char* command,
  1906. std::string* stdOut,
  1907. std::string* stdErr,
  1908. int *retVal,
  1909. const char* dir,
  1910. double timeout)
  1911. {
  1912. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  1913. if(args.size() < 1)
  1914. {
  1915. return false;
  1916. }
  1917. std::vector<const char*> argv;
  1918. for(std::vector<cmStdString>::const_iterator a = args.begin();
  1919. a != args.end(); ++a)
  1920. {
  1921. argv.push_back(a->c_str());
  1922. }
  1923. argv.push_back(0);
  1924. *stdOut = "";
  1925. *stdErr = "";
  1926. cmsysProcess* cp = cmsysProcess_New();
  1927. cmsysProcess_SetCommand(cp, &*argv.begin());
  1928. cmsysProcess_SetWorkingDirectory(cp, dir);
  1929. if(cmSystemTools::GetRunCommandHideConsole())
  1930. {
  1931. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  1932. }
  1933. cmsysProcess_SetTimeout(cp, timeout);
  1934. cmsysProcess_Execute(cp);
  1935. std::vector<char> tempOutput;
  1936. std::vector<char> tempError;
  1937. char* data;
  1938. int length;
  1939. int res;
  1940. bool done = false;
  1941. while(!done)
  1942. {
  1943. res = cmsysProcess_WaitForData(cp, &data, &length, 0);
  1944. switch ( res )
  1945. {
  1946. case cmsysProcess_Pipe_STDOUT:
  1947. tempOutput.insert(tempOutput.end(), data, data+length);
  1948. break;
  1949. case cmsysProcess_Pipe_STDERR:
  1950. tempError.insert(tempError.end(), data, data+length);
  1951. break;
  1952. default:
  1953. done = true;
  1954. }
  1955. if ( (res == cmsysProcess_Pipe_STDOUT || res == cmsysProcess_Pipe_STDERR) && m_ExtraVerbose )
  1956. {
  1957. cmSystemTools::Stdout(data, length);
  1958. }
  1959. }
  1960. cmsysProcess_WaitForExit(cp, 0);
  1961. stdOut->append(&*tempOutput.begin(), tempOutput.size());
  1962. stdErr->append(&*tempError.begin(), tempError.size());
  1963. bool result = true;
  1964. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  1965. {
  1966. if ( retVal )
  1967. {
  1968. *retVal = cmsysProcess_GetExitValue(cp);
  1969. }
  1970. else
  1971. {
  1972. if ( cmsysProcess_GetExitValue(cp) != 0 )
  1973. {
  1974. result = false;
  1975. }
  1976. }
  1977. }
  1978. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
  1979. {
  1980. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  1981. cmCTestLog(this, ERROR_MESSAGE, exception_str << std::endl);
  1982. stdErr->append(exception_str, strlen(exception_str));
  1983. result = false;
  1984. }
  1985. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  1986. {
  1987. const char* error_str = cmsysProcess_GetErrorString(cp);
  1988. cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
  1989. stdErr->append(error_str, strlen(error_str));
  1990. result = false;
  1991. }
  1992. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  1993. {
  1994. const char* error_str = "Process terminated due to timeout\n";
  1995. cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
  1996. stdErr->append(error_str, strlen(error_str));
  1997. result = false;
  1998. }
  1999. cmsysProcess_Delete(cp);
  2000. return result;
  2001. }
  2002. //----------------------------------------------------------------------
  2003. void cmCTest::SetOutputLogFileName(const char* name)
  2004. {
  2005. if ( m_OutputLogFile)
  2006. {
  2007. delete m_OutputLogFile;
  2008. m_OutputLogFile= 0;
  2009. }
  2010. if ( name )
  2011. {
  2012. m_OutputLogFile = new cmGeneratedFileStream(name);
  2013. }
  2014. }
  2015. //----------------------------------------------------------------------
  2016. static const char* cmCTestStringLogType[] =
  2017. {
  2018. "DEBUG",
  2019. "OUTPUT",
  2020. "HANDLER_OUTPUT",
  2021. "HANDLER_VERBOSE_OUTPUT",
  2022. "WARNING",
  2023. "ERROR_MESSAGE",
  2024. 0
  2025. };
  2026. //----------------------------------------------------------------------
  2027. #ifdef cerr
  2028. # undef cerr
  2029. #endif
  2030. #ifdef cout
  2031. # undef cout
  2032. #endif
  2033. #define cmCTestLogOutputFileLine(stream) \
  2034. if ( m_ShowLineNumbers ) \
  2035. { \
  2036. (stream) << std::endl << file << ":" << line << " "; \
  2037. }
  2038. void cmCTest::Log(int logType, const char* file, int line, const char* msg)
  2039. {
  2040. if ( !msg || !*msg )
  2041. {
  2042. return;
  2043. }
  2044. if ( m_OutputLogFile )
  2045. {
  2046. bool display = true;
  2047. if ( logType == cmCTest::DEBUG && !m_Debug ) { display = false; }
  2048. if ( logType == cmCTest::HANDLER_VERBOSE_OUTPUT && !m_Debug && !m_ExtraVerbose ) { display = false; }
  2049. if ( display )
  2050. {
  2051. cmCTestLogOutputFileLine(*m_OutputLogFile);
  2052. if ( logType != m_OutputLogFileLastTag )
  2053. {
  2054. *m_OutputLogFile << "[";
  2055. if ( logType >= OTHER || logType < 0 )
  2056. {
  2057. *m_OutputLogFile << "OTHER";
  2058. }
  2059. else
  2060. {
  2061. *m_OutputLogFile << cmCTestStringLogType[logType];
  2062. }
  2063. *m_OutputLogFile << "] " << std::endl << std::flush;
  2064. }
  2065. *m_OutputLogFile << msg << std::flush;
  2066. if ( logType != m_OutputLogFileLastTag )
  2067. {
  2068. *m_OutputLogFile << std::endl << std::flush;
  2069. m_OutputLogFileLastTag = logType;
  2070. }
  2071. }
  2072. }
  2073. if ( !m_Quiet )
  2074. {
  2075. switch ( logType )
  2076. {
  2077. case DEBUG:
  2078. if ( m_Debug )
  2079. {
  2080. cmCTestLogOutputFileLine(std::cout);
  2081. std::cout << msg;
  2082. std::cout.flush();
  2083. }
  2084. break;
  2085. case OUTPUT: case HANDLER_OUTPUT:
  2086. if ( m_Debug || m_Verbose )
  2087. {
  2088. cmCTestLogOutputFileLine(std::cout);
  2089. std::cout << msg;
  2090. std::cout.flush();
  2091. }
  2092. break;
  2093. case HANDLER_VERBOSE_OUTPUT:
  2094. if ( m_Debug || m_ExtraVerbose )
  2095. {
  2096. cmCTestLogOutputFileLine(std::cout);
  2097. std::cout << msg;
  2098. std::cout.flush();
  2099. }
  2100. break;
  2101. case WARNING:
  2102. cmCTestLogOutputFileLine(std::cerr);
  2103. std::cerr << msg;
  2104. std::cerr.flush();
  2105. break;
  2106. case ERROR_MESSAGE:
  2107. cmCTestLogOutputFileLine(std::cerr);
  2108. std::cerr << msg;
  2109. std::cerr.flush();
  2110. cmSystemTools::SetErrorOccured();
  2111. break;
  2112. default:
  2113. cmCTestLogOutputFileLine(std::cout);
  2114. std::cout << msg;
  2115. std::cout.flush();
  2116. }
  2117. }
  2118. }