cmGraphVizWriter.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #include <memory>
  16. static const char* getShapeForTarget(const cmTarget* target)
  17. {
  18. if (!target)
  19. {
  20. return "ellipse";
  21. }
  22. switch ( target->GetType() )
  23. {
  24. case cmTarget::EXECUTABLE:
  25. return "house";
  26. case cmTarget::STATIC_LIBRARY:
  27. return "diamond";
  28. case cmTarget::SHARED_LIBRARY:
  29. return "polygon";
  30. case cmTarget::MODULE_LIBRARY:
  31. return "octagon";
  32. default:
  33. break;
  34. }
  35. return "box";
  36. }
  37. cmGraphVizWriter::cmGraphVizWriter(const std::vector<cmLocalGenerator*>&
  38. localGenerators)
  39. :GraphType("digraph")
  40. ,GraphName("GG")
  41. ,GraphHeader("node [\n fontsize = \"12\"\n];")
  42. ,GraphNodePrefix("node")
  43. ,GenerateForExecutables(true)
  44. ,GenerateForStaticLibs(true)
  45. ,GenerateForSharedLibs(true)
  46. ,GenerateForModuleLibs(true)
  47. ,GenerateForExternals(true)
  48. ,LocalGenerators(localGenerators)
  49. ,HaveTargetsAndLibs(false)
  50. {
  51. }
  52. void cmGraphVizWriter::ReadSettings(const char* settingsFileName,
  53. const char* fallbackSettingsFileName)
  54. {
  55. cmake cm;
  56. cmGlobalGenerator ggi;
  57. ggi.SetCMakeInstance(&cm);
  58. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  59. cmMakefile *mf = lg->GetMakefile();
  60. const char* inFileName = settingsFileName;
  61. if ( !cmSystemTools::FileExists(inFileName) )
  62. {
  63. inFileName = fallbackSettingsFileName;
  64. if ( !cmSystemTools::FileExists(inFileName) )
  65. {
  66. return;
  67. }
  68. }
  69. if ( !mf->ReadListFile(0, inFileName) )
  70. {
  71. cmSystemTools::Error("Problem opening GraphViz options file: ",
  72. inFileName);
  73. return;
  74. }
  75. std::cout << "Reading GraphViz options file: " << inFileName << std::endl;
  76. #define __set_if_set(var, cmakeDefinition) \
  77. { \
  78. const char* value = mf->GetDefinition(cmakeDefinition); \
  79. if ( value ) \
  80. { \
  81. var = value; \
  82. } \
  83. }
  84. __set_if_set(this->GraphType, "GRAPHVIZ_GRAPH_TYPE");
  85. __set_if_set(this->GraphName, "GRAPHVIZ_GRAPH_NAME");
  86. __set_if_set(this->GraphHeader, "GRAPHVIZ_GRAPH_HEADER");
  87. __set_if_set(this->GraphNodePrefix, "GRAPHVIZ_NODE_PREFIX");
  88. #define __set_bool_if_set(var, cmakeDefinition) \
  89. { \
  90. const char* value = mf->GetDefinition(cmakeDefinition); \
  91. if ( value ) \
  92. { \
  93. var = mf->IsOn(cmakeDefinition); \
  94. } \
  95. }
  96. __set_bool_if_set(this->GenerateForExecutables, "GRAPHVIZ_EXECUTABLES");
  97. __set_bool_if_set(this->GenerateForStaticLibs, "GRAPHVIZ_STATIC_LIBS");
  98. __set_bool_if_set(this->GenerateForSharedLibs, "GRAPHVIZ_SHARED_LIBS");
  99. __set_bool_if_set(this->GenerateForModuleLibs, "GRAPHVIZ_MODULE_LIBS");
  100. __set_bool_if_set(this->GenerateForExternals, "GRAPHVIZ_EXTERNAL_LIBS");
  101. cmStdString ignoreTargetsRegexes;
  102. __set_if_set(ignoreTargetsRegexes, "GRAPHVIZ_IGNORE_TARGETS");
  103. this->TargetsToIgnoreRegex.clear();
  104. if (ignoreTargetsRegexes.size() > 0)
  105. {
  106. std::vector<std::string> ignoreTargetsRegExVector;
  107. cmSystemTools::ExpandListArgument(ignoreTargetsRegexes,
  108. ignoreTargetsRegExVector);
  109. for(std::vector<std::string>::const_iterator itvIt
  110. = ignoreTargetsRegExVector.begin();
  111. itvIt != ignoreTargetsRegExVector.end();
  112. ++ itvIt )
  113. {
  114. cmStdString currentRegexString(*itvIt);
  115. cmsys::RegularExpression currentRegex;
  116. if (!currentRegex.compile(currentRegexString.c_str()))
  117. {
  118. std::cerr << "Could not compile bad regex \"" << currentRegexString
  119. << "\"" << std::endl;
  120. }
  121. this->TargetsToIgnoreRegex.push_back(currentRegex);
  122. }
  123. }
  124. }
  125. void cmGraphVizWriter::WritePerTargetFiles(const char* fileName)
  126. {
  127. this->CollectTargetsAndLibs();
  128. for(std::map<cmStdString, const cmTarget*>::const_iterator ptrIt =
  129. this->TargetPtrs.begin();
  130. ptrIt != this->TargetPtrs.end();
  131. ++ptrIt)
  132. {
  133. if (ptrIt->second == NULL)
  134. {
  135. continue;
  136. }
  137. if (this->GenerateForTargetType(ptrIt->second->GetType()) == false)
  138. {
  139. continue;
  140. }
  141. std::set<std::string> insertedConnections;
  142. std::set<std::string> insertedNodes;
  143. std::string currentFilename = fileName;
  144. currentFilename += ".";
  145. currentFilename += ptrIt->first;
  146. cmGeneratedFileStream str(currentFilename.c_str());
  147. if ( !str )
  148. {
  149. return;
  150. }
  151. std::cout << "Writing " << currentFilename << "..." << std::endl;
  152. this->WriteHeader(str);
  153. this->WriteConnections(ptrIt->first.c_str(),
  154. insertedNodes, insertedConnections, str);
  155. this->WriteFooter(str);
  156. }
  157. }
  158. void cmGraphVizWriter::WriteGlobalFile(const char* fileName)
  159. {
  160. this->CollectTargetsAndLibs();
  161. cmGeneratedFileStream str(fileName);
  162. if ( !str )
  163. {
  164. return;
  165. }
  166. this->WriteHeader(str);
  167. std::cout << "Writing " << fileName << "..." << std::endl;
  168. std::set<std::string> insertedConnections;
  169. std::set<std::string> insertedNodes;
  170. for(std::map<cmStdString, const cmTarget*>::const_iterator ptrIt =
  171. this->TargetPtrs.begin();
  172. ptrIt != this->TargetPtrs.end();
  173. ++ptrIt)
  174. {
  175. if (ptrIt->second == NULL)
  176. {
  177. continue;
  178. }
  179. if (this->GenerateForTargetType(ptrIt->second->GetType()) == false)
  180. {
  181. continue;
  182. }
  183. this->WriteConnections(ptrIt->first.c_str(),
  184. insertedNodes, insertedConnections, str);
  185. }
  186. this->WriteFooter(str);
  187. }
  188. void cmGraphVizWriter::WriteHeader(cmGeneratedFileStream& str) const
  189. {
  190. str << this->GraphType << " " << this->GraphName << " {" << std::endl;
  191. str << this->GraphHeader << std::endl;
  192. }
  193. void cmGraphVizWriter::WriteFooter(cmGeneratedFileStream& str) const
  194. {
  195. str << "}" << std::endl;
  196. }
  197. void cmGraphVizWriter::WriteConnections(const char* targetName,
  198. std::set<std::string>& insertedNodes,
  199. std::set<std::string>& insertedConnections,
  200. cmGeneratedFileStream& str) const
  201. {
  202. std::map<cmStdString, const cmTarget* >::const_iterator targetPtrIt =
  203. this->TargetPtrs.find(targetName);
  204. if (targetPtrIt == this->TargetPtrs.end()) // not found at all
  205. {
  206. return;
  207. }
  208. this->WriteNode(targetName, targetPtrIt->second, insertedNodes, str);
  209. if (targetPtrIt->second == NULL) // it's an external library
  210. {
  211. return;
  212. }
  213. std::string myNodeName = this->TargetNamesNodes.find(targetName)->second;
  214. const cmTarget::LinkLibraryVectorType* ll =
  215. &(targetPtrIt->second->GetOriginalLinkLibraries());
  216. for (cmTarget::LinkLibraryVectorType::const_iterator llit = ll->begin();
  217. llit != ll->end();
  218. ++ llit )
  219. {
  220. const char* libName = llit->first.c_str();
  221. std::map<cmStdString, cmStdString>::const_iterator libNameIt =
  222. this->TargetNamesNodes.find(libName);
  223. // can happen e.g. if GRAPHVIZ_TARGET_IGNORE_REGEX is used
  224. if(libNameIt == this->TargetNamesNodes.end())
  225. {
  226. continue;
  227. }
  228. std::string connectionName = myNodeName;
  229. connectionName += "-";
  230. connectionName += libNameIt->second;
  231. if (insertedConnections.find(connectionName) == insertedConnections.end())
  232. {
  233. insertedConnections.insert(connectionName);
  234. this->WriteNode(libName, this->TargetPtrs.find(libName)->second,
  235. insertedNodes, str);
  236. str << " \"" << myNodeName.c_str() << "\" -> \""
  237. << libNameIt->second.c_str() << "\"";
  238. str << " // " << targetName << " -> " << libName << std::endl;
  239. this->WriteConnections(libName, insertedNodes, insertedConnections, str);
  240. }
  241. }
  242. }
  243. void cmGraphVizWriter::WriteNode(const char* targetName,
  244. const cmTarget* target,
  245. std::set<std::string>& insertedNodes,
  246. cmGeneratedFileStream& str) const
  247. {
  248. if (insertedNodes.find(targetName) == insertedNodes.end())
  249. {
  250. insertedNodes.insert(targetName);
  251. std::map<cmStdString, cmStdString>::const_iterator nameIt =
  252. this->TargetNamesNodes.find(targetName);
  253. str << " \"" << nameIt->second.c_str() << "\" [ label=\""
  254. << targetName << "\" shape=\"" << getShapeForTarget(target)
  255. << "\"];" << std::endl;
  256. }
  257. }
  258. void cmGraphVizWriter::CollectTargetsAndLibs()
  259. {
  260. if (this->HaveTargetsAndLibs == false)
  261. {
  262. this->HaveTargetsAndLibs = true;
  263. int cnt = this->CollectAllTargets();
  264. if (this->GenerateForExternals)
  265. {
  266. this->CollectAllExternalLibs(cnt);
  267. }
  268. }
  269. }
  270. int cmGraphVizWriter::CollectAllTargets()
  271. {
  272. int cnt = 0;
  273. // First pass get the list of all cmake targets
  274. for (std::vector<cmLocalGenerator*>::const_iterator lit =
  275. this->LocalGenerators.begin();
  276. lit != this->LocalGenerators.end();
  277. ++ lit )
  278. {
  279. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  280. for ( cmTargets::const_iterator tit = targets->begin();
  281. tit != targets->end();
  282. ++ tit )
  283. {
  284. const char* realTargetName = tit->first.c_str();
  285. if(this->IgnoreThisTarget(realTargetName))
  286. {
  287. // Skip ignored targets
  288. continue;
  289. }
  290. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  291. cmOStringStream ostr;
  292. ostr << this->GraphNodePrefix << cnt++;
  293. this->TargetNamesNodes[realTargetName] = ostr.str();
  294. this->TargetPtrs[realTargetName] = &tit->second;
  295. }
  296. }
  297. return cnt;
  298. }
  299. int cmGraphVizWriter::CollectAllExternalLibs(int cnt)
  300. {
  301. // Ok, now find all the stuff we link to that is not in cmake
  302. for (std::vector<cmLocalGenerator*>::const_iterator lit =
  303. this->LocalGenerators.begin();
  304. lit != this->LocalGenerators.end();
  305. ++ lit )
  306. {
  307. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  308. for ( cmTargets::const_iterator tit = targets->begin();
  309. tit != targets->end();
  310. ++ tit )
  311. {
  312. const char* realTargetName = tit->first.c_str();
  313. if (this->IgnoreThisTarget(realTargetName))
  314. {
  315. // Skip ignored targets
  316. continue;
  317. }
  318. const cmTarget::LinkLibraryVectorType* ll =
  319. &(tit->second.GetOriginalLinkLibraries());
  320. for (cmTarget::LinkLibraryVectorType::const_iterator llit = ll->begin();
  321. llit != ll->end();
  322. ++ llit )
  323. {
  324. const char* libName = llit->first.c_str();
  325. if (this->IgnoreThisTarget(libName))
  326. {
  327. // Skip ignored targets
  328. continue;
  329. }
  330. std::map<cmStdString, const cmTarget*>::const_iterator tarIt =
  331. this->TargetPtrs.find(libName);
  332. if ( tarIt == this->TargetPtrs.end() )
  333. {
  334. cmOStringStream ostr;
  335. ostr << this->GraphNodePrefix << cnt++;
  336. this->TargetNamesNodes[libName] = ostr.str();
  337. this->TargetPtrs[libName] = NULL;
  338. //str << " \"" << ostr.c_str() << "\" [ label=\"" << libName
  339. //<< "\" shape=\"ellipse\"];" << std::endl;
  340. }
  341. }
  342. }
  343. }
  344. return cnt;
  345. }
  346. bool cmGraphVizWriter::IgnoreThisTarget(const char* name)
  347. {
  348. for(std::vector<cmsys::RegularExpression>::iterator itvIt
  349. = this->TargetsToIgnoreRegex.begin();
  350. itvIt != this->TargetsToIgnoreRegex.end();
  351. ++ itvIt )
  352. {
  353. cmsys::RegularExpression& regEx = *itvIt;
  354. if (regEx.is_valid())
  355. {
  356. if (regEx.find(name))
  357. {
  358. return true;
  359. }
  360. }
  361. }
  362. return false;
  363. }
  364. bool cmGraphVizWriter::GenerateForTargetType(cmTarget::TargetType targetType)
  365. const
  366. {
  367. switch (targetType)
  368. {
  369. case cmTarget::EXECUTABLE:
  370. return this->GenerateForExecutables;
  371. case cmTarget::STATIC_LIBRARY:
  372. return this->GenerateForStaticLibs;
  373. case cmTarget::SHARED_LIBRARY:
  374. return this->GenerateForSharedLibs;
  375. case cmTarget::MODULE_LIBRARY:
  376. return this->GenerateForModuleLibs;
  377. default:
  378. break;
  379. }
  380. return false;
  381. }