cmCTest.cxx 100 KB

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