cmCTest.cxx 101 KB

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