cmGraphVizWriter.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 "cmGraphVizWriter.h"
  11. #include "cmMakefile.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. static const char* getShapeForTarget(const cmTarget* target)
  16. {
  17. if (!target)
  18. {
  19. return "ellipse";
  20. }
  21. switch ( target->GetType() )
  22. {
  23. case cmTarget::EXECUTABLE:
  24. return "house";
  25. case cmTarget::STATIC_LIBRARY:
  26. return "diamond";
  27. case cmTarget::SHARED_LIBRARY:
  28. return "polygon";
  29. case cmTarget::MODULE_LIBRARY:
  30. return "octagon";
  31. default:
  32. break;
  33. }
  34. return "box";
  35. }
  36. cmGraphVizWriter::cmGraphVizWriter(const std::vector<cmLocalGenerator*>&
  37. localGenerators)
  38. :GraphType("digraph")
  39. ,GraphName("GG")
  40. ,GraphHeader("node [\n fontsize = \"12\"\n];")
  41. ,GraphNodePrefix("node")
  42. ,LocalGenerators(localGenerators)
  43. ,GenerateForExecutables(true)
  44. ,GenerateForStaticLibs(true)
  45. ,GenerateForSharedLibs(true)
  46. ,GenerateForModuleLibs(true)
  47. ,GenerateForExternals(true)
  48. ,GeneratePerTarget(true)
  49. ,GenerateDependers(true)
  50. ,HaveTargetsAndLibs(false)
  51. {
  52. }
  53. void cmGraphVizWriter::ReadSettings(const char* settingsFileName,
  54. const char* fallbackSettingsFileName)
  55. {
  56. cmake cm;
  57. cm.SetHomeDirectory("");
  58. cm.SetHomeOutputDirectory("");
  59. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  60. cmGlobalGenerator ggi(&cm);
  61. cmsys::auto_ptr<cmMakefile> mf(
  62. new cmMakefile(&ggi, cm.GetCurrentSnapshot()));
  63. cmsys::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator(mf.get()));
  64. const char* inFileName = settingsFileName;
  65. if ( !cmSystemTools::FileExists(inFileName) )
  66. {
  67. inFileName = fallbackSettingsFileName;
  68. if ( !cmSystemTools::FileExists(inFileName) )
  69. {
  70. return;
  71. }
  72. }
  73. if ( !mf->ReadListFile(inFileName) )
  74. {
  75. cmSystemTools::Error("Problem opening GraphViz options file: ",
  76. inFileName);
  77. return;
  78. }
  79. std::cout << "Reading GraphViz options file: " << inFileName << std::endl;
  80. #define __set_if_set(var, cmakeDefinition) \
  81. { \
  82. const char* value = mf->GetDefinition(cmakeDefinition); \
  83. if ( value ) \
  84. { \
  85. var = value; \
  86. } \
  87. }
  88. __set_if_set(this->GraphType, "GRAPHVIZ_GRAPH_TYPE");
  89. __set_if_set(this->GraphName, "GRAPHVIZ_GRAPH_NAME");
  90. __set_if_set(this->GraphHeader, "GRAPHVIZ_GRAPH_HEADER");
  91. __set_if_set(this->GraphNodePrefix, "GRAPHVIZ_NODE_PREFIX");
  92. #define __set_bool_if_set(var, cmakeDefinition) \
  93. { \
  94. const char* value = mf->GetDefinition(cmakeDefinition); \
  95. if ( value ) \
  96. { \
  97. var = mf->IsOn(cmakeDefinition); \
  98. } \
  99. }
  100. __set_bool_if_set(this->GenerateForExecutables, "GRAPHVIZ_EXECUTABLES");
  101. __set_bool_if_set(this->GenerateForStaticLibs, "GRAPHVIZ_STATIC_LIBS");
  102. __set_bool_if_set(this->GenerateForSharedLibs, "GRAPHVIZ_SHARED_LIBS");
  103. __set_bool_if_set(this->GenerateForModuleLibs, "GRAPHVIZ_MODULE_LIBS");
  104. __set_bool_if_set(this->GenerateForExternals, "GRAPHVIZ_EXTERNAL_LIBS");
  105. __set_bool_if_set(this->GeneratePerTarget, "GRAPHVIZ_GENERATE_PER_TARGET");
  106. __set_bool_if_set(this->GenerateDependers, "GRAPHVIZ_GENERATE_DEPENDERS");
  107. std::string ignoreTargetsRegexes;
  108. __set_if_set(ignoreTargetsRegexes, "GRAPHVIZ_IGNORE_TARGETS");
  109. this->TargetsToIgnoreRegex.clear();
  110. if (!ignoreTargetsRegexes.empty())
  111. {
  112. std::vector<std::string> ignoreTargetsRegExVector;
  113. cmSystemTools::ExpandListArgument(ignoreTargetsRegexes,
  114. ignoreTargetsRegExVector);
  115. for(std::vector<std::string>::const_iterator itvIt
  116. = ignoreTargetsRegExVector.begin();
  117. itvIt != ignoreTargetsRegExVector.end();
  118. ++ itvIt )
  119. {
  120. std::string currentRegexString(*itvIt);
  121. cmsys::RegularExpression currentRegex;
  122. if (!currentRegex.compile(currentRegexString.c_str()))
  123. {
  124. std::cerr << "Could not compile bad regex \"" << currentRegexString
  125. << "\"" << std::endl;
  126. }
  127. this->TargetsToIgnoreRegex.push_back(currentRegex);
  128. }
  129. }
  130. }
  131. // Iterate over all targets and write for each one a graph which shows
  132. // which other targets depend on it.
  133. void cmGraphVizWriter::WriteTargetDependersFiles(const char* fileName)
  134. {
  135. if(this->GenerateDependers == false)
  136. {
  137. return;
  138. }
  139. this->CollectTargetsAndLibs();
  140. for(std::map<std::string, const cmTarget*>::const_iterator ptrIt =
  141. this->TargetPtrs.begin();
  142. ptrIt != this->TargetPtrs.end();
  143. ++ptrIt)
  144. {
  145. if (ptrIt->second == NULL)
  146. {
  147. continue;
  148. }
  149. if (this->GenerateForTargetType(ptrIt->second->GetType()) == false)
  150. {
  151. continue;
  152. }
  153. std::string currentFilename = fileName;
  154. currentFilename += ".";
  155. currentFilename += ptrIt->first;
  156. currentFilename += ".dependers";
  157. cmGeneratedFileStream str(currentFilename.c_str());
  158. if ( !str )
  159. {
  160. return;
  161. }
  162. std::set<std::string> insertedConnections;
  163. std::set<std::string> insertedNodes;
  164. std::cout << "Writing " << currentFilename << "..." << std::endl;
  165. this->WriteHeader(str);
  166. this->WriteDependerConnections(ptrIt->first,
  167. insertedNodes, insertedConnections, str);
  168. this->WriteFooter(str);
  169. }
  170. }
  171. // Iterate over all targets and write for each one a graph which shows
  172. // on which targets it depends.
  173. void cmGraphVizWriter::WritePerTargetFiles(const char* fileName)
  174. {
  175. if(this->GeneratePerTarget == false)
  176. {
  177. return;
  178. }
  179. this->CollectTargetsAndLibs();
  180. for(std::map<std::string, const cmTarget*>::const_iterator ptrIt =
  181. this->TargetPtrs.begin();
  182. ptrIt != this->TargetPtrs.end();
  183. ++ptrIt)
  184. {
  185. if (ptrIt->second == NULL)
  186. {
  187. continue;
  188. }
  189. if (this->GenerateForTargetType(ptrIt->second->GetType()) == false)
  190. {
  191. continue;
  192. }
  193. std::set<std::string> insertedConnections;
  194. std::set<std::string> insertedNodes;
  195. std::string currentFilename = fileName;
  196. currentFilename += ".";
  197. currentFilename += ptrIt->first;
  198. cmGeneratedFileStream str(currentFilename.c_str());
  199. if ( !str )
  200. {
  201. return;
  202. }
  203. std::cout << "Writing " << currentFilename << "..." << std::endl;
  204. this->WriteHeader(str);
  205. this->WriteConnections(ptrIt->first,
  206. insertedNodes, insertedConnections, str);
  207. this->WriteFooter(str);
  208. }
  209. }
  210. void cmGraphVizWriter::WriteGlobalFile(const char* fileName)
  211. {
  212. this->CollectTargetsAndLibs();
  213. cmGeneratedFileStream str(fileName);
  214. if ( !str )
  215. {
  216. return;
  217. }
  218. this->WriteHeader(str);
  219. std::cout << "Writing " << fileName << "..." << std::endl;
  220. std::set<std::string> insertedConnections;
  221. std::set<std::string> insertedNodes;
  222. for(std::map<std::string, const cmTarget*>::const_iterator ptrIt =
  223. this->TargetPtrs.begin();
  224. ptrIt != this->TargetPtrs.end();
  225. ++ptrIt)
  226. {
  227. if (ptrIt->second == NULL)
  228. {
  229. continue;
  230. }
  231. if (this->GenerateForTargetType(ptrIt->second->GetType()) == false)
  232. {
  233. continue;
  234. }
  235. this->WriteConnections(ptrIt->first,
  236. insertedNodes, insertedConnections, str);
  237. }
  238. this->WriteFooter(str);
  239. }
  240. void cmGraphVizWriter::WriteHeader(cmGeneratedFileStream& str) const
  241. {
  242. str << this->GraphType << " " << this->GraphName << " {" << std::endl;
  243. str << this->GraphHeader << std::endl;
  244. }
  245. void cmGraphVizWriter::WriteFooter(cmGeneratedFileStream& str) const
  246. {
  247. str << "}" << std::endl;
  248. }
  249. void cmGraphVizWriter::WriteConnections(const std::string& targetName,
  250. std::set<std::string>& insertedNodes,
  251. std::set<std::string>& insertedConnections,
  252. cmGeneratedFileStream& str) const
  253. {
  254. std::map<std::string, const cmTarget* >::const_iterator targetPtrIt =
  255. this->TargetPtrs.find(targetName);
  256. if (targetPtrIt == this->TargetPtrs.end()) // not found at all
  257. {
  258. return;
  259. }
  260. this->WriteNode(targetName, targetPtrIt->second, insertedNodes, str);
  261. if (targetPtrIt->second == NULL) // it's an external library
  262. {
  263. return;
  264. }
  265. std::string myNodeName = this->TargetNamesNodes.find(targetName)->second;
  266. const cmTarget::LinkLibraryVectorType* ll =
  267. &(targetPtrIt->second->GetOriginalLinkLibraries());
  268. for (cmTarget::LinkLibraryVectorType::const_iterator llit = ll->begin();
  269. llit != ll->end();
  270. ++ llit )
  271. {
  272. const char* libName = llit->first.c_str();
  273. std::map<std::string, std::string>::const_iterator libNameIt =
  274. this->TargetNamesNodes.find(libName);
  275. // can happen e.g. if GRAPHVIZ_TARGET_IGNORE_REGEX is used
  276. if(libNameIt == this->TargetNamesNodes.end())
  277. {
  278. continue;
  279. }
  280. std::string connectionName = myNodeName;
  281. connectionName += "-";
  282. connectionName += libNameIt->second;
  283. if (insertedConnections.find(connectionName) == insertedConnections.end())
  284. {
  285. insertedConnections.insert(connectionName);
  286. this->WriteNode(libName, this->TargetPtrs.find(libName)->second,
  287. insertedNodes, str);
  288. str << " \"" << myNodeName << "\" -> \""
  289. << libNameIt->second << "\"";
  290. str << " // " << targetName << " -> " << libName << std::endl;
  291. this->WriteConnections(libName, insertedNodes, insertedConnections, str);
  292. }
  293. }
  294. }
  295. void cmGraphVizWriter::WriteDependerConnections(const std::string& targetName,
  296. std::set<std::string>& insertedNodes,
  297. std::set<std::string>& insertedConnections,
  298. cmGeneratedFileStream& str) const
  299. {
  300. std::map<std::string, const cmTarget* >::const_iterator targetPtrIt =
  301. this->TargetPtrs.find(targetName);
  302. if (targetPtrIt == this->TargetPtrs.end()) // not found at all
  303. {
  304. return;
  305. }
  306. this->WriteNode(targetName, targetPtrIt->second, insertedNodes, str);
  307. if (targetPtrIt->second == NULL) // it's an external library
  308. {
  309. return;
  310. }
  311. std::string myNodeName = this->TargetNamesNodes.find(targetName)->second;
  312. // now search who links against me
  313. for(std::map<std::string, const cmTarget*>::const_iterator dependerIt =
  314. this->TargetPtrs.begin();
  315. dependerIt != this->TargetPtrs.end();
  316. ++dependerIt)
  317. {
  318. if (dependerIt->second == NULL)
  319. {
  320. continue;
  321. }
  322. if (this->GenerateForTargetType(dependerIt->second->GetType()) == false)
  323. {
  324. continue;
  325. }
  326. // Now we have a target, check whether it links against targetName.
  327. // If so, draw a connection, and then continue with dependers on that one.
  328. const cmTarget::LinkLibraryVectorType* ll =
  329. &(dependerIt->second->GetOriginalLinkLibraries());
  330. for (cmTarget::LinkLibraryVectorType::const_iterator llit = ll->begin();
  331. llit != ll->end();
  332. ++ llit )
  333. {
  334. std::string libName = llit->first;
  335. if (libName == targetName)
  336. {
  337. // So this target links against targetName.
  338. std::map<std::string, std::string>::const_iterator dependerNodeNameIt =
  339. this->TargetNamesNodes.find(dependerIt->first);
  340. if(dependerNodeNameIt != this->TargetNamesNodes.end())
  341. {
  342. std::string connectionName = dependerNodeNameIt->second;
  343. connectionName += "-";
  344. connectionName += myNodeName;
  345. if (insertedConnections.find(connectionName) ==
  346. insertedConnections.end())
  347. {
  348. insertedConnections.insert(connectionName);
  349. this->WriteNode(dependerIt->first, dependerIt->second,
  350. insertedNodes, str);
  351. str << " \"" << dependerNodeNameIt->second << "\" -> \""
  352. << myNodeName << "\"";
  353. str << " // " <<targetName<< " -> " <<dependerIt->first<<std::endl;
  354. this->WriteDependerConnections(dependerIt->first,
  355. insertedNodes, insertedConnections, str);
  356. }
  357. }
  358. break;
  359. }
  360. }
  361. }
  362. }
  363. void cmGraphVizWriter::WriteNode(const std::string& targetName,
  364. const cmTarget* target,
  365. std::set<std::string>& insertedNodes,
  366. cmGeneratedFileStream& str) const
  367. {
  368. if (insertedNodes.find(targetName) == insertedNodes.end())
  369. {
  370. insertedNodes.insert(targetName);
  371. std::map<std::string, std::string>::const_iterator nameIt =
  372. this->TargetNamesNodes.find(targetName);
  373. str << " \"" << nameIt->second << "\" [ label=\""
  374. << targetName << "\" shape=\"" << getShapeForTarget(target)
  375. << "\"];" << std::endl;
  376. }
  377. }
  378. void cmGraphVizWriter::CollectTargetsAndLibs()
  379. {
  380. if (this->HaveTargetsAndLibs == false)
  381. {
  382. this->HaveTargetsAndLibs = true;
  383. int cnt = this->CollectAllTargets();
  384. if (this->GenerateForExternals)
  385. {
  386. this->CollectAllExternalLibs(cnt);
  387. }
  388. }
  389. }
  390. int cmGraphVizWriter::CollectAllTargets()
  391. {
  392. int cnt = 0;
  393. // First pass get the list of all cmake targets
  394. for (std::vector<cmLocalGenerator*>::const_iterator lit =
  395. this->LocalGenerators.begin();
  396. lit != this->LocalGenerators.end();
  397. ++ lit )
  398. {
  399. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  400. for ( cmTargets::const_iterator tit = targets->begin();
  401. tit != targets->end();
  402. ++ tit )
  403. {
  404. const char* realTargetName = tit->first.c_str();
  405. if(this->IgnoreThisTarget(realTargetName))
  406. {
  407. // Skip ignored targets
  408. continue;
  409. }
  410. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  411. std::ostringstream ostr;
  412. ostr << this->GraphNodePrefix << cnt++;
  413. this->TargetNamesNodes[realTargetName] = ostr.str();
  414. this->TargetPtrs[realTargetName] = &tit->second;
  415. }
  416. }
  417. return cnt;
  418. }
  419. int cmGraphVizWriter::CollectAllExternalLibs(int cnt)
  420. {
  421. // Ok, now find all the stuff we link to that is not in cmake
  422. for (std::vector<cmLocalGenerator*>::const_iterator lit =
  423. this->LocalGenerators.begin();
  424. lit != this->LocalGenerators.end();
  425. ++ lit )
  426. {
  427. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  428. for ( cmTargets::const_iterator tit = targets->begin();
  429. tit != targets->end();
  430. ++ tit )
  431. {
  432. const char* realTargetName = tit->first.c_str();
  433. if (this->IgnoreThisTarget(realTargetName))
  434. {
  435. // Skip ignored targets
  436. continue;
  437. }
  438. const cmTarget::LinkLibraryVectorType* ll =
  439. &(tit->second.GetOriginalLinkLibraries());
  440. for (cmTarget::LinkLibraryVectorType::const_iterator llit = ll->begin();
  441. llit != ll->end();
  442. ++ llit )
  443. {
  444. const char* libName = llit->first.c_str();
  445. if (this->IgnoreThisTarget(libName))
  446. {
  447. // Skip ignored targets
  448. continue;
  449. }
  450. std::map<std::string, const cmTarget*>::const_iterator tarIt =
  451. this->TargetPtrs.find(libName);
  452. if ( tarIt == this->TargetPtrs.end() )
  453. {
  454. std::ostringstream ostr;
  455. ostr << this->GraphNodePrefix << cnt++;
  456. this->TargetNamesNodes[libName] = ostr.str();
  457. this->TargetPtrs[libName] = NULL;
  458. // str << " \"" << ostr.c_str() << "\" [ label=\"" << libName
  459. // << "\" shape=\"ellipse\"];" << std::endl;
  460. }
  461. }
  462. }
  463. }
  464. return cnt;
  465. }
  466. bool cmGraphVizWriter::IgnoreThisTarget(const std::string& name)
  467. {
  468. for(std::vector<cmsys::RegularExpression>::iterator itvIt
  469. = this->TargetsToIgnoreRegex.begin();
  470. itvIt != this->TargetsToIgnoreRegex.end();
  471. ++ itvIt )
  472. {
  473. cmsys::RegularExpression& regEx = *itvIt;
  474. if (regEx.is_valid())
  475. {
  476. if (regEx.find(name))
  477. {
  478. return true;
  479. }
  480. }
  481. }
  482. return false;
  483. }
  484. bool cmGraphVizWriter::GenerateForTargetType(cmTarget::TargetType targetType)
  485. const
  486. {
  487. switch (targetType)
  488. {
  489. case cmTarget::EXECUTABLE:
  490. return this->GenerateForExecutables;
  491. case cmTarget::STATIC_LIBRARY:
  492. return this->GenerateForStaticLibs;
  493. case cmTarget::SHARED_LIBRARY:
  494. return this->GenerateForSharedLibs;
  495. case cmTarget::MODULE_LIBRARY:
  496. return this->GenerateForModuleLibs;
  497. default:
  498. break;
  499. }
  500. return false;
  501. }