cmCTest.cxx 89 KB

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