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