cmCTest.cxx 115 KB

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