cmGraphVizWriter.cxx 17 KB

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