cmCTest.cxx 115 KB

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