cmCTest.cxx 98 KB

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