cmCTest.cxx 98 KB

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