cmCTest.cxx 91 KB

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