cmCTest.cxx 128 KB

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