cmComputeLinkDepends.cxx 32 KB

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