cmGraphVizWriter.cxx 17 KB

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