cmCTest.cxx 103 KB

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