cmCTest.cxx 102 KB

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