cmGraphVizWriter.cxx 17 KB

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