cmCTest.cxx 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cm_curl.h"
  11. #include "cmCTest.h"
  12. #include "cmake.h"
  13. #include "cmMakefile.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmGlobalGenerator.h"
  16. #include <cmsys/Base64.h>
  17. #include <cmsys/Directory.hxx>
  18. #include <cmsys/SystemInformation.hxx>
  19. #include "cmDynamicLoader.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "cmXMLSafe.h"
  22. #include "cmVersionMacros.h"
  23. #include "cmCTestCommand.h"
  24. #include "cmCTestStartCommand.h"
  25. #include "cmCTestBuildHandler.h"
  26. #include "cmCTestBuildAndTestHandler.h"
  27. #include "cmCTestConfigureHandler.h"
  28. #include "cmCTestCoverageHandler.h"
  29. #include "cmCTestMemCheckHandler.h"
  30. #include "cmCTestScriptHandler.h"
  31. #include "cmCTestSubmitHandler.h"
  32. #include "cmCTestTestHandler.h"
  33. #include "cmCTestUpdateHandler.h"
  34. #include "cmCTestUploadHandler.h"
  35. #include "cmVersion.h"
  36. #include <cmsys/RegularExpression.hxx>
  37. #include <cmsys/Process.h>
  38. #include <cmsys/Glob.hxx>
  39. #include <stdlib.h>
  40. #include <math.h>
  41. #include <float.h>
  42. #include <ctype.h>
  43. #include <memory> // auto_ptr
  44. #include <cm_zlib.h>
  45. #include <cmsys/Base64.h>
  46. #if defined(__BEOS__) && !defined(__HAIKU__)
  47. #include <be/kernel/OS.h> /* disable_debugger() API. */
  48. #endif
  49. #if defined(__HAIKU__)
  50. #include <os/kernel/OS.h> /* disable_debugger() API. */
  51. #endif
  52. #define DEBUGOUT std::cout << __LINE__ << " "; std::cout
  53. #define DEBUGERR std::cerr << __LINE__ << " "; std::cerr
  54. //----------------------------------------------------------------------
  55. struct tm* cmCTest::GetNightlyTime(std::string str,
  56. bool tomorrowtag)
  57. {
  58. struct tm* lctime;
  59. time_t tctime = time(0);
  60. lctime = gmtime(&tctime);
  61. char buf[1024];
  62. // add todays year day and month to the time in str because
  63. // curl_getdate no longer assumes the day is today
  64. sprintf(buf, "%d%02d%02d %s",
  65. lctime->tm_year+1900,
  66. lctime->tm_mon +1,
  67. lctime->tm_mday,
  68. str.c_str());
  69. cmCTestLog(this, OUTPUT, "Determine Nightly Start Time" << std::endl
  70. << " Specified time: " << str.c_str() << std::endl);
  71. //Convert the nightly start time to seconds. Since we are
  72. //providing only a time and a timezone, the current date of
  73. //the local machine is assumed. Consequently, nightlySeconds
  74. //is the time at which the nightly dashboard was opened or
  75. //will be opened on the date of the current client machine.
  76. //As such, this time may be in the past or in the future.
  77. time_t ntime = curl_getdate(buf, &tctime);
  78. cmCTestLog(this, DEBUG, " Get curl time: " << ntime << std::endl);
  79. tctime = time(0);
  80. cmCTestLog(this, DEBUG, " Get the current time: " << tctime << std::endl);
  81. const int dayLength = 24 * 60 * 60;
  82. cmCTestLog(this, DEBUG, "Seconds: " << tctime << std::endl);
  83. while ( ntime > tctime )
  84. {
  85. // If nightlySeconds is in the past, this is the current
  86. // open dashboard, then return nightlySeconds. If
  87. // nightlySeconds is in the future, this is the next
  88. // dashboard to be opened, so subtract 24 hours to get the
  89. // time of the current open dashboard
  90. ntime -= dayLength;
  91. cmCTestLog(this, DEBUG, "Pick yesterday" << std::endl);
  92. cmCTestLog(this, DEBUG, " Future time, subtract day: " << ntime
  93. << std::endl);
  94. }
  95. while ( tctime > (ntime + dayLength) )
  96. {
  97. ntime += dayLength;
  98. cmCTestLog(this, DEBUG, " Past time, add day: " << ntime << std::endl);
  99. }
  100. cmCTestLog(this, DEBUG, "nightlySeconds: " << ntime << std::endl);
  101. cmCTestLog(this, DEBUG, " Current time: " << tctime
  102. << " Nightly time: " << ntime << std::endl);
  103. if ( tomorrowtag )
  104. {
  105. cmCTestLog(this, OUTPUT, " Use future tag, Add a day" << std::endl);
  106. ntime += dayLength;
  107. }
  108. lctime = gmtime(&ntime);
  109. return lctime;
  110. }
  111. //----------------------------------------------------------------------
  112. std::string cmCTest::CleanString(const std::string& str)
  113. {
  114. std::string::size_type spos = str.find_first_not_of(" \n\t\r\f\v");
  115. std::string::size_type epos = str.find_last_not_of(" \n\t\r\f\v");
  116. if ( spos == str.npos )
  117. {
  118. return std::string();
  119. }
  120. if ( epos != str.npos )
  121. {
  122. epos = epos - spos + 1;
  123. }
  124. return str.substr(spos, epos);
  125. }
  126. //----------------------------------------------------------------------
  127. std::string cmCTest::CurrentTime()
  128. {
  129. time_t currenttime = time(0);
  130. struct tm* t = localtime(&currenttime);
  131. //return ::CleanString(ctime(&currenttime));
  132. char current_time[1024];
  133. if ( this->ShortDateFormat )
  134. {
  135. strftime(current_time, 1000, "%b %d %H:%M %Z", t);
  136. }
  137. else
  138. {
  139. strftime(current_time, 1000, "%a %b %d %H:%M:%S %Z %Y", t);
  140. }
  141. cmCTestLog(this, DEBUG, " Current_Time: " << current_time << std::endl);
  142. return cmXMLSafe(cmCTest::CleanString(current_time)).str();
  143. }
  144. //----------------------------------------------------------------------
  145. std::string cmCTest::GetCostDataFile()
  146. {
  147. std::string fname = this->GetCTestConfiguration("CostDataFile");
  148. if(fname == "")
  149. {
  150. fname= this->GetBinaryDir() + "/Testing/Temporary/CTestCostData.txt";
  151. }
  152. return fname;
  153. }
  154. #ifdef CMAKE_BUILD_WITH_CMAKE
  155. //----------------------------------------------------------------------------
  156. static size_t
  157. HTTPResponseCallback(void *ptr, size_t size, size_t nmemb, void *data)
  158. {
  159. register int realsize = (int)(size * nmemb);
  160. std::string *response
  161. = static_cast<std::string*>(data);
  162. const char* chPtr = static_cast<char*>(ptr);
  163. *response += chPtr;
  164. return realsize;
  165. }
  166. //----------------------------------------------------------------------------
  167. int cmCTest::HTTPRequest(std::string url, HTTPMethod method,
  168. std::string& response,
  169. std::string fields,
  170. std::string putFile, int timeout)
  171. {
  172. CURL* curl;
  173. FILE* file;
  174. ::curl_global_init(CURL_GLOBAL_ALL);
  175. curl = ::curl_easy_init();
  176. //set request options based on method
  177. switch(method)
  178. {
  179. case cmCTest::HTTP_POST:
  180. ::curl_easy_setopt(curl, CURLOPT_POST, 1);
  181. ::curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str());
  182. break;
  183. case cmCTest::HTTP_PUT:
  184. if(!cmSystemTools::FileExists(putFile.c_str()))
  185. {
  186. response = "Error: File ";
  187. response += putFile + " does not exist.\n";
  188. return -1;
  189. }
  190. ::curl_easy_setopt(curl, CURLOPT_PUT, 1);
  191. file = ::fopen(putFile.c_str(), "rb");
  192. ::curl_easy_setopt(curl, CURLOPT_INFILE, file);
  193. //fall through to append GET fields
  194. case cmCTest::HTTP_GET:
  195. if(fields.size())
  196. {
  197. url += "?" + fields;
  198. }
  199. break;
  200. default:
  201. break;
  202. }
  203. ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  204. ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  205. ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  206. //set response options
  207. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HTTPResponseCallback);
  208. ::curl_easy_setopt(curl, CURLOPT_FILE, (void *)&response);
  209. ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  210. CURLcode res = ::curl_easy_perform(curl);
  211. ::curl_easy_cleanup(curl);
  212. ::curl_global_cleanup();
  213. return static_cast<int>(res);
  214. }
  215. #endif
  216. //----------------------------------------------------------------------
  217. std::string cmCTest::MakeURLSafe(const std::string& str)
  218. {
  219. cmOStringStream ost;
  220. char buffer[10];
  221. for ( std::string::size_type pos = 0; pos < str.size(); pos ++ )
  222. {
  223. unsigned char ch = str[pos];
  224. if ( ( ch > 126 || ch < 32 ||
  225. ch == '&' ||
  226. ch == '%' ||
  227. ch == '+' ||
  228. ch == '=' ||
  229. ch == '@'
  230. ) && ch != 9 )
  231. {
  232. sprintf(buffer, "%02x;", (unsigned int)ch);
  233. ost << buffer;
  234. }
  235. else
  236. {
  237. ost << ch;
  238. }
  239. }
  240. return ost.str();
  241. }
  242. //----------------------------------------------------------------------------
  243. std::string cmCTest::DecodeURL(const std::string& in)
  244. {
  245. std::string out;
  246. for(const char* c = in.c_str(); *c; ++c)
  247. {
  248. if(*c == '%' && isxdigit(*(c+1)) && isxdigit(*(c+2)))
  249. {
  250. char buf[3] = {*(c+1), *(c+2), 0};
  251. out.append(1, char(strtoul(buf, 0, 16)));
  252. c += 2;
  253. }
  254. else
  255. {
  256. out.append(1, *c);
  257. }
  258. }
  259. return out;
  260. }
  261. //----------------------------------------------------------------------
  262. cmCTest::cmCTest()
  263. {
  264. this->LabelSummary = true;
  265. this->ParallelLevel = 1;
  266. this->SubmitIndex = 0;
  267. this->Failover = false;
  268. this->BatchJobs = false;
  269. this->ForceNewCTestProcess = false;
  270. this->TomorrowTag = false;
  271. this->Verbose = false;
  272. this->Debug = false;
  273. this->ShowLineNumbers = false;
  274. this->Quiet = false;
  275. this->ExtraVerbose = false;
  276. this->ProduceXML = false;
  277. this->ShowOnly = false;
  278. this->RunConfigurationScript = false;
  279. this->UseHTTP10 = false;
  280. this->PrintLabels = false;
  281. this->CompressTestOutput = true;
  282. this->CompressMemCheckOutput = true;
  283. this->TestModel = cmCTest::EXPERIMENTAL;
  284. this->MaxTestNameWidth = 30;
  285. this->InteractiveDebugMode = true;
  286. this->TimeOut = 0;
  287. this->GlobalTimeout = 0;
  288. this->LastStopTimeout = 24 * 60 * 60;
  289. this->CompressXMLFiles = false;
  290. this->CTestConfigFile = "";
  291. this->ScheduleType = "";
  292. this->StopTime = "";
  293. this->NextDayStopTime = false;
  294. this->OutputLogFile = 0;
  295. this->OutputLogFileLastTag = -1;
  296. this->SuppressUpdatingCTestConfiguration = false;
  297. this->DartVersion = 1;
  298. this->OutputTestOutputOnTestFailure = false;
  299. this->ComputedCompressTestOutput = false;
  300. this->ComputedCompressMemCheckOutput = false;
  301. if(cmSystemTools::GetEnv("CTEST_OUTPUT_ON_FAILURE"))
  302. {
  303. this->OutputTestOutputOnTestFailure = true;
  304. }
  305. this->InitStreams();
  306. this->Parts[PartStart].SetName("Start");
  307. this->Parts[PartUpdate].SetName("Update");
  308. this->Parts[PartConfigure].SetName("Configure");
  309. this->Parts[PartBuild].SetName("Build");
  310. this->Parts[PartTest].SetName("Test");
  311. this->Parts[PartCoverage].SetName("Coverage");
  312. this->Parts[PartMemCheck].SetName("MemCheck");
  313. this->Parts[PartSubmit].SetName("Submit");
  314. this->Parts[PartNotes].SetName("Notes");
  315. this->Parts[PartExtraFiles].SetName("ExtraFiles");
  316. this->Parts[PartUpload].SetName("Upload");
  317. // Fill the part name-to-id map.
  318. for(Part p = PartStart; p != PartCount; p = Part(p+1))
  319. {
  320. this->PartMap[cmSystemTools::LowerCase(this->Parts[p].GetName())] = p;
  321. }
  322. this->ShortDateFormat = true;
  323. this->TestingHandlers["build"] = new cmCTestBuildHandler;
  324. this->TestingHandlers["buildtest"] = new cmCTestBuildAndTestHandler;
  325. this->TestingHandlers["coverage"] = new cmCTestCoverageHandler;
  326. this->TestingHandlers["script"] = new cmCTestScriptHandler;
  327. this->TestingHandlers["test"] = new cmCTestTestHandler;
  328. this->TestingHandlers["update"] = new cmCTestUpdateHandler;
  329. this->TestingHandlers["configure"] = new cmCTestConfigureHandler;
  330. this->TestingHandlers["memcheck"] = new cmCTestMemCheckHandler;
  331. this->TestingHandlers["submit"] = new cmCTestSubmitHandler;
  332. this->TestingHandlers["upload"] = new cmCTestUploadHandler;
  333. cmCTest::t_TestingHandlers::iterator it;
  334. for ( it = this->TestingHandlers.begin();
  335. it != this->TestingHandlers.end(); ++ it )
  336. {
  337. it->second->SetCTestInstance(this);
  338. }
  339. // Make sure we can capture the build tool output.
  340. cmSystemTools::EnableVSConsoleOutput();
  341. }
  342. //----------------------------------------------------------------------
  343. cmCTest::~cmCTest()
  344. {
  345. cmCTest::t_TestingHandlers::iterator it;
  346. for ( it = this->TestingHandlers.begin();
  347. it != this->TestingHandlers.end(); ++ it )
  348. {
  349. delete it->second;
  350. it->second = 0;
  351. }
  352. this->SetOutputLogFileName(0);
  353. }
  354. void cmCTest::SetParallelLevel(int level)
  355. {
  356. this->ParallelLevel = level < 1 ? 1 : level;
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmCTest::ShouldCompressTestOutput()
  360. {
  361. if(!this->ComputedCompressTestOutput)
  362. {
  363. std::string cdashVersion = this->GetCDashVersion();
  364. //version >= 1.6?
  365. bool cdashSupportsGzip = cmSystemTools::VersionCompare(
  366. cmSystemTools::OP_GREATER, cdashVersion.c_str(), "1.6") ||
  367. cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL,
  368. cdashVersion.c_str(), "1.6");
  369. this->CompressTestOutput &= cdashSupportsGzip;
  370. this->ComputedCompressTestOutput = true;
  371. }
  372. return this->CompressTestOutput;
  373. }
  374. //----------------------------------------------------------------------------
  375. bool cmCTest::ShouldCompressMemCheckOutput()
  376. {
  377. if(!this->ComputedCompressMemCheckOutput)
  378. {
  379. std::string cdashVersion = this->GetCDashVersion();
  380. bool compressionSupported = cmSystemTools::VersionCompare(
  381. cmSystemTools::OP_GREATER, cdashVersion.c_str(), "1.9.0");
  382. this->CompressMemCheckOutput &= compressionSupported;
  383. this->ComputedCompressMemCheckOutput = true;
  384. }
  385. return this->CompressMemCheckOutput;
  386. }
  387. //----------------------------------------------------------------------------
  388. std::string cmCTest::GetCDashVersion()
  389. {
  390. #ifdef CMAKE_BUILD_WITH_CMAKE
  391. //First query the server. If that fails, fall back to the local setting
  392. std::string response;
  393. std::string url = "http://";
  394. url += this->GetCTestConfiguration("DropSite");
  395. std::string cdashUri = this->GetCTestConfiguration("DropLocation");
  396. cdashUri = cdashUri.substr(0, cdashUri.find("/submit.php"));
  397. url += cdashUri + "/api/getversion.php";
  398. int res = cmCTest::HTTPRequest(url, cmCTest::HTTP_GET, response, "", "", 3);
  399. return res ? this->GetCTestConfiguration("CDashVersion") : response;
  400. #else
  401. return this->GetCTestConfiguration("CDashVersion");
  402. #endif
  403. }
  404. //----------------------------------------------------------------------------
  405. cmCTest::Part cmCTest::GetPartFromName(const char* name)
  406. {
  407. // Look up by lower-case to make names case-insensitive.
  408. std::string lower_name = cmSystemTools::LowerCase(name);
  409. PartMapType::const_iterator i = this->PartMap.find(lower_name);
  410. if(i != this->PartMap.end())
  411. {
  412. return i->second;
  413. }
  414. // The string does not name a valid part.
  415. return PartCount;
  416. }
  417. //----------------------------------------------------------------------
  418. int cmCTest::Initialize(const char* binary_dir, cmCTestStartCommand* command)
  419. {
  420. cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
  421. if(!this->InteractiveDebugMode)
  422. {
  423. this->BlockTestErrorDiagnostics();
  424. }
  425. else
  426. {
  427. cmSystemTools::PutEnv("CTEST_INTERACTIVE_DEBUG_MODE=1");
  428. }
  429. this->BinaryDir = binary_dir;
  430. cmSystemTools::ConvertToUnixSlashes(this->BinaryDir);
  431. this->UpdateCTestConfiguration();
  432. cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
  433. if ( this->ProduceXML )
  434. {
  435. cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
  436. cmCTestLog(this, OUTPUT,
  437. " Site: " << this->GetCTestConfiguration("Site") << std::endl
  438. << " Build name: " << this->GetCTestConfiguration("BuildName")
  439. << std::endl);
  440. cmCTestLog(this, DEBUG, "Produce XML is on" << std::endl);
  441. if ( this->TestModel == cmCTest::NIGHTLY &&
  442. this->GetCTestConfiguration("NightlyStartTime").empty() )
  443. {
  444. cmCTestLog(this, WARNING,
  445. "WARNING: No nightly start time found please set in"
  446. " CTestConfig.cmake or DartConfig.cmake" << std::endl);
  447. cmCTestLog(this, DEBUG, "Here: " << __LINE__ << std::endl);
  448. return 0;
  449. }
  450. }
  451. cmake cm;
  452. cmGlobalGenerator gg;
  453. gg.SetCMakeInstance(&cm);
  454. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  455. cmMakefile *mf = lg->GetMakefile();
  456. if ( !this->ReadCustomConfigurationFileTree(this->BinaryDir.c_str(), mf) )
  457. {
  458. cmCTestLog(this, DEBUG, "Cannot find custom configuration file tree"
  459. << std::endl);
  460. return 0;
  461. }
  462. if ( this->ProduceXML )
  463. {
  464. // Verify "Testing" directory exists:
  465. //
  466. std::string testingDir = this->BinaryDir + "/Testing";
  467. if ( cmSystemTools::FileExists(testingDir.c_str()) )
  468. {
  469. if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
  470. {
  471. cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
  472. << " is in the place of the testing directory" << std::endl);
  473. return 0;
  474. }
  475. }
  476. else
  477. {
  478. if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
  479. {
  480. cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory "
  481. << testingDir << std::endl);
  482. return 0;
  483. }
  484. }
  485. // Create new "TAG" file or read existing one:
  486. //
  487. bool createNewTag = true;
  488. if (command)
  489. {
  490. createNewTag = command->ShouldCreateNewTag();
  491. }
  492. std::string tagfile = testingDir + "/TAG";
  493. std::ifstream tfin(tagfile.c_str());
  494. std::string tag;
  495. if (createNewTag)
  496. {
  497. time_t tctime = time(0);
  498. if ( this->TomorrowTag )
  499. {
  500. tctime += ( 24 * 60 * 60 );
  501. }
  502. struct tm *lctime = gmtime(&tctime);
  503. if ( tfin && cmSystemTools::GetLineFromStream(tfin, tag) )
  504. {
  505. int year = 0;
  506. int mon = 0;
  507. int day = 0;
  508. int hour = 0;
  509. int min = 0;
  510. sscanf(tag.c_str(), "%04d%02d%02d-%02d%02d",
  511. &year, &mon, &day, &hour, &min);
  512. if ( year != lctime->tm_year + 1900 ||
  513. mon != lctime->tm_mon+1 ||
  514. day != lctime->tm_mday )
  515. {
  516. tag = "";
  517. }
  518. std::string tagmode;
  519. if ( cmSystemTools::GetLineFromStream(tfin, tagmode) )
  520. {
  521. if (tagmode.size() > 4 && !this->Parts[PartStart])
  522. {
  523. this->TestModel = cmCTest::GetTestModelFromString(tagmode.c_str());
  524. }
  525. }
  526. tfin.close();
  527. }
  528. if (tag.size() == 0 || (0 != command) || this->Parts[PartStart])
  529. {
  530. cmCTestLog(this, DEBUG, "TestModel: " << this->GetTestModelString()
  531. << std::endl);
  532. cmCTestLog(this, DEBUG, "TestModel: " << this->TestModel << std::endl);
  533. if ( this->TestModel == cmCTest::NIGHTLY )
  534. {
  535. lctime = this->GetNightlyTime(
  536. this->GetCTestConfiguration("NightlyStartTime"),
  537. this->TomorrowTag);
  538. }
  539. char datestring[100];
  540. sprintf(datestring, "%04d%02d%02d-%02d%02d",
  541. lctime->tm_year + 1900,
  542. lctime->tm_mon+1,
  543. lctime->tm_mday,
  544. lctime->tm_hour,
  545. lctime->tm_min);
  546. tag = datestring;
  547. std::ofstream ofs(tagfile.c_str());
  548. if ( ofs )
  549. {
  550. ofs << tag << std::endl;
  551. ofs << this->GetTestModelString() << std::endl;
  552. }
  553. ofs.close();
  554. if ( 0 == command )
  555. {
  556. cmCTestLog(this, OUTPUT, "Create new tag: " << tag << " - "
  557. << this->GetTestModelString() << std::endl);
  558. }
  559. }
  560. }
  561. else
  562. {
  563. if ( tfin )
  564. {
  565. cmSystemTools::GetLineFromStream(tfin, tag);
  566. tfin.close();
  567. }
  568. if ( tag.empty() )
  569. {
  570. cmCTestLog(this, ERROR_MESSAGE,
  571. "Cannot read existing TAG file in " << testingDir
  572. << std::endl);
  573. return 0;
  574. }
  575. cmCTestLog(this, OUTPUT, " Use existing tag: " << tag << " - "
  576. << this->GetTestModelString() << std::endl);
  577. }
  578. this->CurrentTag = tag;
  579. }
  580. return 1;
  581. }
  582. //----------------------------------------------------------------------
  583. bool cmCTest::InitializeFromCommand(cmCTestStartCommand* command)
  584. {
  585. std::string src_dir
  586. = this->GetCTestConfiguration("SourceDirectory").c_str();
  587. std::string bld_dir = this->GetCTestConfiguration("BuildDirectory").c_str();
  588. this->DartVersion = 1;
  589. for(Part p = PartStart; p != PartCount; p = Part(p+1))
  590. {
  591. this->Parts[p].SubmitFiles.clear();
  592. }
  593. cmMakefile* mf = command->GetMakefile();
  594. std::string fname = src_dir;
  595. fname += "/CTestConfig.cmake";
  596. cmSystemTools::ConvertToUnixSlashes(fname);
  597. if ( cmSystemTools::FileExists(fname.c_str()) )
  598. {
  599. cmCTestLog(this, OUTPUT, " Reading ctest configuration file: "
  600. << fname.c_str() << std::endl);
  601. bool readit = mf->ReadListFile(mf->GetCurrentListFile(),
  602. fname.c_str() );
  603. if(!readit)
  604. {
  605. std::string m = "Could not find include file: ";
  606. m += fname;
  607. command->SetError(m.c_str());
  608. return false;
  609. }
  610. }
  611. else
  612. {
  613. cmCTestLog(this, WARNING, "Cannot locate CTest configuration: "
  614. << fname.c_str() << std::endl);
  615. }
  616. this->SetCTestConfigurationFromCMakeVariable(mf, "NightlyStartTime",
  617. "CTEST_NIGHTLY_START_TIME");
  618. this->SetCTestConfigurationFromCMakeVariable(mf, "Site", "CTEST_SITE");
  619. this->SetCTestConfigurationFromCMakeVariable(mf, "BuildName",
  620. "CTEST_BUILD_NAME");
  621. const char* dartVersion = mf->GetDefinition("CTEST_DART_SERVER_VERSION");
  622. if ( dartVersion )
  623. {
  624. this->DartVersion = atoi(dartVersion);
  625. if ( this->DartVersion < 0 )
  626. {
  627. cmCTestLog(this, ERROR_MESSAGE, "Invalid Dart server version: "
  628. << dartVersion << ". Please specify the version number."
  629. << std::endl);
  630. return false;
  631. }
  632. }
  633. if ( !this->Initialize(bld_dir.c_str(), command) )
  634. {
  635. return false;
  636. }
  637. cmCTestLog(this, OUTPUT, " Use " << this->GetTestModelString()
  638. << " tag: " << this->GetCurrentTag() << std::endl);
  639. return true;
  640. }
  641. //----------------------------------------------------------------------
  642. bool cmCTest::UpdateCTestConfiguration()
  643. {
  644. if ( this->SuppressUpdatingCTestConfiguration )
  645. {
  646. return true;
  647. }
  648. std::string fileName = this->CTestConfigFile;
  649. if ( fileName.empty() )
  650. {
  651. fileName = this->BinaryDir + "/CTestConfiguration.ini";
  652. if ( !cmSystemTools::FileExists(fileName.c_str()) )
  653. {
  654. fileName = this->BinaryDir + "/DartConfiguration.tcl";
  655. }
  656. }
  657. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "UpdateCTestConfiguration from :"
  658. << fileName.c_str() << "\n");
  659. if ( !cmSystemTools::FileExists(fileName.c_str()) )
  660. {
  661. // No need to exit if we are not producing XML
  662. if ( this->ProduceXML )
  663. {
  664. cmCTestLog(this, ERROR_MESSAGE, "Cannot find file: " << fileName.c_str()
  665. << std::endl);
  666. return false;
  667. }
  668. }
  669. else
  670. {
  671. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Parse Config file:"
  672. << fileName.c_str() << "\n");
  673. // parse the dart test file
  674. std::ifstream fin(fileName.c_str());
  675. if(!fin)
  676. {
  677. return false;
  678. }
  679. char buffer[1024];
  680. while ( fin )
  681. {
  682. buffer[0] = 0;
  683. fin.getline(buffer, 1023);
  684. buffer[1023] = 0;
  685. std::string line = cmCTest::CleanString(buffer);
  686. if(line.size() == 0)
  687. {
  688. continue;
  689. }
  690. while ( fin && (line[line.size()-1] == '\\') )
  691. {
  692. line = line.substr(0, line.size()-1);
  693. buffer[0] = 0;
  694. fin.getline(buffer, 1023);
  695. buffer[1023] = 0;
  696. line += cmCTest::CleanString(buffer);
  697. }
  698. if ( line[0] == '#' )
  699. {
  700. continue;
  701. }
  702. std::string::size_type cpos = line.find_first_of(":");
  703. if ( cpos == line.npos )
  704. {
  705. continue;
  706. }
  707. std::string key = line.substr(0, cpos);
  708. std::string value
  709. = cmCTest::CleanString(line.substr(cpos+1, line.npos));
  710. this->CTestConfiguration[key] = value;
  711. }
  712. fin.close();
  713. }
  714. if ( !this->GetCTestConfiguration("BuildDirectory").empty() )
  715. {
  716. this->BinaryDir = this->GetCTestConfiguration("BuildDirectory");
  717. cmSystemTools::ChangeDirectory(this->BinaryDir.c_str());
  718. }
  719. this->TimeOut = atoi(this->GetCTestConfiguration("TimeOut").c_str());
  720. if ( this->ProduceXML )
  721. {
  722. this->CompressXMLFiles = cmSystemTools::IsOn(
  723. this->GetCTestConfiguration("CompressSubmission").c_str());
  724. }
  725. return true;
  726. }
  727. //----------------------------------------------------------------------
  728. void cmCTest::BlockTestErrorDiagnostics()
  729. {
  730. cmSystemTools::PutEnv("DART_TEST_FROM_DART=1");
  731. cmSystemTools::PutEnv("DASHBOARD_TEST_FROM_CTEST=" CMake_VERSION);
  732. #if defined(_WIN32)
  733. SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
  734. #elif defined(__BEOS__) || defined(__HAIKU__)
  735. disable_debugger(1);
  736. #endif
  737. }
  738. //----------------------------------------------------------------------
  739. void cmCTest::SetTestModel(int mode)
  740. {
  741. this->InteractiveDebugMode = false;
  742. this->TestModel = mode;
  743. }
  744. //----------------------------------------------------------------------
  745. bool cmCTest::SetTest(const char* ttype, bool report)
  746. {
  747. if ( cmSystemTools::LowerCase(ttype) == "all" )
  748. {
  749. for(Part p = PartStart; p != PartCount; p = Part(p+1))
  750. {
  751. this->Parts[p].Enable();
  752. }
  753. return true;
  754. }
  755. Part p = this->GetPartFromName(ttype);
  756. if(p != PartCount)
  757. {
  758. this->Parts[p].Enable();
  759. return true;
  760. }
  761. else
  762. {
  763. if ( report )
  764. {
  765. cmCTestLog(this, ERROR_MESSAGE, "Don't know about test \"" << ttype
  766. << "\" yet..." << std::endl);
  767. }
  768. return false;
  769. }
  770. }
  771. //----------------------------------------------------------------------
  772. void cmCTest::Finalize()
  773. {
  774. }
  775. //----------------------------------------------------------------------
  776. bool cmCTest::OpenOutputFile(const std::string& path,
  777. const std::string& name, cmGeneratedFileStream& stream,
  778. bool compress)
  779. {
  780. std::string testingDir = this->BinaryDir + "/Testing";
  781. if ( path.size() > 0 )
  782. {
  783. testingDir += "/" + path;
  784. }
  785. if ( cmSystemTools::FileExists(testingDir.c_str()) )
  786. {
  787. if ( !cmSystemTools::FileIsDirectory(testingDir.c_str()) )
  788. {
  789. cmCTestLog(this, ERROR_MESSAGE, "File " << testingDir
  790. << " is in the place of the testing directory"
  791. << std::endl);
  792. return false;
  793. }
  794. }
  795. else
  796. {
  797. if ( !cmSystemTools::MakeDirectory(testingDir.c_str()) )
  798. {
  799. cmCTestLog(this, ERROR_MESSAGE, "Cannot create directory " << testingDir
  800. << std::endl);
  801. return false;
  802. }
  803. }
  804. std::string filename = testingDir + "/" + name;
  805. stream.Open(filename.c_str());
  806. if( !stream )
  807. {
  808. cmCTestLog(this, ERROR_MESSAGE, "Problem opening file: " << filename
  809. << std::endl);
  810. return false;
  811. }
  812. if ( compress )
  813. {
  814. if ( this->CompressXMLFiles )
  815. {
  816. stream.SetCompression(true);
  817. }
  818. }
  819. return true;
  820. }
  821. //----------------------------------------------------------------------
  822. bool cmCTest::AddIfExists(Part part, const char* file)
  823. {
  824. if ( this->CTestFileExists(file) )
  825. {
  826. this->AddSubmitFile(part, file);
  827. }
  828. else
  829. {
  830. std::string name = file;
  831. name += ".gz";
  832. if ( this->CTestFileExists(name.c_str()) )
  833. {
  834. this->AddSubmitFile(part, file);
  835. }
  836. else
  837. {
  838. return false;
  839. }
  840. }
  841. return true;
  842. }
  843. //----------------------------------------------------------------------
  844. bool cmCTest::CTestFileExists(const std::string& filename)
  845. {
  846. std::string testingDir = this->BinaryDir + "/Testing/" +
  847. this->CurrentTag + "/" + filename;
  848. return cmSystemTools::FileExists(testingDir.c_str());
  849. }
  850. //----------------------------------------------------------------------
  851. cmCTestGenericHandler* cmCTest::GetInitializedHandler(const char* handler)
  852. {
  853. cmCTest::t_TestingHandlers::iterator it =
  854. this->TestingHandlers.find(handler);
  855. if ( it == this->TestingHandlers.end() )
  856. {
  857. return 0;
  858. }
  859. it->second->Initialize();
  860. return it->second;
  861. }
  862. //----------------------------------------------------------------------
  863. cmCTestGenericHandler* cmCTest::GetHandler(const char* handler)
  864. {
  865. cmCTest::t_TestingHandlers::iterator it =
  866. this->TestingHandlers.find(handler);
  867. if ( it == this->TestingHandlers.end() )
  868. {
  869. return 0;
  870. }
  871. return it->second;
  872. }
  873. //----------------------------------------------------------------------
  874. int cmCTest::ExecuteHandler(const char* shandler)
  875. {
  876. cmCTestGenericHandler* handler = this->GetHandler(shandler);
  877. if ( !handler )
  878. {
  879. return -1;
  880. }
  881. handler->Initialize();
  882. return handler->ProcessHandler();
  883. }
  884. //----------------------------------------------------------------------
  885. int cmCTest::ProcessTests()
  886. {
  887. int res = 0;
  888. bool notest = true;
  889. int update_count = 0;
  890. for(Part p = PartStart; notest && p != PartCount; p = Part(p+1))
  891. {
  892. notest = !this->Parts[p];
  893. }
  894. if (this->Parts[PartUpdate] &&
  895. (this->GetRemainingTimeAllowed() - 120 > 0))
  896. {
  897. cmCTestGenericHandler* uphandler = this->GetHandler("update");
  898. uphandler->SetPersistentOption("SourceDirectory",
  899. this->GetCTestConfiguration("SourceDirectory").c_str());
  900. update_count = uphandler->ProcessHandler();
  901. if ( update_count < 0 )
  902. {
  903. res |= cmCTest::UPDATE_ERRORS;
  904. }
  905. }
  906. if ( this->TestModel == cmCTest::CONTINUOUS && !update_count )
  907. {
  908. return 0;
  909. }
  910. if (this->Parts[PartConfigure] &&
  911. (this->GetRemainingTimeAllowed() - 120 > 0))
  912. {
  913. if (this->GetHandler("configure")->ProcessHandler() < 0)
  914. {
  915. res |= cmCTest::CONFIGURE_ERRORS;
  916. }
  917. }
  918. if (this->Parts[PartBuild] &&
  919. (this->GetRemainingTimeAllowed() - 120 > 0))
  920. {
  921. this->UpdateCTestConfiguration();
  922. if (this->GetHandler("build")->ProcessHandler() < 0)
  923. {
  924. res |= cmCTest::BUILD_ERRORS;
  925. }
  926. }
  927. if ((this->Parts[PartTest] || notest) &&
  928. (this->GetRemainingTimeAllowed() - 120 > 0))
  929. {
  930. this->UpdateCTestConfiguration();
  931. if (this->GetHandler("test")->ProcessHandler() < 0)
  932. {
  933. res |= cmCTest::TEST_ERRORS;
  934. }
  935. }
  936. if (this->Parts[PartCoverage] &&
  937. (this->GetRemainingTimeAllowed() - 120 > 0))
  938. {
  939. this->UpdateCTestConfiguration();
  940. if (this->GetHandler("coverage")->ProcessHandler() < 0)
  941. {
  942. res |= cmCTest::COVERAGE_ERRORS;
  943. }
  944. }
  945. if (this->Parts[PartMemCheck] &&
  946. (this->GetRemainingTimeAllowed() - 120 > 0))
  947. {
  948. this->UpdateCTestConfiguration();
  949. if (this->GetHandler("memcheck")->ProcessHandler() < 0)
  950. {
  951. res |= cmCTest::MEMORY_ERRORS;
  952. }
  953. }
  954. if ( !notest )
  955. {
  956. std::string notes_dir = this->BinaryDir + "/Testing/Notes";
  957. if ( cmSystemTools::FileIsDirectory(notes_dir.c_str()) )
  958. {
  959. cmsys::Directory d;
  960. d.Load(notes_dir.c_str());
  961. unsigned long kk;
  962. for ( kk = 0; kk < d.GetNumberOfFiles(); kk ++ )
  963. {
  964. const char* file = d.GetFile(kk);
  965. std::string fullname = notes_dir + "/" + file;
  966. if ( cmSystemTools::FileExists(fullname.c_str()) &&
  967. !cmSystemTools::FileIsDirectory(fullname.c_str()) )
  968. {
  969. if ( this->NotesFiles.size() > 0 )
  970. {
  971. this->NotesFiles += ";";
  972. }
  973. this->NotesFiles += fullname;
  974. this->Parts[PartNotes].Enable();
  975. }
  976. }
  977. }
  978. }
  979. if (this->Parts[PartNotes])
  980. {
  981. this->UpdateCTestConfiguration();
  982. if ( this->NotesFiles.size() )
  983. {
  984. this->GenerateNotesFile(this->NotesFiles.c_str());
  985. }
  986. }
  987. if (this->Parts[PartSubmit])
  988. {
  989. this->UpdateCTestConfiguration();
  990. if (this->GetHandler("submit")->ProcessHandler() < 0)
  991. {
  992. res |= cmCTest::SUBMIT_ERRORS;
  993. }
  994. }
  995. if ( res != 0 )
  996. {
  997. cmCTestLog(this, ERROR_MESSAGE, "Errors while running CTest"
  998. << std::endl);
  999. }
  1000. return res;
  1001. }
  1002. //----------------------------------------------------------------------
  1003. std::string cmCTest::GetTestModelString()
  1004. {
  1005. if ( !this->SpecificTrack.empty() )
  1006. {
  1007. return this->SpecificTrack;
  1008. }
  1009. switch ( this->TestModel )
  1010. {
  1011. case cmCTest::NIGHTLY:
  1012. return "Nightly";
  1013. case cmCTest::CONTINUOUS:
  1014. return "Continuous";
  1015. }
  1016. return "Experimental";
  1017. }
  1018. //----------------------------------------------------------------------
  1019. int cmCTest::GetTestModelFromString(const char* str)
  1020. {
  1021. if ( !str )
  1022. {
  1023. return cmCTest::EXPERIMENTAL;
  1024. }
  1025. std::string rstr = cmSystemTools::LowerCase(str);
  1026. if ( strncmp(rstr.c_str(), "cont", 4) == 0 )
  1027. {
  1028. return cmCTest::CONTINUOUS;
  1029. }
  1030. if ( strncmp(rstr.c_str(), "nigh", 4) == 0 )
  1031. {
  1032. return cmCTest::NIGHTLY;
  1033. }
  1034. return cmCTest::EXPERIMENTAL;
  1035. }
  1036. //######################################################################
  1037. //######################################################################
  1038. //######################################################################
  1039. //######################################################################
  1040. //----------------------------------------------------------------------
  1041. int cmCTest::RunMakeCommand(const char* command, std::string* output,
  1042. int* retVal, const char* dir, int timeout, std::ofstream& ofs)
  1043. {
  1044. // First generate the command and arguments
  1045. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  1046. if(args.size() < 1)
  1047. {
  1048. return false;
  1049. }
  1050. std::vector<const char*> argv;
  1051. for(std::vector<cmStdString>::const_iterator a = args.begin();
  1052. a != args.end(); ++a)
  1053. {
  1054. argv.push_back(a->c_str());
  1055. }
  1056. argv.push_back(0);
  1057. if ( output )
  1058. {
  1059. *output = "";
  1060. }
  1061. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Run command:");
  1062. std::vector<const char*>::iterator ait;
  1063. for ( ait = argv.begin(); ait != argv.end() && *ait; ++ ait )
  1064. {
  1065. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, " \"" << *ait << "\"");
  1066. }
  1067. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, std::endl);
  1068. // Now create process object
  1069. cmsysProcess* cp = cmsysProcess_New();
  1070. cmsysProcess_SetCommand(cp, &*argv.begin());
  1071. cmsysProcess_SetWorkingDirectory(cp, dir);
  1072. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  1073. cmsysProcess_SetTimeout(cp, timeout);
  1074. cmsysProcess_Execute(cp);
  1075. // Initialize tick's
  1076. std::string::size_type tick = 0;
  1077. std::string::size_type tick_len = 1024;
  1078. std::string::size_type tick_line_len = 50;
  1079. char* data;
  1080. int length;
  1081. cmCTestLog(this, HANDLER_OUTPUT,
  1082. " Each . represents " << tick_len << " bytes of output" << std::endl
  1083. << " " << std::flush);
  1084. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  1085. {
  1086. if ( output )
  1087. {
  1088. for(int cc =0; cc < length; ++cc)
  1089. {
  1090. if(data[cc] == 0)
  1091. {
  1092. data[cc] = '\n';
  1093. }
  1094. }
  1095. output->append(data, length);
  1096. while ( output->size() > (tick * tick_len) )
  1097. {
  1098. tick ++;
  1099. cmCTestLog(this, HANDLER_OUTPUT, "." << std::flush);
  1100. if ( tick % tick_line_len == 0 && tick > 0 )
  1101. {
  1102. cmCTestLog(this, HANDLER_OUTPUT, " Size: "
  1103. << int((double(output->size()) / 1024.0) + 1) << "K" << std::endl
  1104. << " " << std::flush);
  1105. }
  1106. }
  1107. }
  1108. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
  1109. if ( ofs )
  1110. {
  1111. ofs << cmCTestLogWrite(data, length);
  1112. }
  1113. }
  1114. cmCTestLog(this, OUTPUT, " Size of output: "
  1115. << int(double(output->size()) / 1024.0) << "K" << std::endl);
  1116. cmsysProcess_WaitForExit(cp, 0);
  1117. int result = cmsysProcess_GetState(cp);
  1118. if(result == cmsysProcess_State_Exited)
  1119. {
  1120. *retVal = cmsysProcess_GetExitValue(cp);
  1121. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "Command exited with the value: "
  1122. << *retVal << std::endl);
  1123. }
  1124. else if(result == cmsysProcess_State_Exception)
  1125. {
  1126. *retVal = cmsysProcess_GetExitException(cp);
  1127. cmCTestLog(this, WARNING, "There was an exception: " << *retVal
  1128. << std::endl);
  1129. }
  1130. else if(result == cmsysProcess_State_Expired)
  1131. {
  1132. cmCTestLog(this, WARNING, "There was a timeout" << std::endl);
  1133. }
  1134. else if(result == cmsysProcess_State_Error)
  1135. {
  1136. *output += "\n*** ERROR executing: ";
  1137. *output += cmsysProcess_GetErrorString(cp);
  1138. *output += "\n***The build process failed.";
  1139. cmCTestLog(this, ERROR_MESSAGE, "There was an error: "
  1140. << cmsysProcess_GetErrorString(cp) << std::endl);
  1141. }
  1142. cmsysProcess_Delete(cp);
  1143. return result;
  1144. }
  1145. //######################################################################
  1146. //######################################################################
  1147. //######################################################################
  1148. //######################################################################
  1149. //----------------------------------------------------------------------
  1150. int cmCTest::RunTest(std::vector<const char*> argv,
  1151. std::string* output, int *retVal,
  1152. std::ostream* log, double testTimeOut,
  1153. std::vector<std::string>* environment)
  1154. {
  1155. std::vector<std::string> origEnv;
  1156. bool modifyEnv = (environment && environment->size()>0);
  1157. // determine how much time we have
  1158. double timeout = this->GetRemainingTimeAllowed() - 120;
  1159. if (this->TimeOut > 0 && this->TimeOut < timeout)
  1160. {
  1161. timeout = this->TimeOut;
  1162. }
  1163. if (testTimeOut > 0
  1164. && testTimeOut < this->GetRemainingTimeAllowed())
  1165. {
  1166. timeout = testTimeOut;
  1167. }
  1168. // always have at least 1 second if we got to here
  1169. if (timeout <= 0)
  1170. {
  1171. timeout = 1;
  1172. }
  1173. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
  1174. "Test timeout computed to be: " << timeout << "\n");
  1175. if(cmSystemTools::SameFile(argv[0], this->CTestSelf.c_str()) &&
  1176. !this->ForceNewCTestProcess)
  1177. {
  1178. cmCTest inst;
  1179. inst.ConfigType = this->ConfigType;
  1180. inst.TimeOut = timeout;
  1181. // Capture output of the child ctest.
  1182. cmOStringStream oss;
  1183. inst.SetStreams(&oss, &oss);
  1184. std::vector<std::string> args;
  1185. for(unsigned int i =0; i < argv.size(); ++i)
  1186. {
  1187. if(argv[i])
  1188. {
  1189. // make sure we pass the timeout in for any build and test
  1190. // invocations. Since --build-generator is required this is a
  1191. // good place to check for it, and to add the arguments in
  1192. if (strcmp(argv[i],"--build-generator") == 0 && timeout > 0)
  1193. {
  1194. args.push_back("--test-timeout");
  1195. cmOStringStream msg;
  1196. msg << timeout;
  1197. args.push_back(msg.str());
  1198. }
  1199. args.push_back(argv[i]);
  1200. }
  1201. }
  1202. if ( log )
  1203. {
  1204. *log << "* Run internal CTest" << std::endl;
  1205. }
  1206. std::string oldpath = cmSystemTools::GetCurrentWorkingDirectory();
  1207. if (modifyEnv)
  1208. {
  1209. origEnv = cmSystemTools::AppendEnv(environment);
  1210. }
  1211. *retVal = inst.Run(args, output);
  1212. *output += oss.str();
  1213. if ( log )
  1214. {
  1215. *log << output->c_str();
  1216. }
  1217. cmSystemTools::ChangeDirectory(oldpath.c_str());
  1218. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
  1219. "Internal cmCTest object used to run test." << std::endl
  1220. << *output << std::endl);
  1221. if (modifyEnv)
  1222. {
  1223. cmSystemTools::RestoreEnv(origEnv);
  1224. }
  1225. return cmsysProcess_State_Exited;
  1226. }
  1227. std::vector<char> tempOutput;
  1228. if ( output )
  1229. {
  1230. *output = "";
  1231. }
  1232. if (modifyEnv)
  1233. {
  1234. origEnv = cmSystemTools::AppendEnv(environment);
  1235. }
  1236. cmsysProcess* cp = cmsysProcess_New();
  1237. cmsysProcess_SetCommand(cp, &*argv.begin());
  1238. cmCTestLog(this, DEBUG, "Command is: " << argv[0] << std::endl);
  1239. if(cmSystemTools::GetRunCommandHideConsole())
  1240. {
  1241. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  1242. }
  1243. cmsysProcess_SetTimeout(cp, timeout);
  1244. cmsysProcess_Execute(cp);
  1245. char* data;
  1246. int length;
  1247. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  1248. {
  1249. if ( output )
  1250. {
  1251. tempOutput.insert(tempOutput.end(), data, data+length);
  1252. }
  1253. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, cmCTestLogWrite(data, length));
  1254. if ( log )
  1255. {
  1256. log->write(data, length);
  1257. }
  1258. }
  1259. cmsysProcess_WaitForExit(cp, 0);
  1260. if(output && tempOutput.begin() != tempOutput.end())
  1261. {
  1262. output->append(&*tempOutput.begin(), tempOutput.size());
  1263. }
  1264. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "-- Process completed"
  1265. << std::endl);
  1266. int result = cmsysProcess_GetState(cp);
  1267. if(result == cmsysProcess_State_Exited)
  1268. {
  1269. *retVal = cmsysProcess_GetExitValue(cp);
  1270. if(*retVal != 0 && this->OutputTestOutputOnTestFailure)
  1271. {
  1272. OutputTestErrors(tempOutput);
  1273. }
  1274. }
  1275. else if(result == cmsysProcess_State_Exception)
  1276. {
  1277. if(this->OutputTestOutputOnTestFailure)
  1278. {
  1279. OutputTestErrors(tempOutput);
  1280. }
  1281. *retVal = cmsysProcess_GetExitException(cp);
  1282. std::string outerr = "\n*** Exception executing: ";
  1283. outerr += cmsysProcess_GetExceptionString(cp);
  1284. *output += outerr;
  1285. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl
  1286. << std::flush);
  1287. }
  1288. else if(result == cmsysProcess_State_Error)
  1289. {
  1290. std::string outerr = "\n*** ERROR executing: ";
  1291. outerr += cmsysProcess_GetErrorString(cp);
  1292. *output += outerr;
  1293. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, outerr.c_str() << std::endl
  1294. << std::flush);
  1295. }
  1296. cmsysProcess_Delete(cp);
  1297. if (modifyEnv)
  1298. {
  1299. cmSystemTools::RestoreEnv(origEnv);
  1300. }
  1301. return result;
  1302. }
  1303. //----------------------------------------------------------------------
  1304. void cmCTest::StartXML(std::ostream& ostr, bool append)
  1305. {
  1306. if(this->CurrentTag.empty())
  1307. {
  1308. cmCTestLog(this, ERROR_MESSAGE,
  1309. "Current Tag empty, this may mean"
  1310. " NightlStartTime was not set correctly." << std::endl);
  1311. cmSystemTools::SetFatalErrorOccured();
  1312. }
  1313. // find out about the system
  1314. cmsys::SystemInformation info;
  1315. info.RunCPUCheck();
  1316. info.RunOSCheck();
  1317. info.RunMemoryCheck();
  1318. ostr << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1319. << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName")
  1320. << "\"\n\tBuildStamp=\"" << this->CurrentTag << "-"
  1321. << this->GetTestModelString() << "\"\n\tName=\""
  1322. << this->GetCTestConfiguration("Site") << "\"\n\tGenerator=\"ctest-"
  1323. << cmVersion::GetCMakeVersion() << "\"\n"
  1324. << (append? "\tAppend=\"true\"\n":"")
  1325. << "\tCompilerName=\"" << this->GetCTestConfiguration("Compiler")
  1326. << "\"\n"
  1327. #ifdef _COMPILER_VERSION
  1328. << "\tCompilerVersion=\"_COMPILER_VERSION\"\n"
  1329. #endif
  1330. << "\tOSName=\"" << info.GetOSName() << "\"\n"
  1331. << "\tHostname=\"" << info.GetHostname() << "\"\n"
  1332. << "\tOSRelease=\"" << info.GetOSRelease() << "\"\n"
  1333. << "\tOSVersion=\"" << info.GetOSVersion() << "\"\n"
  1334. << "\tOSPlatform=\"" << info.GetOSPlatform() << "\"\n"
  1335. << "\tIs64Bits=\"" << info.Is64Bits() << "\"\n"
  1336. << "\tVendorString=\"" << info.GetVendorString() << "\"\n"
  1337. << "\tVendorID=\"" << info.GetVendorID() << "\"\n"
  1338. << "\tFamilyID=\"" << info.GetFamilyID() << "\"\n"
  1339. << "\tModelID=\"" << info.GetModelID() << "\"\n"
  1340. << "\tProcessorCacheSize=\"" << info.GetProcessorCacheSize() << "\"\n"
  1341. << "\tNumberOfLogicalCPU=\"" << info.GetNumberOfLogicalCPU() << "\"\n"
  1342. << "\tNumberOfPhysicalCPU=\""<< info.GetNumberOfPhysicalCPU() << "\"\n"
  1343. << "\tTotalVirtualMemory=\"" << info.GetTotalVirtualMemory() << "\"\n"
  1344. << "\tTotalPhysicalMemory=\""<< info.GetTotalPhysicalMemory() << "\"\n"
  1345. << "\tLogicalProcessorsPerPhysical=\""
  1346. << info.GetLogicalProcessorsPerPhysical() << "\"\n"
  1347. << "\tProcessorClockFrequency=\""
  1348. << info.GetProcessorClockFrequency() << "\"\n"
  1349. << ">" << std::endl;
  1350. this->AddSiteProperties(ostr);
  1351. }
  1352. //----------------------------------------------------------------------
  1353. void cmCTest::AddSiteProperties(std::ostream& ostr)
  1354. {
  1355. cmCTestScriptHandler* ch =
  1356. static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  1357. cmake* cm = ch->GetCMake();
  1358. // if no CMake then this is the old style script and props like
  1359. // this will not work anyway.
  1360. if(!cm)
  1361. {
  1362. return;
  1363. }
  1364. // This code should go when cdash is changed to use labels only
  1365. const char* subproject = cm->GetProperty("SubProject", cmProperty::GLOBAL);
  1366. if(subproject)
  1367. {
  1368. ostr << "<Subproject name=\"" << subproject << "\">\n";
  1369. const char* labels =
  1370. ch->GetCMake()->GetProperty("SubProjectLabels", cmProperty::GLOBAL);
  1371. if(labels)
  1372. {
  1373. ostr << " <Labels>\n";
  1374. std::string l = labels;
  1375. std::vector<std::string> args;
  1376. cmSystemTools::ExpandListArgument(l, args);
  1377. for(std::vector<std::string>::iterator i = args.begin();
  1378. i != args.end(); ++i)
  1379. {
  1380. ostr << " <Label>" << i->c_str() << "</Label>\n";
  1381. }
  1382. ostr << " </Labels>\n";
  1383. }
  1384. ostr << "</Subproject>\n";
  1385. }
  1386. // This code should stay when cdash only does label based sub-projects
  1387. const char* label = cm->GetProperty("Label", cmProperty::GLOBAL);
  1388. if(label)
  1389. {
  1390. ostr << "<Labels>\n";
  1391. ostr << " <Label>" << label << "</Label>\n";
  1392. ostr << "</Labels>\n";
  1393. }
  1394. }
  1395. //----------------------------------------------------------------------
  1396. void cmCTest::EndXML(std::ostream& ostr)
  1397. {
  1398. ostr << "</Site>" << std::endl;
  1399. }
  1400. //----------------------------------------------------------------------
  1401. int cmCTest::GenerateCTestNotesOutput(std::ostream& os,
  1402. const cmCTest::VectorOfStrings& files)
  1403. {
  1404. cmCTest::VectorOfStrings::const_iterator it;
  1405. os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1406. << "<?xml-stylesheet type=\"text/xsl\" "
  1407. "href=\"Dart/Source/Server/XSL/Build.xsl "
  1408. "<file:///Dart/Source/Server/XSL/Build.xsl> \"?>\n"
  1409. << "<Site BuildName=\"" << this->GetCTestConfiguration("BuildName")
  1410. << "\" BuildStamp=\""
  1411. << this->CurrentTag << "-" << this->GetTestModelString() << "\" Name=\""
  1412. << this->GetCTestConfiguration("Site") << "\" Generator=\"ctest"
  1413. << cmVersion::GetCMakeVersion()
  1414. << "\">\n";
  1415. this->AddSiteProperties(os);
  1416. os << "<Notes>" << std::endl;
  1417. for ( it = files.begin(); it != files.end(); it ++ )
  1418. {
  1419. cmCTestLog(this, OUTPUT, "\tAdd file: " << it->c_str() << std::endl);
  1420. std::string note_time = this->CurrentTime();
  1421. os << "<Note Name=\"" << cmXMLSafe(*it) << "\">\n"
  1422. << "<Time>" << cmSystemTools::GetTime() << "</Time>\n"
  1423. << "<DateTime>" << note_time << "</DateTime>\n"
  1424. << "<Text>" << std::endl;
  1425. std::ifstream ifs(it->c_str());
  1426. if ( ifs )
  1427. {
  1428. std::string line;
  1429. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  1430. {
  1431. os << cmXMLSafe(line) << std::endl;
  1432. }
  1433. ifs.close();
  1434. }
  1435. else
  1436. {
  1437. os << "Problem reading file: " << it->c_str() << std::endl;
  1438. cmCTestLog(this, ERROR_MESSAGE, "Problem reading file: " << it->c_str()
  1439. << " while creating notes" << std::endl);
  1440. }
  1441. os << "</Text>\n"
  1442. << "</Note>" << std::endl;
  1443. }
  1444. os << "</Notes>\n"
  1445. << "</Site>" << std::endl;
  1446. return 1;
  1447. }
  1448. //----------------------------------------------------------------------
  1449. int cmCTest::GenerateNotesFile(const std::vector<cmStdString> &files)
  1450. {
  1451. cmGeneratedFileStream ofs;
  1452. if ( !this->OpenOutputFile(this->CurrentTag, "Notes.xml", ofs) )
  1453. {
  1454. cmCTestLog(this, ERROR_MESSAGE, "Cannot open notes file" << std::endl);
  1455. return 1;
  1456. }
  1457. this->GenerateCTestNotesOutput(ofs, files);
  1458. return 0;
  1459. }
  1460. //----------------------------------------------------------------------
  1461. int cmCTest::GenerateNotesFile(const char* cfiles)
  1462. {
  1463. if ( !cfiles )
  1464. {
  1465. return 1;
  1466. }
  1467. std::vector<cmStdString> files;
  1468. cmCTestLog(this, OUTPUT, "Create notes file" << std::endl);
  1469. files = cmSystemTools::SplitString(cfiles, ';');
  1470. if ( files.size() == 0 )
  1471. {
  1472. return 1;
  1473. }
  1474. return this->GenerateNotesFile(files);
  1475. }
  1476. //----------------------------------------------------------------------
  1477. std::string cmCTest::Base64GzipEncodeFile(std::string file)
  1478. {
  1479. std::string tarFile = file + "_temp.tar.gz";
  1480. std::vector<cmStdString> files;
  1481. files.push_back(file);
  1482. if(!cmSystemTools::CreateTar(tarFile.c_str(), files, true, false, false))
  1483. {
  1484. cmCTestLog(this, ERROR_MESSAGE, "Error creating tar while "
  1485. "encoding file: " << file << std::endl);
  1486. return "";
  1487. }
  1488. std::string base64 = this->Base64EncodeFile(tarFile);
  1489. cmSystemTools::RemoveFile(tarFile.c_str());
  1490. return base64;
  1491. }
  1492. //----------------------------------------------------------------------
  1493. std::string cmCTest::Base64EncodeFile(std::string file)
  1494. {
  1495. long len = cmSystemTools::FileLength(file.c_str());
  1496. std::ifstream ifs(file.c_str(), std::ios::in
  1497. #ifdef _WIN32
  1498. | std::ios::binary
  1499. #endif
  1500. );
  1501. unsigned char *file_buffer = new unsigned char [ len + 1 ];
  1502. ifs.read(reinterpret_cast<char*>(file_buffer), len);
  1503. ifs.close();
  1504. unsigned char *encoded_buffer
  1505. = new unsigned char [ static_cast<int>(
  1506. static_cast<double>(len) * 1.5 + 5.0) ];
  1507. unsigned long rlen
  1508. = cmsysBase64_Encode(file_buffer, len, encoded_buffer, 1);
  1509. std::string base64 = "";
  1510. for(unsigned long i = 0; i < rlen; i++)
  1511. {
  1512. base64 += encoded_buffer[i];
  1513. }
  1514. delete [] file_buffer;
  1515. delete [] encoded_buffer;
  1516. return base64;
  1517. }
  1518. //----------------------------------------------------------------------
  1519. bool cmCTest::SubmitExtraFiles(const std::vector<cmStdString> &files)
  1520. {
  1521. std::vector<cmStdString>::const_iterator it;
  1522. for ( it = files.begin();
  1523. it != files.end();
  1524. ++ it )
  1525. {
  1526. if ( !cmSystemTools::FileExists(it->c_str()) )
  1527. {
  1528. cmCTestLog(this, ERROR_MESSAGE, "Cannot find extra file: "
  1529. << it->c_str() << " to submit."
  1530. << std::endl;);
  1531. return false;
  1532. }
  1533. this->AddSubmitFile(PartExtraFiles, it->c_str());
  1534. }
  1535. return true;
  1536. }
  1537. //----------------------------------------------------------------------
  1538. bool cmCTest::SubmitExtraFiles(const char* cfiles)
  1539. {
  1540. if ( !cfiles )
  1541. {
  1542. return 1;
  1543. }
  1544. std::vector<cmStdString> files;
  1545. cmCTestLog(this, OUTPUT, "Submit extra files" << std::endl);
  1546. files = cmSystemTools::SplitString(cfiles, ';');
  1547. if ( files.size() == 0 )
  1548. {
  1549. return 1;
  1550. }
  1551. return this->SubmitExtraFiles(files);
  1552. }
  1553. //-------------------------------------------------------
  1554. // for a -D argument convert the next argument into
  1555. // the proper list of dashboard steps via SetTest
  1556. bool cmCTest::AddTestsForDashboardType(std::string &targ)
  1557. {
  1558. if ( targ == "Experimental" )
  1559. {
  1560. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1561. this->SetTest("Start");
  1562. this->SetTest("Configure");
  1563. this->SetTest("Build");
  1564. this->SetTest("Test");
  1565. this->SetTest("Coverage");
  1566. this->SetTest("Submit");
  1567. }
  1568. else if ( targ == "ExperimentalStart" )
  1569. {
  1570. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1571. this->SetTest("Start");
  1572. }
  1573. else if ( targ == "ExperimentalUpdate" )
  1574. {
  1575. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1576. this->SetTest("Update");
  1577. }
  1578. else if ( targ == "ExperimentalConfigure" )
  1579. {
  1580. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1581. this->SetTest("Configure");
  1582. }
  1583. else if ( targ == "ExperimentalBuild" )
  1584. {
  1585. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1586. this->SetTest("Build");
  1587. }
  1588. else if ( targ == "ExperimentalTest" )
  1589. {
  1590. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1591. this->SetTest("Test");
  1592. }
  1593. else if ( targ == "ExperimentalMemCheck"
  1594. || targ == "ExperimentalPurify" )
  1595. {
  1596. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1597. this->SetTest("MemCheck");
  1598. }
  1599. else if ( targ == "ExperimentalCoverage" )
  1600. {
  1601. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1602. this->SetTest("Coverage");
  1603. }
  1604. else if ( targ == "ExperimentalSubmit" )
  1605. {
  1606. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1607. this->SetTest("Submit");
  1608. }
  1609. else if ( targ == "Continuous" )
  1610. {
  1611. this->SetTestModel(cmCTest::CONTINUOUS);
  1612. this->SetTest("Start");
  1613. this->SetTest("Update");
  1614. this->SetTest("Configure");
  1615. this->SetTest("Build");
  1616. this->SetTest("Test");
  1617. this->SetTest("Coverage");
  1618. this->SetTest("Submit");
  1619. }
  1620. else if ( targ == "ContinuousStart" )
  1621. {
  1622. this->SetTestModel(cmCTest::CONTINUOUS);
  1623. this->SetTest("Start");
  1624. }
  1625. else if ( targ == "ContinuousUpdate" )
  1626. {
  1627. this->SetTestModel(cmCTest::CONTINUOUS);
  1628. this->SetTest("Update");
  1629. }
  1630. else if ( targ == "ContinuousConfigure" )
  1631. {
  1632. this->SetTestModel(cmCTest::CONTINUOUS);
  1633. this->SetTest("Configure");
  1634. }
  1635. else if ( targ == "ContinuousBuild" )
  1636. {
  1637. this->SetTestModel(cmCTest::CONTINUOUS);
  1638. this->SetTest("Build");
  1639. }
  1640. else if ( targ == "ContinuousTest" )
  1641. {
  1642. this->SetTestModel(cmCTest::CONTINUOUS);
  1643. this->SetTest("Test");
  1644. }
  1645. else if ( targ == "ContinuousMemCheck"
  1646. || targ == "ContinuousPurify" )
  1647. {
  1648. this->SetTestModel(cmCTest::CONTINUOUS);
  1649. this->SetTest("MemCheck");
  1650. }
  1651. else if ( targ == "ContinuousCoverage" )
  1652. {
  1653. this->SetTestModel(cmCTest::CONTINUOUS);
  1654. this->SetTest("Coverage");
  1655. }
  1656. else if ( targ == "ContinuousSubmit" )
  1657. {
  1658. this->SetTestModel(cmCTest::CONTINUOUS);
  1659. this->SetTest("Submit");
  1660. }
  1661. else if ( targ == "Nightly" )
  1662. {
  1663. this->SetTestModel(cmCTest::NIGHTLY);
  1664. this->SetTest("Start");
  1665. this->SetTest("Update");
  1666. this->SetTest("Configure");
  1667. this->SetTest("Build");
  1668. this->SetTest("Test");
  1669. this->SetTest("Coverage");
  1670. this->SetTest("Submit");
  1671. }
  1672. else if ( targ == "NightlyStart" )
  1673. {
  1674. this->SetTestModel(cmCTest::NIGHTLY);
  1675. this->SetTest("Start");
  1676. }
  1677. else if ( targ == "NightlyUpdate" )
  1678. {
  1679. this->SetTestModel(cmCTest::NIGHTLY);
  1680. this->SetTest("Update");
  1681. }
  1682. else if ( targ == "NightlyConfigure" )
  1683. {
  1684. this->SetTestModel(cmCTest::NIGHTLY);
  1685. this->SetTest("Configure");
  1686. }
  1687. else if ( targ == "NightlyBuild" )
  1688. {
  1689. this->SetTestModel(cmCTest::NIGHTLY);
  1690. this->SetTest("Build");
  1691. }
  1692. else if ( targ == "NightlyTest" )
  1693. {
  1694. this->SetTestModel(cmCTest::NIGHTLY);
  1695. this->SetTest("Test");
  1696. }
  1697. else if ( targ == "NightlyMemCheck"
  1698. || targ == "NightlyPurify" )
  1699. {
  1700. this->SetTestModel(cmCTest::NIGHTLY);
  1701. this->SetTest("MemCheck");
  1702. }
  1703. else if ( targ == "NightlyCoverage" )
  1704. {
  1705. this->SetTestModel(cmCTest::NIGHTLY);
  1706. this->SetTest("Coverage");
  1707. }
  1708. else if ( targ == "NightlySubmit" )
  1709. {
  1710. this->SetTestModel(cmCTest::NIGHTLY);
  1711. this->SetTest("Submit");
  1712. }
  1713. else if ( targ == "MemoryCheck" )
  1714. {
  1715. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1716. this->SetTest("Start");
  1717. this->SetTest("Configure");
  1718. this->SetTest("Build");
  1719. this->SetTest("MemCheck");
  1720. this->SetTest("Coverage");
  1721. this->SetTest("Submit");
  1722. }
  1723. else if ( targ == "NightlyMemoryCheck" )
  1724. {
  1725. this->SetTestModel(cmCTest::NIGHTLY);
  1726. this->SetTest("Start");
  1727. this->SetTest("Update");
  1728. this->SetTest("Configure");
  1729. this->SetTest("Build");
  1730. this->SetTest("MemCheck");
  1731. this->SetTest("Coverage");
  1732. this->SetTest("Submit");
  1733. }
  1734. else
  1735. {
  1736. cmCTestLog(this, ERROR_MESSAGE,
  1737. "CTest -D called with incorrect option: "
  1738. << targ << std::endl);
  1739. cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
  1740. << " " << "ctest" << " -D Continuous" << std::endl
  1741. << " " << "ctest"
  1742. << " -D Continuous(Start|Update|Configure|Build)" << std::endl
  1743. << " " << "ctest"
  1744. << " -D Continuous(Test|Coverage|MemCheck|Submit)"
  1745. << std::endl
  1746. << " " << "ctest" << " -D Experimental" << std::endl
  1747. << " " << "ctest"
  1748. << " -D Experimental(Start|Update|Configure|Build)"
  1749. << std::endl
  1750. << " " << "ctest"
  1751. << " -D Experimental(Test|Coverage|MemCheck|Submit)"
  1752. << std::endl
  1753. << " " << "ctest" << " -D Nightly" << std::endl
  1754. << " " << "ctest"
  1755. << " -D Nightly(Start|Update|Configure|Build)" << std::endl
  1756. << " " << "ctest"
  1757. << " -D Nightly(Test|Coverage|MemCheck|Submit)" << std::endl
  1758. << " " << "ctest" << " -D NightlyMemoryCheck" << std::endl);
  1759. return false;
  1760. }
  1761. return true;
  1762. }
  1763. //----------------------------------------------------------------------
  1764. bool cmCTest::CheckArgument(const std::string& arg, const char* varg1,
  1765. const char* varg2)
  1766. {
  1767. return (varg1 && arg == varg1) || (varg2 && arg == varg2);
  1768. }
  1769. //----------------------------------------------------------------------
  1770. // Processes one command line argument (and its arguments if any)
  1771. // for many simple options and then returns
  1772. void cmCTest::HandleCommandLineArguments(size_t &i,
  1773. std::vector<std::string> &args)
  1774. {
  1775. std::string arg = args[i];
  1776. if(this->CheckArgument(arg, "-F"))
  1777. {
  1778. this->Failover = true;
  1779. }
  1780. if(this->CheckArgument(arg, "-j", "--parallel") && i < args.size() - 1)
  1781. {
  1782. i++;
  1783. int plevel = atoi(args[i].c_str());
  1784. this->SetParallelLevel(plevel);
  1785. }
  1786. else if(arg.find("-j") == 0)
  1787. {
  1788. int plevel = atoi(arg.substr(2).c_str());
  1789. this->SetParallelLevel(plevel);
  1790. }
  1791. if(this->CheckArgument(arg, "--no-compress-output"))
  1792. {
  1793. this->CompressTestOutput = false;
  1794. this->CompressMemCheckOutput = false;
  1795. }
  1796. if(this->CheckArgument(arg, "--print-labels"))
  1797. {
  1798. this->PrintLabels = true;
  1799. }
  1800. if(this->CheckArgument(arg, "--http1.0"))
  1801. {
  1802. this->UseHTTP10 = true;
  1803. }
  1804. if(this->CheckArgument(arg, "--timeout") && i < args.size() - 1)
  1805. {
  1806. i++;
  1807. double timeout = (double)atof(args[i].c_str());
  1808. this->GlobalTimeout = timeout;
  1809. }
  1810. if(this->CheckArgument(arg, "--stop-time") && i < args.size() - 1)
  1811. {
  1812. i++;
  1813. this->SetStopTime(args[i]);
  1814. }
  1815. if(this->CheckArgument(arg, "-C", "--build-config") &&
  1816. i < args.size() - 1)
  1817. {
  1818. i++;
  1819. this->SetConfigType(args[i].c_str());
  1820. }
  1821. if(this->CheckArgument(arg, "--debug"))
  1822. {
  1823. this->Debug = true;
  1824. this->ShowLineNumbers = true;
  1825. }
  1826. if(this->CheckArgument(arg, "--track") && i < args.size() - 1)
  1827. {
  1828. i++;
  1829. this->SpecificTrack = args[i];
  1830. }
  1831. if(this->CheckArgument(arg, "--show-line-numbers"))
  1832. {
  1833. this->ShowLineNumbers = true;
  1834. }
  1835. if(this->CheckArgument(arg, "--no-label-summary"))
  1836. {
  1837. this->LabelSummary = false;
  1838. }
  1839. if(this->CheckArgument(arg, "-Q", "--quiet"))
  1840. {
  1841. this->Quiet = true;
  1842. }
  1843. if(this->CheckArgument(arg, "-V", "--verbose"))
  1844. {
  1845. this->Verbose = true;
  1846. }
  1847. if(this->CheckArgument(arg, "-B"))
  1848. {
  1849. this->BatchJobs = true;
  1850. }
  1851. if(this->CheckArgument(arg, "-VV", "--extra-verbose"))
  1852. {
  1853. this->ExtraVerbose = true;
  1854. this->Verbose = true;
  1855. }
  1856. if(this->CheckArgument(arg, "--output-on-failure"))
  1857. {
  1858. this->OutputTestOutputOnTestFailure = true;
  1859. }
  1860. if(this->CheckArgument(arg, "-N", "--show-only"))
  1861. {
  1862. this->ShowOnly = true;
  1863. }
  1864. if(this->CheckArgument(arg, "-O", "--output-log") && i < args.size() - 1 )
  1865. {
  1866. i++;
  1867. this->SetOutputLogFileName(args[i].c_str());
  1868. }
  1869. if(this->CheckArgument(arg, "--tomorrow-tag"))
  1870. {
  1871. this->TomorrowTag = true;
  1872. }
  1873. if(this->CheckArgument(arg, "--force-new-ctest-process"))
  1874. {
  1875. this->ForceNewCTestProcess = true;
  1876. }
  1877. if(this->CheckArgument(arg, "-W", "--max-width") && i < args.size() - 1)
  1878. {
  1879. i++;
  1880. this->MaxTestNameWidth = atoi(args[i].c_str());
  1881. }
  1882. if(this->CheckArgument(arg, "--interactive-debug-mode") &&
  1883. i < args.size() - 1 )
  1884. {
  1885. i++;
  1886. this->InteractiveDebugMode = cmSystemTools::IsOn(args[i].c_str());
  1887. }
  1888. if(this->CheckArgument(arg, "--submit-index") && i < args.size() - 1 )
  1889. {
  1890. i++;
  1891. this->SubmitIndex = atoi(args[i].c_str());
  1892. if ( this->SubmitIndex < 0 )
  1893. {
  1894. this->SubmitIndex = 0;
  1895. }
  1896. }
  1897. if(this->CheckArgument(arg, "--overwrite") && i < args.size() - 1)
  1898. {
  1899. i++;
  1900. this->AddCTestConfigurationOverwrite(args[i].c_str());
  1901. }
  1902. if(this->CheckArgument(arg, "-A", "--add-notes") && i < args.size() - 1)
  1903. {
  1904. this->ProduceXML = true;
  1905. this->SetTest("Notes");
  1906. i++;
  1907. this->SetNotesFiles(args[i].c_str());
  1908. }
  1909. // options that control what tests are run
  1910. if(this->CheckArgument(arg, "-I", "--tests-information") &&
  1911. i < args.size() - 1)
  1912. {
  1913. i++;
  1914. this->GetHandler("test")->SetPersistentOption("TestsToRunInformation",
  1915. args[i].c_str());
  1916. this->GetHandler("memcheck")->
  1917. SetPersistentOption("TestsToRunInformation",args[i].c_str());
  1918. }
  1919. if(this->CheckArgument(arg, "-U", "--union"))
  1920. {
  1921. this->GetHandler("test")->SetPersistentOption("UseUnion", "true");
  1922. this->GetHandler("memcheck")->SetPersistentOption("UseUnion", "true");
  1923. }
  1924. if(this->CheckArgument(arg, "-R", "--tests-regex") && i < args.size() - 1)
  1925. {
  1926. i++;
  1927. this->GetHandler("test")->
  1928. SetPersistentOption("IncludeRegularExpression", args[i].c_str());
  1929. this->GetHandler("memcheck")->
  1930. SetPersistentOption("IncludeRegularExpression", args[i].c_str());
  1931. }
  1932. if(this->CheckArgument(arg, "-L", "--label-regex") && i < args.size() - 1)
  1933. {
  1934. i++;
  1935. this->GetHandler("test")->
  1936. SetPersistentOption("LabelRegularExpression", args[i].c_str());
  1937. this->GetHandler("memcheck")->
  1938. SetPersistentOption("LabelRegularExpression", args[i].c_str());
  1939. }
  1940. if(this->CheckArgument(arg, "-LE", "--label-exclude") && i < args.size() - 1)
  1941. {
  1942. i++;
  1943. this->GetHandler("test")->
  1944. SetPersistentOption("ExcludeLabelRegularExpression", args[i].c_str());
  1945. this->GetHandler("memcheck")->
  1946. SetPersistentOption("ExcludeLabelRegularExpression", args[i].c_str());
  1947. }
  1948. if(this->CheckArgument(arg, "-E", "--exclude-regex") &&
  1949. i < args.size() - 1)
  1950. {
  1951. i++;
  1952. this->GetHandler("test")->
  1953. SetPersistentOption("ExcludeRegularExpression", args[i].c_str());
  1954. this->GetHandler("memcheck")->
  1955. SetPersistentOption("ExcludeRegularExpression", args[i].c_str());
  1956. }
  1957. }
  1958. //----------------------------------------------------------------------
  1959. // handle the -S -SR and -SP arguments
  1960. void cmCTest::HandleScriptArguments(size_t &i,
  1961. std::vector<std::string> &args,
  1962. bool &SRArgumentSpecified)
  1963. {
  1964. std::string arg = args[i];
  1965. if(this->CheckArgument(arg, "-SP", "--script-new-process") &&
  1966. i < args.size() - 1 )
  1967. {
  1968. this->RunConfigurationScript = true;
  1969. i++;
  1970. cmCTestScriptHandler* ch
  1971. = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  1972. // -SR is an internal argument, -SP should be ignored when it is passed
  1973. if (!SRArgumentSpecified)
  1974. {
  1975. ch->AddConfigurationScript(args[i].c_str(),false);
  1976. }
  1977. }
  1978. if(this->CheckArgument(arg, "-SR", "--script-run") &&
  1979. i < args.size() - 1 )
  1980. {
  1981. SRArgumentSpecified = true;
  1982. this->RunConfigurationScript = true;
  1983. i++;
  1984. cmCTestScriptHandler* ch
  1985. = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  1986. ch->AddConfigurationScript(args[i].c_str(),true);
  1987. }
  1988. if(this->CheckArgument(arg, "-S", "--script") && i < args.size() - 1 )
  1989. {
  1990. this->RunConfigurationScript = true;
  1991. i++;
  1992. cmCTestScriptHandler* ch
  1993. = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  1994. // -SR is an internal argument, -S should be ignored when it is passed
  1995. if (!SRArgumentSpecified)
  1996. {
  1997. ch->AddConfigurationScript(args[i].c_str(),true);
  1998. }
  1999. }
  2000. }
  2001. //----------------------------------------------------------------------
  2002. // the main entry point of ctest, called from main
  2003. int cmCTest::Run(std::vector<std::string> &args, std::string* output)
  2004. {
  2005. this->FindRunningCMake();
  2006. const char* ctestExec = "ctest";
  2007. bool cmakeAndTest = false;
  2008. bool performSomeTest = true;
  2009. bool SRArgumentSpecified = false;
  2010. // copy the command line
  2011. for(size_t i=0; i < args.size(); ++i)
  2012. {
  2013. this->InitialCommandLineArguments.push_back(args[i]);
  2014. }
  2015. // process the command line arguments
  2016. for(size_t i=1; i < args.size(); ++i)
  2017. {
  2018. // handle the simple commandline arguments
  2019. this->HandleCommandLineArguments(i,args);
  2020. // handle the script arguments -S -SR -SP
  2021. this->HandleScriptArguments(i,args,SRArgumentSpecified);
  2022. // handle a request for a dashboard
  2023. std::string arg = args[i];
  2024. if(this->CheckArgument(arg, "-D", "--dashboard") && i < args.size() - 1 )
  2025. {
  2026. this->ProduceXML = true;
  2027. i++;
  2028. std::string targ = args[i];
  2029. // AddTestsForDashboard parses the dashborad type and converts it
  2030. // into the separate stages
  2031. if (!this->AddTestsForDashboardType(targ))
  2032. {
  2033. performSomeTest = false;
  2034. }
  2035. }
  2036. if(this->CheckArgument(arg, "-T", "--test-action") &&
  2037. (i < args.size() -1) )
  2038. {
  2039. this->ProduceXML = true;
  2040. i++;
  2041. if ( !this->SetTest(args[i].c_str(), false) )
  2042. {
  2043. performSomeTest = false;
  2044. cmCTestLog(this, ERROR_MESSAGE,
  2045. "CTest -T called with incorrect option: "
  2046. << args[i].c_str() << std::endl);
  2047. cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
  2048. << " " << ctestExec << " -T all" << std::endl
  2049. << " " << ctestExec << " -T start" << std::endl
  2050. << " " << ctestExec << " -T update" << std::endl
  2051. << " " << ctestExec << " -T configure" << std::endl
  2052. << " " << ctestExec << " -T build" << std::endl
  2053. << " " << ctestExec << " -T test" << std::endl
  2054. << " " << ctestExec << " -T coverage" << std::endl
  2055. << " " << ctestExec << " -T memcheck" << std::endl
  2056. << " " << ctestExec << " -T notes" << std::endl
  2057. << " " << ctestExec << " -T submit" << std::endl);
  2058. }
  2059. }
  2060. // what type of test model
  2061. if(this->CheckArgument(arg, "-M", "--test-model") &&
  2062. (i < args.size() -1) )
  2063. {
  2064. i++;
  2065. std::string const& str = args[i];
  2066. if ( cmSystemTools::LowerCase(str) == "nightly" )
  2067. {
  2068. this->SetTestModel(cmCTest::NIGHTLY);
  2069. }
  2070. else if ( cmSystemTools::LowerCase(str) == "continuous" )
  2071. {
  2072. this->SetTestModel(cmCTest::CONTINUOUS);
  2073. }
  2074. else if ( cmSystemTools::LowerCase(str) == "experimental" )
  2075. {
  2076. this->SetTestModel(cmCTest::EXPERIMENTAL);
  2077. }
  2078. else
  2079. {
  2080. performSomeTest = false;
  2081. cmCTestLog(this, ERROR_MESSAGE,
  2082. "CTest -M called with incorrect option: " << str.c_str()
  2083. << std::endl);
  2084. cmCTestLog(this, ERROR_MESSAGE, "Available options are:" << std::endl
  2085. << " " << ctestExec << " -M Continuous" << std::endl
  2086. << " " << ctestExec << " -M Experimental" << std::endl
  2087. << " " << ctestExec << " -M Nightly" << std::endl);
  2088. }
  2089. }
  2090. if(this->CheckArgument(arg, "--extra-submit") && i < args.size() - 1)
  2091. {
  2092. this->ProduceXML = true;
  2093. this->SetTest("Submit");
  2094. i++;
  2095. if ( !this->SubmitExtraFiles(args[i].c_str()) )
  2096. {
  2097. return 0;
  2098. }
  2099. }
  2100. // --build-and-test options
  2101. if(this->CheckArgument(arg, "--build-and-test") && i < args.size() - 1)
  2102. {
  2103. cmakeAndTest = true;
  2104. }
  2105. if(this->CheckArgument(arg, "--schedule-random"))
  2106. {
  2107. this->ScheduleType = "Random";
  2108. }
  2109. // pass the argument to all the handlers as well, but i may no longer be
  2110. // set to what it was originally so I'm not sure this is working as
  2111. // intended
  2112. cmCTest::t_TestingHandlers::iterator it;
  2113. for ( it = this->TestingHandlers.begin();
  2114. it != this->TestingHandlers.end();
  2115. ++ it )
  2116. {
  2117. if ( !it->second->ProcessCommandLineArguments(arg, i, args) )
  2118. {
  2119. cmCTestLog(this, ERROR_MESSAGE,
  2120. "Problem parsing command line arguments within a handler");
  2121. return 0;
  2122. }
  2123. }
  2124. } // the close of the for argument loop
  2125. // now what sould cmake do? if --build-and-test was specified then
  2126. // we run the build and test handler and return
  2127. if(cmakeAndTest)
  2128. {
  2129. this->Verbose = true;
  2130. cmCTestBuildAndTestHandler* handler =
  2131. static_cast<cmCTestBuildAndTestHandler*>(this->GetHandler("buildtest"));
  2132. int retv = handler->ProcessHandler();
  2133. *output = handler->GetOutput();
  2134. #ifdef CMAKE_BUILD_WITH_CMAKE
  2135. cmDynamicLoader::FlushCache();
  2136. #endif
  2137. if(retv != 0)
  2138. {
  2139. cmCTestLog(this, DEBUG, "build and test failing returing: " << retv
  2140. << std::endl);
  2141. }
  2142. return retv;
  2143. }
  2144. // if some tests must be run
  2145. if(performSomeTest)
  2146. {
  2147. int res;
  2148. // call process directory
  2149. if (this->RunConfigurationScript)
  2150. {
  2151. if ( this->ExtraVerbose )
  2152. {
  2153. cmCTestLog(this, OUTPUT, "* Extra verbosity turned on" << std::endl);
  2154. }
  2155. cmCTest::t_TestingHandlers::iterator it;
  2156. for ( it = this->TestingHandlers.begin();
  2157. it != this->TestingHandlers.end();
  2158. ++ it )
  2159. {
  2160. it->second->SetVerbose(this->ExtraVerbose);
  2161. it->second->SetSubmitIndex(this->SubmitIndex);
  2162. }
  2163. this->GetHandler("script")->SetVerbose(this->Verbose);
  2164. res = this->GetHandler("script")->ProcessHandler();
  2165. if(res != 0)
  2166. {
  2167. cmCTestLog(this, DEBUG, "running script failing returning: " << res
  2168. << std::endl);
  2169. }
  2170. }
  2171. else
  2172. {
  2173. // What is this? -V seems to be the same as -VV,
  2174. // and Verbose is always on in this case
  2175. this->ExtraVerbose = this->Verbose;
  2176. this->Verbose = true;
  2177. cmCTest::t_TestingHandlers::iterator it;
  2178. for ( it = this->TestingHandlers.begin();
  2179. it != this->TestingHandlers.end();
  2180. ++ it )
  2181. {
  2182. it->second->SetVerbose(this->Verbose);
  2183. it->second->SetSubmitIndex(this->SubmitIndex);
  2184. }
  2185. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2186. if(!this->Initialize(cwd.c_str(), 0))
  2187. {
  2188. res = 12;
  2189. cmCTestLog(this, ERROR_MESSAGE, "Problem initializing the dashboard."
  2190. << std::endl);
  2191. }
  2192. else
  2193. {
  2194. res = this->ProcessTests();
  2195. }
  2196. this->Finalize();
  2197. }
  2198. if(res != 0)
  2199. {
  2200. cmCTestLog(this, DEBUG, "Running a test(s) failed returning : " << res
  2201. << std::endl);
  2202. }
  2203. return res;
  2204. }
  2205. return 1;
  2206. }
  2207. //----------------------------------------------------------------------
  2208. void cmCTest::FindRunningCMake()
  2209. {
  2210. // Find our own executable.
  2211. this->CTestSelf = cmSystemTools::GetExecutableDirectory();
  2212. this->CTestSelf += "/ctest";
  2213. this->CTestSelf += cmSystemTools::GetExecutableExtension();
  2214. if(!cmSystemTools::FileExists(this->CTestSelf.c_str()))
  2215. {
  2216. cmSystemTools::Error("CTest executable cannot be found at ",
  2217. this->CTestSelf.c_str());
  2218. }
  2219. this->CMakeSelf = cmSystemTools::GetExecutableDirectory();
  2220. this->CMakeSelf += "/cmake";
  2221. this->CMakeSelf += cmSystemTools::GetExecutableExtension();
  2222. if(!cmSystemTools::FileExists(this->CMakeSelf.c_str()))
  2223. {
  2224. cmSystemTools::Error("CMake executable cannot be found at ",
  2225. this->CMakeSelf.c_str());
  2226. }
  2227. }
  2228. //----------------------------------------------------------------------
  2229. void cmCTest::SetNotesFiles(const char* notes)
  2230. {
  2231. if ( !notes )
  2232. {
  2233. return;
  2234. }
  2235. this->NotesFiles = notes;
  2236. }
  2237. //----------------------------------------------------------------------
  2238. void cmCTest::SetStopTime(std::string time)
  2239. {
  2240. this->StopTime = time;
  2241. this->DetermineNextDayStop();
  2242. }
  2243. //----------------------------------------------------------------------
  2244. int cmCTest::ReadCustomConfigurationFileTree(const char* dir, cmMakefile* mf)
  2245. {
  2246. bool found = false;
  2247. VectorOfStrings dirs;
  2248. VectorOfStrings ndirs;
  2249. cmCTestLog(this, DEBUG, "* Read custom CTest configuration directory: "
  2250. << dir << std::endl);
  2251. std::string fname = dir;
  2252. fname += "/CTestCustom.cmake";
  2253. cmCTestLog(this, DEBUG, "* Check for file: "
  2254. << fname.c_str() << std::endl);
  2255. if ( cmSystemTools::FileExists(fname.c_str()) )
  2256. {
  2257. cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: "
  2258. << fname.c_str() << std::endl);
  2259. bool erroroc = cmSystemTools::GetErrorOccuredFlag();
  2260. cmSystemTools::ResetErrorOccuredFlag();
  2261. if ( !mf->ReadListFile(0, fname.c_str()) ||
  2262. cmSystemTools::GetErrorOccuredFlag() )
  2263. {
  2264. cmCTestLog(this, ERROR_MESSAGE,
  2265. "Problem reading custom configuration: "
  2266. << fname.c_str() << std::endl);
  2267. }
  2268. found = true;
  2269. if ( erroroc )
  2270. {
  2271. cmSystemTools::SetErrorOccured();
  2272. }
  2273. }
  2274. std::string rexpr = dir;
  2275. rexpr += "/CTestCustom.ctest";
  2276. cmCTestLog(this, DEBUG, "* Check for file: "
  2277. << rexpr.c_str() << std::endl);
  2278. if ( !found && cmSystemTools::FileExists(rexpr.c_str()) )
  2279. {
  2280. cmsys::Glob gl;
  2281. gl.RecurseOn();
  2282. gl.FindFiles(rexpr);
  2283. std::vector<std::string>& files = gl.GetFiles();
  2284. std::vector<std::string>::iterator fileIt;
  2285. for ( fileIt = files.begin(); fileIt != files.end();
  2286. ++ fileIt )
  2287. {
  2288. cmCTestLog(this, DEBUG, "* Read custom CTest configuration file: "
  2289. << fileIt->c_str() << std::endl);
  2290. if ( !mf->ReadListFile(0, fileIt->c_str()) ||
  2291. cmSystemTools::GetErrorOccuredFlag() )
  2292. {
  2293. cmCTestLog(this, ERROR_MESSAGE,
  2294. "Problem reading custom configuration: "
  2295. << fileIt->c_str() << std::endl);
  2296. }
  2297. }
  2298. found = true;
  2299. }
  2300. if ( found )
  2301. {
  2302. cmCTest::t_TestingHandlers::iterator it;
  2303. for ( it = this->TestingHandlers.begin();
  2304. it != this->TestingHandlers.end(); ++ it )
  2305. {
  2306. cmCTestLog(this, DEBUG,
  2307. "* Read custom CTest configuration vectors for handler: "
  2308. << it->first.c_str() << " (" << it->second << ")" << std::endl);
  2309. it->second->PopulateCustomVectors(mf);
  2310. }
  2311. }
  2312. return 1;
  2313. }
  2314. //----------------------------------------------------------------------
  2315. void cmCTest::PopulateCustomVector(cmMakefile* mf, const char* def,
  2316. VectorOfStrings& vec)
  2317. {
  2318. if ( !def)
  2319. {
  2320. return;
  2321. }
  2322. const char* dval = mf->GetDefinition(def);
  2323. if ( !dval )
  2324. {
  2325. return;
  2326. }
  2327. cmCTestLog(this, DEBUG, "PopulateCustomVector: " << def << std::endl);
  2328. std::vector<std::string> slist;
  2329. cmSystemTools::ExpandListArgument(dval, slist);
  2330. std::vector<std::string>::iterator it;
  2331. for ( it = slist.begin(); it != slist.end(); ++it )
  2332. {
  2333. cmCTestLog(this, DEBUG, " -- " << it->c_str() << std::endl);
  2334. vec.push_back(it->c_str());
  2335. }
  2336. }
  2337. //----------------------------------------------------------------------
  2338. void cmCTest::PopulateCustomInteger(cmMakefile* mf, const char* def, int& val)
  2339. {
  2340. if ( !def)
  2341. {
  2342. return;
  2343. }
  2344. const char* dval = mf->GetDefinition(def);
  2345. if ( !dval )
  2346. {
  2347. return;
  2348. }
  2349. val = atoi(dval);
  2350. }
  2351. //----------------------------------------------------------------------
  2352. std::string cmCTest::GetShortPathToFile(const char* cfname)
  2353. {
  2354. const std::string& sourceDir
  2355. = cmSystemTools::CollapseFullPath(
  2356. this->GetCTestConfiguration("SourceDirectory").c_str());
  2357. const std::string& buildDir
  2358. = cmSystemTools::CollapseFullPath(
  2359. this->GetCTestConfiguration("BuildDirectory").c_str());
  2360. std::string fname = cmSystemTools::CollapseFullPath(cfname);
  2361. // Find relative paths to both directories
  2362. std::string srcRelpath
  2363. = cmSystemTools::RelativePath(sourceDir.c_str(), fname.c_str());
  2364. std::string bldRelpath
  2365. = cmSystemTools::RelativePath(buildDir.c_str(), fname.c_str());
  2366. // If any contains "." it is not parent directory
  2367. bool inSrc = srcRelpath.find("..") == srcRelpath.npos;
  2368. bool inBld = bldRelpath.find("..") == bldRelpath.npos;
  2369. // TODO: Handle files with .. in their name
  2370. std::string* res = 0;
  2371. if ( inSrc && inBld )
  2372. {
  2373. // If both have relative path with no dots, pick the shorter one
  2374. if ( srcRelpath.size() < bldRelpath.size() )
  2375. {
  2376. res = &srcRelpath;
  2377. }
  2378. else
  2379. {
  2380. res = &bldRelpath;
  2381. }
  2382. }
  2383. else if ( inSrc )
  2384. {
  2385. res = &srcRelpath;
  2386. }
  2387. else if ( inBld )
  2388. {
  2389. res = &bldRelpath;
  2390. }
  2391. std::string path;
  2392. if ( !res )
  2393. {
  2394. path = fname;
  2395. }
  2396. else
  2397. {
  2398. cmSystemTools::ConvertToUnixSlashes(*res);
  2399. path = "./" + *res;
  2400. if ( path[path.size()-1] == '/' )
  2401. {
  2402. path = path.substr(0, path.size()-1);
  2403. }
  2404. }
  2405. cmsys::SystemTools::ReplaceString(path, ":", "_");
  2406. cmsys::SystemTools::ReplaceString(path, " ", "_");
  2407. return path;
  2408. }
  2409. //----------------------------------------------------------------------
  2410. std::string cmCTest::GetCTestConfiguration(const char *name)
  2411. {
  2412. if ( this->CTestConfigurationOverwrites.find(name) !=
  2413. this->CTestConfigurationOverwrites.end() )
  2414. {
  2415. return this->CTestConfigurationOverwrites[name];
  2416. }
  2417. return this->CTestConfiguration[name];
  2418. }
  2419. //----------------------------------------------------------------------
  2420. void cmCTest::EmptyCTestConfiguration()
  2421. {
  2422. this->CTestConfiguration.clear();
  2423. }
  2424. //----------------------------------------------------------------------
  2425. void cmCTest::DetermineNextDayStop()
  2426. {
  2427. struct tm* lctime;
  2428. time_t current_time = time(0);
  2429. lctime = gmtime(&current_time);
  2430. int gm_hour = lctime->tm_hour;
  2431. time_t gm_time = mktime(lctime);
  2432. lctime = localtime(&current_time);
  2433. int local_hour = lctime->tm_hour;
  2434. int tzone_offset = local_hour - gm_hour;
  2435. if(gm_time > current_time && gm_hour < local_hour)
  2436. {
  2437. // this means gm_time is on the next day
  2438. tzone_offset -= 24;
  2439. }
  2440. else if(gm_time < current_time && gm_hour > local_hour)
  2441. {
  2442. // this means gm_time is on the previous day
  2443. tzone_offset += 24;
  2444. }
  2445. tzone_offset *= 100;
  2446. char buf[1024];
  2447. sprintf(buf, "%d%02d%02d %s %+05i",
  2448. lctime->tm_year + 1900,
  2449. lctime->tm_mon + 1,
  2450. lctime->tm_mday,
  2451. this->StopTime.c_str(),
  2452. tzone_offset);
  2453. time_t stop_time = curl_getdate(buf, &current_time);
  2454. if(stop_time < current_time)
  2455. {
  2456. this->NextDayStopTime = true;
  2457. }
  2458. }
  2459. //----------------------------------------------------------------------
  2460. void cmCTest::SetCTestConfiguration(const char *name, const char* value)
  2461. {
  2462. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT, "SetCTestConfiguration:"
  2463. << name << ":" << (value ? value : "(null)") << "\n");
  2464. if ( !name )
  2465. {
  2466. return;
  2467. }
  2468. if ( !value )
  2469. {
  2470. this->CTestConfiguration.erase(name);
  2471. return;
  2472. }
  2473. this->CTestConfiguration[name] = value;
  2474. }
  2475. //----------------------------------------------------------------------
  2476. std::string cmCTest::GetCurrentTag()
  2477. {
  2478. return this->CurrentTag;
  2479. }
  2480. //----------------------------------------------------------------------
  2481. std::string cmCTest::GetBinaryDir()
  2482. {
  2483. return this->BinaryDir;
  2484. }
  2485. //----------------------------------------------------------------------
  2486. std::string const& cmCTest::GetConfigType()
  2487. {
  2488. return this->ConfigType;
  2489. }
  2490. //----------------------------------------------------------------------
  2491. bool cmCTest::GetShowOnly()
  2492. {
  2493. return this->ShowOnly;
  2494. }
  2495. //----------------------------------------------------------------------
  2496. int cmCTest::GetMaxTestNameWidth() const
  2497. {
  2498. return this->MaxTestNameWidth;
  2499. }
  2500. //----------------------------------------------------------------------
  2501. void cmCTest::SetProduceXML(bool v)
  2502. {
  2503. this->ProduceXML = v;
  2504. }
  2505. //----------------------------------------------------------------------
  2506. bool cmCTest::GetProduceXML()
  2507. {
  2508. return this->ProduceXML;
  2509. }
  2510. //----------------------------------------------------------------------
  2511. const char* cmCTest::GetSpecificTrack()
  2512. {
  2513. if ( this->SpecificTrack.empty() )
  2514. {
  2515. return 0;
  2516. }
  2517. return this->SpecificTrack.c_str();
  2518. }
  2519. //----------------------------------------------------------------------
  2520. void cmCTest::SetSpecificTrack(const char* track)
  2521. {
  2522. if ( !track )
  2523. {
  2524. this->SpecificTrack = "";
  2525. return;
  2526. }
  2527. this->SpecificTrack = track;
  2528. }
  2529. //----------------------------------------------------------------------
  2530. void cmCTest::AddSubmitFile(Part part, const char* name)
  2531. {
  2532. this->Parts[part].SubmitFiles.push_back(name);
  2533. }
  2534. //----------------------------------------------------------------------
  2535. void cmCTest::AddCTestConfigurationOverwrite(const char* encstr)
  2536. {
  2537. std::string overStr = encstr;
  2538. size_t epos = overStr.find("=");
  2539. if ( epos == overStr.npos )
  2540. {
  2541. cmCTestLog(this, ERROR_MESSAGE,
  2542. "CTest configuration overwrite specified in the wrong format."
  2543. << std::endl
  2544. << "Valid format is: --overwrite key=value" << std::endl
  2545. << "The specified was: --overwrite " << overStr.c_str() << std::endl);
  2546. return;
  2547. }
  2548. std::string key = overStr.substr(0, epos);
  2549. std::string value = overStr.substr(epos+1, overStr.npos);
  2550. this->CTestConfigurationOverwrites[key] = value;
  2551. }
  2552. //----------------------------------------------------------------------
  2553. void cmCTest::SetConfigType(const char* ct)
  2554. {
  2555. this->ConfigType = ct?ct:"";
  2556. cmSystemTools::ReplaceString(this->ConfigType, ".\\", "");
  2557. std::string confTypeEnv
  2558. = "CMAKE_CONFIG_TYPE=" + this->ConfigType;
  2559. cmSystemTools::PutEnv(confTypeEnv.c_str());
  2560. }
  2561. //----------------------------------------------------------------------
  2562. bool cmCTest::SetCTestConfigurationFromCMakeVariable(cmMakefile* mf,
  2563. const char* dconfig, const char* cmake_var)
  2564. {
  2565. const char* ctvar;
  2566. ctvar = mf->GetDefinition(cmake_var);
  2567. if ( !ctvar )
  2568. {
  2569. return false;
  2570. }
  2571. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
  2572. "SetCTestConfigurationFromCMakeVariable:"
  2573. << dconfig << ":" << cmake_var);
  2574. this->SetCTestConfiguration(dconfig, ctvar);
  2575. return true;
  2576. }
  2577. bool cmCTest::RunCommand(
  2578. const char* command,
  2579. std::string* stdOut,
  2580. std::string* stdErr,
  2581. int *retVal,
  2582. const char* dir,
  2583. double timeout)
  2584. {
  2585. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  2586. if(args.size() < 1)
  2587. {
  2588. return false;
  2589. }
  2590. std::vector<const char*> argv;
  2591. for(std::vector<cmStdString>::const_iterator a = args.begin();
  2592. a != args.end(); ++a)
  2593. {
  2594. argv.push_back(a->c_str());
  2595. }
  2596. argv.push_back(0);
  2597. *stdOut = "";
  2598. *stdErr = "";
  2599. cmsysProcess* cp = cmsysProcess_New();
  2600. cmsysProcess_SetCommand(cp, &*argv.begin());
  2601. cmsysProcess_SetWorkingDirectory(cp, dir);
  2602. if(cmSystemTools::GetRunCommandHideConsole())
  2603. {
  2604. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  2605. }
  2606. cmsysProcess_SetTimeout(cp, timeout);
  2607. cmsysProcess_Execute(cp);
  2608. std::vector<char> tempOutput;
  2609. std::vector<char> tempError;
  2610. char* data;
  2611. int length;
  2612. int res;
  2613. bool done = false;
  2614. while(!done)
  2615. {
  2616. res = cmsysProcess_WaitForData(cp, &data, &length, 0);
  2617. switch ( res )
  2618. {
  2619. case cmsysProcess_Pipe_STDOUT:
  2620. tempOutput.insert(tempOutput.end(), data, data+length);
  2621. break;
  2622. case cmsysProcess_Pipe_STDERR:
  2623. tempError.insert(tempError.end(), data, data+length);
  2624. break;
  2625. default:
  2626. done = true;
  2627. }
  2628. if ( (res == cmsysProcess_Pipe_STDOUT ||
  2629. res == cmsysProcess_Pipe_STDERR) && this->ExtraVerbose )
  2630. {
  2631. cmSystemTools::Stdout(data, length);
  2632. }
  2633. }
  2634. cmsysProcess_WaitForExit(cp, 0);
  2635. if ( tempOutput.size() > 0 )
  2636. {
  2637. stdOut->append(&*tempOutput.begin(), tempOutput.size());
  2638. }
  2639. if ( tempError.size() > 0 )
  2640. {
  2641. stdErr->append(&*tempError.begin(), tempError.size());
  2642. }
  2643. bool result = true;
  2644. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  2645. {
  2646. if ( retVal )
  2647. {
  2648. *retVal = cmsysProcess_GetExitValue(cp);
  2649. }
  2650. else
  2651. {
  2652. if ( cmsysProcess_GetExitValue(cp) != 0 )
  2653. {
  2654. result = false;
  2655. }
  2656. }
  2657. }
  2658. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
  2659. {
  2660. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  2661. cmCTestLog(this, ERROR_MESSAGE, exception_str << std::endl);
  2662. stdErr->append(exception_str, strlen(exception_str));
  2663. result = false;
  2664. }
  2665. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  2666. {
  2667. const char* error_str = cmsysProcess_GetErrorString(cp);
  2668. cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
  2669. stdErr->append(error_str, strlen(error_str));
  2670. result = false;
  2671. }
  2672. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  2673. {
  2674. const char* error_str = "Process terminated due to timeout\n";
  2675. cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
  2676. stdErr->append(error_str, strlen(error_str));
  2677. result = false;
  2678. }
  2679. cmsysProcess_Delete(cp);
  2680. return result;
  2681. }
  2682. //----------------------------------------------------------------------
  2683. void cmCTest::SetOutputLogFileName(const char* name)
  2684. {
  2685. if ( this->OutputLogFile)
  2686. {
  2687. delete this->OutputLogFile;
  2688. this->OutputLogFile= 0;
  2689. }
  2690. if ( name )
  2691. {
  2692. this->OutputLogFile = new cmGeneratedFileStream(name);
  2693. }
  2694. }
  2695. //----------------------------------------------------------------------
  2696. static const char* cmCTestStringLogType[] =
  2697. {
  2698. "DEBUG",
  2699. "OUTPUT",
  2700. "HANDLER_OUTPUT",
  2701. "HANDLER_VERBOSE_OUTPUT",
  2702. "WARNING",
  2703. "ERROR_MESSAGE",
  2704. 0
  2705. };
  2706. //----------------------------------------------------------------------
  2707. #ifdef cerr
  2708. # undef cerr
  2709. #endif
  2710. #ifdef cout
  2711. # undef cout
  2712. #endif
  2713. #define cmCTestLogOutputFileLine(stream) \
  2714. if ( this->ShowLineNumbers ) \
  2715. { \
  2716. (stream) << std::endl << file << ":" << line << " "; \
  2717. }
  2718. void cmCTest::InitStreams()
  2719. {
  2720. // By default we write output to the process output streams.
  2721. this->StreamOut = &std::cout;
  2722. this->StreamErr = &std::cerr;
  2723. }
  2724. void cmCTest::Log(int logType, const char* file, int line, const char* msg)
  2725. {
  2726. if ( !msg || !*msg )
  2727. {
  2728. return;
  2729. }
  2730. if ( this->OutputLogFile )
  2731. {
  2732. bool display = true;
  2733. if ( logType == cmCTest::DEBUG && !this->Debug ) { display = false; }
  2734. if ( logType == cmCTest::HANDLER_VERBOSE_OUTPUT && !this->Debug &&
  2735. !this->ExtraVerbose ) { display = false; }
  2736. if ( display )
  2737. {
  2738. cmCTestLogOutputFileLine(*this->OutputLogFile);
  2739. if ( logType != this->OutputLogFileLastTag )
  2740. {
  2741. *this->OutputLogFile << "[";
  2742. if ( logType >= OTHER || logType < 0 )
  2743. {
  2744. *this->OutputLogFile << "OTHER";
  2745. }
  2746. else
  2747. {
  2748. *this->OutputLogFile << cmCTestStringLogType[logType];
  2749. }
  2750. *this->OutputLogFile << "] " << std::endl << std::flush;
  2751. }
  2752. *this->OutputLogFile << msg << std::flush;
  2753. if ( logType != this->OutputLogFileLastTag )
  2754. {
  2755. *this->OutputLogFile << std::endl << std::flush;
  2756. this->OutputLogFileLastTag = logType;
  2757. }
  2758. }
  2759. }
  2760. if ( !this->Quiet )
  2761. {
  2762. std::ostream& out = *this->StreamOut;
  2763. std::ostream& err = *this->StreamErr;
  2764. switch ( logType )
  2765. {
  2766. case DEBUG:
  2767. if ( this->Debug )
  2768. {
  2769. cmCTestLogOutputFileLine(out);
  2770. out << msg;
  2771. out.flush();
  2772. }
  2773. break;
  2774. case OUTPUT: case HANDLER_OUTPUT:
  2775. if ( this->Debug || this->Verbose )
  2776. {
  2777. cmCTestLogOutputFileLine(out);
  2778. out << msg;
  2779. out.flush();
  2780. }
  2781. break;
  2782. case HANDLER_VERBOSE_OUTPUT:
  2783. if ( this->Debug || this->ExtraVerbose )
  2784. {
  2785. cmCTestLogOutputFileLine(out);
  2786. out << msg;
  2787. out.flush();
  2788. }
  2789. break;
  2790. case WARNING:
  2791. cmCTestLogOutputFileLine(err);
  2792. err << msg;
  2793. err.flush();
  2794. break;
  2795. case ERROR_MESSAGE:
  2796. cmCTestLogOutputFileLine(err);
  2797. err << msg;
  2798. err.flush();
  2799. cmSystemTools::SetErrorOccured();
  2800. break;
  2801. default:
  2802. cmCTestLogOutputFileLine(out);
  2803. out << msg;
  2804. out.flush();
  2805. }
  2806. }
  2807. }
  2808. //-------------------------------------------------------------------------
  2809. double cmCTest::GetRemainingTimeAllowed()
  2810. {
  2811. if (!this->GetHandler("script"))
  2812. {
  2813. return 1.0e7;
  2814. }
  2815. cmCTestScriptHandler* ch
  2816. = static_cast<cmCTestScriptHandler*>(this->GetHandler("script"));
  2817. return ch->GetRemainingTimeAllowed();
  2818. }
  2819. //----------------------------------------------------------------------
  2820. void cmCTest::OutputTestErrors(std::vector<char> const &process_output)
  2821. {
  2822. std::string test_outputs("\n*** Test Failed:\n");
  2823. if(process_output.size())
  2824. {
  2825. test_outputs.append(&*process_output.begin(), process_output.size());
  2826. }
  2827. cmCTestLog(this, HANDLER_OUTPUT, test_outputs << std::endl << std::flush);
  2828. }
  2829. //----------------------------------------------------------------------
  2830. bool cmCTest::CompressString(std::string& str)
  2831. {
  2832. int ret;
  2833. z_stream strm;
  2834. unsigned char* in = reinterpret_cast<unsigned char*>(
  2835. const_cast<char*>(str.c_str()));
  2836. //zlib makes the guarantee that this is the maximum output size
  2837. int outSize = static_cast<int>(
  2838. static_cast<double>(str.size()) * 1.001 + 13.0);
  2839. unsigned char* out = new unsigned char[outSize];
  2840. strm.zalloc = Z_NULL;
  2841. strm.zfree = Z_NULL;
  2842. strm.opaque = Z_NULL;
  2843. ret = deflateInit(&strm, -1); //default compression level
  2844. if (ret != Z_OK)
  2845. {
  2846. return false;
  2847. }
  2848. strm.avail_in = str.size();
  2849. strm.next_in = in;
  2850. strm.avail_out = outSize;
  2851. strm.next_out = out;
  2852. ret = deflate(&strm, Z_FINISH);
  2853. if(ret == Z_STREAM_ERROR || ret != Z_STREAM_END)
  2854. {
  2855. cmCTestLog(this, ERROR_MESSAGE, "Error during gzip compression."
  2856. << std::endl);
  2857. return false;
  2858. }
  2859. (void)deflateEnd(&strm);
  2860. // Now base64 encode the resulting binary string
  2861. unsigned char* base64EncodedBuffer
  2862. = new unsigned char[static_cast<int>(outSize * 1.5)];
  2863. unsigned long rlen
  2864. = cmsysBase64_Encode(out, strm.total_out, base64EncodedBuffer, 1);
  2865. str = "";
  2866. str.append(reinterpret_cast<char*>(base64EncodedBuffer), rlen);
  2867. delete [] base64EncodedBuffer;
  2868. delete [] out;
  2869. return true;
  2870. }