cmGraphVizWriter.cxx 17 KB

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