cmComputeLinkDepends.cxx 31 KB

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