cmCTest.cxx 98 KB

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