cmComputeLinkDepends.cxx 26 KB

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