cmGraphVizWriter.cxx 17 KB

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