cmCTest.cxx 102 KB

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