cmCTest.cxx 90 KB

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