1
0

cmCTestCoverageHandler.cxx 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestCoverageHandler.h"
  11. #include "cmParsePHPCoverage.h"
  12. #include "cmParseCoberturaCoverage.h"
  13. #include "cmParseGTMCoverage.h"
  14. #include "cmParseCacheCoverage.h"
  15. #include "cmParseJacocoCoverage.h"
  16. #include "cmParseDelphiCoverage.h"
  17. #include "cmParseBlanketJSCoverage.h"
  18. #include "cmCTest.h"
  19. #include "cmake.h"
  20. #include "cmMakefile.h"
  21. #include "cmSystemTools.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include "cmXMLSafe.h"
  24. #include <cmsys/Process.h>
  25. #include <cmsys/RegularExpression.hxx>
  26. #include <cmsys/Glob.hxx>
  27. #include <cmsys/stl/iterator>
  28. #include <cmsys/stl/algorithm>
  29. #include <cmsys/FStream.hxx>
  30. #include <stdlib.h>
  31. #include <math.h>
  32. #include <float.h>
  33. #define SAFEDIV(x,y) (((y)!=0)?((x)/(y)):(0))
  34. class cmCTestRunProcess
  35. {
  36. public:
  37. cmCTestRunProcess()
  38. {
  39. this->Process = cmsysProcess_New();
  40. this->PipeState = -1;
  41. this->TimeOut = -1;
  42. }
  43. ~cmCTestRunProcess()
  44. {
  45. if(!(this->PipeState == -1)
  46. && !(this->PipeState == cmsysProcess_Pipe_None )
  47. && !(this->PipeState == cmsysProcess_Pipe_Timeout))
  48. {
  49. this->WaitForExit();
  50. }
  51. cmsysProcess_Delete(this->Process);
  52. }
  53. void SetCommand(const char* command)
  54. {
  55. this->CommandLineStrings.clear();
  56. this->CommandLineStrings.push_back(command);;
  57. }
  58. void AddArgument(const char* arg)
  59. {
  60. if(arg)
  61. {
  62. this->CommandLineStrings.push_back(arg);
  63. }
  64. }
  65. void SetWorkingDirectory(const char* dir)
  66. {
  67. this->WorkingDirectory = dir;
  68. }
  69. void SetTimeout(double t)
  70. {
  71. this->TimeOut = t;
  72. }
  73. bool StartProcess()
  74. {
  75. std::vector<const char*> args;
  76. for(std::vector<std::string>::iterator i =
  77. this->CommandLineStrings.begin();
  78. i != this->CommandLineStrings.end(); ++i)
  79. {
  80. args.push_back(i->c_str());
  81. }
  82. args.push_back(0); // null terminate
  83. cmsysProcess_SetCommand(this->Process, &*args.begin());
  84. if(this->WorkingDirectory.size())
  85. {
  86. cmsysProcess_SetWorkingDirectory(this->Process,
  87. this->WorkingDirectory.c_str());
  88. }
  89. cmsysProcess_SetOption(this->Process,
  90. cmsysProcess_Option_HideWindow, 1);
  91. if(this->TimeOut != -1)
  92. {
  93. cmsysProcess_SetTimeout(this->Process, this->TimeOut);
  94. }
  95. cmsysProcess_Execute(this->Process);
  96. this->PipeState = cmsysProcess_GetState(this->Process);
  97. // if the process is running or exited return true
  98. if(this->PipeState == cmsysProcess_State_Executing
  99. || this->PipeState == cmsysProcess_State_Exited)
  100. {
  101. return true;
  102. }
  103. return false;
  104. }
  105. void SetStdoutFile(const char* fname)
  106. {
  107. cmsysProcess_SetPipeFile(this->Process, cmsysProcess_Pipe_STDOUT, fname);
  108. }
  109. void SetStderrFile(const char* fname)
  110. {
  111. cmsysProcess_SetPipeFile(this->Process, cmsysProcess_Pipe_STDERR, fname);
  112. }
  113. int WaitForExit(double* timeout =0)
  114. {
  115. this->PipeState = cmsysProcess_WaitForExit(this->Process,
  116. timeout);
  117. return this->PipeState;
  118. }
  119. int GetProcessState() { return this->PipeState;}
  120. private:
  121. int PipeState;
  122. cmsysProcess* Process;
  123. std::vector<std::string> CommandLineStrings;
  124. std::string WorkingDirectory;
  125. double TimeOut;
  126. };
  127. //----------------------------------------------------------------------
  128. //----------------------------------------------------------------------
  129. cmCTestCoverageHandler::cmCTestCoverageHandler()
  130. {
  131. }
  132. //----------------------------------------------------------------------
  133. void cmCTestCoverageHandler::Initialize()
  134. {
  135. this->Superclass::Initialize();
  136. this->CustomCoverageExclude.clear();
  137. this->SourceLabels.clear();
  138. this->TargetDirs.clear();
  139. this->LabelIdMap.clear();
  140. this->Labels.clear();
  141. this->LabelFilter.clear();
  142. }
  143. //----------------------------------------------------------------------------
  144. void cmCTestCoverageHandler::CleanCoverageLogFiles(std::ostream& log)
  145. {
  146. std::string logGlob = this->CTest->GetCTestConfiguration("BuildDirectory");
  147. logGlob += "/Testing/";
  148. logGlob += this->CTest->GetCurrentTag();
  149. logGlob += "/CoverageLog*";
  150. cmsys::Glob gl;
  151. gl.FindFiles(logGlob);
  152. std::vector<std::string> const& files = gl.GetFiles();
  153. for(std::vector<std::string>::const_iterator fi = files.begin();
  154. fi != files.end(); ++fi)
  155. {
  156. log << "Removing old coverage log: " << *fi << "\n";
  157. cmSystemTools::RemoveFile(*fi);
  158. }
  159. }
  160. //----------------------------------------------------------------------
  161. bool cmCTestCoverageHandler::StartCoverageLogFile(
  162. cmGeneratedFileStream& covLogFile, int logFileCount)
  163. {
  164. char covLogFilename[1024];
  165. sprintf(covLogFilename, "CoverageLog-%d", logFileCount);
  166. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Open file: "
  167. << covLogFilename << std::endl);
  168. if(!this->StartResultingXML(cmCTest::PartCoverage,
  169. covLogFilename, covLogFile))
  170. {
  171. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open log file: "
  172. << covLogFilename << std::endl);
  173. return false;
  174. }
  175. std::string local_start_time = this->CTest->CurrentTime();
  176. this->CTest->StartXML(covLogFile, this->AppendXML);
  177. covLogFile << "<CoverageLog>" << std::endl
  178. << "\t<StartDateTime>" << local_start_time << "</StartDateTime>"
  179. << "\t<StartTime>"
  180. << static_cast<unsigned int>(cmSystemTools::GetTime())
  181. << "</StartTime>"
  182. << std::endl;
  183. return true;
  184. }
  185. //----------------------------------------------------------------------
  186. void cmCTestCoverageHandler::EndCoverageLogFile(cmGeneratedFileStream& ostr,
  187. int logFileCount)
  188. {
  189. std::string local_end_time = this->CTest->CurrentTime();
  190. ostr << "\t<EndDateTime>" << local_end_time << "</EndDateTime>" << std::endl
  191. << "\t<EndTime>" <<
  192. static_cast<unsigned int>(cmSystemTools::GetTime())
  193. << "</EndTime>" << std::endl
  194. << "</CoverageLog>" << std::endl;
  195. this->CTest->EndXML(ostr);
  196. char covLogFilename[1024];
  197. sprintf(covLogFilename, "CoverageLog-%d.xml", logFileCount);
  198. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Close file: "
  199. << covLogFilename << std::endl);
  200. ostr.Close();
  201. }
  202. //----------------------------------------------------------------------
  203. bool cmCTestCoverageHandler::ShouldIDoCoverage(const char* file,
  204. const char* srcDir,
  205. const char* binDir)
  206. {
  207. if(this->IsFilteredOut(file))
  208. {
  209. return false;
  210. }
  211. std::vector<cmsys::RegularExpression>::iterator sit;
  212. for ( sit = this->CustomCoverageExcludeRegex.begin();
  213. sit != this->CustomCoverageExcludeRegex.end(); ++ sit )
  214. {
  215. if ( sit->find(file) )
  216. {
  217. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " File " << file
  218. << " is excluded in CTestCustom.ctest" << std::endl;);
  219. return false;
  220. }
  221. }
  222. std::string fSrcDir = cmSystemTools::CollapseFullPath(srcDir);
  223. std::string fBinDir = cmSystemTools::CollapseFullPath(binDir);
  224. std::string fFile = cmSystemTools::CollapseFullPath(file);
  225. bool sourceSubDir = cmSystemTools::IsSubDirectory(fFile,
  226. fSrcDir);
  227. bool buildSubDir = cmSystemTools::IsSubDirectory(fFile,
  228. fBinDir);
  229. // Always check parent directory of the file.
  230. std::string fileDir = cmSystemTools::GetFilenamePath(fFile);
  231. std::string checkDir;
  232. // We also need to check the binary/source directory pair.
  233. if ( sourceSubDir && buildSubDir )
  234. {
  235. if ( fSrcDir.size() > fBinDir.size() )
  236. {
  237. checkDir = fSrcDir;
  238. }
  239. else
  240. {
  241. checkDir = fBinDir;
  242. }
  243. }
  244. else if ( sourceSubDir )
  245. {
  246. checkDir = fSrcDir;
  247. }
  248. else if ( buildSubDir )
  249. {
  250. checkDir = fBinDir;
  251. }
  252. std::string ndc
  253. = cmSystemTools::FileExistsInParentDirectories(".NoDartCoverage",
  254. fFile.c_str(), checkDir.c_str());
  255. if ( ndc.size() )
  256. {
  257. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Found: " << ndc
  258. << " so skip coverage of " << file << std::endl);
  259. return false;
  260. }
  261. // By now checkDir should be set to parent directory of the file.
  262. // Get the relative path to the file an apply it to the opposite directory.
  263. // If it is the same as fileDir, then ignore, otherwise check.
  264. std::string relPath;
  265. if(checkDir.size() )
  266. {
  267. relPath = cmSystemTools::RelativePath(checkDir.c_str(),
  268. fFile.c_str());
  269. }
  270. else
  271. {
  272. relPath = fFile;
  273. }
  274. if ( checkDir == fSrcDir )
  275. {
  276. checkDir = fBinDir;
  277. }
  278. else
  279. {
  280. checkDir = fSrcDir;
  281. }
  282. fFile = checkDir + "/" + relPath;
  283. fFile = cmSystemTools::GetFilenamePath(fFile);
  284. if ( fileDir == fFile )
  285. {
  286. // This is in-source build, so we trust the previous check.
  287. return true;
  288. }
  289. ndc = cmSystemTools::FileExistsInParentDirectories(".NoDartCoverage",
  290. fFile.c_str(), checkDir.c_str());
  291. if ( ndc.size() )
  292. {
  293. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Found: " << ndc
  294. << " so skip coverage of: " << file << std::endl);
  295. return false;
  296. }
  297. // Ok, nothing in source tree, nothing in binary tree
  298. return true;
  299. }
  300. //----------------------------------------------------------------------
  301. //clearly it would be nice if this were broken up into a few smaller
  302. //functions and commented...
  303. int cmCTestCoverageHandler::ProcessHandler()
  304. {
  305. this->CTest->ClearSubmitFiles(cmCTest::PartCoverage);
  306. int error = 0;
  307. // do we have time for this
  308. if (this->CTest->GetRemainingTimeAllowed() < 120)
  309. {
  310. return error;
  311. }
  312. std::string coverage_start_time = this->CTest->CurrentTime();
  313. unsigned int coverage_start_time_time = static_cast<unsigned int>(
  314. cmSystemTools::GetTime());
  315. std::string sourceDir
  316. = this->CTest->GetCTestConfiguration("SourceDirectory");
  317. std::string binaryDir
  318. = this->CTest->GetCTestConfiguration("BuildDirectory");
  319. this->LoadLabels();
  320. cmGeneratedFileStream ofs;
  321. double elapsed_time_start = cmSystemTools::GetTime();
  322. if ( !this->StartLogFile("Coverage", ofs) )
  323. {
  324. cmCTestLog(this->CTest, ERROR_MESSAGE,
  325. "Cannot create LastCoverage.log file" << std::endl);
  326. }
  327. ofs << "Performing coverage: " << elapsed_time_start << std::endl;
  328. this->CleanCoverageLogFiles(ofs);
  329. cmSystemTools::ConvertToUnixSlashes(sourceDir);
  330. cmSystemTools::ConvertToUnixSlashes(binaryDir);
  331. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Performing coverage" << std::endl);
  332. cmCTestCoverageHandlerContainer cont;
  333. cont.Error = error;
  334. cont.SourceDir = sourceDir;
  335. cont.BinaryDir = binaryDir;
  336. cont.OFS = &ofs;
  337. // setup the regex exclude stuff
  338. this->CustomCoverageExcludeRegex.clear();
  339. std::vector<std::string>::iterator rexIt;
  340. for ( rexIt = this->CustomCoverageExclude.begin();
  341. rexIt != this->CustomCoverageExclude.end();
  342. ++ rexIt )
  343. {
  344. this->CustomCoverageExcludeRegex.push_back(
  345. cmsys::RegularExpression(rexIt->c_str()));
  346. }
  347. if(this->HandleBullseyeCoverage(&cont))
  348. {
  349. return cont.Error;
  350. }
  351. int file_count = 0;
  352. file_count += this->HandleGCovCoverage(&cont);
  353. error = cont.Error;
  354. if ( file_count < 0 )
  355. {
  356. return error;
  357. }
  358. file_count += this->HandleLCovCoverage(&cont);
  359. error = cont.Error;
  360. if ( file_count < 0 )
  361. {
  362. return error;
  363. }
  364. file_count += this->HandleTracePyCoverage(&cont);
  365. error = cont.Error;
  366. if ( file_count < 0 )
  367. {
  368. return error;
  369. }
  370. file_count += this->HandlePHPCoverage(&cont);
  371. error = cont.Error;
  372. if ( file_count < 0 )
  373. {
  374. return error;
  375. }
  376. file_count += this->HandleCoberturaCoverage(&cont);
  377. error = cont.Error;
  378. if ( file_count < 0 )
  379. {
  380. return error;
  381. }
  382. file_count += this->HandleMumpsCoverage(&cont);
  383. error = cont.Error;
  384. if ( file_count < 0 )
  385. {
  386. return error;
  387. }
  388. file_count += this->HandleJacocoCoverage(&cont);
  389. error = cont.Error;
  390. if ( file_count < 0 )
  391. {
  392. return error;
  393. }
  394. file_count += this->HandleBlanketJSCoverage(&cont);
  395. error = cont.Error;
  396. if ( file_count < 0 )
  397. {
  398. return error;
  399. }
  400. file_count += this->HandleDelphiCoverage(&cont);
  401. error = cont.Error;
  402. if ( file_count < 0 )
  403. {
  404. return error;
  405. }
  406. std::set<std::string> uncovered = this->FindUncoveredFiles(&cont);
  407. if ( file_count == 0 )
  408. {
  409. cmCTestLog(this->CTest, WARNING,
  410. " Cannot find any coverage files. Ignoring Coverage request."
  411. << std::endl);
  412. return error;
  413. }
  414. cmGeneratedFileStream covSumFile;
  415. cmGeneratedFileStream covLogFile;
  416. if(!this->StartResultingXML(cmCTest::PartCoverage, "Coverage", covSumFile))
  417. {
  418. cmCTestLog(this->CTest, ERROR_MESSAGE,
  419. "Cannot open coverage summary file." << std::endl);
  420. return -1;
  421. }
  422. this->CTest->StartXML(covSumFile, this->AppendXML);
  423. // Produce output xml files
  424. covSumFile << "<Coverage>" << std::endl
  425. << "\t<StartDateTime>" << coverage_start_time << "</StartDateTime>"
  426. << std::endl
  427. << "\t<StartTime>" << coverage_start_time_time << "</StartTime>"
  428. << std::endl;
  429. int logFileCount = 0;
  430. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  431. {
  432. return -1;
  433. }
  434. cmCTestCoverageHandlerContainer::TotalCoverageMap::iterator fileIterator;
  435. int cnt = 0;
  436. long total_tested = 0;
  437. long total_untested = 0;
  438. //std::string fullSourceDir = sourceDir + "/";
  439. //std::string fullBinaryDir = binaryDir + "/";
  440. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  441. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  442. " Accumulating results (each . represents one file):" << std::endl);
  443. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  444. std::vector<std::string> errorsWhileAccumulating;
  445. file_count = 0;
  446. for ( fileIterator = cont.TotalCoverage.begin();
  447. fileIterator != cont.TotalCoverage.end();
  448. ++fileIterator )
  449. {
  450. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  451. file_count ++;
  452. if ( file_count % 50 == 0 )
  453. {
  454. cmCTestLog(this->CTest, HANDLER_OUTPUT, " processed: " << file_count
  455. << " out of "
  456. << cont.TotalCoverage.size() << std::endl);
  457. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  458. }
  459. const std::string fullFileName = fileIterator->first;
  460. bool shouldIDoCoverage
  461. = this->ShouldIDoCoverage(fullFileName.c_str(),
  462. sourceDir.c_str(), binaryDir.c_str());
  463. if ( !shouldIDoCoverage )
  464. {
  465. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  466. ".NoDartCoverage found, so skip coverage check for: "
  467. << fullFileName
  468. << std::endl);
  469. continue;
  470. }
  471. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  472. "Process file: " << fullFileName << std::endl);
  473. if ( !cmSystemTools::FileExists(fullFileName.c_str()) )
  474. {
  475. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find file: "
  476. << fullFileName << std::endl);
  477. continue;
  478. }
  479. if ( ++cnt % 100 == 0 )
  480. {
  481. this->EndCoverageLogFile(covLogFile, logFileCount);
  482. logFileCount ++;
  483. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  484. {
  485. return -1;
  486. }
  487. }
  488. const std::string fileName
  489. = cmSystemTools::GetFilenameName(fullFileName);
  490. std::string shortFileName =
  491. this->CTest->GetShortPathToFile(fullFileName.c_str());
  492. const cmCTestCoverageHandlerContainer::SingleFileCoverageVector& fcov
  493. = fileIterator->second;
  494. covLogFile << "\t<File Name=\"" << cmXMLSafe(fileName)
  495. << "\" FullPath=\"" << cmXMLSafe(shortFileName) << "\">\n"
  496. << "\t\t<Report>" << std::endl;
  497. cmsys::ifstream ifs(fullFileName.c_str());
  498. if ( !ifs)
  499. {
  500. cmOStringStream ostr;
  501. ostr << "Cannot open source file: " << fullFileName;
  502. errorsWhileAccumulating.push_back(ostr.str());
  503. error ++;
  504. continue;
  505. }
  506. int tested = 0;
  507. int untested = 0;
  508. cmCTestCoverageHandlerContainer::SingleFileCoverageVector::size_type cc;
  509. std::string line;
  510. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  511. "Actually performing coverage for: " << fullFileName << std::endl);
  512. for ( cc= 0; cc < fcov.size(); cc ++ )
  513. {
  514. if ( !cmSystemTools::GetLineFromStream(ifs, line) &&
  515. cc != fcov.size() -1 )
  516. {
  517. cmOStringStream ostr;
  518. ostr << "Problem reading source file: " << fullFileName
  519. << " line:" << cc << " out total: " << fcov.size()-1;
  520. errorsWhileAccumulating.push_back(ostr.str());
  521. error ++;
  522. break;
  523. }
  524. covLogFile << "\t\t<Line Number=\"" << cc << "\" Count=\"" << fcov[cc]
  525. << "\">"
  526. << cmXMLSafe(line) << "</Line>" << std::endl;
  527. if ( fcov[cc] == 0 )
  528. {
  529. untested ++;
  530. }
  531. else if ( fcov[cc] > 0 )
  532. {
  533. tested ++;
  534. }
  535. }
  536. if ( cmSystemTools::GetLineFromStream(ifs, line) )
  537. {
  538. cmOStringStream ostr;
  539. ostr << "Looks like there are more lines in the file: " << line;
  540. errorsWhileAccumulating.push_back(ostr.str());
  541. }
  542. float cper = 0;
  543. float cmet = 0;
  544. if ( tested + untested > 0 )
  545. {
  546. cper = (100 * SAFEDIV(static_cast<float>(tested),
  547. static_cast<float>(tested + untested)));
  548. cmet = ( SAFEDIV(static_cast<float>(tested + 10),
  549. static_cast<float>(tested + untested + 10)));
  550. }
  551. total_tested += tested;
  552. total_untested += untested;
  553. covLogFile << "\t\t</Report>" << std::endl
  554. << "\t</File>" << std::endl;
  555. covSumFile << "\t<File Name=\"" << cmXMLSafe(fileName)
  556. << "\" FullPath=\"" << cmXMLSafe(
  557. this->CTest->GetShortPathToFile(fullFileName.c_str()))
  558. << "\" Covered=\"" << (tested+untested > 0 ? "true":"false") << "\">\n"
  559. << "\t\t<LOCTested>" << tested << "</LOCTested>\n"
  560. << "\t\t<LOCUnTested>" << untested << "</LOCUnTested>\n"
  561. << "\t\t<PercentCoverage>";
  562. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  563. covSumFile.precision(2);
  564. covSumFile << (cper) << "</PercentCoverage>\n"
  565. << "\t\t<CoverageMetric>";
  566. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  567. covSumFile.precision(2);
  568. covSumFile << (cmet) << "</CoverageMetric>\n";
  569. this->WriteXMLLabels(covSumFile, shortFileName);
  570. covSumFile << "\t</File>" << std::endl;
  571. }
  572. //Handle all the files in the extra coverage globs that have no cov data
  573. for(std::set<std::string>::iterator i = uncovered.begin();
  574. i != uncovered.end(); ++i)
  575. {
  576. std::string fileName = cmSystemTools::GetFilenameName(*i);
  577. std::string fullPath = cont.SourceDir + "/" + *i;
  578. covLogFile << "\t<File Name=\"" << cmXMLSafe(fileName)
  579. << "\" FullPath=\"" << cmXMLSafe(*i) << "\">\n"
  580. << "\t\t<Report>" << std::endl;
  581. cmsys::ifstream ifs(fullPath.c_str());
  582. if (!ifs)
  583. {
  584. cmOStringStream ostr;
  585. ostr << "Cannot open source file: " << fullPath;
  586. errorsWhileAccumulating.push_back(ostr.str());
  587. error ++;
  588. continue;
  589. }
  590. int untested = 0;
  591. std::string line;
  592. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  593. "Actually performing coverage for: " << *i << std::endl);
  594. while (cmSystemTools::GetLineFromStream(ifs, line))
  595. {
  596. covLogFile << "\t\t<Line Number=\"" << untested << "\" Count=\"0\">"
  597. << cmXMLSafe(line) << "</Line>" << std::endl;
  598. untested ++;
  599. }
  600. covLogFile << "\t\t</Report>\n\t</File>" << std::endl;
  601. total_untested += untested;
  602. covSumFile << "\t<File Name=\"" << cmXMLSafe(fileName)
  603. << "\" FullPath=\"" << cmXMLSafe(i->c_str())
  604. << "\" Covered=\"true\">\n"
  605. << "\t\t<LOCTested>0</LOCTested>\n"
  606. << "\t\t<LOCUnTested>" << untested << "</LOCUnTested>\n"
  607. << "\t\t<PercentCoverage>0</PercentCoverage>\n"
  608. << "\t\t<CoverageMetric>0</CoverageMetric>\n";
  609. this->WriteXMLLabels(covSumFile, *i);
  610. covSumFile << "\t</File>" << std::endl;
  611. }
  612. this->EndCoverageLogFile(covLogFile, logFileCount);
  613. if ( errorsWhileAccumulating.size() > 0 )
  614. {
  615. cmCTestLog(this->CTest, ERROR_MESSAGE, std::endl);
  616. cmCTestLog(this->CTest, ERROR_MESSAGE,
  617. "Error(s) while accumulating results:" << std::endl);
  618. std::vector<std::string>::iterator erIt;
  619. for ( erIt = errorsWhileAccumulating.begin();
  620. erIt != errorsWhileAccumulating.end();
  621. ++ erIt )
  622. {
  623. cmCTestLog(this->CTest, ERROR_MESSAGE,
  624. " " << *erIt << std::endl);
  625. }
  626. }
  627. long total_lines = total_tested + total_untested;
  628. float percent_coverage = 100 * SAFEDIV(static_cast<float>(total_tested),
  629. static_cast<float>(total_lines));
  630. if ( total_lines == 0 )
  631. {
  632. percent_coverage = 0;
  633. }
  634. std::string end_time = this->CTest->CurrentTime();
  635. covSumFile << "\t<LOCTested>" << total_tested << "</LOCTested>\n"
  636. << "\t<LOCUntested>" << total_untested << "</LOCUntested>\n"
  637. << "\t<LOC>" << total_lines << "</LOC>\n"
  638. << "\t<PercentCoverage>";
  639. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  640. covSumFile.precision(2);
  641. covSumFile << (percent_coverage)<< "</PercentCoverage>\n"
  642. << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  643. << "\t<EndTime>" <<
  644. static_cast<unsigned int>(cmSystemTools::GetTime())
  645. << "</EndTime>\n";
  646. covSumFile << "<ElapsedMinutes>" <<
  647. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  648. << "</ElapsedMinutes>"
  649. << "</Coverage>" << std::endl;
  650. this->CTest->EndXML(covSumFile);
  651. cmCTestLog(this->CTest, HANDLER_OUTPUT, "" << std::endl
  652. << "\tCovered LOC: "
  653. << total_tested << std::endl
  654. << "\tNot covered LOC: " << total_untested << std::endl
  655. << "\tTotal LOC: " << total_lines << std::endl
  656. << "\tPercentage Coverage: "
  657. << std::setiosflags(std::ios::fixed)
  658. << std::setprecision(2)
  659. << (percent_coverage) << "%" << std::endl);
  660. ofs << "\tCovered LOC: " << total_tested << std::endl
  661. << "\tNot covered LOC: " << total_untested << std::endl
  662. << "\tTotal LOC: " << total_lines << std::endl
  663. << "\tPercentage Coverage: "
  664. << std::setiosflags(std::ios::fixed)
  665. << std::setprecision(2)
  666. << (percent_coverage) << "%" << std::endl;
  667. if ( error )
  668. {
  669. return -1;
  670. }
  671. return 0;
  672. }
  673. //----------------------------------------------------------------------
  674. void cmCTestCoverageHandler::PopulateCustomVectors(cmMakefile *mf)
  675. {
  676. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  677. " Add coverage exclude regular expressions." << std::endl);
  678. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_COVERAGE_EXCLUDE",
  679. this->CustomCoverageExclude);
  680. this->CTest->PopulateCustomVector(mf, "CTEST_EXTRA_COVERAGE_GLOB",
  681. this->ExtraCoverageGlobs);
  682. std::vector<std::string>::iterator it;
  683. for ( it = this->CustomCoverageExclude.begin();
  684. it != this->CustomCoverageExclude.end();
  685. ++ it )
  686. {
  687. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " Add coverage exclude: "
  688. << *it << std::endl);
  689. }
  690. for ( it = this->ExtraCoverageGlobs.begin();
  691. it != this->ExtraCoverageGlobs.end(); ++it)
  692. {
  693. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " Add coverage glob: "
  694. << *it << std::endl);
  695. }
  696. }
  697. //----------------------------------------------------------------------
  698. // Fix for issue #4971 where the case of the drive letter component of
  699. // the filenames might be different when analyzing gcov output.
  700. //
  701. // Compare file names: fnc(fn1) == fnc(fn2) // fnc == file name compare
  702. //
  703. #ifdef _WIN32
  704. #define fnc(s) cmSystemTools::LowerCase(s)
  705. #else
  706. #define fnc(s) s
  707. #endif
  708. //----------------------------------------------------------------------
  709. bool IsFileInDir(const std::string &infile, const std::string &indir)
  710. {
  711. std::string file = cmSystemTools::CollapseFullPath(infile);
  712. std::string dir = cmSystemTools::CollapseFullPath(indir);
  713. if (
  714. file.size() > dir.size() &&
  715. (fnc(file.substr(0, dir.size())) == fnc(dir)) &&
  716. file[dir.size()] == '/'
  717. )
  718. {
  719. return true;
  720. }
  721. return false;
  722. }
  723. //----------------------------------------------------------------------
  724. int cmCTestCoverageHandler::HandlePHPCoverage(
  725. cmCTestCoverageHandlerContainer* cont)
  726. {
  727. cmParsePHPCoverage cov(*cont, this->CTest);
  728. std::string coverageDir = this->CTest->GetBinaryDir() + "/xdebugCoverage";
  729. if(cmSystemTools::FileIsDirectory(coverageDir))
  730. {
  731. cov.ReadPHPCoverageDirectory(coverageDir.c_str());
  732. }
  733. return static_cast<int>(cont->TotalCoverage.size());
  734. }
  735. //----------------------------------------------------------------------
  736. int cmCTestCoverageHandler::HandleCoberturaCoverage(
  737. cmCTestCoverageHandlerContainer* cont)
  738. {
  739. cmParseCoberturaCoverage cov(*cont, this->CTest);
  740. // Assume the coverage.xml is in the source directory
  741. std::string coverageXMLFile = this->CTest->GetBinaryDir() + "/coverage.xml";
  742. if(cmSystemTools::FileExists(coverageXMLFile.c_str()))
  743. {
  744. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  745. "Parsing Cobertura XML file: " << coverageXMLFile
  746. << std::endl);
  747. cov.ReadCoverageXML(coverageXMLFile.c_str());
  748. }
  749. else
  750. {
  751. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  752. "Cannot find Cobertura XML file: " << coverageXMLFile
  753. << std::endl);
  754. }
  755. return static_cast<int>(cont->TotalCoverage.size());
  756. }
  757. //----------------------------------------------------------------------
  758. int cmCTestCoverageHandler::HandleMumpsCoverage(
  759. cmCTestCoverageHandlerContainer* cont)
  760. {
  761. // try gtm coverage
  762. cmParseGTMCoverage cov(*cont, this->CTest);
  763. std::string coverageFile = this->CTest->GetBinaryDir() +
  764. "/gtm_coverage.mcov";
  765. if(cmSystemTools::FileExists(coverageFile.c_str()))
  766. {
  767. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  768. "Parsing Cache Coverage: " << coverageFile
  769. << std::endl);
  770. cov.ReadCoverageFile(coverageFile.c_str());
  771. return static_cast<int>(cont->TotalCoverage.size());
  772. }
  773. else
  774. {
  775. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  776. " Cannot find foobar GTM coverage file: " << coverageFile
  777. << std::endl);
  778. }
  779. cmParseCacheCoverage ccov(*cont, this->CTest);
  780. coverageFile = this->CTest->GetBinaryDir() +
  781. "/cache_coverage.cmcov";
  782. if(cmSystemTools::FileExists(coverageFile.c_str()))
  783. {
  784. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  785. "Parsing Cache Coverage: " << coverageFile
  786. << std::endl);
  787. ccov.ReadCoverageFile(coverageFile.c_str());
  788. }
  789. else
  790. {
  791. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  792. " Cannot find Cache coverage file: " << coverageFile
  793. << std::endl);
  794. }
  795. return static_cast<int>(cont->TotalCoverage.size());
  796. }
  797. struct cmCTestCoverageHandlerLocale
  798. {
  799. cmCTestCoverageHandlerLocale()
  800. {
  801. if(const char* l = cmSystemTools::GetEnv("LC_ALL"))
  802. {
  803. lc_all = l;
  804. }
  805. if(lc_all != "C")
  806. {
  807. cmSystemTools::PutEnv("LC_ALL=C");
  808. }
  809. }
  810. ~cmCTestCoverageHandlerLocale()
  811. {
  812. if(!lc_all.empty())
  813. {
  814. cmSystemTools::PutEnv(("LC_ALL=" + lc_all).c_str());
  815. }
  816. else
  817. {
  818. cmSystemTools::UnsetEnv("LC_ALL");
  819. }
  820. }
  821. std::string lc_all;
  822. };
  823. //----------------------------------------------------------------------
  824. int cmCTestCoverageHandler::HandleJacocoCoverage(
  825. cmCTestCoverageHandlerContainer* cont)
  826. {
  827. cmParseJacocoCoverage cov =
  828. cmParseJacocoCoverage(*cont, this->CTest);
  829. cmsys::Glob g;
  830. std::vector<std::string> files;
  831. g.SetRecurse(true);
  832. std::string SourceDir
  833. = this->CTest->GetCTestConfiguration("SourceDirectory");
  834. std::string coverageFile = SourceDir+ "/*jacoco.xml";
  835. g.FindFiles(coverageFile);
  836. files=g.GetFiles();
  837. if (files.size() > 0)
  838. {
  839. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  840. "Found Jacoco Files, Performing Coverage" << std::endl);
  841. cov.LoadCoverageData(files);
  842. }
  843. else
  844. {
  845. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  846. " Cannot find Jacoco coverage files: " << coverageFile
  847. << std::endl);
  848. }
  849. return static_cast<int>(cont->TotalCoverage.size());
  850. }
  851. //----------------------------------------------------------------------
  852. int cmCTestCoverageHandler::HandleDelphiCoverage(
  853. cmCTestCoverageHandlerContainer* cont)
  854. {
  855. cmParseDelphiCoverage cov =
  856. cmParseDelphiCoverage(*cont, this->CTest);
  857. cmsys::Glob g;
  858. std::vector<std::string> files;
  859. g.SetRecurse(true);
  860. std::string BinDir
  861. = this->CTest->GetBinaryDir();
  862. std::string coverageFile = BinDir+ "/*.html";
  863. g.FindFiles(coverageFile);
  864. files=g.GetFiles();
  865. if (files.size() > 0)
  866. {
  867. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  868. "Found Delphi HTML Files, Performing Coverage" << std::endl);
  869. cov.LoadCoverageData(files);
  870. }
  871. else
  872. {
  873. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  874. " Cannot find Delphi coverage files: " << coverageFile
  875. << std::endl);
  876. }
  877. return static_cast<int>(cont->TotalCoverage.size());
  878. }
  879. //----------------------------------------------------------------------
  880. int cmCTestCoverageHandler::HandleBlanketJSCoverage(
  881. cmCTestCoverageHandlerContainer* cont)
  882. {
  883. cmParseBlanketJSCoverage cov =
  884. cmParseBlanketJSCoverage(*cont, this->CTest);
  885. std::string SourceDir
  886. = this->CTest->GetCTestConfiguration("SourceDirectory");
  887. //Look for something other than output.json, still JSON extension.
  888. std::string coverageFile = SourceDir+ "/*.json";
  889. cmsys::Glob g;
  890. std::vector<std::string> files;
  891. g.FindFiles(coverageFile);
  892. files=g.GetFiles();
  893. if (files.size() > 0)
  894. {
  895. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  896. "Found BlanketJS output JSON, Performing Coverage" << std::endl);
  897. cov.LoadCoverageData(files);
  898. }
  899. else
  900. {
  901. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  902. " Cannot find BlanketJS coverage files: " << coverageFile
  903. << std::endl);
  904. }
  905. return static_cast<int>(cont->TotalCoverage.size());
  906. }
  907. //----------------------------------------------------------------------
  908. int cmCTestCoverageHandler::HandleGCovCoverage(
  909. cmCTestCoverageHandlerContainer* cont)
  910. {
  911. std::string gcovCommand
  912. = this->CTest->GetCTestConfiguration("CoverageCommand");
  913. if (gcovCommand.empty())
  914. {
  915. cmCTestLog(this->CTest, ERROR_MESSAGE,
  916. "Could not find gcov." << std::endl);
  917. return 0;
  918. }
  919. std::string gcovExtraFlags
  920. = this->CTest->GetCTestConfiguration("CoverageExtraFlags");
  921. // Immediately skip to next coverage option since codecov is only for Intel
  922. // compiler
  923. if ( gcovCommand == "codecov" )
  924. {
  925. return 0;
  926. }
  927. // Style 1
  928. std::string st1gcovOutputRex1
  929. = "[0-9]+\\.[0-9]+% of [0-9]+ (source |)lines executed in file (.*)$";
  930. std::string st1gcovOutputRex2 = "^Creating (.*\\.gcov)\\.";
  931. cmsys::RegularExpression st1re1(st1gcovOutputRex1.c_str());
  932. cmsys::RegularExpression st1re2(st1gcovOutputRex2.c_str());
  933. // Style 2
  934. std::string st2gcovOutputRex1 = "^File *[`'](.*)'$";
  935. std::string st2gcovOutputRex2
  936. = "Lines executed: *[0-9]+\\.[0-9]+% of [0-9]+$";
  937. std::string st2gcovOutputRex3 = "^(.*)reating [`'](.*\\.gcov)'";
  938. std::string st2gcovOutputRex4 = "^(.*):unexpected EOF *$";
  939. std::string st2gcovOutputRex5 = "^(.*):cannot open source file*$";
  940. std::string st2gcovOutputRex6
  941. = "^(.*):source file is newer than graph file `(.*)'$";
  942. cmsys::RegularExpression st2re1(st2gcovOutputRex1.c_str());
  943. cmsys::RegularExpression st2re2(st2gcovOutputRex2.c_str());
  944. cmsys::RegularExpression st2re3(st2gcovOutputRex3.c_str());
  945. cmsys::RegularExpression st2re4(st2gcovOutputRex4.c_str());
  946. cmsys::RegularExpression st2re5(st2gcovOutputRex5.c_str());
  947. cmsys::RegularExpression st2re6(st2gcovOutputRex6.c_str());
  948. std::vector<std::string> files;
  949. this->FindGCovFiles(files);
  950. std::vector<std::string>::iterator it;
  951. if ( files.size() == 0 )
  952. {
  953. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  954. " Cannot find any GCov coverage files."
  955. << std::endl);
  956. // No coverage files is a valid thing, so the exit code is 0
  957. return 0;
  958. }
  959. std::string testingDir = this->CTest->GetBinaryDir() + "/Testing";
  960. std::string tempDir = testingDir + "/CoverageInfo";
  961. std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  962. cmSystemTools::MakeDirectory(tempDir.c_str());
  963. cmSystemTools::ChangeDirectory(tempDir);
  964. int gcovStyle = 0;
  965. std::set<std::string> missingFiles;
  966. std::string actualSourceFile = "";
  967. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  968. " Processing coverage (each . represents one file):" << std::endl);
  969. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  970. int file_count = 0;
  971. // make sure output from gcov is in English!
  972. cmCTestCoverageHandlerLocale locale_C;
  973. static_cast<void>(locale_C);
  974. // files is a list of *.da and *.gcda files with coverage data in them.
  975. // These are binary files that you give as input to gcov so that it will
  976. // give us text output we can analyze to summarize coverage.
  977. //
  978. for ( it = files.begin(); it != files.end(); ++ it )
  979. {
  980. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  981. // Call gcov to get coverage data for this *.gcda file:
  982. //
  983. std::string fileDir = cmSystemTools::GetFilenamePath(*it);
  984. std::string command = "\"" + gcovCommand + "\" " +
  985. gcovExtraFlags + " " +
  986. "-o \"" + fileDir + "\" " +
  987. "\"" + *it + "\"";
  988. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, command.c_str()
  989. << std::endl);
  990. std::string output = "";
  991. std::string errors = "";
  992. int retVal = 0;
  993. *cont->OFS << "* Run coverage for: " << fileDir << std::endl;
  994. *cont->OFS << " Command: " << command << std::endl;
  995. int res = this->CTest->RunCommand(command.c_str(), &output, &errors,
  996. &retVal, tempDir.c_str(), 0 /*this->TimeOut*/);
  997. *cont->OFS << " Output: " << output << std::endl;
  998. *cont->OFS << " Errors: " << errors << std::endl;
  999. if ( ! res )
  1000. {
  1001. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1002. "Problem running coverage on file: " << *it << std::endl);
  1003. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1004. "Command produced error: " << errors << std::endl);
  1005. cont->Error ++;
  1006. continue;
  1007. }
  1008. if ( retVal != 0 )
  1009. {
  1010. cmCTestLog(this->CTest, ERROR_MESSAGE, "Coverage command returned: "
  1011. << retVal << " while processing: " << *it << std::endl);
  1012. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1013. "Command produced error: " << cont->Error << std::endl);
  1014. }
  1015. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1016. "--------------------------------------------------------------"
  1017. << std::endl
  1018. << output << std::endl
  1019. << "--------------------------------------------------------------"
  1020. << std::endl);
  1021. std::vector<std::string> lines;
  1022. std::vector<std::string>::iterator line;
  1023. cmSystemTools::Split(output.c_str(), lines);
  1024. for ( line = lines.begin(); line != lines.end(); ++line)
  1025. {
  1026. std::string sourceFile;
  1027. std::string gcovFile;
  1028. cmCTestLog(this->CTest, DEBUG, "Line: [" << *line << "]"
  1029. << std::endl);
  1030. if ( line->size() == 0 )
  1031. {
  1032. // Ignore empty line; probably style 2
  1033. }
  1034. else if ( st1re1.find(line->c_str()) )
  1035. {
  1036. if ( gcovStyle == 0 )
  1037. {
  1038. gcovStyle = 1;
  1039. }
  1040. if ( gcovStyle != 1 )
  1041. {
  1042. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e1"
  1043. << std::endl);
  1044. cont->Error ++;
  1045. break;
  1046. }
  1047. actualSourceFile = "";
  1048. sourceFile = st1re1.match(2);
  1049. }
  1050. else if ( st1re2.find(line->c_str() ) )
  1051. {
  1052. if ( gcovStyle == 0 )
  1053. {
  1054. gcovStyle = 1;
  1055. }
  1056. if ( gcovStyle != 1 )
  1057. {
  1058. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e2"
  1059. << std::endl);
  1060. cont->Error ++;
  1061. break;
  1062. }
  1063. gcovFile = st1re2.match(1);
  1064. }
  1065. else if ( st2re1.find(line->c_str() ) )
  1066. {
  1067. if ( gcovStyle == 0 )
  1068. {
  1069. gcovStyle = 2;
  1070. }
  1071. if ( gcovStyle != 2 )
  1072. {
  1073. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e3"
  1074. << std::endl);
  1075. cont->Error ++;
  1076. break;
  1077. }
  1078. actualSourceFile = "";
  1079. sourceFile = st2re1.match(1);
  1080. }
  1081. else if ( st2re2.find(line->c_str() ) )
  1082. {
  1083. if ( gcovStyle == 0 )
  1084. {
  1085. gcovStyle = 2;
  1086. }
  1087. if ( gcovStyle != 2 )
  1088. {
  1089. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e4"
  1090. << std::endl);
  1091. cont->Error ++;
  1092. break;
  1093. }
  1094. }
  1095. else if ( st2re3.find(line->c_str() ) )
  1096. {
  1097. if ( gcovStyle == 0 )
  1098. {
  1099. gcovStyle = 2;
  1100. }
  1101. if ( gcovStyle != 2 )
  1102. {
  1103. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e5"
  1104. << std::endl);
  1105. cont->Error ++;
  1106. break;
  1107. }
  1108. gcovFile = st2re3.match(2);
  1109. }
  1110. else if ( st2re4.find(line->c_str() ) )
  1111. {
  1112. if ( gcovStyle == 0 )
  1113. {
  1114. gcovStyle = 2;
  1115. }
  1116. if ( gcovStyle != 2 )
  1117. {
  1118. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e6"
  1119. << std::endl);
  1120. cont->Error ++;
  1121. break;
  1122. }
  1123. cmCTestLog(this->CTest, WARNING, "Warning: " << st2re4.match(1)
  1124. << " had unexpected EOF" << std::endl);
  1125. }
  1126. else if ( st2re5.find(line->c_str() ) )
  1127. {
  1128. if ( gcovStyle == 0 )
  1129. {
  1130. gcovStyle = 2;
  1131. }
  1132. if ( gcovStyle != 2 )
  1133. {
  1134. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e7"
  1135. << std::endl);
  1136. cont->Error ++;
  1137. break;
  1138. }
  1139. cmCTestLog(this->CTest, WARNING, "Warning: Cannot open file: "
  1140. << st2re5.match(1) << std::endl);
  1141. }
  1142. else if ( st2re6.find(line->c_str() ) )
  1143. {
  1144. if ( gcovStyle == 0 )
  1145. {
  1146. gcovStyle = 2;
  1147. }
  1148. if ( gcovStyle != 2 )
  1149. {
  1150. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown gcov output style e8"
  1151. << std::endl);
  1152. cont->Error ++;
  1153. break;
  1154. }
  1155. cmCTestLog(this->CTest, WARNING, "Warning: File: " << st2re6.match(1)
  1156. << " is newer than " << st2re6.match(2) << std::endl);
  1157. }
  1158. else
  1159. {
  1160. // gcov 4.7 can have output lines saying "No executable lines" and
  1161. // "Removing 'filename.gcov'"... Don't log those as "errors."
  1162. if(*line != "No executable lines" &&
  1163. !cmSystemTools::StringStartsWith(line->c_str(), "Removing "))
  1164. {
  1165. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1166. "Unknown gcov output line: [" << *line << "]"
  1167. << std::endl);
  1168. cont->Error ++;
  1169. //abort();
  1170. }
  1171. }
  1172. // If the last line of gcov output gave us a valid value for gcovFile,
  1173. // and we have an actualSourceFile, then insert a (or add to existing)
  1174. // SingleFileCoverageVector for actualSourceFile:
  1175. //
  1176. if ( !gcovFile.empty() && !actualSourceFile.empty() )
  1177. {
  1178. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& vec
  1179. = cont->TotalCoverage[actualSourceFile];
  1180. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " in gcovFile: "
  1181. << gcovFile << std::endl);
  1182. cmsys::ifstream ifile(gcovFile.c_str());
  1183. if ( ! ifile )
  1184. {
  1185. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open file: "
  1186. << gcovFile << std::endl);
  1187. }
  1188. else
  1189. {
  1190. long cnt = -1;
  1191. std::string nl;
  1192. while ( cmSystemTools::GetLineFromStream(ifile, nl) )
  1193. {
  1194. cnt ++;
  1195. //TODO: Handle gcov 3.0 non-coverage lines
  1196. // Skip empty lines
  1197. if ( !nl.size() )
  1198. {
  1199. continue;
  1200. }
  1201. // Skip unused lines
  1202. if ( nl.size() < 12 )
  1203. {
  1204. continue;
  1205. }
  1206. // Read the coverage count from the beginning of the gcov output
  1207. // line
  1208. std::string prefix = nl.substr(0, 12);
  1209. int cov = atoi(prefix.c_str());
  1210. // Read the line number starting at the 10th character of the gcov
  1211. // output line
  1212. std::string lineNumber = nl.substr(10, 5);
  1213. int lineIdx = atoi(lineNumber.c_str())-1;
  1214. if ( lineIdx >= 0 )
  1215. {
  1216. while ( vec.size() <= static_cast<size_t>(lineIdx) )
  1217. {
  1218. vec.push_back(-1);
  1219. }
  1220. // Initially all entries are -1 (not used). If we get coverage
  1221. // information, increment it to 0 first.
  1222. if ( vec[lineIdx] < 0 )
  1223. {
  1224. if ( cov > 0 || prefix.find("#") != prefix.npos )
  1225. {
  1226. vec[lineIdx] = 0;
  1227. }
  1228. }
  1229. vec[lineIdx] += cov;
  1230. }
  1231. }
  1232. }
  1233. actualSourceFile = "";
  1234. }
  1235. if ( !sourceFile.empty() && actualSourceFile.empty() )
  1236. {
  1237. gcovFile = "";
  1238. // Is it in the source dir or the binary dir?
  1239. //
  1240. if ( IsFileInDir(sourceFile, cont->SourceDir) )
  1241. {
  1242. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " produced s: "
  1243. << sourceFile << std::endl);
  1244. *cont->OFS << " produced in source dir: " << sourceFile
  1245. << std::endl;
  1246. actualSourceFile
  1247. = cmSystemTools::CollapseFullPath(sourceFile);
  1248. }
  1249. else if ( IsFileInDir(sourceFile, cont->BinaryDir) )
  1250. {
  1251. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " produced b: "
  1252. << sourceFile << std::endl);
  1253. *cont->OFS << " produced in binary dir: " << sourceFile
  1254. << std::endl;
  1255. actualSourceFile
  1256. = cmSystemTools::CollapseFullPath(sourceFile);
  1257. }
  1258. if ( actualSourceFile.empty() )
  1259. {
  1260. if ( missingFiles.find(sourceFile) == missingFiles.end() )
  1261. {
  1262. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1263. "Something went wrong" << std::endl);
  1264. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1265. "Cannot find file: ["
  1266. << sourceFile << "]" << std::endl);
  1267. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1268. " in source dir: ["
  1269. << cont->SourceDir << "]"
  1270. << std::endl);
  1271. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1272. " or binary dir: ["
  1273. << cont->BinaryDir.size() << "]"
  1274. << std::endl);
  1275. *cont->OFS << " Something went wrong. Cannot find file: "
  1276. << sourceFile
  1277. << " in source dir: " << cont->SourceDir
  1278. << " or binary dir: " << cont->BinaryDir << std::endl;
  1279. missingFiles.insert(sourceFile);
  1280. }
  1281. }
  1282. }
  1283. }
  1284. file_count++;
  1285. if ( file_count % 50 == 0 )
  1286. {
  1287. cmCTestLog(this->CTest, HANDLER_OUTPUT, " processed: " << file_count
  1288. << " out of " << files.size() << std::endl);
  1289. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  1290. }
  1291. }
  1292. cmSystemTools::ChangeDirectory(currentDirectory);
  1293. return file_count;
  1294. }
  1295. //----------------------------------------------------------------------
  1296. int cmCTestCoverageHandler::HandleLCovCoverage(
  1297. cmCTestCoverageHandlerContainer* cont)
  1298. {
  1299. std::string lcovCommand
  1300. = this->CTest->GetCTestConfiguration("CoverageCommand");
  1301. std::string lcovExtraFlags
  1302. = this->CTest->GetCTestConfiguration("CoverageExtraFlags");
  1303. if ( lcovCommand != "codecov" )
  1304. {
  1305. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1306. " Not a valid Intel Coverage command."
  1307. << std::endl);
  1308. return 0;
  1309. }
  1310. // There is only percentage completed output from LCOV
  1311. std::string st2lcovOutputRex3 = "[0-9]+%";
  1312. cmsys::RegularExpression st2re3(st2lcovOutputRex3.c_str());
  1313. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1314. " This is coverage command: " << lcovCommand
  1315. << std::endl);
  1316. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1317. " These are coverage command flags: " << lcovExtraFlags
  1318. << std::endl);
  1319. std::vector<std::string> files;
  1320. this->FindLCovFiles(files);
  1321. std::vector<std::string>::iterator it;
  1322. if ( files.size() == 0 )
  1323. {
  1324. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1325. " Cannot find any LCov coverage files."
  1326. << std::endl);
  1327. // No coverage files is a valid thing, so the exit code is 0
  1328. return 0;
  1329. }
  1330. std::string testingDir = this->CTest->GetBinaryDir();
  1331. std::string tempDir = testingDir;
  1332. std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  1333. std::set<std::string> missingFiles;
  1334. std::string actualSourceFile = "";
  1335. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  1336. " Processing coverage (each . represents one file):" << std::endl);
  1337. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  1338. int file_count = 0;
  1339. // make sure output from lcov is in English!
  1340. cmCTestCoverageHandlerLocale locale_C;
  1341. static_cast<void>(locale_C);
  1342. // In intel compiler we have to call codecov only once in each executable
  1343. // directory. It collects all *.dyn files to generate .dpi file.
  1344. for ( it = files.begin(); it != files.end(); ++ it )
  1345. {
  1346. cmCTestLog(this->CTest, HANDLER_OUTPUT, "." << std::flush);
  1347. std::string fileDir = cmSystemTools::GetFilenamePath(*it);
  1348. cmSystemTools::ChangeDirectory(fileDir);
  1349. std::string command = "\"" + lcovCommand + "\" " +
  1350. lcovExtraFlags + " ";
  1351. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Current coverage dir: "
  1352. << fileDir.c_str() << std::endl);
  1353. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, command.c_str()
  1354. << std::endl);
  1355. std::string output = "";
  1356. std::string errors = "";
  1357. int retVal = 0;
  1358. *cont->OFS << "* Run coverage for: " << fileDir.c_str() << std::endl;
  1359. *cont->OFS << " Command: " << command.c_str() << std::endl;
  1360. int res = this->CTest->RunCommand(command.c_str(), &output, &errors,
  1361. &retVal, fileDir.c_str(), 0 /*this->TimeOut*/);
  1362. *cont->OFS << " Output: " << output.c_str() << std::endl;
  1363. *cont->OFS << " Errors: " << errors.c_str() << std::endl;
  1364. if ( ! res )
  1365. {
  1366. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1367. "Problem running coverage on file: " << it->c_str() << std::endl);
  1368. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1369. "Command produced error: " << errors << std::endl);
  1370. cont->Error ++;
  1371. continue;
  1372. }
  1373. if ( retVal != 0 )
  1374. {
  1375. cmCTestLog(this->CTest, ERROR_MESSAGE, "Coverage command returned: "
  1376. << retVal << " while processing: " << it->c_str() << std::endl);
  1377. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1378. "Command produced error: " << cont->Error << std::endl);
  1379. }
  1380. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1381. "--------------------------------------------------------------"
  1382. << std::endl
  1383. << output << std::endl
  1384. << "--------------------------------------------------------------"
  1385. << std::endl);
  1386. std::vector<std::string> lines;
  1387. std::vector<std::string>::iterator line;
  1388. cmSystemTools::Split(output.c_str(), lines);
  1389. for ( line = lines.begin(); line != lines.end(); ++line)
  1390. {
  1391. std::string sourceFile;
  1392. std::string lcovFile;
  1393. if ( line->size() == 0 )
  1394. {
  1395. // Ignore empty line
  1396. }
  1397. // Look for LCOV files in binary directory
  1398. // Intel Compiler creates a CodeCoverage dir for each subfolder and
  1399. // each subfolder has LCOV files
  1400. cmsys::Glob gl;
  1401. gl.RecurseOn();
  1402. gl.RecurseThroughSymlinksOff();
  1403. std::string dir;
  1404. std::vector<std::string> lcovFiles;
  1405. dir = this->CTest->GetBinaryDir();
  1406. std::string daGlob;
  1407. daGlob = dir;
  1408. daGlob += "/*.LCOV";
  1409. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1410. " looking for LCOV files in: " << daGlob << std::endl);
  1411. gl.FindFiles(daGlob);
  1412. // Keep a list of all LCOV files
  1413. lcovFiles.insert(lcovFiles.end(), gl.GetFiles().begin(),
  1414. gl.GetFiles().end());
  1415. for(std::vector<std::string>::iterator a = lcovFiles.begin();
  1416. a != lcovFiles.end(); ++a)
  1417. {
  1418. lcovFile = *a;
  1419. cmsys::ifstream srcead(lcovFile.c_str());
  1420. if ( ! srcead )
  1421. {
  1422. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open file: "
  1423. << lcovFile << std::endl);
  1424. }
  1425. std::string srcname;
  1426. int success = cmSystemTools::GetLineFromStream(srcead, srcname);
  1427. if ( !success )
  1428. {
  1429. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1430. "Error while parsing lcov file '" << lcovFile << "':"
  1431. << " No source file name found!" << std::endl);
  1432. return 0;
  1433. }
  1434. srcname = srcname.substr(18);
  1435. // We can directly read found LCOV files to determine the source
  1436. // files
  1437. sourceFile = srcname;
  1438. actualSourceFile = srcname;
  1439. for(std::vector<std::string>::iterator t = lcovFiles.begin();
  1440. t != lcovFiles.end(); ++t)
  1441. {
  1442. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Found LCOV File: "
  1443. << *t << std::endl);
  1444. }
  1445. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "SourceFile: "
  1446. << sourceFile << std::endl);
  1447. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "lCovFile: "
  1448. << lcovFile << std::endl);
  1449. // If we have some LCOV files to process
  1450. if ( !lcovFile.empty() && !actualSourceFile.empty() )
  1451. {
  1452. cmCTestCoverageHandlerContainer::SingleFileCoverageVector& vec
  1453. = cont->TotalCoverage[actualSourceFile];
  1454. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " in lcovFile: "
  1455. << lcovFile << std::endl);
  1456. cmsys::ifstream ifile(lcovFile.c_str());
  1457. if ( ! ifile )
  1458. {
  1459. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open file: "
  1460. << lcovFile << std::endl);
  1461. }
  1462. else
  1463. {
  1464. long cnt = -1;
  1465. std::string nl;
  1466. // Skip the first line
  1467. cmSystemTools::GetLineFromStream(ifile, nl);
  1468. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1469. "File is ready, start reading." << std::endl);
  1470. while ( cmSystemTools::GetLineFromStream(ifile, nl) )
  1471. {
  1472. cnt ++;
  1473. // Skip empty lines
  1474. if ( !nl.size() )
  1475. {
  1476. continue;
  1477. }
  1478. // Skip unused lines
  1479. if ( nl.size() < 12 )
  1480. {
  1481. continue;
  1482. }
  1483. // Read the coverage count from the beginning of the lcov
  1484. // output line
  1485. std::string prefix = nl.substr(0, 17);
  1486. int cov = atoi(prefix.c_str());
  1487. // Read the line number starting at the 17th character of the
  1488. // lcov output line
  1489. std::string lineNumber = nl.substr(17, 7);
  1490. int lineIdx = atoi(lineNumber.c_str())-1;
  1491. if ( lineIdx >= 0 )
  1492. {
  1493. while ( vec.size() <= static_cast<size_t>(lineIdx) )
  1494. {
  1495. vec.push_back(-1);
  1496. }
  1497. // Initially all entries are -1 (not used). If we get coverage
  1498. // information, increment it to 0 first.
  1499. if ( vec[lineIdx] < 0 )
  1500. {
  1501. if ( cov > 0 || prefix.find("#") != prefix.npos )
  1502. {
  1503. vec[lineIdx] = 0;
  1504. }
  1505. }
  1506. vec[lineIdx] += cov;
  1507. }
  1508. }
  1509. }
  1510. actualSourceFile = "";
  1511. }
  1512. }
  1513. }
  1514. file_count++;
  1515. if ( file_count % 50 == 0 )
  1516. {
  1517. cmCTestLog(this->CTest, HANDLER_OUTPUT, " processed: " << file_count
  1518. << " out of " << files.size() << std::endl);
  1519. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  1520. }
  1521. }
  1522. cmSystemTools::ChangeDirectory(currentDirectory);
  1523. return file_count;
  1524. }
  1525. //----------------------------------------------------------------------------
  1526. void cmCTestCoverageHandler::FindGCovFiles(std::vector<std::string>& files)
  1527. {
  1528. cmsys::Glob gl;
  1529. gl.RecurseOn();
  1530. gl.RecurseThroughSymlinksOff();
  1531. for(LabelMapType::const_iterator lmi = this->TargetDirs.begin();
  1532. lmi != this->TargetDirs.end(); ++lmi)
  1533. {
  1534. // Skip targets containing no interesting labels.
  1535. if(!this->IntersectsFilter(lmi->second))
  1536. {
  1537. continue;
  1538. }
  1539. // Coverage files appear next to their object files in the target
  1540. // support directory.
  1541. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1542. " globbing for coverage in: " << lmi->first << std::endl);
  1543. std::string daGlob = lmi->first;
  1544. daGlob += "/*.da";
  1545. gl.FindFiles(daGlob);
  1546. files.insert(files.end(), gl.GetFiles().begin(), gl.GetFiles().end());
  1547. daGlob = lmi->first;
  1548. daGlob += "/*.gcda";
  1549. gl.FindFiles(daGlob);
  1550. files.insert(files.end(), gl.GetFiles().begin(), gl.GetFiles().end());
  1551. }
  1552. }
  1553. //----------------------------------------------------------------------------
  1554. void cmCTestCoverageHandler::FindLCovFiles(std::vector<std::string>& files)
  1555. {
  1556. cmsys::Glob gl;
  1557. gl.RecurseOff(); // No need of recurse if -prof_dir${BUILD_DIR} flag is
  1558. // used while compiling.
  1559. gl.RecurseThroughSymlinksOff();
  1560. std::string prevBinaryDir;
  1561. cmSystemTools::ChangeDirectory(
  1562. this->CTest->GetCTestConfiguration("BuildDirectory"));
  1563. // Run profmerge to merge all *.dyn files into dpi files
  1564. cmSystemTools::RunSingleCommand("profmerge");
  1565. prevBinaryDir = cmSystemTools::GetCurrentWorkingDirectory().c_str();
  1566. // DPI file should appear in build directory
  1567. std::string daGlob;
  1568. daGlob = prevBinaryDir;
  1569. daGlob += "/*.dpi";
  1570. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1571. " looking for dpi files in: " << daGlob << std::endl);
  1572. gl.FindFiles(daGlob);
  1573. files.insert(files.end(), gl.GetFiles().begin(), gl.GetFiles().end());
  1574. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1575. "Now searching in: " << daGlob << std::endl);
  1576. }
  1577. //----------------------------------------------------------------------
  1578. int cmCTestCoverageHandler::HandleTracePyCoverage(
  1579. cmCTestCoverageHandlerContainer* cont)
  1580. {
  1581. cmsys::Glob gl;
  1582. gl.RecurseOn();
  1583. gl.RecurseThroughSymlinksOff();
  1584. std::string daGlob = cont->BinaryDir + "/*.cover";
  1585. gl.FindFiles(daGlob);
  1586. std::vector<std::string> files = gl.GetFiles();
  1587. if ( files.size() == 0 )
  1588. {
  1589. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1590. " Cannot find any Python Trace.py coverage files."
  1591. << std::endl);
  1592. // No coverage files is a valid thing, so the exit code is 0
  1593. return 0;
  1594. }
  1595. std::string testingDir = this->CTest->GetBinaryDir() + "/Testing";
  1596. std::string tempDir = testingDir + "/CoverageInfo";
  1597. std::string currentDirectory = cmSystemTools::GetCurrentWorkingDirectory();
  1598. cmSystemTools::MakeDirectory(tempDir.c_str());
  1599. cmSystemTools::ChangeDirectory(tempDir);
  1600. cmSystemTools::ChangeDirectory(currentDirectory);
  1601. std::vector<std::string>::iterator fileIt;
  1602. int file_count = 0;
  1603. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  1604. {
  1605. std::string fileName = this->FindFile(cont, *fileIt);
  1606. if ( fileName.empty() )
  1607. {
  1608. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1609. "Cannot find source Python file corresponding to: "
  1610. << *fileIt << std::endl);
  1611. continue;
  1612. }
  1613. std::string actualSourceFile
  1614. = cmSystemTools::CollapseFullPath(fileName);
  1615. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1616. " Check coverage for file: " << actualSourceFile
  1617. << std::endl);
  1618. cmCTestCoverageHandlerContainer::SingleFileCoverageVector* vec
  1619. = &cont->TotalCoverage[actualSourceFile];
  1620. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1621. " in file: " << *fileIt << std::endl);
  1622. cmsys::ifstream ifile(fileIt->c_str());
  1623. if ( ! ifile )
  1624. {
  1625. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot open file: "
  1626. << *fileIt << std::endl);
  1627. }
  1628. else
  1629. {
  1630. long cnt = -1;
  1631. std::string nl;
  1632. while ( cmSystemTools::GetLineFromStream(ifile, nl) )
  1633. {
  1634. cnt ++;
  1635. // Skip empty lines
  1636. if ( !nl.size() )
  1637. {
  1638. continue;
  1639. }
  1640. // Skip unused lines
  1641. if ( nl.size() < 12 )
  1642. {
  1643. continue;
  1644. }
  1645. // Read the coverage count from the beginning of the Trace.py output
  1646. // line
  1647. std::string prefix = nl.substr(0, 6);
  1648. if ( prefix[5] != ' ' && prefix[5] != ':' )
  1649. {
  1650. // This is a hack. We should really do something more elaborate
  1651. prefix = nl.substr(0, 7);
  1652. if ( prefix[6] != ' ' && prefix[6] != ':' )
  1653. {
  1654. prefix = nl.substr(0, 8);
  1655. if ( prefix[7] != ' ' && prefix[7] != ':' )
  1656. {
  1657. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1658. "Currently the limit is maximum coverage of 999999"
  1659. << std::endl);
  1660. }
  1661. }
  1662. }
  1663. int cov = atoi(prefix.c_str());
  1664. if ( prefix[prefix.size()-1] != ':' )
  1665. {
  1666. // This line does not have ':' so no coverage here. That said,
  1667. // Trace.py does not handle not covered lines versus comments etc.
  1668. // So, this will be set to 0.
  1669. cov = 0;
  1670. }
  1671. cmCTestLog(this->CTest, DEBUG, "Prefix: " << prefix
  1672. << " cov: " << cov
  1673. << std::endl);
  1674. // Read the line number starting at the 10th character of the gcov
  1675. // output line
  1676. long lineIdx = cnt;
  1677. if ( lineIdx >= 0 )
  1678. {
  1679. while ( vec->size() <=
  1680. static_cast<size_t>(lineIdx) )
  1681. {
  1682. vec->push_back(-1);
  1683. }
  1684. // Initially all entries are -1 (not used). If we get coverage
  1685. // information, increment it to 0 first.
  1686. if ( (*vec)[lineIdx] < 0 )
  1687. {
  1688. if ( cov >= 0 )
  1689. {
  1690. (*vec)[lineIdx] = 0;
  1691. }
  1692. }
  1693. (*vec)[lineIdx] += cov;
  1694. }
  1695. }
  1696. }
  1697. ++ file_count;
  1698. }
  1699. cmSystemTools::ChangeDirectory(currentDirectory);
  1700. return file_count;
  1701. }
  1702. //----------------------------------------------------------------------
  1703. std::string cmCTestCoverageHandler::FindFile(
  1704. cmCTestCoverageHandlerContainer* cont,
  1705. std::string fileName)
  1706. {
  1707. std::string fileNameNoE
  1708. = cmSystemTools::GetFilenameWithoutLastExtension(fileName);
  1709. // First check in source and binary directory
  1710. std::string fullName = cont->SourceDir + "/" + fileNameNoE + ".py";
  1711. if ( cmSystemTools::FileExists(fullName.c_str()) )
  1712. {
  1713. return fullName;
  1714. }
  1715. fullName = cont->BinaryDir + "/" + fileNameNoE + ".py";
  1716. if ( cmSystemTools::FileExists(fullName.c_str()) )
  1717. {
  1718. return fullName;
  1719. }
  1720. return "";
  1721. }
  1722. // This is a header put on each marked up source file
  1723. namespace
  1724. {
  1725. const char* bullseyeHelp[] =
  1726. {" Coverage produced by bullseye covbr tool: ",
  1727. " www.bullseye.com/help/ref_covbr.html",
  1728. " * An arrow --> indicates incomplete coverage.",
  1729. " * An X indicates a function that was invoked, a switch label that ",
  1730. " was exercised, a try-block that finished, or an exception handler ",
  1731. " that was invoked.",
  1732. " * A T or F indicates a boolean decision that evaluated true or false,",
  1733. " respectively.",
  1734. " * A t or f indicates a boolean condition within a decision if the ",
  1735. " condition evaluated true or false, respectively.",
  1736. " * A k indicates a constant decision or condition.",
  1737. " * The slash / means this probe is excluded from summary results. ",
  1738. 0};
  1739. }
  1740. //----------------------------------------------------------------------
  1741. int cmCTestCoverageHandler::RunBullseyeCoverageBranch(
  1742. cmCTestCoverageHandlerContainer* cont,
  1743. std::set<std::string>& coveredFileNames,
  1744. std::vector<std::string>& files,
  1745. std::vector<std::string>& filesFullPath)
  1746. {
  1747. if(files.size() != filesFullPath.size())
  1748. {
  1749. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1750. "Files and full path files not the same size?:\n");
  1751. return 0;
  1752. }
  1753. // create the output stream for the CoverageLog-N.xml file
  1754. cmGeneratedFileStream covLogFile;
  1755. int logFileCount = 0;
  1756. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  1757. {
  1758. return -1;
  1759. }
  1760. // for each file run covbr on that file to get the coverage
  1761. // information for that file
  1762. std::string outputFile;
  1763. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1764. "run covbr: "
  1765. << std::endl);
  1766. if(!this->RunBullseyeCommand(cont, "covbr", 0, outputFile))
  1767. {
  1768. cmCTestLog(this->CTest, ERROR_MESSAGE, "error running covbr for." << "\n");
  1769. return -1;
  1770. }
  1771. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1772. "covbr output in " << outputFile
  1773. << std::endl);
  1774. // open the output file
  1775. cmsys::ifstream fin(outputFile.c_str());
  1776. if(!fin)
  1777. {
  1778. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1779. "Cannot open coverage file: " <<
  1780. outputFile << std::endl);
  1781. return 0;
  1782. }
  1783. std::map<std::string, std::string> fileMap;
  1784. std::vector<std::string>::iterator fp = filesFullPath.begin();
  1785. for(std::vector<std::string>::iterator f = files.begin();
  1786. f != files.end(); ++f, ++fp)
  1787. {
  1788. fileMap[*f] = *fp;
  1789. }
  1790. int count =0; // keep count of the number of files
  1791. // Now parse each line from the bullseye cov log file
  1792. std::string lineIn;
  1793. bool valid = false; // are we in a valid output file
  1794. int line = 0; // line of the current file
  1795. std::string file;
  1796. while(cmSystemTools::GetLineFromStream(fin, lineIn))
  1797. {
  1798. bool startFile = false;
  1799. if(lineIn.size() > 1 && lineIn[lineIn.size()-1] == ':')
  1800. {
  1801. file = lineIn.substr(0, lineIn.size()-1);
  1802. if(coveredFileNames.find(file) != coveredFileNames.end())
  1803. {
  1804. startFile = true;
  1805. }
  1806. }
  1807. if(startFile)
  1808. {
  1809. // if we are in a valid file close it because a new one started
  1810. if(valid)
  1811. {
  1812. covLogFile << "\t\t</Report>" << std::endl
  1813. << "\t</File>" << std::endl;
  1814. }
  1815. // only allow 100 files in each log file
  1816. if ( count != 0 && count % 100 == 0 )
  1817. {
  1818. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1819. "start a new log file: "
  1820. << count
  1821. << std::endl);
  1822. this->EndCoverageLogFile(covLogFile, logFileCount);
  1823. logFileCount ++;
  1824. if ( !this->StartCoverageLogFile(covLogFile, logFileCount) )
  1825. {
  1826. return -1;
  1827. }
  1828. count++; // move on one
  1829. }
  1830. std::map<std::string, std::string>::iterator
  1831. i = fileMap.find(file);
  1832. // if the file should be covered write out the header for that file
  1833. if(i != fileMap.end())
  1834. {
  1835. // we have a new file so count it in the output
  1836. count++;
  1837. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1838. "Produce coverage for file: "
  1839. << file << " " << count
  1840. << std::endl);
  1841. // start the file output
  1842. covLogFile << "\t<File Name=\""
  1843. << cmXMLSafe(i->first)
  1844. << "\" FullPath=\"" << cmXMLSafe(
  1845. this->CTest->GetShortPathToFile(
  1846. i->second.c_str())) << "\">" << std::endl
  1847. << "\t\t<Report>" << std::endl;
  1848. // write the bullseye header
  1849. line =0;
  1850. for(int k =0; bullseyeHelp[k] != 0; ++k)
  1851. {
  1852. covLogFile << "\t\t<Line Number=\"" << line << "\" Count=\"-1\">"
  1853. << cmXMLSafe(bullseyeHelp[k])
  1854. << "</Line>" << std::endl;
  1855. line++;
  1856. }
  1857. valid = true; // we are in a valid file section
  1858. }
  1859. else
  1860. {
  1861. // this is not a file that we want coverage for
  1862. valid = false;
  1863. }
  1864. }
  1865. // we are not at a start file, and we are in a valid file output the line
  1866. else if(valid)
  1867. {
  1868. covLogFile << "\t\t<Line Number=\"" << line << "\" Count=\"-1\">"
  1869. << cmXMLSafe(lineIn)
  1870. << "</Line>" << std::endl;
  1871. line++;
  1872. }
  1873. }
  1874. // if we ran out of lines a valid file then close that file
  1875. if(valid)
  1876. {
  1877. covLogFile << "\t\t</Report>" << std::endl
  1878. << "\t</File>" << std::endl;
  1879. }
  1880. this->EndCoverageLogFile(covLogFile, logFileCount);
  1881. return 1;
  1882. }
  1883. //----------------------------------------------------------------------
  1884. int cmCTestCoverageHandler::RunBullseyeCommand(
  1885. cmCTestCoverageHandlerContainer* cont,
  1886. const char* cmd,
  1887. const char* arg,
  1888. std::string& outputFile)
  1889. {
  1890. std::string program = cmSystemTools::FindProgram(cmd);
  1891. if(program.size() == 0)
  1892. {
  1893. cmCTestLog(this->CTest, ERROR_MESSAGE, "Cannot find :" << cmd << "\n");
  1894. return 0;
  1895. }
  1896. if(arg)
  1897. {
  1898. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1899. "Run : " << program << " " << arg << "\n");
  1900. }
  1901. else
  1902. {
  1903. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  1904. "Run : " << program << "\n");
  1905. }
  1906. // create a process object and start it
  1907. cmCTestRunProcess runCoverageSrc;
  1908. runCoverageSrc.SetCommand(program.c_str());
  1909. runCoverageSrc.AddArgument(arg);
  1910. std::string stdoutFile = cont->BinaryDir + "/Testing/Temporary/";
  1911. stdoutFile += this->GetCTestInstance()->GetCurrentTag();
  1912. stdoutFile += "-";
  1913. stdoutFile += cmd;
  1914. std::string stderrFile = stdoutFile;
  1915. stdoutFile += ".stdout";
  1916. stderrFile += ".stderr";
  1917. runCoverageSrc.SetStdoutFile(stdoutFile.c_str());
  1918. runCoverageSrc.SetStderrFile(stderrFile.c_str());
  1919. if(!runCoverageSrc.StartProcess())
  1920. {
  1921. cmCTestLog(this->CTest, ERROR_MESSAGE, "Could not run : "
  1922. << program << " " << arg << "\n"
  1923. << "kwsys process state : "
  1924. << runCoverageSrc.GetProcessState());
  1925. return 0;
  1926. }
  1927. // since we set the output file names wait for it to end
  1928. runCoverageSrc.WaitForExit();
  1929. outputFile = stdoutFile;
  1930. return 1;
  1931. }
  1932. //----------------------------------------------------------------------
  1933. int cmCTestCoverageHandler::RunBullseyeSourceSummary(
  1934. cmCTestCoverageHandlerContainer* cont)
  1935. {
  1936. // Run the covsrc command and create a temp outputfile
  1937. std::string outputFile;
  1938. if(!this->RunBullseyeCommand(cont, "covsrc", "-c", outputFile))
  1939. {
  1940. cmCTestLog(this->CTest, ERROR_MESSAGE, "error running covsrc:\n");
  1941. return 0;
  1942. }
  1943. std::ostream& tmpLog = *cont->OFS;
  1944. // copen the Coverage.xml file in the Testing directory
  1945. cmGeneratedFileStream covSumFile;
  1946. if(!this->StartResultingXML(cmCTest::PartCoverage, "Coverage", covSumFile))
  1947. {
  1948. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1949. "Cannot open coverage summary file." << std::endl);
  1950. return 0;
  1951. }
  1952. this->CTest->StartXML(covSumFile, this->AppendXML);
  1953. double elapsed_time_start = cmSystemTools::GetTime();
  1954. std::string coverage_start_time = this->CTest->CurrentTime();
  1955. covSumFile << "<Coverage>" << std::endl
  1956. << "\t<StartDateTime>"
  1957. << coverage_start_time << "</StartDateTime>"
  1958. << std::endl
  1959. << "\t<StartTime>"
  1960. << static_cast<unsigned int>(cmSystemTools::GetTime())
  1961. << "</StartTime>"
  1962. << std::endl;
  1963. std::string stdline;
  1964. std::string errline;
  1965. // expected output:
  1966. // first line is:
  1967. // "Source","Function Coverage","out of","%","C/D Coverage","out of","%"
  1968. // after that data follows in that format
  1969. std::string sourceFile;
  1970. int functionsCalled = 0;
  1971. int totalFunctions = 0;
  1972. int percentFunction = 0;
  1973. int branchCovered = 0;
  1974. int totalBranches = 0;
  1975. int percentBranch = 0;
  1976. double total_tested = 0;
  1977. double total_untested = 0;
  1978. double total_functions = 0;
  1979. double percent_coverage =0;
  1980. double number_files = 0;
  1981. std::vector<std::string> coveredFiles;
  1982. std::vector<std::string> coveredFilesFullPath;
  1983. // Read and parse the summary output file
  1984. cmsys::ifstream fin(outputFile.c_str());
  1985. if(!fin)
  1986. {
  1987. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1988. "Cannot open coverage summary file: " <<
  1989. outputFile << std::endl);
  1990. return 0;
  1991. }
  1992. std::set<std::string> coveredFileNames;
  1993. while(cmSystemTools::GetLineFromStream(fin, stdline))
  1994. {
  1995. // if we have a line of output from stdout
  1996. if(stdline.size())
  1997. {
  1998. // parse the comma separated output
  1999. this->ParseBullsEyeCovsrcLine(stdline,
  2000. sourceFile,
  2001. functionsCalled,
  2002. totalFunctions,
  2003. percentFunction,
  2004. branchCovered,
  2005. totalBranches,
  2006. percentBranch);
  2007. // The first line is the header
  2008. if(sourceFile == "Source" || sourceFile == "Total")
  2009. {
  2010. continue;
  2011. }
  2012. std::string file = sourceFile;
  2013. coveredFileNames.insert(file);
  2014. if(!cmSystemTools::FileIsFullPath(sourceFile.c_str()))
  2015. {
  2016. // file will be relative to the binary dir
  2017. file = cont->BinaryDir;
  2018. file += "/";
  2019. file += sourceFile;
  2020. }
  2021. file = cmSystemTools::CollapseFullPath(file);
  2022. bool shouldIDoCoverage
  2023. = this->ShouldIDoCoverage(file.c_str(),
  2024. cont->SourceDir.c_str(),
  2025. cont->BinaryDir.c_str());
  2026. if ( !shouldIDoCoverage )
  2027. {
  2028. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  2029. ".NoDartCoverage found, so skip coverage check for: "
  2030. << file
  2031. << std::endl);
  2032. continue;
  2033. }
  2034. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  2035. "Doing coverage for: "
  2036. << file
  2037. << std::endl);
  2038. coveredFiles.push_back(sourceFile);
  2039. coveredFilesFullPath.push_back(file);
  2040. number_files++;
  2041. total_functions += totalFunctions;
  2042. total_tested += functionsCalled;
  2043. total_untested += (totalFunctions - functionsCalled);
  2044. std::string fileName = cmSystemTools::GetFilenameName(file);
  2045. std::string shortFileName =
  2046. this->CTest->GetShortPathToFile(file.c_str());
  2047. float cper = static_cast<float>(percentBranch + percentFunction);
  2048. if(totalBranches > 0)
  2049. {
  2050. cper /= 2.0f;
  2051. }
  2052. percent_coverage += cper;
  2053. float cmet = static_cast<float>(percentFunction + percentBranch);
  2054. if(totalBranches > 0)
  2055. {
  2056. cmet /= 2.0f;
  2057. }
  2058. cmet /= 100.0f;
  2059. tmpLog << stdline << "\n";
  2060. tmpLog << fileName << "\n";
  2061. tmpLog << "functionsCalled: " << functionsCalled/100 << "\n";
  2062. tmpLog << "totalFunctions: " << totalFunctions/100 << "\n";
  2063. tmpLog << "percentFunction: " << percentFunction << "\n";
  2064. tmpLog << "branchCovered: " << branchCovered << "\n";
  2065. tmpLog << "totalBranches: " << totalBranches << "\n";
  2066. tmpLog << "percentBranch: " << percentBranch << "\n";
  2067. tmpLog << "percentCoverage: " << percent_coverage << "\n";
  2068. tmpLog << "coverage metric: " << cmet << "\n";
  2069. covSumFile << "\t<File Name=\"" << cmXMLSafe(sourceFile)
  2070. << "\" FullPath=\"" << cmXMLSafe(shortFileName)
  2071. << "\" Covered=\"" << (cmet>0?"true":"false") << "\">\n"
  2072. << "\t\t<BranchesTested>"
  2073. << branchCovered
  2074. << "</BranchesTested>\n"
  2075. << "\t\t<BranchesUnTested>"
  2076. << totalBranches - branchCovered
  2077. << "</BranchesUnTested>\n"
  2078. << "\t\t<FunctionsTested>"
  2079. << functionsCalled
  2080. << "</FunctionsTested>\n"
  2081. << "\t\t<FunctionsUnTested>"
  2082. << totalFunctions - functionsCalled
  2083. << "</FunctionsUnTested>\n"
  2084. // Hack for conversion of function to loc assume a function
  2085. // has 100 lines of code
  2086. << "\t\t<LOCTested>" << functionsCalled *100
  2087. << "</LOCTested>\n"
  2088. << "\t\t<LOCUnTested>"
  2089. << (totalFunctions - functionsCalled)*100
  2090. << "</LOCUnTested>\n"
  2091. << "\t\t<PercentCoverage>";
  2092. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  2093. covSumFile.precision(2);
  2094. covSumFile << (cper) << "</PercentCoverage>\n"
  2095. << "\t\t<CoverageMetric>";
  2096. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  2097. covSumFile.precision(2);
  2098. covSumFile << (cmet) << "</CoverageMetric>\n";
  2099. this->WriteXMLLabels(covSumFile, shortFileName);
  2100. covSumFile << "\t</File>" << std::endl;
  2101. }
  2102. }
  2103. std::string end_time = this->CTest->CurrentTime();
  2104. covSumFile << "\t<LOCTested>" << total_tested << "</LOCTested>\n"
  2105. << "\t<LOCUntested>" << total_untested << "</LOCUntested>\n"
  2106. << "\t<LOC>" << total_functions << "</LOC>\n"
  2107. << "\t<PercentCoverage>";
  2108. covSumFile.setf(std::ios::fixed, std::ios::floatfield);
  2109. covSumFile.precision(2);
  2110. covSumFile
  2111. << SAFEDIV(percent_coverage,number_files)<< "</PercentCoverage>\n"
  2112. << "\t<EndDateTime>" << end_time << "</EndDateTime>\n"
  2113. << "\t<EndTime>" << static_cast<unsigned int>(cmSystemTools::GetTime())
  2114. << "</EndTime>\n";
  2115. covSumFile
  2116. << "<ElapsedMinutes>" <<
  2117. static_cast<int>((cmSystemTools::GetTime() - elapsed_time_start)/6)/10.0
  2118. << "</ElapsedMinutes>"
  2119. << "</Coverage>" << std::endl;
  2120. this->CTest->EndXML(covSumFile);
  2121. // Now create the coverage information for each file
  2122. return this->RunBullseyeCoverageBranch(cont,
  2123. coveredFileNames,
  2124. coveredFiles,
  2125. coveredFilesFullPath);
  2126. }
  2127. //----------------------------------------------------------------------
  2128. int cmCTestCoverageHandler::HandleBullseyeCoverage(
  2129. cmCTestCoverageHandlerContainer* cont)
  2130. {
  2131. const char* covfile = cmSystemTools::GetEnv("COVFILE");
  2132. if(!covfile || strlen(covfile) == 0)
  2133. {
  2134. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  2135. " COVFILE environment variable not found, not running "
  2136. " bullseye\n");
  2137. return 0;
  2138. }
  2139. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  2140. " run covsrc with COVFILE=["
  2141. << covfile
  2142. << "]" << std::endl);
  2143. if(!this->RunBullseyeSourceSummary(cont))
  2144. {
  2145. cmCTestLog(this->CTest, ERROR_MESSAGE,
  2146. "Error running bullseye summary.\n");
  2147. return 0;
  2148. }
  2149. cmCTestLog(this->CTest, DEBUG, "HandleBullseyeCoverage return 1 "
  2150. << std::endl);
  2151. return 1;
  2152. }
  2153. bool cmCTestCoverageHandler::GetNextInt(std::string const& inputLine,
  2154. std::string::size_type& pos,
  2155. int& value)
  2156. {
  2157. std::string::size_type start = pos;
  2158. pos = inputLine.find(',', start);
  2159. value = atoi(inputLine.substr(start, pos).c_str());
  2160. if(pos == inputLine.npos)
  2161. {
  2162. return true;
  2163. }
  2164. pos++;
  2165. return true;
  2166. }
  2167. bool cmCTestCoverageHandler::ParseBullsEyeCovsrcLine(
  2168. std::string const& inputLine,
  2169. std::string& sourceFile,
  2170. int& functionsCalled,
  2171. int& totalFunctions,
  2172. int& percentFunction,
  2173. int& branchCovered,
  2174. int& totalBranches,
  2175. int& percentBranch)
  2176. {
  2177. // find the first comma
  2178. std::string::size_type pos = inputLine.find(',');
  2179. if(pos == inputLine.npos)
  2180. {
  2181. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error parsing string : "
  2182. << inputLine << "\n");
  2183. return false;
  2184. }
  2185. // the source file has "" around it so extract out the file name
  2186. sourceFile = inputLine.substr(1,pos-2);
  2187. pos++;
  2188. if(!this->GetNextInt(inputLine, pos, functionsCalled))
  2189. {
  2190. return false;
  2191. }
  2192. if(!this->GetNextInt(inputLine, pos, totalFunctions))
  2193. {
  2194. return false;
  2195. }
  2196. if(!this->GetNextInt(inputLine, pos, percentFunction))
  2197. {
  2198. return false;
  2199. }
  2200. if(!this->GetNextInt(inputLine, pos, branchCovered))
  2201. {
  2202. return false;
  2203. }
  2204. if(!this->GetNextInt(inputLine, pos, totalBranches))
  2205. {
  2206. return false;
  2207. }
  2208. if(!this->GetNextInt(inputLine, pos, percentBranch))
  2209. {
  2210. return false;
  2211. }
  2212. // should be at the end now
  2213. if(pos != inputLine.npos)
  2214. {
  2215. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error parsing input : "
  2216. << inputLine << " last pos not npos = " << pos <<
  2217. "\n");
  2218. }
  2219. return true;
  2220. }
  2221. //----------------------------------------------------------------------
  2222. int cmCTestCoverageHandler::GetLabelId(std::string const& label)
  2223. {
  2224. LabelIdMapType::iterator i = this->LabelIdMap.find(label);
  2225. if(i == this->LabelIdMap.end())
  2226. {
  2227. int n = int(this->Labels.size());
  2228. this->Labels.push_back(label);
  2229. LabelIdMapType::value_type entry(label, n);
  2230. i = this->LabelIdMap.insert(entry).first;
  2231. }
  2232. return i->second;
  2233. }
  2234. //----------------------------------------------------------------------
  2235. void cmCTestCoverageHandler::LoadLabels()
  2236. {
  2237. std::string fileList = this->CTest->GetBinaryDir();
  2238. fileList += cmake::GetCMakeFilesDirectory();
  2239. fileList += "/TargetDirectories.txt";
  2240. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  2241. " target directory list [" << fileList << "]\n");
  2242. cmsys::ifstream finList(fileList.c_str());
  2243. std::string line;
  2244. while(cmSystemTools::GetLineFromStream(finList, line))
  2245. {
  2246. this->LoadLabels(line.c_str());
  2247. }
  2248. }
  2249. //----------------------------------------------------------------------
  2250. void cmCTestCoverageHandler::LoadLabels(const char* dir)
  2251. {
  2252. LabelSet& dirLabels = this->TargetDirs[dir];
  2253. std::string fname = dir;
  2254. fname += "/Labels.txt";
  2255. cmsys::ifstream fin(fname.c_str());
  2256. if(!fin)
  2257. {
  2258. return;
  2259. }
  2260. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  2261. " loading labels from [" << fname << "]\n");
  2262. bool inTarget = true;
  2263. std::string source;
  2264. std::string line;
  2265. std::vector<int> targetLabels;
  2266. while(cmSystemTools::GetLineFromStream(fin, line))
  2267. {
  2268. if(line.empty() || line[0] == '#')
  2269. {
  2270. // Ignore blank and comment lines.
  2271. continue;
  2272. }
  2273. else if(line[0] == ' ')
  2274. {
  2275. // Label lines appear indented by one space.
  2276. std::string label = line.substr(1);
  2277. int id = this->GetLabelId(label);
  2278. dirLabels.insert(id);
  2279. if(inTarget)
  2280. {
  2281. targetLabels.push_back(id);
  2282. }
  2283. else
  2284. {
  2285. this->SourceLabels[source].insert(id);
  2286. }
  2287. }
  2288. else
  2289. {
  2290. // Non-indented lines specify a source file name. The first one
  2291. // is the end of the target-wide labels.
  2292. inTarget = false;
  2293. source = this->CTest->GetShortPathToFile(line.c_str());
  2294. // Label the source with the target labels.
  2295. LabelSet& labelSet = this->SourceLabels[source];
  2296. for(std::vector<int>::const_iterator li = targetLabels.begin();
  2297. li != targetLabels.end(); ++li)
  2298. {
  2299. labelSet.insert(*li);
  2300. }
  2301. }
  2302. }
  2303. }
  2304. //----------------------------------------------------------------------
  2305. void cmCTestCoverageHandler::WriteXMLLabels(std::ostream& os,
  2306. std::string const& source)
  2307. {
  2308. LabelMapType::const_iterator li = this->SourceLabels.find(source);
  2309. if(li != this->SourceLabels.end() && !li->second.empty())
  2310. {
  2311. os << "\t\t<Labels>\n";
  2312. for(LabelSet::const_iterator lsi = li->second.begin();
  2313. lsi != li->second.end(); ++lsi)
  2314. {
  2315. os << "\t\t\t<Label>" << cmXMLSafe(this->Labels[*lsi]) << "</Label>\n";
  2316. }
  2317. os << "\t\t</Labels>\n";
  2318. }
  2319. }
  2320. //----------------------------------------------------------------------------
  2321. void
  2322. cmCTestCoverageHandler::SetLabelFilter(std::set<std::string> const& labels)
  2323. {
  2324. this->LabelFilter.clear();
  2325. for(std::set<std::string>::const_iterator li = labels.begin();
  2326. li != labels.end(); ++li)
  2327. {
  2328. this->LabelFilter.insert(this->GetLabelId(*li));
  2329. }
  2330. }
  2331. //----------------------------------------------------------------------
  2332. bool cmCTestCoverageHandler::IntersectsFilter(LabelSet const& labels)
  2333. {
  2334. // If there is no label filter then nothing is filtered out.
  2335. if(this->LabelFilter.empty())
  2336. {
  2337. return true;
  2338. }
  2339. std::vector<int> ids;
  2340. cmsys_stl::set_intersection
  2341. (labels.begin(), labels.end(),
  2342. this->LabelFilter.begin(), this->LabelFilter.end(),
  2343. cmsys_stl::back_inserter(ids));
  2344. return !ids.empty();
  2345. }
  2346. //----------------------------------------------------------------------
  2347. bool cmCTestCoverageHandler::IsFilteredOut(std::string const& source)
  2348. {
  2349. // If there is no label filter then nothing is filtered out.
  2350. if(this->LabelFilter.empty())
  2351. {
  2352. return false;
  2353. }
  2354. // The source is filtered out if it does not have any labels in
  2355. // common with the filter set.
  2356. std::string shortSrc = this->CTest->GetShortPathToFile(source.c_str());
  2357. LabelMapType::const_iterator li = this->SourceLabels.find(shortSrc);
  2358. if(li != this->SourceLabels.end())
  2359. {
  2360. return !this->IntersectsFilter(li->second);
  2361. }
  2362. return true;
  2363. }
  2364. //----------------------------------------------------------------------
  2365. std::set<std::string> cmCTestCoverageHandler::FindUncoveredFiles(
  2366. cmCTestCoverageHandlerContainer* cont)
  2367. {
  2368. std::set<std::string> extraMatches;
  2369. for(std::vector<std::string>::iterator i = this->ExtraCoverageGlobs.begin();
  2370. i != this->ExtraCoverageGlobs.end(); ++i)
  2371. {
  2372. cmsys::Glob gl;
  2373. gl.RecurseOn();
  2374. gl.RecurseThroughSymlinksOff();
  2375. std::string glob = cont->SourceDir + "/" + *i;
  2376. gl.FindFiles(glob);
  2377. std::vector<std::string> files = gl.GetFiles();
  2378. for(std::vector<std::string>::iterator f = files.begin();
  2379. f != files.end(); ++f)
  2380. {
  2381. if(this->ShouldIDoCoverage(f->c_str(),
  2382. cont->SourceDir.c_str(), cont->BinaryDir.c_str()))
  2383. {
  2384. extraMatches.insert(this->CTest->GetShortPathToFile(
  2385. f->c_str()));
  2386. }
  2387. }
  2388. }
  2389. if(extraMatches.size())
  2390. {
  2391. for(cmCTestCoverageHandlerContainer::TotalCoverageMap::iterator i =
  2392. cont->TotalCoverage.begin(); i != cont->TotalCoverage.end(); ++i)
  2393. {
  2394. std::string shortPath = this->CTest->GetShortPathToFile(
  2395. i->first.c_str());
  2396. extraMatches.erase(shortPath);
  2397. }
  2398. }
  2399. return extraMatches;
  2400. }