cmGraphVizWriter.cxx 17 KB

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