cmCTest.cxx 98 KB

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