cmComputeLinkDepends.cxx 30 KB

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