cmCTest.cxx 101 KB

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