cmCTest.cxx 64 KB

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