cmCTest.cxx 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243
  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 <memory> // IWYU pragma: keep
  18. #include <sstream>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <string>
  23. #include <time.h>
  24. #include <utility>
  25. #include <vector>
  26. #if defined(_WIN32)
  27. # include <windows.h> // IWYU pragma: keep
  28. #else
  29. # include <unistd.h> // IWYU pragma: keep
  30. #endif
  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 << " bytes of output"
  983. << std::endl
  984. << " " << std::flush);
  985. while (cmsysProcess_WaitForData(cp, &data, &length, nullptr)) {
  986. processOutput.DecodeText(data, length, strdata);
  987. for (char& cc : strdata) {
  988. if (cc == 0) {
  989. cc = '\n';
  990. }
  991. }
  992. output.append(strdata);
  993. while (output.size() > (tick * tick_len)) {
  994. tick++;
  995. cmCTestLog(this, HANDLER_PROGRESS_OUTPUT, "." << std::flush);
  996. if (tick % tick_line_len == 0 && tick > 0) {
  997. cmCTestLog(this, HANDLER_PROGRESS_OUTPUT,
  998. " Size: " << int((double(output.size()) / 1024.0) + 1)
  999. << "K" << std::endl
  1000. << " " << 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,
  1188. outerr << std::endl
  1189. << std::flush);
  1190. } else if (result == cmsysProcess_State_Error) {
  1191. std::string outerr = "\n*** ERROR executing: ";
  1192. outerr += cmsysProcess_GetErrorString(cp);
  1193. if (output) {
  1194. *output += outerr;
  1195. }
  1196. cmCTestLog(this, HANDLER_VERBOSE_OUTPUT,
  1197. outerr << std::endl
  1198. << std::flush);
  1199. }
  1200. cmsysProcess_Delete(cp);
  1201. return result;
  1202. }
  1203. std::string cmCTest::SafeBuildIdField(const std::string& value)
  1204. {
  1205. std::string safevalue(value);
  1206. if (!safevalue.empty()) {
  1207. // Disallow non-filename and non-space whitespace characters.
  1208. // If they occur, replace them with ""
  1209. //
  1210. const char* disallowed = "\\:*?\"<>|\n\r\t\f\v";
  1211. if (safevalue.find_first_of(disallowed) != std::string::npos) {
  1212. std::string::size_type i = 0;
  1213. std::string::size_type n = strlen(disallowed);
  1214. char replace[2];
  1215. replace[1] = 0;
  1216. for (i = 0; i < n; ++i) {
  1217. replace[0] = disallowed[i];
  1218. cmSystemTools::ReplaceString(safevalue, replace, "");
  1219. }
  1220. }
  1221. }
  1222. if (safevalue.empty()) {
  1223. safevalue = "(empty)";
  1224. }
  1225. return safevalue;
  1226. }
  1227. void cmCTest::StartXML(cmXMLWriter& xml, bool append)
  1228. {
  1229. if (this->Impl->CurrentTag.empty()) {
  1230. cmCTestLog(this, ERROR_MESSAGE,
  1231. "Current Tag empty, this may mean"
  1232. " NightlStartTime was not set correctly."
  1233. << std::endl);
  1234. cmSystemTools::SetFatalErrorOccured();
  1235. }
  1236. // find out about the system
  1237. cmsys::SystemInformation info;
  1238. info.RunCPUCheck();
  1239. info.RunOSCheck();
  1240. info.RunMemoryCheck();
  1241. std::string buildname =
  1242. cmCTest::SafeBuildIdField(this->GetCTestConfiguration("BuildName"));
  1243. std::string stamp = cmCTest::SafeBuildIdField(this->Impl->CurrentTag + "-" +
  1244. this->GetTestModelString());
  1245. std::string site =
  1246. cmCTest::SafeBuildIdField(this->GetCTestConfiguration("Site"));
  1247. xml.StartDocument();
  1248. xml.StartElement("Site");
  1249. xml.Attribute("BuildName", buildname);
  1250. xml.BreakAttributes();
  1251. xml.Attribute("BuildStamp", stamp);
  1252. xml.Attribute("Name", site);
  1253. xml.Attribute("Generator",
  1254. std::string("ctest-") + cmVersion::GetCMakeVersion());
  1255. if (append) {
  1256. xml.Attribute("Append", "true");
  1257. }
  1258. xml.Attribute("CompilerName", this->GetCTestConfiguration("Compiler"));
  1259. xml.Attribute("CompilerVersion",
  1260. this->GetCTestConfiguration("CompilerVersion"));
  1261. xml.Attribute("OSName", info.GetOSName());
  1262. xml.Attribute("Hostname", info.GetHostname());
  1263. xml.Attribute("OSRelease", info.GetOSRelease());
  1264. xml.Attribute("OSVersion", info.GetOSVersion());
  1265. xml.Attribute("OSPlatform", info.GetOSPlatform());
  1266. xml.Attribute("Is64Bits", info.Is64Bits());
  1267. xml.Attribute("VendorString", info.GetVendorString());
  1268. xml.Attribute("VendorID", info.GetVendorID());
  1269. xml.Attribute("FamilyID", info.GetFamilyID());
  1270. xml.Attribute("ModelID", info.GetModelID());
  1271. xml.Attribute("ProcessorCacheSize", info.GetProcessorCacheSize());
  1272. xml.Attribute("NumberOfLogicalCPU", info.GetNumberOfLogicalCPU());
  1273. xml.Attribute("NumberOfPhysicalCPU", info.GetNumberOfPhysicalCPU());
  1274. xml.Attribute("TotalVirtualMemory", info.GetTotalVirtualMemory());
  1275. xml.Attribute("TotalPhysicalMemory", info.GetTotalPhysicalMemory());
  1276. xml.Attribute("LogicalProcessorsPerPhysical",
  1277. info.GetLogicalProcessorsPerPhysical());
  1278. xml.Attribute("ProcessorClockFrequency", info.GetProcessorClockFrequency());
  1279. std::string changeId = this->GetCTestConfiguration("ChangeId");
  1280. if (!changeId.empty()) {
  1281. xml.Attribute("ChangeId", changeId);
  1282. }
  1283. this->AddSiteProperties(xml);
  1284. }
  1285. void cmCTest::AddSiteProperties(cmXMLWriter& xml)
  1286. {
  1287. cmCTestScriptHandler* ch = this->GetScriptHandler();
  1288. cmake* cm = ch->GetCMake();
  1289. // if no CMake then this is the old style script and props like
  1290. // this will not work anyway.
  1291. if (!cm) {
  1292. return;
  1293. }
  1294. // This code should go when cdash is changed to use labels only
  1295. const char* subproject = cm->GetState()->GetGlobalProperty("SubProject");
  1296. if (subproject) {
  1297. xml.StartElement("Subproject");
  1298. xml.Attribute("name", subproject);
  1299. const char* labels =
  1300. ch->GetCMake()->GetState()->GetGlobalProperty("SubProjectLabels");
  1301. if (labels) {
  1302. xml.StartElement("Labels");
  1303. std::string l = labels;
  1304. std::vector<std::string> args;
  1305. cmSystemTools::ExpandListArgument(l, args);
  1306. for (std::string const& i : args) {
  1307. xml.Element("Label", i);
  1308. }
  1309. xml.EndElement();
  1310. }
  1311. xml.EndElement();
  1312. }
  1313. // This code should stay when cdash only does label based sub-projects
  1314. const char* label = cm->GetState()->GetGlobalProperty("Label");
  1315. if (label) {
  1316. xml.StartElement("Labels");
  1317. xml.Element("Label", label);
  1318. xml.EndElement();
  1319. }
  1320. }
  1321. void cmCTest::GenerateSubprojectsOutput(cmXMLWriter& xml)
  1322. {
  1323. for (std::string const& subproj : this->GetLabelsForSubprojects()) {
  1324. xml.StartElement("Subproject");
  1325. xml.Attribute("name", subproj);
  1326. xml.Element("Label", subproj);
  1327. xml.EndElement(); // Subproject
  1328. }
  1329. }
  1330. std::vector<std::string> cmCTest::GetLabelsForSubprojects()
  1331. {
  1332. std::string labelsForSubprojects =
  1333. this->GetCTestConfiguration("LabelsForSubprojects");
  1334. std::vector<std::string> subprojects;
  1335. cmSystemTools::ExpandListArgument(labelsForSubprojects, subprojects);
  1336. // sort the array
  1337. std::sort(subprojects.begin(), subprojects.end());
  1338. // remove duplicates
  1339. std::vector<std::string>::iterator new_end =
  1340. std::unique(subprojects.begin(), subprojects.end());
  1341. subprojects.erase(new_end, subprojects.end());
  1342. return subprojects;
  1343. }
  1344. void cmCTest::EndXML(cmXMLWriter& xml)
  1345. {
  1346. xml.EndElement(); // Site
  1347. xml.EndDocument();
  1348. }
  1349. int cmCTest::GenerateCTestNotesOutput(cmXMLWriter& xml,
  1350. std::vector<std::string> const& files)
  1351. {
  1352. std::string buildname =
  1353. cmCTest::SafeBuildIdField(this->GetCTestConfiguration("BuildName"));
  1354. xml.StartDocument();
  1355. xml.ProcessingInstruction("xml-stylesheet",
  1356. "type=\"text/xsl\" "
  1357. "href=\"Dart/Source/Server/XSL/Build.xsl "
  1358. "<file:///Dart/Source/Server/XSL/Build.xsl> \"");
  1359. xml.StartElement("Site");
  1360. xml.Attribute("BuildName", buildname);
  1361. xml.Attribute("BuildStamp",
  1362. this->Impl->CurrentTag + "-" + this->GetTestModelString());
  1363. xml.Attribute("Name", this->GetCTestConfiguration("Site"));
  1364. xml.Attribute("Generator",
  1365. std::string("ctest-") + cmVersion::GetCMakeVersion());
  1366. this->AddSiteProperties(xml);
  1367. xml.StartElement("Notes");
  1368. for (std::string const& file : files) {
  1369. cmCTestLog(this, OUTPUT, "\tAdd file: " << file << std::endl);
  1370. std::string note_time = this->CurrentTime();
  1371. xml.StartElement("Note");
  1372. xml.Attribute("Name", file);
  1373. xml.Element("Time", std::chrono::system_clock::now());
  1374. xml.Element("DateTime", note_time);
  1375. xml.StartElement("Text");
  1376. cmsys::ifstream ifs(file.c_str());
  1377. if (ifs) {
  1378. std::string line;
  1379. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  1380. xml.Content(line);
  1381. xml.Content("\n");
  1382. }
  1383. ifs.close();
  1384. } else {
  1385. xml.Content("Problem reading file: " + file + "\n");
  1386. cmCTestLog(this, ERROR_MESSAGE,
  1387. "Problem reading file: " << file << " while creating notes"
  1388. << std::endl);
  1389. }
  1390. xml.EndElement(); // Text
  1391. xml.EndElement(); // Note
  1392. }
  1393. xml.EndElement(); // Notes
  1394. xml.EndElement(); // Site
  1395. xml.EndDocument();
  1396. return 1;
  1397. }
  1398. int cmCTest::GenerateNotesFile(std::vector<std::string> const& files)
  1399. {
  1400. cmGeneratedFileStream ofs;
  1401. if (!this->OpenOutputFile(this->Impl->CurrentTag, "Notes.xml", ofs)) {
  1402. cmCTestLog(this, ERROR_MESSAGE, "Cannot open notes file" << std::endl);
  1403. return 1;
  1404. }
  1405. cmXMLWriter xml(ofs);
  1406. this->GenerateCTestNotesOutput(xml, files);
  1407. return 0;
  1408. }
  1409. int cmCTest::GenerateNotesFile(const char* cfiles)
  1410. {
  1411. if (!cfiles) {
  1412. return 1;
  1413. }
  1414. cmCTestLog(this, OUTPUT, "Create notes file" << std::endl);
  1415. std::vector<std::string> const files =
  1416. cmSystemTools::SplitString(cfiles, ';');
  1417. if (files.empty()) {
  1418. return 1;
  1419. }
  1420. return this->GenerateNotesFile(files);
  1421. }
  1422. int cmCTest::GenerateDoneFile()
  1423. {
  1424. cmGeneratedFileStream ofs;
  1425. if (!this->OpenOutputFile(this->Impl->CurrentTag, "Done.xml", ofs)) {
  1426. cmCTestLog(this, ERROR_MESSAGE, "Cannot open done file" << std::endl);
  1427. return 1;
  1428. }
  1429. cmXMLWriter xml(ofs);
  1430. xml.StartDocument();
  1431. xml.StartElement("Done");
  1432. xml.Element("buildId", this->Impl->BuildID);
  1433. xml.Element("time", std::chrono::system_clock::now());
  1434. xml.EndElement(); // Done
  1435. xml.EndDocument();
  1436. return 0;
  1437. }
  1438. std::string cmCTest::Base64GzipEncodeFile(std::string const& file)
  1439. {
  1440. std::string tarFile = file + "_temp.tar.gz";
  1441. std::vector<std::string> files;
  1442. files.push_back(file);
  1443. if (!cmSystemTools::CreateTar(tarFile, files, cmSystemTools::TarCompressGZip,
  1444. false)) {
  1445. cmCTestLog(this, ERROR_MESSAGE,
  1446. "Error creating tar while "
  1447. "encoding file: "
  1448. << file << std::endl);
  1449. return "";
  1450. }
  1451. std::string base64 = this->Base64EncodeFile(tarFile);
  1452. cmSystemTools::RemoveFile(tarFile);
  1453. return base64;
  1454. }
  1455. std::string cmCTest::Base64EncodeFile(std::string const& file)
  1456. {
  1457. size_t const len = cmSystemTools::FileLength(file);
  1458. cmsys::ifstream ifs(file.c_str(),
  1459. std::ios::in
  1460. #ifdef _WIN32
  1461. | std::ios::binary
  1462. #endif
  1463. );
  1464. std::vector<char> file_buffer(len + 1);
  1465. ifs.read(&file_buffer[0], len);
  1466. ifs.close();
  1467. std::vector<char> encoded_buffer((len * 3) / 2 + 5);
  1468. size_t const rlen = cmsysBase64_Encode(
  1469. reinterpret_cast<unsigned char*>(&file_buffer[0]), len,
  1470. reinterpret_cast<unsigned char*>(&encoded_buffer[0]), 1);
  1471. return std::string(&encoded_buffer[0], rlen);
  1472. }
  1473. bool cmCTest::SubmitExtraFiles(std::vector<std::string> const& files)
  1474. {
  1475. for (std::string const& file : files) {
  1476. if (!cmSystemTools::FileExists(file)) {
  1477. cmCTestLog(this, ERROR_MESSAGE,
  1478. "Cannot find extra file: " << file << " to submit."
  1479. << std::endl;);
  1480. return false;
  1481. }
  1482. this->AddSubmitFile(PartExtraFiles, file.c_str());
  1483. }
  1484. return true;
  1485. }
  1486. bool cmCTest::SubmitExtraFiles(const char* cfiles)
  1487. {
  1488. if (!cfiles) {
  1489. return true;
  1490. }
  1491. cmCTestLog(this, OUTPUT, "Submit extra files" << std::endl);
  1492. std::vector<std::string> const files =
  1493. cmSystemTools::SplitString(cfiles, ';');
  1494. if (files.empty()) {
  1495. return true;
  1496. }
  1497. return this->SubmitExtraFiles(files);
  1498. }
  1499. // for a -D argument convert the next argument into
  1500. // the proper list of dashboard steps via SetTest
  1501. bool cmCTest::AddTestsForDashboardType(std::string& targ)
  1502. {
  1503. if (targ == "Experimental") {
  1504. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1505. this->SetTest("Start");
  1506. this->SetTest("Configure");
  1507. this->SetTest("Build");
  1508. this->SetTest("Test");
  1509. this->SetTest("Coverage");
  1510. this->SetTest("Submit");
  1511. } else if (targ == "ExperimentalStart") {
  1512. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1513. this->SetTest("Start");
  1514. } else if (targ == "ExperimentalUpdate") {
  1515. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1516. this->SetTest("Update");
  1517. } else if (targ == "ExperimentalConfigure") {
  1518. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1519. this->SetTest("Configure");
  1520. } else if (targ == "ExperimentalBuild") {
  1521. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1522. this->SetTest("Build");
  1523. } else if (targ == "ExperimentalTest") {
  1524. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1525. this->SetTest("Test");
  1526. } else if (targ == "ExperimentalMemCheck" || targ == "ExperimentalPurify") {
  1527. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1528. this->SetTest("MemCheck");
  1529. } else if (targ == "ExperimentalCoverage") {
  1530. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1531. this->SetTest("Coverage");
  1532. } else if (targ == "ExperimentalSubmit") {
  1533. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1534. this->SetTest("Submit");
  1535. } else if (targ == "Continuous") {
  1536. this->SetTestModel(cmCTest::CONTINUOUS);
  1537. this->SetTest("Start");
  1538. this->SetTest("Update");
  1539. this->SetTest("Configure");
  1540. this->SetTest("Build");
  1541. this->SetTest("Test");
  1542. this->SetTest("Coverage");
  1543. this->SetTest("Submit");
  1544. } else if (targ == "ContinuousStart") {
  1545. this->SetTestModel(cmCTest::CONTINUOUS);
  1546. this->SetTest("Start");
  1547. } else if (targ == "ContinuousUpdate") {
  1548. this->SetTestModel(cmCTest::CONTINUOUS);
  1549. this->SetTest("Update");
  1550. } else if (targ == "ContinuousConfigure") {
  1551. this->SetTestModel(cmCTest::CONTINUOUS);
  1552. this->SetTest("Configure");
  1553. } else if (targ == "ContinuousBuild") {
  1554. this->SetTestModel(cmCTest::CONTINUOUS);
  1555. this->SetTest("Build");
  1556. } else if (targ == "ContinuousTest") {
  1557. this->SetTestModel(cmCTest::CONTINUOUS);
  1558. this->SetTest("Test");
  1559. } else if (targ == "ContinuousMemCheck" || targ == "ContinuousPurify") {
  1560. this->SetTestModel(cmCTest::CONTINUOUS);
  1561. this->SetTest("MemCheck");
  1562. } else if (targ == "ContinuousCoverage") {
  1563. this->SetTestModel(cmCTest::CONTINUOUS);
  1564. this->SetTest("Coverage");
  1565. } else if (targ == "ContinuousSubmit") {
  1566. this->SetTestModel(cmCTest::CONTINUOUS);
  1567. this->SetTest("Submit");
  1568. } else if (targ == "Nightly") {
  1569. this->SetTestModel(cmCTest::NIGHTLY);
  1570. this->SetTest("Start");
  1571. this->SetTest("Update");
  1572. this->SetTest("Configure");
  1573. this->SetTest("Build");
  1574. this->SetTest("Test");
  1575. this->SetTest("Coverage");
  1576. this->SetTest("Submit");
  1577. } else if (targ == "NightlyStart") {
  1578. this->SetTestModel(cmCTest::NIGHTLY);
  1579. this->SetTest("Start");
  1580. } else if (targ == "NightlyUpdate") {
  1581. this->SetTestModel(cmCTest::NIGHTLY);
  1582. this->SetTest("Update");
  1583. } else if (targ == "NightlyConfigure") {
  1584. this->SetTestModel(cmCTest::NIGHTLY);
  1585. this->SetTest("Configure");
  1586. } else if (targ == "NightlyBuild") {
  1587. this->SetTestModel(cmCTest::NIGHTLY);
  1588. this->SetTest("Build");
  1589. } else if (targ == "NightlyTest") {
  1590. this->SetTestModel(cmCTest::NIGHTLY);
  1591. this->SetTest("Test");
  1592. } else if (targ == "NightlyMemCheck" || targ == "NightlyPurify") {
  1593. this->SetTestModel(cmCTest::NIGHTLY);
  1594. this->SetTest("MemCheck");
  1595. } else if (targ == "NightlyCoverage") {
  1596. this->SetTestModel(cmCTest::NIGHTLY);
  1597. this->SetTest("Coverage");
  1598. } else if (targ == "NightlySubmit") {
  1599. this->SetTestModel(cmCTest::NIGHTLY);
  1600. this->SetTest("Submit");
  1601. } else if (targ == "MemoryCheck") {
  1602. this->SetTestModel(cmCTest::EXPERIMENTAL);
  1603. this->SetTest("Start");
  1604. this->SetTest("Configure");
  1605. this->SetTest("Build");
  1606. this->SetTest("MemCheck");
  1607. this->SetTest("Coverage");
  1608. this->SetTest("Submit");
  1609. } else if (targ == "NightlyMemoryCheck") {
  1610. this->SetTestModel(cmCTest::NIGHTLY);
  1611. this->SetTest("Start");
  1612. this->SetTest("Update");
  1613. this->SetTest("Configure");
  1614. this->SetTest("Build");
  1615. this->SetTest("MemCheck");
  1616. this->SetTest("Coverage");
  1617. this->SetTest("Submit");
  1618. } else {
  1619. return false;
  1620. }
  1621. return true;
  1622. }
  1623. void cmCTest::ErrorMessageUnknownDashDValue(std::string& val)
  1624. {
  1625. cmCTestLog(this, ERROR_MESSAGE,
  1626. "CTest -D called with incorrect option: " << val << std::endl);
  1627. cmCTestLog(
  1628. this, ERROR_MESSAGE,
  1629. "Available options are:"
  1630. << std::endl
  1631. << " ctest -D Continuous" << std::endl
  1632. << " ctest -D Continuous(Start|Update|Configure|Build)" << std::endl
  1633. << " ctest -D Continuous(Test|Coverage|MemCheck|Submit)" << std::endl
  1634. << " ctest -D Experimental" << std::endl
  1635. << " ctest -D Experimental(Start|Update|Configure|Build)" << std::endl
  1636. << " ctest -D Experimental(Test|Coverage|MemCheck|Submit)" << std::endl
  1637. << " ctest -D Nightly" << std::endl
  1638. << " ctest -D Nightly(Start|Update|Configure|Build)" << std::endl
  1639. << " ctest -D Nightly(Test|Coverage|MemCheck|Submit)" << std::endl
  1640. << " ctest -D NightlyMemoryCheck" << std::endl);
  1641. }
  1642. bool cmCTest::CheckArgument(const std::string& arg, const char* varg1,
  1643. const char* varg2)
  1644. {
  1645. return (varg1 && arg == varg1) || (varg2 && arg == varg2);
  1646. }
  1647. // Processes one command line argument (and its arguments if any)
  1648. // for many simple options and then returns
  1649. bool cmCTest::HandleCommandLineArguments(size_t& i,
  1650. std::vector<std::string>& args,
  1651. std::string& errormsg)
  1652. {
  1653. std::string arg = args[i];
  1654. if (this->CheckArgument(arg, "-F")) {
  1655. this->Impl->Failover = true;
  1656. }
  1657. if (this->CheckArgument(arg, "-j", "--parallel") && i < args.size() - 1) {
  1658. i++;
  1659. int plevel = atoi(args[i].c_str());
  1660. this->SetParallelLevel(plevel);
  1661. this->Impl->ParallelLevelSetInCli = true;
  1662. } else if (arg.find("-j") == 0) {
  1663. int plevel = atoi(arg.substr(2).c_str());
  1664. this->SetParallelLevel(plevel);
  1665. this->Impl->ParallelLevelSetInCli = true;
  1666. }
  1667. if (this->CheckArgument(arg, "--repeat-until-fail")) {
  1668. if (i >= args.size() - 1) {
  1669. errormsg = "'--repeat-until-fail' requires an argument";
  1670. return false;
  1671. }
  1672. i++;
  1673. long repeat = 1;
  1674. if (!cmSystemTools::StringToLong(args[i].c_str(), &repeat)) {
  1675. errormsg =
  1676. "'--repeat-until-fail' given non-integer value '" + args[i] + "'";
  1677. return false;
  1678. }
  1679. this->Impl->RepeatTests = static_cast<int>(repeat);
  1680. if (repeat > 1) {
  1681. this->Impl->RepeatUntilFail = true;
  1682. }
  1683. }
  1684. if (this->CheckArgument(arg, "--test-load") && i < args.size() - 1) {
  1685. i++;
  1686. unsigned long load;
  1687. if (cmSystemTools::StringToULong(args[i].c_str(), &load)) {
  1688. this->SetTestLoad(load);
  1689. } else {
  1690. cmCTestLog(this, WARNING,
  1691. "Invalid value for 'Test Load' : " << args[i] << std::endl);
  1692. }
  1693. }
  1694. if (this->CheckArgument(arg, "--no-compress-output")) {
  1695. this->Impl->CompressTestOutput = false;
  1696. }
  1697. if (this->CheckArgument(arg, "--print-labels")) {
  1698. this->Impl->PrintLabels = true;
  1699. }
  1700. if (this->CheckArgument(arg, "--http1.0")) {
  1701. this->Impl->UseHTTP10 = true;
  1702. }
  1703. if (this->CheckArgument(arg, "--timeout") && i < args.size() - 1) {
  1704. i++;
  1705. auto timeout = cmDuration(atof(args[i].c_str()));
  1706. this->Impl->GlobalTimeout = timeout;
  1707. }
  1708. if (this->CheckArgument(arg, "--stop-time") && i < args.size() - 1) {
  1709. i++;
  1710. this->SetStopTime(args[i]);
  1711. }
  1712. if (this->CheckArgument(arg, "-C", "--build-config") &&
  1713. i < args.size() - 1) {
  1714. i++;
  1715. this->SetConfigType(args[i].c_str());
  1716. }
  1717. if (this->CheckArgument(arg, "--debug")) {
  1718. this->Impl->Debug = true;
  1719. this->Impl->ShowLineNumbers = true;
  1720. }
  1721. if (this->CheckArgument(arg, "--track") && i < args.size() - 1) {
  1722. i++;
  1723. this->Impl->SpecificTrack = args[i];
  1724. }
  1725. if (this->CheckArgument(arg, "--show-line-numbers")) {
  1726. this->Impl->ShowLineNumbers = true;
  1727. }
  1728. if (this->CheckArgument(arg, "--no-label-summary")) {
  1729. this->Impl->LabelSummary = false;
  1730. }
  1731. if (this->CheckArgument(arg, "--no-subproject-summary")) {
  1732. this->Impl->SubprojectSummary = false;
  1733. }
  1734. if (this->CheckArgument(arg, "-Q", "--quiet")) {
  1735. this->Impl->Quiet = true;
  1736. }
  1737. if (this->CheckArgument(arg, "--progress")) {
  1738. this->Impl->TestProgressOutput = true;
  1739. }
  1740. if (this->CheckArgument(arg, "-V", "--verbose")) {
  1741. this->Impl->Verbose = true;
  1742. }
  1743. if (this->CheckArgument(arg, "-VV", "--extra-verbose")) {
  1744. this->Impl->ExtraVerbose = true;
  1745. this->Impl->Verbose = true;
  1746. }
  1747. if (this->CheckArgument(arg, "--output-on-failure")) {
  1748. this->Impl->OutputTestOutputOnTestFailure = true;
  1749. }
  1750. if (this->CheckArgument(arg, "--test-output-size-passed") &&
  1751. i < args.size() - 1) {
  1752. i++;
  1753. long outputSize;
  1754. if (cmSystemTools::StringToLong(args[i].c_str(), &outputSize)) {
  1755. this->Impl->TestHandler.SetTestOutputSizePassed(int(outputSize));
  1756. } else {
  1757. cmCTestLog(this, WARNING,
  1758. "Invalid value for '--test-output-size-passed': " << args[i]
  1759. << "\n");
  1760. }
  1761. }
  1762. if (this->CheckArgument(arg, "--test-output-size-failed") &&
  1763. i < args.size() - 1) {
  1764. i++;
  1765. long outputSize;
  1766. if (cmSystemTools::StringToLong(args[i].c_str(), &outputSize)) {
  1767. this->Impl->TestHandler.SetTestOutputSizeFailed(int(outputSize));
  1768. } else {
  1769. cmCTestLog(this, WARNING,
  1770. "Invalid value for '--test-output-size-failed': " << args[i]
  1771. << "\n");
  1772. }
  1773. }
  1774. if (this->CheckArgument(arg, "-N", "--show-only")) {
  1775. this->Impl->ShowOnly = true;
  1776. }
  1777. if (cmSystemTools::StringStartsWith(arg.c_str(), "--show-only=")) {
  1778. this->Impl->ShowOnly = true;
  1779. // Check if a specific format is requested. Defaults to human readable
  1780. // text.
  1781. std::string argWithFormat = "--show-only=";
  1782. std::string format = arg.substr(argWithFormat.length());
  1783. if (format == "json-v1") {
  1784. // Force quiet mode so the only output is the json object model.
  1785. this->Impl->Quiet = true;
  1786. this->Impl->OutputAsJson = true;
  1787. this->Impl->OutputAsJsonVersion = 1;
  1788. } else if (format != "human") {
  1789. errormsg = "'--show-only=' given unknown value '" + format + "'";
  1790. return false;
  1791. }
  1792. }
  1793. if (this->CheckArgument(arg, "-O", "--output-log") && i < args.size() - 1) {
  1794. i++;
  1795. this->SetOutputLogFileName(args[i].c_str());
  1796. }
  1797. if (this->CheckArgument(arg, "--tomorrow-tag")) {
  1798. this->Impl->TomorrowTag = true;
  1799. }
  1800. if (this->CheckArgument(arg, "--force-new-ctest-process")) {
  1801. this->Impl->ForceNewCTestProcess = true;
  1802. }
  1803. if (this->CheckArgument(arg, "-W", "--max-width") && i < args.size() - 1) {
  1804. i++;
  1805. this->Impl->MaxTestNameWidth = atoi(args[i].c_str());
  1806. }
  1807. if (this->CheckArgument(arg, "--interactive-debug-mode") &&
  1808. i < args.size() - 1) {
  1809. i++;
  1810. this->Impl->InteractiveDebugMode = cmSystemTools::IsOn(args[i]);
  1811. }
  1812. if (this->CheckArgument(arg, "--submit-index") && i < args.size() - 1) {
  1813. i++;
  1814. this->Impl->SubmitIndex = atoi(args[i].c_str());
  1815. if (this->Impl->SubmitIndex < 0) {
  1816. this->Impl->SubmitIndex = 0;
  1817. }
  1818. }
  1819. if (this->CheckArgument(arg, "--overwrite") && i < args.size() - 1) {
  1820. i++;
  1821. this->AddCTestConfigurationOverwrite(args[i]);
  1822. }
  1823. if (this->CheckArgument(arg, "-A", "--add-notes") && i < args.size() - 1) {
  1824. this->Impl->ProduceXML = true;
  1825. this->SetTest("Notes");
  1826. i++;
  1827. this->SetNotesFiles(args[i].c_str());
  1828. }
  1829. // options that control what tests are run
  1830. if (this->CheckArgument(arg, "-I", "--tests-information") &&
  1831. i < args.size() - 1) {
  1832. i++;
  1833. this->GetTestHandler()->SetPersistentOption("TestsToRunInformation",
  1834. args[i].c_str());
  1835. this->GetMemCheckHandler()->SetPersistentOption("TestsToRunInformation",
  1836. args[i].c_str());
  1837. }
  1838. if (this->CheckArgument(arg, "-U", "--union")) {
  1839. this->GetTestHandler()->SetPersistentOption("UseUnion", "true");
  1840. this->GetMemCheckHandler()->SetPersistentOption("UseUnion", "true");
  1841. }
  1842. if (this->CheckArgument(arg, "-R", "--tests-regex") && i < args.size() - 1) {
  1843. i++;
  1844. this->GetTestHandler()->SetPersistentOption("IncludeRegularExpression",
  1845. args[i].c_str());
  1846. this->GetMemCheckHandler()->SetPersistentOption("IncludeRegularExpression",
  1847. args[i].c_str());
  1848. }
  1849. if (this->CheckArgument(arg, "-L", "--label-regex") && i < args.size() - 1) {
  1850. i++;
  1851. this->GetTestHandler()->SetPersistentOption("LabelRegularExpression",
  1852. args[i].c_str());
  1853. this->GetMemCheckHandler()->SetPersistentOption("LabelRegularExpression",
  1854. args[i].c_str());
  1855. }
  1856. if (this->CheckArgument(arg, "-LE", "--label-exclude") &&
  1857. i < args.size() - 1) {
  1858. i++;
  1859. this->GetTestHandler()->SetPersistentOption(
  1860. "ExcludeLabelRegularExpression", args[i].c_str());
  1861. this->GetMemCheckHandler()->SetPersistentOption(
  1862. "ExcludeLabelRegularExpression", args[i].c_str());
  1863. }
  1864. if (this->CheckArgument(arg, "-E", "--exclude-regex") &&
  1865. i < args.size() - 1) {
  1866. i++;
  1867. this->GetTestHandler()->SetPersistentOption("ExcludeRegularExpression",
  1868. args[i].c_str());
  1869. this->GetMemCheckHandler()->SetPersistentOption("ExcludeRegularExpression",
  1870. args[i].c_str());
  1871. }
  1872. if (this->CheckArgument(arg, "-FA", "--fixture-exclude-any") &&
  1873. i < args.size() - 1) {
  1874. i++;
  1875. this->GetTestHandler()->SetPersistentOption(
  1876. "ExcludeFixtureRegularExpression", args[i].c_str());
  1877. this->GetMemCheckHandler()->SetPersistentOption(
  1878. "ExcludeFixtureRegularExpression", args[i].c_str());
  1879. }
  1880. if (this->CheckArgument(arg, "-FS", "--fixture-exclude-setup") &&
  1881. i < args.size() - 1) {
  1882. i++;
  1883. this->GetTestHandler()->SetPersistentOption(
  1884. "ExcludeFixtureSetupRegularExpression", args[i].c_str());
  1885. this->GetMemCheckHandler()->SetPersistentOption(
  1886. "ExcludeFixtureSetupRegularExpression", args[i].c_str());
  1887. }
  1888. if (this->CheckArgument(arg, "-FC", "--fixture-exclude-cleanup") &&
  1889. i < args.size() - 1) {
  1890. i++;
  1891. this->GetTestHandler()->SetPersistentOption(
  1892. "ExcludeFixtureCleanupRegularExpression", args[i].c_str());
  1893. this->GetMemCheckHandler()->SetPersistentOption(
  1894. "ExcludeFixtureCleanupRegularExpression", args[i].c_str());
  1895. }
  1896. if (this->CheckArgument(arg, "--rerun-failed")) {
  1897. this->GetTestHandler()->SetPersistentOption("RerunFailed", "true");
  1898. this->GetMemCheckHandler()->SetPersistentOption("RerunFailed", "true");
  1899. }
  1900. return true;
  1901. }
  1902. #if !defined(_WIN32)
  1903. bool cmCTest::ConsoleIsNotDumb()
  1904. {
  1905. std::string term_env_variable;
  1906. if (cmSystemTools::GetEnv("TERM", term_env_variable)) {
  1907. return isatty(1) && term_env_variable != "dumb";
  1908. }
  1909. return false;
  1910. }
  1911. #endif
  1912. bool cmCTest::ProgressOutputSupportedByConsole()
  1913. {
  1914. #if defined(_WIN32)
  1915. // On Windows we need a console buffer.
  1916. void* console = GetStdHandle(STD_OUTPUT_HANDLE);
  1917. CONSOLE_SCREEN_BUFFER_INFO csbi;
  1918. return GetConsoleScreenBufferInfo(console, &csbi);
  1919. #else
  1920. // On UNIX we need a non-dumb tty.
  1921. return ConsoleIsNotDumb();
  1922. #endif
  1923. }
  1924. bool cmCTest::ColoredOutputSupportedByConsole()
  1925. {
  1926. #if defined(_WIN32)
  1927. // Not supported on Windows
  1928. return false;
  1929. #else
  1930. // On UNIX we need a non-dumb tty.
  1931. return ConsoleIsNotDumb();
  1932. #endif
  1933. }
  1934. // handle the -S -SR and -SP arguments
  1935. void cmCTest::HandleScriptArguments(size_t& i, std::vector<std::string>& args,
  1936. bool& SRArgumentSpecified)
  1937. {
  1938. std::string arg = args[i];
  1939. if (this->CheckArgument(arg, "-SP", "--script-new-process") &&
  1940. i < args.size() - 1) {
  1941. this->Impl->RunConfigurationScript = true;
  1942. i++;
  1943. cmCTestScriptHandler* ch = this->GetScriptHandler();
  1944. // -SR is an internal argument, -SP should be ignored when it is passed
  1945. if (!SRArgumentSpecified) {
  1946. ch->AddConfigurationScript(args[i].c_str(), false);
  1947. }
  1948. }
  1949. if (this->CheckArgument(arg, "-SR", "--script-run") && i < args.size() - 1) {
  1950. SRArgumentSpecified = true;
  1951. this->Impl->RunConfigurationScript = true;
  1952. i++;
  1953. cmCTestScriptHandler* ch = this->GetScriptHandler();
  1954. ch->AddConfigurationScript(args[i].c_str(), true);
  1955. }
  1956. if (this->CheckArgument(arg, "-S", "--script") && i < args.size() - 1) {
  1957. this->Impl->RunConfigurationScript = true;
  1958. i++;
  1959. cmCTestScriptHandler* ch = this->GetScriptHandler();
  1960. // -SR is an internal argument, -S should be ignored when it is passed
  1961. if (!SRArgumentSpecified) {
  1962. ch->AddConfigurationScript(args[i].c_str(), true);
  1963. }
  1964. }
  1965. }
  1966. bool cmCTest::AddVariableDefinition(const std::string& arg)
  1967. {
  1968. std::string name;
  1969. std::string value;
  1970. cmStateEnums::CacheEntryType type = cmStateEnums::UNINITIALIZED;
  1971. if (cmake::ParseCacheEntry(arg, name, value, type)) {
  1972. this->Impl->Definitions[name] = value;
  1973. return true;
  1974. }
  1975. return false;
  1976. }
  1977. // the main entry point of ctest, called from main
  1978. int cmCTest::Run(std::vector<std::string>& args, std::string* output)
  1979. {
  1980. const char* ctestExec = "ctest";
  1981. bool cmakeAndTest = false;
  1982. bool executeTests = true;
  1983. bool SRArgumentSpecified = false;
  1984. // copy the command line
  1985. cmAppend(this->Impl->InitialCommandLineArguments, args);
  1986. // process the command line arguments
  1987. for (size_t i = 1; i < args.size(); ++i) {
  1988. // handle the simple commandline arguments
  1989. std::string errormsg;
  1990. if (!this->HandleCommandLineArguments(i, args, errormsg)) {
  1991. cmSystemTools::Error(errormsg);
  1992. return 1;
  1993. }
  1994. // handle the script arguments -S -SR -SP
  1995. this->HandleScriptArguments(i, args, SRArgumentSpecified);
  1996. // --dashboard: handle a request for a dashboard
  1997. std::string arg = args[i];
  1998. if (this->CheckArgument(arg, "-D", "--dashboard") && i < args.size() - 1) {
  1999. this->Impl->ProduceXML = true;
  2000. i++;
  2001. std::string targ = args[i];
  2002. // AddTestsForDashboard parses the dashboard type and converts it
  2003. // into the separate stages
  2004. if (!this->AddTestsForDashboardType(targ)) {
  2005. if (!this->AddVariableDefinition(targ)) {
  2006. this->ErrorMessageUnknownDashDValue(targ);
  2007. executeTests = false;
  2008. }
  2009. }
  2010. }
  2011. // If it's not exactly -D, but it starts with -D, then try to parse out
  2012. // a variable definition from it, same as CMake does. Unsuccessful
  2013. // attempts are simply ignored since previous ctest versions ignore
  2014. // this too. (As well as many other unknown command line args.)
  2015. //
  2016. if (arg != "-D" && cmSystemTools::StringStartsWith(arg.c_str(), "-D")) {
  2017. std::string input = arg.substr(2);
  2018. this->AddVariableDefinition(input);
  2019. }
  2020. // --test-action: calls SetTest(<stage>, /*report=*/ false) to enable
  2021. // the corresponding stage
  2022. if (!this->HandleTestActionArgument(ctestExec, i, args)) {
  2023. executeTests = false;
  2024. }
  2025. // --test-model: what type of test model
  2026. if (!this->HandleTestModelArgument(ctestExec, i, args)) {
  2027. executeTests = false;
  2028. }
  2029. // --extra-submit
  2030. if (this->CheckArgument(arg, "--extra-submit") && i < args.size() - 1) {
  2031. this->Impl->ProduceXML = true;
  2032. this->SetTest("Submit");
  2033. i++;
  2034. if (!this->SubmitExtraFiles(args[i].c_str())) {
  2035. return 0;
  2036. }
  2037. }
  2038. // --build-and-test options
  2039. if (this->CheckArgument(arg, "--build-and-test") && i < args.size() - 1) {
  2040. cmakeAndTest = true;
  2041. }
  2042. // --schedule-random
  2043. if (this->CheckArgument(arg, "--schedule-random")) {
  2044. this->Impl->ScheduleType = "Random";
  2045. }
  2046. // pass the argument to all the handlers as well, but i may no longer be
  2047. // set to what it was originally so I'm not sure this is working as
  2048. // intended
  2049. for (auto& handler : this->Impl->GetTestingHandlers()) {
  2050. if (!handler->ProcessCommandLineArguments(arg, i, args)) {
  2051. cmCTestLog(this, ERROR_MESSAGE,
  2052. "Problem parsing command line arguments within a handler");
  2053. return 0;
  2054. }
  2055. }
  2056. } // the close of the for argument loop
  2057. // handle CTEST_PARALLEL_LEVEL environment variable
  2058. if (!this->Impl->ParallelLevelSetInCli) {
  2059. std::string parallel;
  2060. if (cmSystemTools::GetEnv("CTEST_PARALLEL_LEVEL", parallel)) {
  2061. int plevel = atoi(parallel.c_str());
  2062. this->SetParallelLevel(plevel);
  2063. }
  2064. }
  2065. // TestProgressOutput only supported if console supports it and not logging
  2066. // to a file
  2067. this->Impl->TestProgressOutput = this->Impl->TestProgressOutput &&
  2068. !this->Impl->OutputLogFile && this->ProgressOutputSupportedByConsole();
  2069. #ifdef _WIN32
  2070. if (this->Impl->TestProgressOutput) {
  2071. // Disable output line buffering so we can print content without
  2072. // a newline.
  2073. std::setvbuf(stdout, nullptr, _IONBF, 0);
  2074. }
  2075. #endif
  2076. // now what should cmake do? if --build-and-test was specified then
  2077. // we run the build and test handler and return
  2078. if (cmakeAndTest) {
  2079. return this->RunCMakeAndTest(output);
  2080. }
  2081. if (executeTests) {
  2082. return this->ExecuteTests();
  2083. }
  2084. return 1;
  2085. }
  2086. bool cmCTest::HandleTestActionArgument(const char* ctestExec, size_t& i,
  2087. const std::vector<std::string>& args)
  2088. {
  2089. bool success = true;
  2090. std::string arg = args[i];
  2091. if (this->CheckArgument(arg, "-T", "--test-action") &&
  2092. (i < args.size() - 1)) {
  2093. this->Impl->ProduceXML = true;
  2094. i++;
  2095. if (!this->SetTest(args[i].c_str(), false)) {
  2096. success = false;
  2097. cmCTestLog(this, ERROR_MESSAGE,
  2098. "CTest -T called with incorrect option: " << args[i]
  2099. << std::endl);
  2100. cmCTestLog(this, ERROR_MESSAGE,
  2101. "Available options are:"
  2102. << std::endl
  2103. << " " << ctestExec << " -T all" << std::endl
  2104. << " " << ctestExec << " -T start" << std::endl
  2105. << " " << ctestExec << " -T update" << std::endl
  2106. << " " << ctestExec << " -T configure" << std::endl
  2107. << " " << ctestExec << " -T build" << std::endl
  2108. << " " << ctestExec << " -T test" << std::endl
  2109. << " " << ctestExec << " -T coverage" << std::endl
  2110. << " " << ctestExec << " -T memcheck" << std::endl
  2111. << " " << ctestExec << " -T notes" << std::endl
  2112. << " " << ctestExec << " -T submit" << std::endl);
  2113. }
  2114. }
  2115. return success;
  2116. }
  2117. bool cmCTest::HandleTestModelArgument(const char* ctestExec, size_t& i,
  2118. const std::vector<std::string>& args)
  2119. {
  2120. bool success = true;
  2121. std::string arg = args[i];
  2122. if (this->CheckArgument(arg, "-M", "--test-model") &&
  2123. (i < args.size() - 1)) {
  2124. i++;
  2125. std::string const& str = args[i];
  2126. if (cmSystemTools::LowerCase(str) == "nightly") {
  2127. this->SetTestModel(cmCTest::NIGHTLY);
  2128. } else if (cmSystemTools::LowerCase(str) == "continuous") {
  2129. this->SetTestModel(cmCTest::CONTINUOUS);
  2130. } else if (cmSystemTools::LowerCase(str) == "experimental") {
  2131. this->SetTestModel(cmCTest::EXPERIMENTAL);
  2132. } else {
  2133. success = false;
  2134. cmCTestLog(this, ERROR_MESSAGE,
  2135. "CTest -M called with incorrect option: " << str
  2136. << std::endl);
  2137. cmCTestLog(this, ERROR_MESSAGE,
  2138. "Available options are:"
  2139. << std::endl
  2140. << " " << ctestExec << " -M Continuous" << std::endl
  2141. << " " << ctestExec << " -M Experimental" << std::endl
  2142. << " " << ctestExec << " -M Nightly" << std::endl);
  2143. }
  2144. }
  2145. return success;
  2146. }
  2147. int cmCTest::ExecuteTests()
  2148. {
  2149. int res;
  2150. // call process directory
  2151. if (this->Impl->RunConfigurationScript) {
  2152. if (this->Impl->ExtraVerbose) {
  2153. cmCTestLog(this, OUTPUT, "* Extra verbosity turned on" << std::endl);
  2154. }
  2155. for (auto& handler : this->Impl->GetTestingHandlers()) {
  2156. handler->SetVerbose(this->Impl->ExtraVerbose);
  2157. handler->SetSubmitIndex(this->Impl->SubmitIndex);
  2158. }
  2159. this->GetScriptHandler()->SetVerbose(this->Impl->Verbose);
  2160. res = this->GetScriptHandler()->ProcessHandler();
  2161. if (res != 0) {
  2162. cmCTestLog(this, DEBUG,
  2163. "running script failing returning: " << res << std::endl);
  2164. }
  2165. } else {
  2166. // What is this? -V seems to be the same as -VV,
  2167. // and Verbose is always on in this case
  2168. this->Impl->ExtraVerbose = this->Impl->Verbose;
  2169. this->Impl->Verbose = true;
  2170. for (auto& handler : this->Impl->GetTestingHandlers()) {
  2171. handler->SetVerbose(this->Impl->Verbose);
  2172. handler->SetSubmitIndex(this->Impl->SubmitIndex);
  2173. }
  2174. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2175. if (!this->Initialize(cwd.c_str(), nullptr)) {
  2176. res = 12;
  2177. cmCTestLog(this, ERROR_MESSAGE,
  2178. "Problem initializing the dashboard." << std::endl);
  2179. } else {
  2180. res = this->ProcessSteps();
  2181. }
  2182. this->Finalize();
  2183. }
  2184. if (res != 0) {
  2185. cmCTestLog(this, DEBUG,
  2186. "Running a test(s) failed returning : " << res << std::endl);
  2187. }
  2188. return res;
  2189. }
  2190. int cmCTest::RunCMakeAndTest(std::string* output)
  2191. {
  2192. this->Impl->Verbose = true;
  2193. cmCTestBuildAndTestHandler* handler = this->GetBuildAndTestHandler();
  2194. int retv = handler->ProcessHandler();
  2195. *output = handler->GetOutput();
  2196. #ifdef CMAKE_BUILD_WITH_CMAKE
  2197. cmDynamicLoader::FlushCache();
  2198. #endif
  2199. if (retv != 0) {
  2200. cmCTestLog(this, DEBUG,
  2201. "build and test failing returning: " << retv << std::endl);
  2202. }
  2203. return retv;
  2204. }
  2205. void cmCTest::SetNotesFiles(const char* notes)
  2206. {
  2207. if (!notes) {
  2208. return;
  2209. }
  2210. this->Impl->NotesFiles = notes;
  2211. }
  2212. std::chrono::system_clock::time_point cmCTest::GetStopTime() const
  2213. {
  2214. return this->Impl->StopTime;
  2215. }
  2216. void cmCTest::SetStopTime(std::string const& time_str)
  2217. {
  2218. struct tm* lctime;
  2219. time_t current_time = time(nullptr);
  2220. lctime = gmtime(&current_time);
  2221. int gm_hour = lctime->tm_hour;
  2222. time_t gm_time = mktime(lctime);
  2223. lctime = localtime(&current_time);
  2224. int local_hour = lctime->tm_hour;
  2225. int tzone_offset = local_hour - gm_hour;
  2226. if (gm_time > current_time && gm_hour < local_hour) {
  2227. // this means gm_time is on the next day
  2228. tzone_offset -= 24;
  2229. } else if (gm_time < current_time && gm_hour > local_hour) {
  2230. // this means gm_time is on the previous day
  2231. tzone_offset += 24;
  2232. }
  2233. tzone_offset *= 100;
  2234. char buf[1024];
  2235. sprintf(buf, "%d%02d%02d %s %+05i", lctime->tm_year + 1900,
  2236. lctime->tm_mon + 1, lctime->tm_mday, time_str.c_str(), tzone_offset);
  2237. time_t stop_time = curl_getdate(buf, &current_time);
  2238. if (stop_time == -1) {
  2239. this->Impl->StopTime = std::chrono::system_clock::time_point();
  2240. return;
  2241. }
  2242. this->Impl->StopTime = std::chrono::system_clock::from_time_t(stop_time);
  2243. if (stop_time < current_time) {
  2244. this->Impl->StopTime += std::chrono::hours(24);
  2245. }
  2246. }
  2247. std::string cmCTest::GetScheduleType() const
  2248. {
  2249. return this->Impl->ScheduleType;
  2250. }
  2251. void cmCTest::SetScheduleType(std::string const& type)
  2252. {
  2253. this->Impl->ScheduleType = type;
  2254. }
  2255. int cmCTest::ReadCustomConfigurationFileTree(const char* dir, cmMakefile* mf)
  2256. {
  2257. bool found = false;
  2258. cmCTestLog(this, DEBUG,
  2259. "* Read custom CTest configuration directory: " << dir
  2260. << std::endl);
  2261. std::string fname = dir;
  2262. fname += "/CTestCustom.cmake";
  2263. cmCTestLog(this, DEBUG, "* Check for file: " << fname << std::endl);
  2264. if (cmSystemTools::FileExists(fname)) {
  2265. cmCTestLog(this, DEBUG,
  2266. "* Read custom CTest configuration file: " << fname
  2267. << std::endl);
  2268. bool erroroc = cmSystemTools::GetErrorOccuredFlag();
  2269. cmSystemTools::ResetErrorOccuredFlag();
  2270. if (!mf->ReadListFile(fname) || cmSystemTools::GetErrorOccuredFlag()) {
  2271. cmCTestLog(this, ERROR_MESSAGE,
  2272. "Problem reading custom configuration: " << fname
  2273. << std::endl);
  2274. }
  2275. found = true;
  2276. if (erroroc) {
  2277. cmSystemTools::SetErrorOccured();
  2278. }
  2279. }
  2280. std::string rexpr = dir;
  2281. rexpr += "/CTestCustom.ctest";
  2282. cmCTestLog(this, DEBUG, "* Check for file: " << rexpr << std::endl);
  2283. if (!found && cmSystemTools::FileExists(rexpr)) {
  2284. cmsys::Glob gl;
  2285. gl.RecurseOn();
  2286. gl.FindFiles(rexpr);
  2287. std::vector<std::string>& files = gl.GetFiles();
  2288. for (const std::string& file : files) {
  2289. cmCTestLog(this, DEBUG,
  2290. "* Read custom CTest configuration file: " << file
  2291. << std::endl);
  2292. if (!mf->ReadListFile(file) || cmSystemTools::GetErrorOccuredFlag()) {
  2293. cmCTestLog(this, ERROR_MESSAGE,
  2294. "Problem reading custom configuration: " << file
  2295. << std::endl);
  2296. }
  2297. }
  2298. found = true;
  2299. }
  2300. if (found) {
  2301. for (auto& handler : this->Impl->GetNamedTestingHandlers()) {
  2302. cmCTestLog(this, DEBUG,
  2303. "* Read custom CTest configuration vectors for handler: "
  2304. << handler.first << " (" << handler.second << ")"
  2305. << std::endl);
  2306. handler.second->PopulateCustomVectors(mf);
  2307. }
  2308. }
  2309. return 1;
  2310. }
  2311. void cmCTest::PopulateCustomVector(cmMakefile* mf, const std::string& def,
  2312. std::vector<std::string>& vec)
  2313. {
  2314. const char* dval = mf->GetDefinition(def);
  2315. if (!dval) {
  2316. return;
  2317. }
  2318. cmCTestLog(this, DEBUG, "PopulateCustomVector: " << def << std::endl);
  2319. vec.clear();
  2320. cmSystemTools::ExpandListArgument(dval, vec);
  2321. for (std::string const& it : vec) {
  2322. cmCTestLog(this, DEBUG, " -- " << it << std::endl);
  2323. }
  2324. }
  2325. void cmCTest::PopulateCustomInteger(cmMakefile* mf, const std::string& def,
  2326. int& val)
  2327. {
  2328. const char* dval = mf->GetDefinition(def);
  2329. if (!dval) {
  2330. return;
  2331. }
  2332. val = atoi(dval);
  2333. }
  2334. std::string cmCTest::GetShortPathToFile(const char* cfname)
  2335. {
  2336. const std::string& sourceDir = cmSystemTools::CollapseFullPath(
  2337. this->GetCTestConfiguration("SourceDirectory"));
  2338. const std::string& buildDir = cmSystemTools::CollapseFullPath(
  2339. this->GetCTestConfiguration("BuildDirectory"));
  2340. std::string fname = cmSystemTools::CollapseFullPath(cfname);
  2341. // Find relative paths to both directories
  2342. std::string srcRelpath = cmSystemTools::RelativePath(sourceDir, fname);
  2343. std::string bldRelpath = cmSystemTools::RelativePath(buildDir, fname);
  2344. // If any contains "." it is not parent directory
  2345. bool inSrc = srcRelpath.find("..") == std::string::npos;
  2346. bool inBld = bldRelpath.find("..") == std::string::npos;
  2347. // TODO: Handle files with .. in their name
  2348. std::string* res = nullptr;
  2349. if (inSrc && inBld) {
  2350. // If both have relative path with no dots, pick the shorter one
  2351. if (srcRelpath.size() < bldRelpath.size()) {
  2352. res = &srcRelpath;
  2353. } else {
  2354. res = &bldRelpath;
  2355. }
  2356. } else if (inSrc) {
  2357. res = &srcRelpath;
  2358. } else if (inBld) {
  2359. res = &bldRelpath;
  2360. }
  2361. std::string path;
  2362. if (!res) {
  2363. path = fname;
  2364. } else {
  2365. cmSystemTools::ConvertToUnixSlashes(*res);
  2366. path = "./" + *res;
  2367. if (path.back() == '/') {
  2368. path = path.substr(0, path.size() - 1);
  2369. }
  2370. }
  2371. cmsys::SystemTools::ReplaceString(path, ":", "_");
  2372. cmsys::SystemTools::ReplaceString(path, " ", "_");
  2373. return path;
  2374. }
  2375. std::string cmCTest::GetCTestConfiguration(const std::string& name)
  2376. {
  2377. if (this->Impl->CTestConfigurationOverwrites.find(name) !=
  2378. this->Impl->CTestConfigurationOverwrites.end()) {
  2379. return this->Impl->CTestConfigurationOverwrites[name];
  2380. }
  2381. return this->Impl->CTestConfiguration[name];
  2382. }
  2383. void cmCTest::EmptyCTestConfiguration()
  2384. {
  2385. this->Impl->CTestConfiguration.clear();
  2386. }
  2387. void cmCTest::SetCTestConfiguration(const char* name, const char* value,
  2388. bool suppress)
  2389. {
  2390. cmCTestOptionalLog(this, HANDLER_VERBOSE_OUTPUT,
  2391. "SetCTestConfiguration:"
  2392. << name << ":" << (value ? value : "(null)") << "\n",
  2393. suppress);
  2394. if (!name) {
  2395. return;
  2396. }
  2397. if (!value) {
  2398. this->Impl->CTestConfiguration.erase(name);
  2399. return;
  2400. }
  2401. this->Impl->CTestConfiguration[name] = value;
  2402. }
  2403. std::string cmCTest::GetSubmitURL()
  2404. {
  2405. std::string url = this->GetCTestConfiguration("SubmitURL");
  2406. if (url.empty()) {
  2407. std::string method = this->GetCTestConfiguration("DropMethod");
  2408. std::string user = this->GetCTestConfiguration("DropSiteUser");
  2409. std::string password = this->GetCTestConfiguration("DropSitePassword");
  2410. std::string site = this->GetCTestConfiguration("DropSite");
  2411. std::string location = this->GetCTestConfiguration("DropLocation");
  2412. url = method.empty() ? "http" : method;
  2413. url += "://";
  2414. if (!user.empty()) {
  2415. url += user;
  2416. if (!password.empty()) {
  2417. url += ':';
  2418. url += password;
  2419. }
  2420. url += '@';
  2421. }
  2422. url += site;
  2423. url += location;
  2424. }
  2425. return url;
  2426. }
  2427. std::string cmCTest::GetCurrentTag()
  2428. {
  2429. return this->Impl->CurrentTag;
  2430. }
  2431. std::string cmCTest::GetBinaryDir()
  2432. {
  2433. return this->Impl->BinaryDir;
  2434. }
  2435. std::string const& cmCTest::GetConfigType()
  2436. {
  2437. return this->Impl->ConfigType;
  2438. }
  2439. cmDuration cmCTest::GetTimeOut() const
  2440. {
  2441. return this->Impl->TimeOut;
  2442. }
  2443. void cmCTest::SetTimeOut(cmDuration t)
  2444. {
  2445. this->Impl->TimeOut = t;
  2446. }
  2447. cmDuration cmCTest::GetGlobalTimeout() const
  2448. {
  2449. return this->Impl->GlobalTimeout;
  2450. }
  2451. bool cmCTest::GetShowOnly()
  2452. {
  2453. return this->Impl->ShowOnly;
  2454. }
  2455. bool cmCTest::GetOutputAsJson()
  2456. {
  2457. return this->Impl->OutputAsJson;
  2458. }
  2459. int cmCTest::GetOutputAsJsonVersion()
  2460. {
  2461. return this->Impl->OutputAsJsonVersion;
  2462. }
  2463. bool cmCTest::ShouldUseHTTP10() const
  2464. {
  2465. return this->Impl->UseHTTP10;
  2466. }
  2467. bool cmCTest::ShouldPrintLabels() const
  2468. {
  2469. return this->Impl->PrintLabels;
  2470. }
  2471. int cmCTest::GetMaxTestNameWidth() const
  2472. {
  2473. return this->Impl->MaxTestNameWidth;
  2474. }
  2475. void cmCTest::SetMaxTestNameWidth(int w)
  2476. {
  2477. this->Impl->MaxTestNameWidth = w;
  2478. }
  2479. void cmCTest::SetProduceXML(bool v)
  2480. {
  2481. this->Impl->ProduceXML = v;
  2482. }
  2483. bool cmCTest::GetProduceXML()
  2484. {
  2485. return this->Impl->ProduceXML;
  2486. }
  2487. std::vector<std::string>& cmCTest::GetInitialCommandLineArguments()
  2488. {
  2489. return this->Impl->InitialCommandLineArguments;
  2490. }
  2491. const char* cmCTest::GetSpecificTrack()
  2492. {
  2493. if (this->Impl->SpecificTrack.empty()) {
  2494. return nullptr;
  2495. }
  2496. return this->Impl->SpecificTrack.c_str();
  2497. }
  2498. void cmCTest::SetSpecificTrack(const char* track)
  2499. {
  2500. if (!track) {
  2501. this->Impl->SpecificTrack.clear();
  2502. return;
  2503. }
  2504. this->Impl->SpecificTrack = track;
  2505. }
  2506. void cmCTest::SetFailover(bool failover)
  2507. {
  2508. this->Impl->Failover = failover;
  2509. }
  2510. bool cmCTest::GetFailover() const
  2511. {
  2512. return this->Impl->Failover;
  2513. }
  2514. bool cmCTest::GetTestProgressOutput() const
  2515. {
  2516. return this->Impl->TestProgressOutput;
  2517. }
  2518. bool cmCTest::GetVerbose() const
  2519. {
  2520. return this->Impl->Verbose;
  2521. }
  2522. bool cmCTest::GetExtraVerbose() const
  2523. {
  2524. return this->Impl->ExtraVerbose;
  2525. }
  2526. void cmCTest::SetStreams(std::ostream* out, std::ostream* err)
  2527. {
  2528. this->Impl->StreamOut = out;
  2529. this->Impl->StreamErr = err;
  2530. }
  2531. bool cmCTest::GetLabelSummary() const
  2532. {
  2533. return this->Impl->LabelSummary;
  2534. }
  2535. bool cmCTest::GetSubprojectSummary() const
  2536. {
  2537. return this->Impl->SubprojectSummary;
  2538. }
  2539. bool cmCTest::GetOutputTestOutputOnTestFailure() const
  2540. {
  2541. return this->Impl->OutputTestOutputOnTestFailure;
  2542. }
  2543. const std::map<std::string, std::string>& cmCTest::GetDefinitions() const
  2544. {
  2545. return this->Impl->Definitions;
  2546. }
  2547. int cmCTest::GetTestRepeat() const
  2548. {
  2549. return this->Impl->RepeatTests;
  2550. }
  2551. bool cmCTest::GetRepeatUntilFail() const
  2552. {
  2553. return this->Impl->RepeatUntilFail;
  2554. }
  2555. void cmCTest::SetBuildID(const std::string& id)
  2556. {
  2557. this->Impl->BuildID = id;
  2558. }
  2559. std::string cmCTest::GetBuildID() const
  2560. {
  2561. return this->Impl->BuildID;
  2562. }
  2563. void cmCTest::AddSubmitFile(Part part, const char* name)
  2564. {
  2565. this->Impl->Parts[part].SubmitFiles.emplace_back(name);
  2566. }
  2567. std::vector<std::string> const& cmCTest::GetSubmitFiles(Part part) const
  2568. {
  2569. return this->Impl->Parts[part].SubmitFiles;
  2570. }
  2571. void cmCTest::ClearSubmitFiles(Part part)
  2572. {
  2573. this->Impl->Parts[part].SubmitFiles.clear();
  2574. }
  2575. void cmCTest::SetSuppressUpdatingCTestConfiguration(bool val)
  2576. {
  2577. this->Impl->SuppressUpdatingCTestConfiguration = val;
  2578. }
  2579. void cmCTest::AddCTestConfigurationOverwrite(const std::string& overStr)
  2580. {
  2581. size_t epos = overStr.find('=');
  2582. if (epos == std::string::npos) {
  2583. cmCTestLog(this, ERROR_MESSAGE,
  2584. "CTest configuration overwrite specified in the wrong format."
  2585. << std::endl
  2586. << "Valid format is: --overwrite key=value" << std::endl
  2587. << "The specified was: --overwrite " << overStr << std::endl);
  2588. return;
  2589. }
  2590. std::string key = overStr.substr(0, epos);
  2591. std::string value = overStr.substr(epos + 1);
  2592. this->Impl->CTestConfigurationOverwrites[key] = value;
  2593. }
  2594. void cmCTest::SetConfigType(const char* ct)
  2595. {
  2596. this->Impl->ConfigType = ct ? ct : "";
  2597. cmSystemTools::ReplaceString(this->Impl->ConfigType, ".\\", "");
  2598. std::string confTypeEnv = "CMAKE_CONFIG_TYPE=" + this->Impl->ConfigType;
  2599. cmSystemTools::PutEnv(confTypeEnv);
  2600. }
  2601. bool cmCTest::SetCTestConfigurationFromCMakeVariable(
  2602. cmMakefile* mf, const char* dconfig, const std::string& cmake_var,
  2603. bool suppress)
  2604. {
  2605. const char* ctvar;
  2606. ctvar = mf->GetDefinition(cmake_var);
  2607. if (!ctvar) {
  2608. return false;
  2609. }
  2610. cmCTestOptionalLog(this, HANDLER_VERBOSE_OUTPUT,
  2611. "SetCTestConfigurationFromCMakeVariable:"
  2612. << dconfig << ":" << cmake_var << std::endl,
  2613. suppress);
  2614. this->SetCTestConfiguration(dconfig, ctvar, suppress);
  2615. return true;
  2616. }
  2617. bool cmCTest::RunCommand(std::vector<std::string> const& args,
  2618. std::string* stdOut, std::string* stdErr, int* retVal,
  2619. const char* dir, cmDuration timeout,
  2620. Encoding encoding)
  2621. {
  2622. std::vector<const char*> argv;
  2623. argv.reserve(args.size() + 1);
  2624. for (std::string const& a : args) {
  2625. argv.push_back(a.c_str());
  2626. }
  2627. argv.push_back(nullptr);
  2628. stdOut->clear();
  2629. stdErr->clear();
  2630. cmsysProcess* cp = cmsysProcess_New();
  2631. cmsysProcess_SetCommand(cp, argv.data());
  2632. cmsysProcess_SetWorkingDirectory(cp, dir);
  2633. if (cmSystemTools::GetRunCommandHideConsole()) {
  2634. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  2635. }
  2636. cmsysProcess_SetTimeout(cp, timeout.count());
  2637. cmsysProcess_Execute(cp);
  2638. std::vector<char> tempOutput;
  2639. std::vector<char> tempError;
  2640. char* data;
  2641. int length;
  2642. cmProcessOutput processOutput(encoding);
  2643. std::string strdata;
  2644. int res;
  2645. bool done = false;
  2646. while (!done) {
  2647. res = cmsysProcess_WaitForData(cp, &data, &length, nullptr);
  2648. switch (res) {
  2649. case cmsysProcess_Pipe_STDOUT:
  2650. cmAppend(tempOutput, data, data + length);
  2651. break;
  2652. case cmsysProcess_Pipe_STDERR:
  2653. cmAppend(tempError, data, data + length);
  2654. break;
  2655. default:
  2656. done = true;
  2657. }
  2658. if ((res == cmsysProcess_Pipe_STDOUT || res == cmsysProcess_Pipe_STDERR) &&
  2659. this->Impl->ExtraVerbose) {
  2660. processOutput.DecodeText(data, length, strdata);
  2661. cmSystemTools::Stdout(strdata);
  2662. }
  2663. }
  2664. if (this->Impl->ExtraVerbose) {
  2665. processOutput.DecodeText(std::string(), strdata);
  2666. if (!strdata.empty()) {
  2667. cmSystemTools::Stdout(strdata);
  2668. }
  2669. }
  2670. cmsysProcess_WaitForExit(cp, nullptr);
  2671. if (!tempOutput.empty()) {
  2672. processOutput.DecodeText(tempOutput, tempOutput);
  2673. stdOut->append(tempOutput.data(), tempOutput.size());
  2674. }
  2675. if (!tempError.empty()) {
  2676. processOutput.DecodeText(tempError, tempError);
  2677. stdErr->append(tempError.data(), tempError.size());
  2678. }
  2679. bool result = true;
  2680. if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exited) {
  2681. if (retVal) {
  2682. *retVal = cmsysProcess_GetExitValue(cp);
  2683. } else {
  2684. if (cmsysProcess_GetExitValue(cp) != 0) {
  2685. result = false;
  2686. }
  2687. }
  2688. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exception) {
  2689. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  2690. cmCTestLog(this, ERROR_MESSAGE, exception_str << std::endl);
  2691. stdErr->append(exception_str, strlen(exception_str));
  2692. result = false;
  2693. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Error) {
  2694. const char* error_str = cmsysProcess_GetErrorString(cp);
  2695. cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
  2696. stdErr->append(error_str, strlen(error_str));
  2697. result = false;
  2698. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Expired) {
  2699. const char* error_str = "Process terminated due to timeout\n";
  2700. cmCTestLog(this, ERROR_MESSAGE, error_str << std::endl);
  2701. stdErr->append(error_str, strlen(error_str));
  2702. result = false;
  2703. }
  2704. cmsysProcess_Delete(cp);
  2705. return result;
  2706. }
  2707. void cmCTest::SetOutputLogFileName(const char* name)
  2708. {
  2709. if (this->Impl->OutputLogFile) {
  2710. delete this->Impl->OutputLogFile;
  2711. this->Impl->OutputLogFile = nullptr;
  2712. }
  2713. if (name) {
  2714. this->Impl->OutputLogFile = new cmGeneratedFileStream(name);
  2715. }
  2716. }
  2717. static const char* cmCTestStringLogType[] = { "DEBUG",
  2718. "OUTPUT",
  2719. "HANDLER_OUTPUT",
  2720. "HANDLER_PROGRESS_OUTPUT",
  2721. "HANDLER_TEST_PROGRESS_OUTPUT",
  2722. "HANDLER_VERBOSE_OUTPUT",
  2723. "WARNING",
  2724. "ERROR_MESSAGE",
  2725. nullptr };
  2726. #define cmCTestLogOutputFileLine(stream) \
  2727. do { \
  2728. if (this->Impl->ShowLineNumbers) { \
  2729. (stream) << std::endl << file << ":" << line << " "; \
  2730. } \
  2731. } while (false)
  2732. void cmCTest::Log(int logType, const char* file, int line, const char* msg,
  2733. bool suppress)
  2734. {
  2735. if (!msg || !*msg) {
  2736. return;
  2737. }
  2738. if (suppress && logType != cmCTest::ERROR_MESSAGE) {
  2739. return;
  2740. }
  2741. if (logType == cmCTest::HANDLER_PROGRESS_OUTPUT &&
  2742. (this->Impl->Debug || this->Impl->ExtraVerbose)) {
  2743. return;
  2744. }
  2745. if (this->Impl->OutputLogFile) {
  2746. bool display = true;
  2747. if (logType == cmCTest::DEBUG && !this->Impl->Debug) {
  2748. display = false;
  2749. }
  2750. if (logType == cmCTest::HANDLER_VERBOSE_OUTPUT && !this->Impl->Debug &&
  2751. !this->Impl->ExtraVerbose) {
  2752. display = false;
  2753. }
  2754. if (display) {
  2755. cmCTestLogOutputFileLine(*this->Impl->OutputLogFile);
  2756. if (logType != this->Impl->OutputLogFileLastTag) {
  2757. *this->Impl->OutputLogFile << "[";
  2758. if (logType >= OTHER || logType < 0) {
  2759. *this->Impl->OutputLogFile << "OTHER";
  2760. } else {
  2761. *this->Impl->OutputLogFile << cmCTestStringLogType[logType];
  2762. }
  2763. *this->Impl->OutputLogFile << "] " << std::endl << std::flush;
  2764. }
  2765. *this->Impl->OutputLogFile << msg << std::flush;
  2766. if (logType != this->Impl->OutputLogFileLastTag) {
  2767. *this->Impl->OutputLogFile << std::endl << std::flush;
  2768. this->Impl->OutputLogFileLastTag = logType;
  2769. }
  2770. }
  2771. }
  2772. if (!this->Impl->Quiet) {
  2773. std::ostream& out = *this->Impl->StreamOut;
  2774. std::ostream& err = *this->Impl->StreamErr;
  2775. if (logType == HANDLER_TEST_PROGRESS_OUTPUT) {
  2776. if (this->Impl->TestProgressOutput) {
  2777. cmCTestLogOutputFileLine(out);
  2778. if (this->Impl->FlushTestProgressLine) {
  2779. printf("\r");
  2780. this->Impl->FlushTestProgressLine = false;
  2781. out.flush();
  2782. }
  2783. std::string msg_str{ msg };
  2784. auto const lineBreakIt = msg_str.find('\n');
  2785. if (lineBreakIt != std::string::npos) {
  2786. this->Impl->FlushTestProgressLine = true;
  2787. msg_str.erase(std::remove(msg_str.begin(), msg_str.end(), '\n'),
  2788. msg_str.end());
  2789. }
  2790. out << msg_str;
  2791. #ifndef _WIN32
  2792. printf("\x1B[K"); // move caret to end
  2793. #endif
  2794. out.flush();
  2795. return;
  2796. }
  2797. logType = HANDLER_OUTPUT;
  2798. }
  2799. switch (logType) {
  2800. case DEBUG:
  2801. if (this->Impl->Debug) {
  2802. cmCTestLogOutputFileLine(out);
  2803. out << msg;
  2804. out.flush();
  2805. }
  2806. break;
  2807. case OUTPUT:
  2808. case HANDLER_OUTPUT:
  2809. if (this->Impl->Debug || this->Impl->Verbose) {
  2810. cmCTestLogOutputFileLine(out);
  2811. out << msg;
  2812. out.flush();
  2813. }
  2814. break;
  2815. case HANDLER_VERBOSE_OUTPUT:
  2816. if (this->Impl->Debug || this->Impl->ExtraVerbose) {
  2817. cmCTestLogOutputFileLine(out);
  2818. out << msg;
  2819. out.flush();
  2820. }
  2821. break;
  2822. case WARNING:
  2823. cmCTestLogOutputFileLine(err);
  2824. err << msg;
  2825. err.flush();
  2826. break;
  2827. case ERROR_MESSAGE:
  2828. cmCTestLogOutputFileLine(err);
  2829. err << msg;
  2830. err.flush();
  2831. cmSystemTools::SetErrorOccured();
  2832. break;
  2833. default:
  2834. cmCTestLogOutputFileLine(out);
  2835. out << msg;
  2836. out.flush();
  2837. }
  2838. }
  2839. }
  2840. std::string cmCTest::GetColorCode(Color color) const
  2841. {
  2842. if (this->Impl->OutputColorCode) {
  2843. #if defined(_WIN32)
  2844. // Not supported on Windows
  2845. static_cast<void>(color);
  2846. #else
  2847. return "\033[0;" + std::to_string(static_cast<int>(color)) + "m";
  2848. #endif
  2849. }
  2850. return "";
  2851. }
  2852. cmDuration cmCTest::GetRemainingTimeAllowed()
  2853. {
  2854. return this->GetScriptHandler()->GetRemainingTimeAllowed();
  2855. }
  2856. cmDuration cmCTest::MaxDuration()
  2857. {
  2858. return cmDuration(1.0e7);
  2859. }
  2860. void cmCTest::SetRunCurrentScript(bool value)
  2861. {
  2862. this->GetScriptHandler()->SetRunCurrentScript(value);
  2863. }
  2864. void cmCTest::OutputTestErrors(std::vector<char> const& process_output)
  2865. {
  2866. std::string test_outputs("\n*** Test Failed:\n");
  2867. if (!process_output.empty()) {
  2868. test_outputs.append(process_output.data(), process_output.size());
  2869. }
  2870. cmCTestLog(this, HANDLER_OUTPUT, test_outputs << std::endl << std::flush);
  2871. }
  2872. bool cmCTest::CompressString(std::string& str)
  2873. {
  2874. int ret;
  2875. z_stream strm;
  2876. strm.zalloc = Z_NULL;
  2877. strm.zfree = Z_NULL;
  2878. strm.opaque = Z_NULL;
  2879. ret = deflateInit(&strm, -1); // default compression level
  2880. if (ret != Z_OK) {
  2881. return false;
  2882. }
  2883. unsigned char* in =
  2884. reinterpret_cast<unsigned char*>(const_cast<char*>(str.c_str()));
  2885. // zlib makes the guarantee that this is the maximum output size
  2886. int outSize =
  2887. static_cast<int>(static_cast<double>(str.size()) * 1.001 + 13.0);
  2888. std::vector<unsigned char> out(outSize);
  2889. strm.avail_in = static_cast<uInt>(str.size());
  2890. strm.next_in = in;
  2891. strm.avail_out = outSize;
  2892. strm.next_out = &out[0];
  2893. ret = deflate(&strm, Z_FINISH);
  2894. if (ret != Z_STREAM_END) {
  2895. cmCTestLog(this, ERROR_MESSAGE,
  2896. "Error during gzip compression." << std::endl);
  2897. return false;
  2898. }
  2899. (void)deflateEnd(&strm);
  2900. // Now base64 encode the resulting binary string
  2901. std::vector<unsigned char> base64EncodedBuffer((outSize * 3) / 2);
  2902. size_t rlen =
  2903. cmsysBase64_Encode(&out[0], strm.total_out, &base64EncodedBuffer[0], 1);
  2904. str.assign(reinterpret_cast<char*>(&base64EncodedBuffer[0]), rlen);
  2905. return true;
  2906. }