cmComputeLinkDepends.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmComputeLinkDepends.h"
  14. #include "cmComputeComponentGraph.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmTarget.h"
  19. #include <cmsys/stl/algorithm>
  20. #include <assert.h>
  21. /*
  22. This file computes an ordered list of link items to use when linking a
  23. single target in one configuration. Each link item is identified by
  24. the string naming it. A graph of dependencies is created in which
  25. each node corresponds to one item and directed eges lead from nodes to
  26. those which must *precede* them on the link line. For example, the
  27. graph
  28. C -> B -> A
  29. will lead to the link line order
  30. A B C
  31. The set of items placed in the graph is formed with a breadth-first
  32. search of the link dependencies starting from the main target.
  33. There are two types of items: those with known direct dependencies and
  34. those without known dependencies. We will call the two types "known
  35. items" and "unknown items", respecitvely. Known items are those whose
  36. names correspond to targets (built or imported) and those for which an
  37. old-style <item>_LIB_DEPENDS variable is defined. All other items are
  38. unknown and we must infer dependencies for them.
  39. Known items have dependency lists ordered based on how the user
  40. specified them. We can use this order to infer potential dependencies
  41. of unknown items. For example, if link items A and B are unknown and
  42. items X and Y are known, then we might have the following dependency
  43. lists:
  44. X: Y A B
  45. Y: A B
  46. The explicitly known dependencies form graph edges
  47. X <- Y , X <- A , X <- B , Y <- A , Y <- B
  48. We can also infer the edge
  49. A <- B
  50. because *every* time A appears B is seen on its right. We do not know
  51. whether A really needs symbols from B to link, but it *might* so we
  52. must preserve their order. This is the case also for the following
  53. explict lists:
  54. X: A B Y
  55. Y: A B
  56. Here, A is followed by the set {B,Y} in one list, and {B} in the other
  57. list. The intersection of these sets is {B}, so we can infer that A
  58. depends on at most B. Meanwhile B is followed by the set {Y} in one
  59. list and {} in the other. The intersection is {} so we can infer that
  60. B has no dependencies.
  61. Let's make a more complex example by adding unknown item C and
  62. considering these dependency lists:
  63. X: A B Y C
  64. Y: A C B
  65. The explicit edges are
  66. X <- Y , X <- A , X <- B , X <- C , Y <- A , Y <- B , Y <- C
  67. For the unknown items, we infer dependencies by looking at the
  68. "follow" sets:
  69. A: intersect( {B,Y,C} , {C,B} ) = {B,C} ; infer edges A <- B , A <- C
  70. B: intersect( {Y,C} , {} ) = {} ; infer no edges
  71. C: intersect( {} , {B} ) = {} ; infer no edges
  72. ------------------------------------------------------------------------------
  73. Once the complete graph is formed from all known and inferred
  74. dependencies we must use it to produce a valid link line. If the
  75. dependency graph were known to be acyclic a simple depth-first-search
  76. would produce a correct link line. Unfortunately we cannot make this
  77. assumption so the following technique is used.
  78. The original graph is converted to a directed acyclic graph in which
  79. each node corresponds to a strongly connected component of the
  80. original graph. For example, the dependency graph
  81. X <- A <- B <- C <- A <- Y
  82. contains strongly connected components {X}, {A,B,C}, and {Y}. The
  83. implied directed acyclic graph (DAG) is
  84. {X} <- {A,B,C} <- {Y}
  85. The final list of link items is constructed by a series of
  86. depth-first-searches through this DAG of components. When visiting a
  87. component all outgoing edges are followed first because the neighbors
  88. must precede it. Once neighbors across all edges have been emitted it
  89. is safe to emit the current component.
  90. Trivial components (those with one item) are handled simply by
  91. emitting the item. Non-trivial components (those with more than one
  92. item) are assumed to consist only of static libraries that may be
  93. safely repeated on the link line. We emit members of the component
  94. multiple times (see code below for details). The final link line for
  95. the example graph might be
  96. X A B C A B C Y
  97. ------------------------------------------------------------------------------
  98. The initial exploration of dependencies using a BFS associates an
  99. integer index with each link item. When the graph is built outgoing
  100. edges are sorted by this index.
  101. This preserves the original link
  102. order as much as possible subject to the dependencies.
  103. After the initial exploration of the link interface tree, any
  104. transitive (dependent) shared libraries that were encountered and not
  105. included in the interface are processed in their own BFS. This BFS
  106. follows only the dependent library lists and not the link interfaces.
  107. They are added to the link items with a mark indicating that the are
  108. transitive dependencies. Then cmComputeLinkInformation deals with
  109. them on a per-platform basis.
  110. */
  111. //----------------------------------------------------------------------------
  112. cmComputeLinkDepends
  113. ::cmComputeLinkDepends(cmTarget* target, const char* config)
  114. {
  115. // Store context information.
  116. this->Target = target;
  117. this->Makefile = this->Target->GetMakefile();
  118. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  119. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  120. // The configuration being linked.
  121. this->Config = config;
  122. // Enable debug mode if requested.
  123. this->DebugMode = this->Makefile->IsOn("CMAKE_LINK_DEPENDS_DEBUG_MODE");
  124. }
  125. //----------------------------------------------------------------------------
  126. cmComputeLinkDepends::~cmComputeLinkDepends()
  127. {
  128. for(std::vector<DependSetList*>::iterator
  129. i = this->InferredDependSets.begin();
  130. i != this->InferredDependSets.end(); ++i)
  131. {
  132. delete *i;
  133. }
  134. }
  135. //----------------------------------------------------------------------------
  136. std::vector<cmComputeLinkDepends::LinkEntry> const&
  137. cmComputeLinkDepends::Compute()
  138. {
  139. // Follow the link dependencies of the target to be linked.
  140. this->AddTargetLinkEntries(-1, this->Target->GetOriginalLinkLibraries());
  141. // Complete the breadth-first search of dependencies.
  142. while(!this->BFSQueue.empty())
  143. {
  144. // Get the next entry.
  145. BFSEntry qe = this->BFSQueue.front();
  146. this->BFSQueue.pop();
  147. // Follow the entry's dependencies.
  148. this->FollowLinkEntry(qe);
  149. }
  150. // Complete the search of shared library dependencies.
  151. while(!this->SharedDepQueue.empty())
  152. {
  153. // Handle the next entry.
  154. this->HandleSharedDependency(this->SharedDepQueue.front());
  155. this->SharedDepQueue.pop();
  156. }
  157. // Infer dependencies of targets for which they were not known.
  158. this->InferDependencies();
  159. // Cleanup the constraint graph.
  160. this->CleanConstraintGraph();
  161. // Display the constraint graph.
  162. if(this->DebugMode)
  163. {
  164. fprintf(stderr,
  165. "---------------------------------------"
  166. "---------------------------------------\n");
  167. fprintf(stderr, "Link dependency analysis for target %s, config %s\n",
  168. this->Target->GetName(), this->Config?this->Config:"noconfig");
  169. this->DisplayConstraintGraph();
  170. }
  171. // Compute the final set of link entries.
  172. this->OrderLinkEntires();
  173. // Display the final set.
  174. if(this->DebugMode)
  175. {
  176. this->DisplayFinalEntries();
  177. }
  178. return this->FinalLinkEntries;
  179. }
  180. //----------------------------------------------------------------------------
  181. std::map<cmStdString, int>::iterator
  182. cmComputeLinkDepends::AllocateLinkEntry(std::string const& item)
  183. {
  184. std::map<cmStdString, int>::value_type
  185. index_entry(item, static_cast<int>(this->EntryList.size()));
  186. std::map<cmStdString, int>::iterator
  187. lei = this->LinkEntryIndex.insert(index_entry).first;
  188. this->EntryList.push_back(LinkEntry());
  189. this->InferredDependSets.push_back(0);
  190. this->EntryConstraintGraph.push_back(NodeList());
  191. return lei;
  192. }
  193. //----------------------------------------------------------------------------
  194. int cmComputeLinkDepends::AddLinkEntry(std::string const& item)
  195. {
  196. // Check if the item entry has already been added.
  197. std::map<cmStdString, int>::iterator lei = this->LinkEntryIndex.find(item);
  198. if(lei != this->LinkEntryIndex.end())
  199. {
  200. // Yes. We do not need to follow the item's dependencies again.
  201. return lei->second;
  202. }
  203. // Allocate a spot for the item entry.
  204. lei = this->AllocateLinkEntry(item);
  205. // Initialize the item entry.
  206. int index = lei->second;
  207. LinkEntry& entry = this->EntryList[index];
  208. entry.Item = item;
  209. entry.Target = this->Makefile->FindTargetToUse(entry.Item.c_str());
  210. // If the item has dependencies queue it to follow them.
  211. if(entry.Target)
  212. {
  213. // Target dependencies are always known. Follow them.
  214. BFSEntry qe = {index, 0};
  215. this->BFSQueue.push(qe);
  216. }
  217. else
  218. {
  219. // Look for an old-style <item>_LIB_DEPENDS variable.
  220. std::string var = entry.Item;
  221. var += "_LIB_DEPENDS";
  222. if(const char* val = this->Makefile->GetDefinition(var.c_str()))
  223. {
  224. // The item dependencies are known. Follow them.
  225. BFSEntry qe = {index, val};
  226. this->BFSQueue.push(qe);
  227. }
  228. else
  229. {
  230. // The item dependencies are not known. We need to infer them.
  231. this->InferredDependSets[index] = new DependSetList;
  232. }
  233. }
  234. return index;
  235. }
  236. //----------------------------------------------------------------------------
  237. void cmComputeLinkDepends::FollowLinkEntry(BFSEntry const& qe)
  238. {
  239. // Get this entry representation.
  240. int depender_index = qe.Index;
  241. LinkEntry const& entry = this->EntryList[depender_index];
  242. // Follow the item's dependencies.
  243. if(entry.Target)
  244. {
  245. // Follow the target dependencies.
  246. if(cmTargetLinkInterface const* iface =
  247. entry.Target->GetLinkInterface(this->Config))
  248. {
  249. // This target provides its own link interface information.
  250. this->AddLinkEntries(depender_index, iface->Libraries);
  251. // Handle dependent shared libraries.
  252. this->QueueSharedDependencies(depender_index, iface->SharedDeps);
  253. }
  254. else if(!entry.Target->IsImported() &&
  255. entry.Target->GetType() != cmTarget::EXECUTABLE)
  256. {
  257. // Use the target's link implementation as the interface.
  258. this->AddTargetLinkEntries(depender_index,
  259. entry.Target->GetOriginalLinkLibraries());
  260. }
  261. }
  262. else
  263. {
  264. // Follow the old-style dependency list.
  265. this->AddVarLinkEntries(depender_index, qe.LibDepends);
  266. }
  267. }
  268. //----------------------------------------------------------------------------
  269. void
  270. cmComputeLinkDepends
  271. ::QueueSharedDependencies(int depender_index,
  272. std::vector<std::string> const& deps)
  273. {
  274. for(std::vector<std::string>::const_iterator li = deps.begin();
  275. li != deps.end(); ++li)
  276. {
  277. SharedDepEntry qe;
  278. qe.Item = *li;
  279. qe.DependerIndex = depender_index;
  280. this->SharedDepQueue.push(qe);
  281. }
  282. }
  283. //----------------------------------------------------------------------------
  284. void cmComputeLinkDepends::HandleSharedDependency(SharedDepEntry const& dep)
  285. {
  286. // Check if the target already has an entry.
  287. std::map<cmStdString, int>::iterator lei =
  288. this->LinkEntryIndex.find(dep.Item);
  289. if(lei == this->LinkEntryIndex.end())
  290. {
  291. // Allocate a spot for the item entry.
  292. lei = this->AllocateLinkEntry(dep.Item);
  293. // Initialize the item entry.
  294. LinkEntry& entry = this->EntryList[lei->second];
  295. entry.Item = dep.Item;
  296. entry.Target = this->Makefile->FindTargetToUse(dep.Item.c_str());
  297. // This item was added specifically because it is a dependent
  298. // shared library. It may get special treatment
  299. // in cmComputeLinkInformation.
  300. entry.IsSharedDep = true;
  301. }
  302. // Get the link entry for this target.
  303. int index = lei->second;
  304. LinkEntry& entry = this->EntryList[index];
  305. // This shared library dependency must be preceded by the item that
  306. // listed it.
  307. this->EntryConstraintGraph[index].push_back(dep.DependerIndex);
  308. // Target items may have their own dependencies.
  309. if(entry.Target)
  310. {
  311. if(cmTargetLinkInterface const* iface =
  312. entry.Target->GetLinkInterface(this->Config))
  313. {
  314. // We use just the shared dependencies, not the interface.
  315. this->QueueSharedDependencies(index, iface->SharedDeps);
  316. }
  317. }
  318. }
  319. //----------------------------------------------------------------------------
  320. void cmComputeLinkDepends::AddVarLinkEntries(int depender_index,
  321. const char* value)
  322. {
  323. // This is called to add the dependencies named by
  324. // <item>_LIB_DEPENDS. The variable contains a semicolon-separated
  325. // list. The list contains link-type;item pairs and just items.
  326. std::vector<std::string> deplist;
  327. cmSystemTools::ExpandListArgument(value, deplist);
  328. // Compute which library configuration to link.
  329. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  330. if(this->Config && cmSystemTools::UpperCase(this->Config) == "DEBUG")
  331. {
  332. linkType = cmTarget::DEBUG;
  333. }
  334. // Look for entries meant for this configuration.
  335. std::vector<std::string> actual_libs;
  336. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  337. bool haveLLT = false;
  338. for(std::vector<std::string>::const_iterator di = deplist.begin();
  339. di != deplist.end(); ++di)
  340. {
  341. if(*di == "debug")
  342. {
  343. llt = cmTarget::DEBUG;
  344. haveLLT = true;
  345. }
  346. else if(*di == "optimized")
  347. {
  348. llt = cmTarget::OPTIMIZED;
  349. haveLLT = true;
  350. }
  351. else if(*di == "general")
  352. {
  353. llt = cmTarget::GENERAL;
  354. haveLLT = true;
  355. }
  356. else if(!di->empty())
  357. {
  358. // If no explicit link type was given prior to this entry then
  359. // check if the entry has its own link type variable. This is
  360. // needed for compatibility with dependency files generated by
  361. // the export_library_dependencies command from CMake 2.4 and
  362. // lower.
  363. if(!haveLLT)
  364. {
  365. std::string var = *di;
  366. var += "_LINK_TYPE";
  367. if(const char* val = this->Makefile->GetDefinition(var.c_str()))
  368. {
  369. if(strcmp(val, "debug") == 0)
  370. {
  371. llt = cmTarget::DEBUG;
  372. }
  373. else if(strcmp(val, "optimized") == 0)
  374. {
  375. llt = cmTarget::OPTIMIZED;
  376. }
  377. }
  378. }
  379. // If the library is meant for this link type then use it.
  380. if(llt == cmTarget::GENERAL || llt == linkType)
  381. {
  382. actual_libs.push_back(*di);
  383. }
  384. // Reset the link type until another explicit type is given.
  385. llt = cmTarget::GENERAL;
  386. haveLLT = false;
  387. }
  388. }
  389. // Add the entries from this list.
  390. this->AddLinkEntries(depender_index, actual_libs);
  391. }
  392. //----------------------------------------------------------------------------
  393. void
  394. cmComputeLinkDepends::AddTargetLinkEntries(int depender_index,
  395. LinkLibraryVectorType const& libs)
  396. {
  397. // Compute which library configuration to link.
  398. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  399. if(this->Config && cmSystemTools::UpperCase(this->Config) == "DEBUG")
  400. {
  401. linkType = cmTarget::DEBUG;
  402. }
  403. // Look for entries meant for this configuration.
  404. std::vector<std::string> actual_libs;
  405. for(cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
  406. li != libs.end(); ++li)
  407. {
  408. if(li->second == cmTarget::GENERAL || li->second == linkType)
  409. {
  410. actual_libs.push_back(li->first);
  411. }
  412. }
  413. // Add these entries.
  414. this->AddLinkEntries(depender_index, actual_libs);
  415. }
  416. //----------------------------------------------------------------------------
  417. void
  418. cmComputeLinkDepends::AddLinkEntries(int depender_index,
  419. std::vector<std::string> const& libs)
  420. {
  421. // Track inferred dependency sets implied by this list.
  422. std::map<int, DependSet> dependSets;
  423. // Loop over the libraries linked directly by the depender.
  424. for(std::vector<std::string>::const_iterator li = libs.begin();
  425. li != libs.end(); ++li)
  426. {
  427. // Skip entries that will resolve to the target getting linked or
  428. // are empty.
  429. std::string item = this->CleanItemName(*li);
  430. if(item == this->Target->GetName() || item.empty())
  431. {
  432. continue;
  433. }
  434. // Add a link entry for this item.
  435. int dependee_index = this->AddLinkEntry(item);
  436. // The depender must come before the dependee.
  437. if(depender_index >= 0)
  438. {
  439. this->EntryConstraintGraph[dependee_index].push_back(depender_index);
  440. }
  441. // Update the inferred dependencies for earlier items.
  442. for(std::map<int, DependSet>::iterator dsi = dependSets.begin();
  443. dsi != dependSets.end(); ++dsi)
  444. {
  445. if(dependee_index != dsi->first)
  446. {
  447. dsi->second.insert(dependee_index);
  448. }
  449. }
  450. // If this item needs to have dependencies inferred, do so.
  451. if(this->InferredDependSets[dependee_index])
  452. {
  453. // Make sure an entry exists to hold the set for the item.
  454. dependSets[dependee_index];
  455. }
  456. }
  457. // Store the inferred dependency sets discovered for this list.
  458. for(std::map<int, DependSet>::iterator dsi = dependSets.begin();
  459. dsi != dependSets.end(); ++dsi)
  460. {
  461. this->InferredDependSets[dsi->first]->push_back(dsi->second);
  462. }
  463. }
  464. //----------------------------------------------------------------------------
  465. std::string cmComputeLinkDepends::CleanItemName(std::string const& item)
  466. {
  467. // Strip whitespace off the library names because we used to do this
  468. // in case variables were expanded at generate time. We no longer
  469. // do the expansion but users link to libraries like " ${VAR} ".
  470. std::string lib = item;
  471. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  472. if(pos != lib.npos)
  473. {
  474. lib = lib.substr(pos, lib.npos);
  475. }
  476. pos = lib.find_last_not_of(" \t\r\n");
  477. if(pos != lib.npos)
  478. {
  479. lib = lib.substr(0, pos+1);
  480. }
  481. if(lib != item && !this->Makefile->NeedBackwardsCompatibility(2,4))
  482. {
  483. cmOStringStream e;
  484. e << "Target \"" << this->Target->GetName() << "\" links to item \""
  485. << item << "\" which has leading or trailing whitespace. "
  486. << "CMake is stripping off the whitespace but this may not be "
  487. << "supported in the future. "
  488. << "Update the CMakeLists.txt files to avoid adding the whitespace. "
  489. << "Set CMAKE_BACKWARDS_COMPATIBILITY to 2.4 or lower to disable this "
  490. << "warning.";
  491. cmSystemTools::Message(e.str().c_str());
  492. }
  493. return lib;
  494. }
  495. //----------------------------------------------------------------------------
  496. void cmComputeLinkDepends::InferDependencies()
  497. {
  498. // The inferred dependency sets for each item list the possible
  499. // dependencies. The intersection of the sets for one item form its
  500. // inferred dependencies.
  501. for(unsigned int depender_index=0;
  502. depender_index < this->InferredDependSets.size(); ++depender_index)
  503. {
  504. // Skip items for which dependencies do not need to be inferred or
  505. // for which the inferred dependency sets are empty.
  506. DependSetList* sets = this->InferredDependSets[depender_index];
  507. if(!sets || sets->empty())
  508. {
  509. continue;
  510. }
  511. // Intersect the sets for this item.
  512. DependSetList::const_iterator i = sets->begin();
  513. DependSet common = *i;
  514. for(++i; i != sets->end(); ++i)
  515. {
  516. DependSet intersection;
  517. cmsys_stl::set_intersection
  518. (common.begin(), common.end(), i->begin(), i->end(),
  519. std::inserter(intersection, intersection.begin()));
  520. common = intersection;
  521. }
  522. // Add the inferred dependencies to the graph.
  523. for(DependSet::const_iterator j = common.begin(); j != common.end(); ++j)
  524. {
  525. int dependee_index = *j;
  526. this->EntryConstraintGraph[dependee_index].push_back(depender_index);
  527. }
  528. }
  529. }
  530. //----------------------------------------------------------------------------
  531. void cmComputeLinkDepends::CleanConstraintGraph()
  532. {
  533. for(Graph::iterator i = this->EntryConstraintGraph.begin();
  534. i != this->EntryConstraintGraph.end(); ++i)
  535. {
  536. // Sort the outgoing edges for each graph node so that the
  537. // original order will be preserved as much as possible.
  538. cmsys_stl::sort(i->begin(), i->end());
  539. // Make the edge list unique.
  540. NodeList::iterator last = cmsys_stl::unique(i->begin(), i->end());
  541. i->erase(last, i->end());
  542. }
  543. }
  544. //----------------------------------------------------------------------------
  545. void cmComputeLinkDepends::DisplayConstraintGraph()
  546. {
  547. // Display the graph nodes and their edges.
  548. cmOStringStream e;
  549. for(unsigned int i=0; i < this->EntryConstraintGraph.size(); ++i)
  550. {
  551. NodeList const& nl = this->EntryConstraintGraph[i];
  552. e << "item " << i << " is [" << this->EntryList[i].Item << "]\n";
  553. for(NodeList::const_iterator j = nl.begin(); j != nl.end(); ++j)
  554. {
  555. e << " item " << *j << " must precede it\n";
  556. }
  557. }
  558. fprintf(stderr, "%s\n", e.str().c_str());
  559. }
  560. //----------------------------------------------------------------------------
  561. void cmComputeLinkDepends::OrderLinkEntires()
  562. {
  563. // Compute the DAG of strongly connected components. The algorithm
  564. // used by cmComputeComponentGraph should identify the components in
  565. // the same order in which the items were originally discovered in
  566. // the BFS. This should preserve the original order when no
  567. // constraints disallow it.
  568. cmComputeComponentGraph ccg(this->EntryConstraintGraph);
  569. Graph const& cgraph = ccg.GetComponentGraph();
  570. if(this->DebugMode)
  571. {
  572. this->DisplayComponents(ccg);
  573. }
  574. // Setup visit tracking.
  575. this->ComponentVisited.resize(cgraph.size(), 0);
  576. // The component graph is guaranteed to be acyclic. Start a DFS
  577. // from every entry.
  578. for(unsigned int c=0; c < cgraph.size(); ++c)
  579. {
  580. this->VisitComponent(ccg, c);
  581. }
  582. }
  583. //----------------------------------------------------------------------------
  584. void
  585. cmComputeLinkDepends::DisplayComponents(cmComputeComponentGraph const& ccg)
  586. {
  587. fprintf(stderr, "The strongly connected components are:\n");
  588. std::vector<NodeList> const& components = ccg.GetComponents();
  589. for(unsigned int c=0; c < components.size(); ++c)
  590. {
  591. fprintf(stderr, "Component (%u):\n", c);
  592. NodeList const& nl = components[c];
  593. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  594. {
  595. int i = *ni;
  596. fprintf(stderr, " item %d [%s]\n", i,
  597. this->EntryList[i].Item.c_str());
  598. }
  599. }
  600. fprintf(stderr, "\n");
  601. }
  602. //----------------------------------------------------------------------------
  603. void
  604. cmComputeLinkDepends::VisitComponent(cmComputeComponentGraph const& ccg,
  605. unsigned int c)
  606. {
  607. // Check if the node has already been visited.
  608. if(this->ComponentVisited[c])
  609. {
  610. return;
  611. }
  612. // We are now visiting this component so mark it.
  613. this->ComponentVisited[c] = 1;
  614. // Visit the neighbors of the component first.
  615. NodeList const& nl = ccg.GetComponentGraphEdges(c);
  616. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  617. {
  618. this->VisitComponent(ccg, *ni);
  619. }
  620. // Now that all items required to come before this one have been
  621. // emmitted, emit this component's items.
  622. this->EmitComponent(ccg.GetComponent(c));
  623. }
  624. //----------------------------------------------------------------------------
  625. void cmComputeLinkDepends::EmitComponent(NodeList const& nl)
  626. {
  627. assert(!nl.empty());
  628. // Handle trivial components.
  629. if(nl.size() == 1)
  630. {
  631. this->FinalLinkEntries.push_back(this->EntryList[nl[0]]);
  632. return;
  633. }
  634. // This is a non-trivial strongly connected component of the
  635. // original graph. It consists of two or more libraries (archives)
  636. // that mutually require objects from one another. In the worst
  637. // case we may have to repeat the list of libraries as many times as
  638. // there are object files in the biggest archive. For now we just
  639. // list them twice.
  640. //
  641. // The list of items in the component has been sorted by the order
  642. // of discovery in the original BFS of dependencies. This has the
  643. // advantage that the item directly linked by a target requiring
  644. // this component will come first which minimizes the number of
  645. // repeats needed.
  646. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  647. {
  648. this->FinalLinkEntries.push_back(this->EntryList[*ni]);
  649. }
  650. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  651. {
  652. this->FinalLinkEntries.push_back(this->EntryList[*ni]);
  653. }
  654. }
  655. //----------------------------------------------------------------------------
  656. void cmComputeLinkDepends::DisplayFinalEntries()
  657. {
  658. fprintf(stderr, "target [%s] links to:\n", this->Target->GetName());
  659. for(std::vector<LinkEntry>::const_iterator lei =
  660. this->FinalLinkEntries.begin();
  661. lei != this->FinalLinkEntries.end(); ++lei)
  662. {
  663. if(lei->Target)
  664. {
  665. fprintf(stderr, " target [%s]\n", lei->Target->GetName());
  666. }
  667. else
  668. {
  669. fprintf(stderr, " item [%s]\n", lei->Item.c_str());
  670. }
  671. }
  672. fprintf(stderr, "\n");
  673. }