cmCTestCoverageHandler.cxx 81 KB

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