cmCTest.cxx 101 KB

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