cmCTestCoverageHandler.cxx 84 KB

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