cmCTest.cxx 92 KB

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