cmComputeLinkDepends.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 "cmComputeLinkDepends.h"
  11. #include "cmComputeComponentGraph.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmTarget.h"
  16. #include "cmake.h"
  17. #include <cmsys/stl/algorithm>
  18. #include <assert.h>
  19. /*
  20. This file computes an ordered list of link items to use when linking a
  21. single target in one configuration. Each link item is identified by
  22. the string naming it. A graph of dependencies is created in which
  23. each node corresponds to one item and directed edges lead from nodes to
  24. those which must *follow* them on the link line. For example, the
  25. graph
  26. A -> B -> C
  27. will lead to the link line order
  28. A B C
  29. The set of items placed in the graph is formed with a breadth-first
  30. search of the link dependencies starting from the main target.
  31. There are two types of items: those with known direct dependencies and
  32. those without known dependencies. We will call the two types "known
  33. items" and "unknown items", respectively. Known items are those whose
  34. names correspond to targets (built or imported) and those for which an
  35. old-style <item>_LIB_DEPENDS variable is defined. All other items are
  36. unknown and we must infer dependencies for them. For items that look
  37. like flags (beginning with '-') we trivially infer no dependencies,
  38. and do not include them in the dependencies of other items.
  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. explicit 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. Note that targets are never inferred as dependees because outside
  73. libraries should not depend on them.
  74. ------------------------------------------------------------------------------
  75. The initial exploration of dependencies using a BFS associates an
  76. integer index with each link item. When the graph is built outgoing
  77. edges are sorted by this index.
  78. After the initial exploration of the link interface tree, any
  79. transitive (dependent) shared libraries that were encountered and not
  80. included in the interface are processed in their own BFS. This BFS
  81. follows only the dependent library lists and not the link interfaces.
  82. They are added to the link items with a mark indicating that the are
  83. transitive dependencies. Then cmComputeLinkInformation deals with
  84. them on a per-platform basis.
  85. The complete graph formed from all known and inferred dependencies may
  86. not be acyclic, so an acyclic version must be created.
  87. The original graph is converted to a directed acyclic graph in which
  88. each node corresponds to a strongly connected component of the
  89. original graph. For example, the dependency graph
  90. X -> A -> B -> C -> A -> Y
  91. contains strongly connected components {X}, {A,B,C}, and {Y}. The
  92. implied directed acyclic graph (DAG) is
  93. {X} -> {A,B,C} -> {Y}
  94. We then compute a topological order for the DAG nodes to serve as a
  95. reference for satisfying dependencies efficiently. We perform the DFS
  96. in reverse order and assign topological order indices counting down so
  97. that the result is as close to the original BFS order as possible
  98. without violating dependencies.
  99. ------------------------------------------------------------------------------
  100. The final link entry order is constructed as follows. We first walk
  101. through and emit the *original* link line as specified by the user.
  102. As each item is emitted, a set of pending nodes in the component DAG
  103. is maintained. When a pending component has been completely seen, it
  104. is removed from the pending set and its dependencies (following edges
  105. of the DAG) are added. A trivial component (those with one item) is
  106. complete as soon as its item is seen. A non-trivial component (one
  107. with more than one item; assumed to be static libraries) is complete
  108. when *all* its entries have been seen *twice* (all entries seen once,
  109. then all entries seen again, not just each entry twice). A pending
  110. component tracks which items have been seen and a count of how many
  111. times the component needs to be seen (once for trivial components,
  112. twice for non-trivial). If at any time another component finishes and
  113. re-adds an already pending component, the pending component is reset
  114. so that it needs to be seen in its entirety again. This ensures that
  115. all dependencies of a component are satisfied no matter where it
  116. appears.
  117. After the original link line has been completed, we append to it the
  118. remaining pending components and their dependencies. This is done by
  119. repeatedly emitting the first item from the first pending component
  120. and following the same update rules as when traversing the original
  121. link line. Since the pending components are kept in topological order
  122. they are emitted with minimal repeats (we do not want to emit a
  123. component just to have it added again when another component is
  124. completed later). This process continues until no pending components
  125. remain. We know it will terminate because the component graph is
  126. guaranteed to be acyclic.
  127. The final list of items produced by this procedure consists of the
  128. original user link line followed by minimal additional items needed to
  129. satisfy dependencies. The final list is then filtered to de-duplicate
  130. items that we know the linker will re-use automatically (shared libs).
  131. */
  132. //----------------------------------------------------------------------------
  133. cmComputeLinkDepends
  134. ::cmComputeLinkDepends(cmTarget const* target, const std::string& config)
  135. {
  136. // Store context information.
  137. this->Target = target;
  138. this->Makefile = this->Target->GetMakefile();
  139. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  140. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  141. this->CMakeInstance = this->GlobalGenerator->GetCMakeInstance();
  142. // The configuration being linked.
  143. this->HasConfig = !config.empty();
  144. this->Config = (this->HasConfig)? config : std::string();
  145. this->LinkType = this->Target->ComputeLinkType(this->Config);
  146. // Enable debug mode if requested.
  147. this->DebugMode = this->Makefile->IsOn("CMAKE_LINK_DEPENDS_DEBUG_MODE");
  148. // Assume no compatibility until set.
  149. this->OldLinkDirMode = false;
  150. // No computation has been done.
  151. this->CCG = 0;
  152. }
  153. //----------------------------------------------------------------------------
  154. cmComputeLinkDepends::~cmComputeLinkDepends()
  155. {
  156. for(std::vector<DependSetList*>::iterator
  157. i = this->InferredDependSets.begin();
  158. i != this->InferredDependSets.end(); ++i)
  159. {
  160. delete *i;
  161. }
  162. delete this->CCG;
  163. }
  164. //----------------------------------------------------------------------------
  165. void cmComputeLinkDepends::SetOldLinkDirMode(bool b)
  166. {
  167. this->OldLinkDirMode = b;
  168. }
  169. //----------------------------------------------------------------------------
  170. std::vector<cmComputeLinkDepends::LinkEntry> const&
  171. cmComputeLinkDepends::Compute()
  172. {
  173. // Follow the link dependencies of the target to be linked.
  174. this->AddDirectLinkEntries();
  175. // Complete the breadth-first search of dependencies.
  176. while(!this->BFSQueue.empty())
  177. {
  178. // Get the next entry.
  179. BFSEntry qe = this->BFSQueue.front();
  180. this->BFSQueue.pop();
  181. // Follow the entry's dependencies.
  182. this->FollowLinkEntry(qe);
  183. }
  184. // Complete the search of shared library dependencies.
  185. while(!this->SharedDepQueue.empty())
  186. {
  187. // Handle the next entry.
  188. this->HandleSharedDependency(this->SharedDepQueue.front());
  189. this->SharedDepQueue.pop();
  190. }
  191. // Infer dependencies of targets for which they were not known.
  192. this->InferDependencies();
  193. // Cleanup the constraint graph.
  194. this->CleanConstraintGraph();
  195. // Display the constraint graph.
  196. if(this->DebugMode)
  197. {
  198. fprintf(stderr,
  199. "---------------------------------------"
  200. "---------------------------------------\n");
  201. fprintf(stderr, "Link dependency analysis for target %s, config %s\n",
  202. this->Target->GetName().c_str(),
  203. this->HasConfig?this->Config.c_str():"noconfig");
  204. this->DisplayConstraintGraph();
  205. }
  206. // Compute the final ordering.
  207. this->OrderLinkEntires();
  208. // Compute the final set of link entries.
  209. // Iterate in reverse order so we can keep only the last occurrence
  210. // of a shared library.
  211. std::set<int> emmitted;
  212. for(std::vector<int>::const_reverse_iterator
  213. li = this->FinalLinkOrder.rbegin(),
  214. le = this->FinalLinkOrder.rend();
  215. li != le; ++li)
  216. {
  217. int i = *li;
  218. LinkEntry const& e = this->EntryList[i];
  219. cmTarget const* t = e.Target;
  220. // Entries that we know the linker will re-use do not need to be repeated.
  221. bool uniquify = t && t->GetType() == cmTarget::SHARED_LIBRARY;
  222. if(!uniquify || emmitted.insert(i).second)
  223. {
  224. this->FinalLinkEntries.push_back(e);
  225. }
  226. }
  227. // Reverse the resulting order since we iterated in reverse.
  228. std::reverse(this->FinalLinkEntries.begin(), this->FinalLinkEntries.end());
  229. // Display the final set.
  230. if(this->DebugMode)
  231. {
  232. this->DisplayFinalEntries();
  233. }
  234. return this->FinalLinkEntries;
  235. }
  236. //----------------------------------------------------------------------------
  237. std::map<std::string, int>::iterator
  238. cmComputeLinkDepends::AllocateLinkEntry(std::string const& item)
  239. {
  240. std::map<std::string, int>::value_type
  241. index_entry(item, static_cast<int>(this->EntryList.size()));
  242. std::map<std::string, int>::iterator
  243. lei = this->LinkEntryIndex.insert(index_entry).first;
  244. this->EntryList.push_back(LinkEntry());
  245. this->InferredDependSets.push_back(0);
  246. this->EntryConstraintGraph.push_back(EdgeList());
  247. return lei;
  248. }
  249. //----------------------------------------------------------------------------
  250. int cmComputeLinkDepends::AddLinkEntry(cmLinkItem const& item)
  251. {
  252. // Check if the item entry has already been added.
  253. std::map<std::string, int>::iterator lei = this->LinkEntryIndex.find(item);
  254. if(lei != this->LinkEntryIndex.end())
  255. {
  256. // Yes. We do not need to follow the item's dependencies again.
  257. return lei->second;
  258. }
  259. // Allocate a spot for the item entry.
  260. lei = this->AllocateLinkEntry(item);
  261. // Initialize the item entry.
  262. int index = lei->second;
  263. LinkEntry& entry = this->EntryList[index];
  264. entry.Item = item;
  265. entry.Target = item.Target;
  266. entry.IsFlag = (!entry.Target && item[0] == '-' && item[1] != 'l' &&
  267. item.substr(0, 10) != "-framework");
  268. // If the item has dependencies queue it to follow them.
  269. if(entry.Target)
  270. {
  271. // Target dependencies are always known. Follow them.
  272. BFSEntry qe = {index, 0};
  273. this->BFSQueue.push(qe);
  274. }
  275. else
  276. {
  277. // Look for an old-style <item>_LIB_DEPENDS variable.
  278. std::string var = entry.Item;
  279. var += "_LIB_DEPENDS";
  280. if(const char* val = this->Makefile->GetDefinition(var))
  281. {
  282. // The item dependencies are known. Follow them.
  283. BFSEntry qe = {index, val};
  284. this->BFSQueue.push(qe);
  285. }
  286. else if(!entry.IsFlag)
  287. {
  288. // The item dependencies are not known. We need to infer them.
  289. this->InferredDependSets[index] = new DependSetList;
  290. }
  291. }
  292. return index;
  293. }
  294. //----------------------------------------------------------------------------
  295. void cmComputeLinkDepends::FollowLinkEntry(BFSEntry const& qe)
  296. {
  297. // Get this entry representation.
  298. int depender_index = qe.Index;
  299. LinkEntry const& entry = this->EntryList[depender_index];
  300. // Follow the item's dependencies.
  301. if(entry.Target)
  302. {
  303. // Follow the target dependencies.
  304. if(cmTarget::LinkInterface const* iface =
  305. entry.Target->GetLinkInterface(this->Config, this->Target))
  306. {
  307. const bool isIface =
  308. entry.Target->GetType() == cmTarget::INTERFACE_LIBRARY;
  309. // This target provides its own link interface information.
  310. this->AddLinkEntries(depender_index, iface->Libraries);
  311. if (isIface)
  312. {
  313. return;
  314. }
  315. // Handle dependent shared libraries.
  316. this->FollowSharedDeps(depender_index, iface);
  317. // Support for CMP0003.
  318. for(std::vector<cmLinkItem>::const_iterator
  319. oi = iface->WrongConfigLibraries.begin();
  320. oi != iface->WrongConfigLibraries.end(); ++oi)
  321. {
  322. this->CheckWrongConfigItem(*oi);
  323. }
  324. }
  325. }
  326. else
  327. {
  328. // Follow the old-style dependency list.
  329. this->AddVarLinkEntries(depender_index, qe.LibDepends);
  330. }
  331. }
  332. //----------------------------------------------------------------------------
  333. void
  334. cmComputeLinkDepends
  335. ::FollowSharedDeps(int depender_index, cmTarget::LinkInterface const* iface,
  336. bool follow_interface)
  337. {
  338. // Follow dependencies if we have not followed them already.
  339. if(this->SharedDepFollowed.insert(depender_index).second)
  340. {
  341. if(follow_interface)
  342. {
  343. this->QueueSharedDependencies(depender_index, iface->Libraries);
  344. }
  345. this->QueueSharedDependencies(depender_index, iface->SharedDeps);
  346. }
  347. }
  348. //----------------------------------------------------------------------------
  349. void
  350. cmComputeLinkDepends
  351. ::QueueSharedDependencies(int depender_index,
  352. std::vector<cmLinkItem> const& deps)
  353. {
  354. for(std::vector<cmLinkItem>::const_iterator li = deps.begin();
  355. li != deps.end(); ++li)
  356. {
  357. SharedDepEntry qe;
  358. qe.Item = *li;
  359. qe.DependerIndex = depender_index;
  360. this->SharedDepQueue.push(qe);
  361. }
  362. }
  363. //----------------------------------------------------------------------------
  364. void cmComputeLinkDepends::HandleSharedDependency(SharedDepEntry const& dep)
  365. {
  366. // Check if the target already has an entry.
  367. std::map<std::string, int>::iterator lei =
  368. this->LinkEntryIndex.find(dep.Item);
  369. if(lei == this->LinkEntryIndex.end())
  370. {
  371. // Allocate a spot for the item entry.
  372. lei = this->AllocateLinkEntry(dep.Item);
  373. // Initialize the item entry.
  374. LinkEntry& entry = this->EntryList[lei->second];
  375. entry.Item = dep.Item;
  376. entry.Target = dep.Item.Target;
  377. // This item was added specifically because it is a dependent
  378. // shared library. It may get special treatment
  379. // in cmComputeLinkInformation.
  380. entry.IsSharedDep = true;
  381. }
  382. // Get the link entry for this target.
  383. int index = lei->second;
  384. LinkEntry& entry = this->EntryList[index];
  385. // This shared library dependency must follow the item that listed
  386. // it.
  387. this->EntryConstraintGraph[dep.DependerIndex].push_back(index);
  388. // Target items may have their own dependencies.
  389. if(entry.Target)
  390. {
  391. if(cmTarget::LinkInterface const* iface =
  392. entry.Target->GetLinkInterface(this->Config, this->Target))
  393. {
  394. // Follow public and private dependencies transitively.
  395. this->FollowSharedDeps(index, iface, true);
  396. }
  397. }
  398. }
  399. //----------------------------------------------------------------------------
  400. void cmComputeLinkDepends::AddVarLinkEntries(int depender_index,
  401. const char* value)
  402. {
  403. // This is called to add the dependencies named by
  404. // <item>_LIB_DEPENDS. The variable contains a semicolon-separated
  405. // list. The list contains link-type;item pairs and just items.
  406. std::vector<std::string> deplist;
  407. cmSystemTools::ExpandListArgument(value, deplist);
  408. // Look for entries meant for this configuration.
  409. std::vector<cmLinkItem> actual_libs;
  410. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  411. bool haveLLT = false;
  412. for(std::vector<std::string>::const_iterator di = deplist.begin();
  413. di != deplist.end(); ++di)
  414. {
  415. if(*di == "debug")
  416. {
  417. llt = cmTarget::DEBUG;
  418. haveLLT = true;
  419. }
  420. else if(*di == "optimized")
  421. {
  422. llt = cmTarget::OPTIMIZED;
  423. haveLLT = true;
  424. }
  425. else if(*di == "general")
  426. {
  427. llt = cmTarget::GENERAL;
  428. haveLLT = true;
  429. }
  430. else if(!di->empty())
  431. {
  432. // If no explicit link type was given prior to this entry then
  433. // check if the entry has its own link type variable. This is
  434. // needed for compatibility with dependency files generated by
  435. // the export_library_dependencies command from CMake 2.4 and
  436. // lower.
  437. if(!haveLLT)
  438. {
  439. std::string var = *di;
  440. var += "_LINK_TYPE";
  441. if(const char* val = this->Makefile->GetDefinition(var))
  442. {
  443. if(strcmp(val, "debug") == 0)
  444. {
  445. llt = cmTarget::DEBUG;
  446. }
  447. else if(strcmp(val, "optimized") == 0)
  448. {
  449. llt = cmTarget::OPTIMIZED;
  450. }
  451. }
  452. }
  453. // If the library is meant for this link type then use it.
  454. if(llt == cmTarget::GENERAL || llt == this->LinkType)
  455. {
  456. cmLinkItem item(*di, this->FindTargetToLink(depender_index, *di));
  457. actual_libs.push_back(item);
  458. }
  459. else if(this->OldLinkDirMode)
  460. {
  461. cmLinkItem item(*di, this->FindTargetToLink(depender_index, *di));
  462. this->CheckWrongConfigItem(item);
  463. }
  464. // Reset the link type until another explicit type is given.
  465. llt = cmTarget::GENERAL;
  466. haveLLT = false;
  467. }
  468. }
  469. // Add the entries from this list.
  470. this->AddLinkEntries(depender_index, actual_libs);
  471. }
  472. //----------------------------------------------------------------------------
  473. void cmComputeLinkDepends::AddDirectLinkEntries()
  474. {
  475. // Add direct link dependencies in this configuration.
  476. cmTarget::LinkImplementation const* impl =
  477. this->Target->GetLinkImplementation(this->Config);
  478. this->AddLinkEntries(-1, impl->Libraries);
  479. for(std::vector<cmLinkItem>::const_iterator
  480. wi = impl->WrongConfigLibraries.begin();
  481. wi != impl->WrongConfigLibraries.end(); ++wi)
  482. {
  483. this->CheckWrongConfigItem(*wi);
  484. }
  485. }
  486. //----------------------------------------------------------------------------
  487. template <typename T>
  488. void
  489. cmComputeLinkDepends::AddLinkEntries(
  490. int depender_index, std::vector<T> const& libs)
  491. {
  492. // Track inferred dependency sets implied by this list.
  493. std::map<int, DependSet> dependSets;
  494. // Loop over the libraries linked directly by the depender.
  495. for(typename std::vector<T>::const_iterator li = libs.begin();
  496. li != libs.end(); ++li)
  497. {
  498. // Skip entries that will resolve to the target getting linked or
  499. // are empty.
  500. cmLinkItem const& item = *li;
  501. if(item == this->Target->GetName() || item.empty())
  502. {
  503. continue;
  504. }
  505. // Add a link entry for this item.
  506. int dependee_index = this->AddLinkEntry(*li);
  507. // The dependee must come after the depender.
  508. if(depender_index >= 0)
  509. {
  510. this->EntryConstraintGraph[depender_index].push_back(dependee_index);
  511. }
  512. else
  513. {
  514. // This is a direct dependency of the target being linked.
  515. this->OriginalEntries.push_back(dependee_index);
  516. }
  517. // Update the inferred dependencies for earlier items.
  518. for(std::map<int, DependSet>::iterator dsi = dependSets.begin();
  519. dsi != dependSets.end(); ++dsi)
  520. {
  521. // Add this item to the inferred dependencies of other items.
  522. // Target items are never inferred dependees because unknown
  523. // items are outside libraries that should not be depending on
  524. // targets.
  525. if(!this->EntryList[dependee_index].Target &&
  526. !this->EntryList[dependee_index].IsFlag &&
  527. dependee_index != dsi->first)
  528. {
  529. dsi->second.insert(dependee_index);
  530. }
  531. }
  532. // If this item needs to have dependencies inferred, do so.
  533. if(this->InferredDependSets[dependee_index])
  534. {
  535. // Make sure an entry exists to hold the set for the item.
  536. dependSets[dependee_index];
  537. }
  538. }
  539. // Store the inferred dependency sets discovered for this list.
  540. for(std::map<int, DependSet>::iterator dsi = dependSets.begin();
  541. dsi != dependSets.end(); ++dsi)
  542. {
  543. this->InferredDependSets[dsi->first]->push_back(dsi->second);
  544. }
  545. }
  546. //----------------------------------------------------------------------------
  547. cmTarget const* cmComputeLinkDepends::FindTargetToLink(int depender_index,
  548. const std::string& name)
  549. {
  550. // Look for a target in the scope of the depender.
  551. cmTarget const* from = this->Target;
  552. if(depender_index >= 0)
  553. {
  554. if(cmTarget const* depender = this->EntryList[depender_index].Target)
  555. {
  556. from = depender;
  557. }
  558. }
  559. return from->FindTargetToLink(name);
  560. }
  561. //----------------------------------------------------------------------------
  562. void cmComputeLinkDepends::InferDependencies()
  563. {
  564. // The inferred dependency sets for each item list the possible
  565. // dependencies. The intersection of the sets for one item form its
  566. // inferred dependencies.
  567. for(unsigned int depender_index=0;
  568. depender_index < this->InferredDependSets.size(); ++depender_index)
  569. {
  570. // Skip items for which dependencies do not need to be inferred or
  571. // for which the inferred dependency sets are empty.
  572. DependSetList* sets = this->InferredDependSets[depender_index];
  573. if(!sets || sets->empty())
  574. {
  575. continue;
  576. }
  577. // Intersect the sets for this item.
  578. DependSetList::const_iterator i = sets->begin();
  579. DependSet common = *i;
  580. for(++i; i != sets->end(); ++i)
  581. {
  582. DependSet intersection;
  583. cmsys_stl::set_intersection
  584. (common.begin(), common.end(), i->begin(), i->end(),
  585. std::inserter(intersection, intersection.begin()));
  586. common = intersection;
  587. }
  588. // Add the inferred dependencies to the graph.
  589. for(DependSet::const_iterator j = common.begin(); j != common.end(); ++j)
  590. {
  591. int dependee_index = *j;
  592. this->EntryConstraintGraph[depender_index].push_back(dependee_index);
  593. }
  594. }
  595. }
  596. //----------------------------------------------------------------------------
  597. void cmComputeLinkDepends::CleanConstraintGraph()
  598. {
  599. for(Graph::iterator i = this->EntryConstraintGraph.begin();
  600. i != this->EntryConstraintGraph.end(); ++i)
  601. {
  602. // Sort the outgoing edges for each graph node so that the
  603. // original order will be preserved as much as possible.
  604. cmsys_stl::sort(i->begin(), i->end());
  605. // Make the edge list unique.
  606. EdgeList::iterator last = cmsys_stl::unique(i->begin(), i->end());
  607. i->erase(last, i->end());
  608. }
  609. }
  610. //----------------------------------------------------------------------------
  611. void cmComputeLinkDepends::DisplayConstraintGraph()
  612. {
  613. // Display the graph nodes and their edges.
  614. cmOStringStream e;
  615. for(unsigned int i=0; i < this->EntryConstraintGraph.size(); ++i)
  616. {
  617. EdgeList const& nl = this->EntryConstraintGraph[i];
  618. e << "item " << i << " is [" << this->EntryList[i].Item << "]\n";
  619. for(EdgeList::const_iterator j = nl.begin(); j != nl.end(); ++j)
  620. {
  621. e << " item " << *j << " must follow it\n";
  622. }
  623. }
  624. fprintf(stderr, "%s\n", e.str().c_str());
  625. }
  626. //----------------------------------------------------------------------------
  627. void cmComputeLinkDepends::OrderLinkEntires()
  628. {
  629. // Compute the DAG of strongly connected components. The algorithm
  630. // used by cmComputeComponentGraph should identify the components in
  631. // the same order in which the items were originally discovered in
  632. // the BFS. This should preserve the original order when no
  633. // constraints disallow it.
  634. this->CCG = new cmComputeComponentGraph(this->EntryConstraintGraph);
  635. // The component graph is guaranteed to be acyclic. Start a DFS
  636. // from every entry to compute a topological order for the
  637. // components.
  638. Graph const& cgraph = this->CCG->GetComponentGraph();
  639. int n = static_cast<int>(cgraph.size());
  640. this->ComponentVisited.resize(cgraph.size(), 0);
  641. this->ComponentOrder.resize(cgraph.size(), n);
  642. this->ComponentOrderId = n;
  643. // Run in reverse order so the topological order will preserve the
  644. // original order where there are no constraints.
  645. for(int c = n-1; c >= 0; --c)
  646. {
  647. this->VisitComponent(c);
  648. }
  649. // Display the component graph.
  650. if(this->DebugMode)
  651. {
  652. this->DisplayComponents();
  653. }
  654. // Start with the original link line.
  655. for(std::vector<int>::const_iterator i = this->OriginalEntries.begin();
  656. i != this->OriginalEntries.end(); ++i)
  657. {
  658. this->VisitEntry(*i);
  659. }
  660. // Now explore anything left pending. Since the component graph is
  661. // guaranteed to be acyclic we know this will terminate.
  662. while(!this->PendingComponents.empty())
  663. {
  664. // Visit one entry from the first pending component. The visit
  665. // logic will update the pending components accordingly. Since
  666. // the pending components are kept in topological order this will
  667. // not repeat one.
  668. int e = *this->PendingComponents.begin()->second.Entries.begin();
  669. this->VisitEntry(e);
  670. }
  671. }
  672. //----------------------------------------------------------------------------
  673. void
  674. cmComputeLinkDepends::DisplayComponents()
  675. {
  676. fprintf(stderr, "The strongly connected components are:\n");
  677. std::vector<NodeList> const& components = this->CCG->GetComponents();
  678. for(unsigned int c=0; c < components.size(); ++c)
  679. {
  680. fprintf(stderr, "Component (%u):\n", c);
  681. NodeList const& nl = components[c];
  682. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  683. {
  684. int i = *ni;
  685. fprintf(stderr, " item %d [%s]\n", i,
  686. this->EntryList[i].Item.c_str());
  687. }
  688. EdgeList const& ol = this->CCG->GetComponentGraphEdges(c);
  689. for(EdgeList::const_iterator oi = ol.begin(); oi != ol.end(); ++oi)
  690. {
  691. int i = *oi;
  692. fprintf(stderr, " followed by Component (%d)\n", i);
  693. }
  694. fprintf(stderr, " topo order index %d\n",
  695. this->ComponentOrder[c]);
  696. }
  697. fprintf(stderr, "\n");
  698. }
  699. //----------------------------------------------------------------------------
  700. void cmComputeLinkDepends::VisitComponent(unsigned int c)
  701. {
  702. // Check if the node has already been visited.
  703. if(this->ComponentVisited[c])
  704. {
  705. return;
  706. }
  707. // We are now visiting this component so mark it.
  708. this->ComponentVisited[c] = 1;
  709. // Visit the neighbors of the component first.
  710. // Run in reverse order so the topological order will preserve the
  711. // original order where there are no constraints.
  712. EdgeList const& nl = this->CCG->GetComponentGraphEdges(c);
  713. for(EdgeList::const_reverse_iterator ni = nl.rbegin();
  714. ni != nl.rend(); ++ni)
  715. {
  716. this->VisitComponent(*ni);
  717. }
  718. // Assign an ordering id to this component.
  719. this->ComponentOrder[c] = --this->ComponentOrderId;
  720. }
  721. //----------------------------------------------------------------------------
  722. void cmComputeLinkDepends::VisitEntry(int index)
  723. {
  724. // Include this entry on the link line.
  725. this->FinalLinkOrder.push_back(index);
  726. // This entry has now been seen. Update its component.
  727. bool completed = false;
  728. int component = this->CCG->GetComponentMap()[index];
  729. std::map<int, PendingComponent>::iterator mi =
  730. this->PendingComponents.find(this->ComponentOrder[component]);
  731. if(mi != this->PendingComponents.end())
  732. {
  733. // The entry is in an already pending component.
  734. PendingComponent& pc = mi->second;
  735. // Remove the entry from those pending in its component.
  736. pc.Entries.erase(index);
  737. if(pc.Entries.empty())
  738. {
  739. // The complete component has been seen since it was last needed.
  740. --pc.Count;
  741. if(pc.Count == 0)
  742. {
  743. // The component has been completed.
  744. this->PendingComponents.erase(mi);
  745. completed = true;
  746. }
  747. else
  748. {
  749. // The whole component needs to be seen again.
  750. NodeList const& nl = this->CCG->GetComponent(component);
  751. assert(nl.size() > 1);
  752. pc.Entries.insert(nl.begin(), nl.end());
  753. }
  754. }
  755. }
  756. else
  757. {
  758. // The entry is not in an already pending component.
  759. NodeList const& nl = this->CCG->GetComponent(component);
  760. if(nl.size() > 1)
  761. {
  762. // This is a non-trivial component. It is now pending.
  763. PendingComponent& pc = this->MakePendingComponent(component);
  764. // The starting entry has already been seen.
  765. pc.Entries.erase(index);
  766. }
  767. else
  768. {
  769. // This is a trivial component, so it is already complete.
  770. completed = true;
  771. }
  772. }
  773. // If the entry completed a component, the component's dependencies
  774. // are now pending.
  775. if(completed)
  776. {
  777. EdgeList const& ol = this->CCG->GetComponentGraphEdges(component);
  778. for(EdgeList::const_iterator oi = ol.begin(); oi != ol.end(); ++oi)
  779. {
  780. // This entire component is now pending no matter whether it has
  781. // been partially seen already.
  782. this->MakePendingComponent(*oi);
  783. }
  784. }
  785. }
  786. //----------------------------------------------------------------------------
  787. cmComputeLinkDepends::PendingComponent&
  788. cmComputeLinkDepends::MakePendingComponent(unsigned int component)
  789. {
  790. // Create an entry (in topological order) for the component.
  791. PendingComponent& pc =
  792. this->PendingComponents[this->ComponentOrder[component]];
  793. pc.Id = component;
  794. NodeList const& nl = this->CCG->GetComponent(component);
  795. if(nl.size() == 1)
  796. {
  797. // Trivial components need be seen only once.
  798. pc.Count = 1;
  799. }
  800. else
  801. {
  802. // This is a non-trivial strongly connected component of the
  803. // original graph. It consists of two or more libraries
  804. // (archives) that mutually require objects from one another. In
  805. // the worst case we may have to repeat the list of libraries as
  806. // many times as there are object files in the biggest archive.
  807. // For now we just list them twice.
  808. //
  809. // The list of items in the component has been sorted by the order
  810. // of discovery in the original BFS of dependencies. This has the
  811. // advantage that the item directly linked by a target requiring
  812. // this component will come first which minimizes the number of
  813. // repeats needed.
  814. pc.Count = this->ComputeComponentCount(nl);
  815. }
  816. // Store the entries to be seen.
  817. pc.Entries.insert(nl.begin(), nl.end());
  818. return pc;
  819. }
  820. //----------------------------------------------------------------------------
  821. int cmComputeLinkDepends::ComputeComponentCount(NodeList const& nl)
  822. {
  823. int count = 2;
  824. for(NodeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
  825. {
  826. if(cmTarget const* target = this->EntryList[*ni].Target)
  827. {
  828. if(cmTarget::LinkInterface const* iface =
  829. target->GetLinkInterface(this->Config, this->Target))
  830. {
  831. if(iface->Multiplicity > count)
  832. {
  833. count = iface->Multiplicity;
  834. }
  835. }
  836. }
  837. }
  838. return count;
  839. }
  840. //----------------------------------------------------------------------------
  841. void cmComputeLinkDepends::DisplayFinalEntries()
  842. {
  843. fprintf(stderr, "target [%s] links to:\n", this->Target->GetName().c_str());
  844. for(std::vector<LinkEntry>::const_iterator lei =
  845. this->FinalLinkEntries.begin();
  846. lei != this->FinalLinkEntries.end(); ++lei)
  847. {
  848. if(lei->Target)
  849. {
  850. fprintf(stderr, " target [%s]\n", lei->Target->GetName().c_str());
  851. }
  852. else
  853. {
  854. fprintf(stderr, " item [%s]\n", lei->Item.c_str());
  855. }
  856. }
  857. fprintf(stderr, "\n");
  858. }
  859. //----------------------------------------------------------------------------
  860. void cmComputeLinkDepends::CheckWrongConfigItem(cmLinkItem const& item)
  861. {
  862. if(!this->OldLinkDirMode)
  863. {
  864. return;
  865. }
  866. // For CMake 2.4 bug-compatibility we need to consider the output
  867. // directories of targets linked in another configuration as link
  868. // directories.
  869. if(item.Target && !item.Target->IsImported())
  870. {
  871. this->OldWrongConfigItems.insert(item.Target);
  872. }
  873. }