cmCTestCoverageHandler.cxx 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  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.empty())
  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.empty())
  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.empty())
  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.empty())
  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. std::ostringstream 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. std::ostringstream 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. std::ostringstream ostr;
  539. ostr << "Looks like there are more lines in the file: " << fullFileName;
  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. std::ostringstream 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.empty())
  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);
  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.empty())
  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.empty())
  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.empty())
  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.empty())
  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->empty())
  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.empty())
  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.empty())
  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 << 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 << std::endl;
  1359. *cont->OFS << " Command: " << command << 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 << std::endl;
  1363. *cont->OFS << " Errors: " << errors << std::endl;
  1364. if ( ! res )
  1365. {
  1366. cmCTestLog(this->CTest, ERROR_MESSAGE,
  1367. "Problem running coverage on file: " << *it << 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 << 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->empty())
  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.empty())
  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.empty())
  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.empty())
  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.empty())
  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.empty())
  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. labelSet.insert(targetLabels.begin(), targetLabels.end());
  2297. }
  2298. }
  2299. }
  2300. //----------------------------------------------------------------------
  2301. void cmCTestCoverageHandler::WriteXMLLabels(std::ostream& os,
  2302. std::string const& source)
  2303. {
  2304. LabelMapType::const_iterator li = this->SourceLabels.find(source);
  2305. if(li != this->SourceLabels.end() && !li->second.empty())
  2306. {
  2307. os << "\t\t<Labels>\n";
  2308. for(LabelSet::const_iterator lsi = li->second.begin();
  2309. lsi != li->second.end(); ++lsi)
  2310. {
  2311. os << "\t\t\t<Label>" << cmXMLSafe(this->Labels[*lsi]) << "</Label>\n";
  2312. }
  2313. os << "\t\t</Labels>\n";
  2314. }
  2315. }
  2316. //----------------------------------------------------------------------------
  2317. void
  2318. cmCTestCoverageHandler::SetLabelFilter(std::set<std::string> const& labels)
  2319. {
  2320. this->LabelFilter.clear();
  2321. for(std::set<std::string>::const_iterator li = labels.begin();
  2322. li != labels.end(); ++li)
  2323. {
  2324. this->LabelFilter.insert(this->GetLabelId(*li));
  2325. }
  2326. }
  2327. //----------------------------------------------------------------------
  2328. bool cmCTestCoverageHandler::IntersectsFilter(LabelSet const& labels)
  2329. {
  2330. // If there is no label filter then nothing is filtered out.
  2331. if(this->LabelFilter.empty())
  2332. {
  2333. return true;
  2334. }
  2335. std::vector<int> ids;
  2336. std::set_intersection
  2337. (labels.begin(), labels.end(),
  2338. this->LabelFilter.begin(), this->LabelFilter.end(),
  2339. std::back_inserter(ids));
  2340. return !ids.empty();
  2341. }
  2342. //----------------------------------------------------------------------
  2343. bool cmCTestCoverageHandler::IsFilteredOut(std::string const& source)
  2344. {
  2345. // If there is no label filter then nothing is filtered out.
  2346. if(this->LabelFilter.empty())
  2347. {
  2348. return false;
  2349. }
  2350. // The source is filtered out if it does not have any labels in
  2351. // common with the filter set.
  2352. std::string shortSrc = this->CTest->GetShortPathToFile(source.c_str());
  2353. LabelMapType::const_iterator li = this->SourceLabels.find(shortSrc);
  2354. if(li != this->SourceLabels.end())
  2355. {
  2356. return !this->IntersectsFilter(li->second);
  2357. }
  2358. return true;
  2359. }
  2360. //----------------------------------------------------------------------
  2361. std::set<std::string> cmCTestCoverageHandler::FindUncoveredFiles(
  2362. cmCTestCoverageHandlerContainer* cont)
  2363. {
  2364. std::set<std::string> extraMatches;
  2365. for(std::vector<std::string>::iterator i = this->ExtraCoverageGlobs.begin();
  2366. i != this->ExtraCoverageGlobs.end(); ++i)
  2367. {
  2368. cmsys::Glob gl;
  2369. gl.RecurseOn();
  2370. gl.RecurseThroughSymlinksOff();
  2371. std::string glob = cont->SourceDir + "/" + *i;
  2372. gl.FindFiles(glob);
  2373. std::vector<std::string> files = gl.GetFiles();
  2374. for(std::vector<std::string>::iterator f = files.begin();
  2375. f != files.end(); ++f)
  2376. {
  2377. if(this->ShouldIDoCoverage(f->c_str(),
  2378. cont->SourceDir.c_str(), cont->BinaryDir.c_str()))
  2379. {
  2380. extraMatches.insert(this->CTest->GetShortPathToFile(
  2381. f->c_str()));
  2382. }
  2383. }
  2384. }
  2385. if(!extraMatches.empty())
  2386. {
  2387. for(cmCTestCoverageHandlerContainer::TotalCoverageMap::iterator i =
  2388. cont->TotalCoverage.begin(); i != cont->TotalCoverage.end(); ++i)
  2389. {
  2390. std::string shortPath = this->CTest->GetShortPathToFile(
  2391. i->first.c_str());
  2392. extraMatches.erase(shortPath);
  2393. }
  2394. }
  2395. return extraMatches;
  2396. }