cmGraphVizWriter.cxx 19 KB

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