cmComputeLinkDepends.cxx 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmComputeLinkDepends.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstddef>
  7. #include <cstdio>
  8. #include <iterator>
  9. #include <sstream>
  10. #include <type_traits>
  11. #include <unordered_map>
  12. #include <utility>
  13. #include <cm/memory>
  14. #include <cm/string_view>
  15. #include <cmext/string_view>
  16. #include "cmsys/RegularExpression.hxx"
  17. #include "cmComputeComponentGraph.h"
  18. #include "cmGeneratorExpression.h"
  19. #include "cmGeneratorExpressionDAGChecker.h"
  20. #include "cmGeneratorTarget.h"
  21. #include "cmGlobalGenerator.h"
  22. #include "cmList.h"
  23. #include "cmListFileCache.h"
  24. #include "cmLocalGenerator.h"
  25. #include "cmMakefile.h"
  26. #include "cmMessageType.h"
  27. #include "cmPolicies.h"
  28. #include "cmRange.h"
  29. #include "cmState.h"
  30. #include "cmStateTypes.h"
  31. #include "cmStringAlgorithms.h"
  32. #include "cmTarget.h"
  33. #include "cmValue.h"
  34. #include "cmake.h"
  35. /*
  36. This file computes an ordered list of link items to use when linking a
  37. single target in one configuration. Each link item is identified by
  38. the string naming it. A graph of dependencies is created in which
  39. each node corresponds to one item and directed edges lead from nodes to
  40. those which must *follow* them on the link line. For example, the
  41. graph
  42. A -> B -> C
  43. will lead to the link line order
  44. A B C
  45. The set of items placed in the graph is formed with a breadth-first
  46. search of the link dependencies starting from the main target.
  47. There are two types of items: those with known direct dependencies and
  48. those without known dependencies. We will call the two types "known
  49. items" and "unknown items", respectively. Known items are those whose
  50. names correspond to targets (built or imported) and those for which an
  51. old-style <item>_LIB_DEPENDS variable is defined. All other items are
  52. unknown and we must infer dependencies for them. For items that look
  53. like flags (beginning with '-') we trivially infer no dependencies,
  54. and do not include them in the dependencies of other items.
  55. Known items have dependency lists ordered based on how the user
  56. specified them. We can use this order to infer potential dependencies
  57. of unknown items. For example, if link items A and B are unknown and
  58. items X and Y are known, then we might have the following dependency
  59. lists:
  60. X: Y A B
  61. Y: A B
  62. The explicitly known dependencies form graph edges
  63. X -> Y , X -> A , X -> B , Y -> A , Y -> B
  64. We can also infer the edge
  65. A -> B
  66. because *every* time A appears B is seen on its right. We do not know
  67. whether A really needs symbols from B to link, but it *might* so we
  68. must preserve their order. This is the case also for the following
  69. explicit lists:
  70. X: A B Y
  71. Y: A B
  72. Here, A is followed by the set {B,Y} in one list, and {B} in the other
  73. list. The intersection of these sets is {B}, so we can infer that A
  74. depends on at most B. Meanwhile B is followed by the set {Y} in one
  75. list and {} in the other. The intersection is {} so we can infer that
  76. B has no dependencies.
  77. Let's make a more complex example by adding unknown item C and
  78. considering these dependency lists:
  79. X: A B Y C
  80. Y: A C B
  81. The explicit edges are
  82. X -> Y , X -> A , X -> B , X -> C , Y -> A , Y -> B , Y -> C
  83. For the unknown items, we infer dependencies by looking at the
  84. "follow" sets:
  85. A: intersect( {B,Y,C} , {C,B} ) = {B,C} ; infer edges A -> B , A -> C
  86. B: intersect( {Y,C} , {} ) = {} ; infer no edges
  87. C: intersect( {} , {B} ) = {} ; infer no edges
  88. Note that targets are never inferred as dependees because outside
  89. libraries should not depend on them.
  90. ------------------------------------------------------------------------------
  91. The initial exploration of dependencies using a BFS associates an
  92. integer index with each link item. When the graph is built outgoing
  93. edges are sorted by this index.
  94. After the initial exploration of the link interface tree, any
  95. transitive (dependent) shared libraries that were encountered and not
  96. included in the interface are processed in their own BFS. This BFS
  97. follows only the dependent library lists and not the link interfaces.
  98. They are added to the link items with a mark indicating that the are
  99. transitive dependencies. Then cmComputeLinkInformation deals with
  100. them on a per-platform basis.
  101. The complete graph formed from all known and inferred dependencies may
  102. not be acyclic, so an acyclic version must be created.
  103. The original graph is converted to a directed acyclic graph in which
  104. each node corresponds to a strongly connected component of the
  105. original graph. For example, the dependency graph
  106. X -> A -> B -> C -> A -> Y
  107. contains strongly connected components {X}, {A,B,C}, and {Y}. The
  108. implied directed acyclic graph (DAG) is
  109. {X} -> {A,B,C} -> {Y}
  110. We then compute a topological order for the DAG nodes to serve as a
  111. reference for satisfying dependencies efficiently. We perform the DFS
  112. in reverse order and assign topological order indices counting down so
  113. that the result is as close to the original BFS order as possible
  114. without violating dependencies.
  115. ------------------------------------------------------------------------------
  116. The final link entry order is constructed as follows. We first walk
  117. through and emit the *original* link line as specified by the user.
  118. As each item is emitted, a set of pending nodes in the component DAG
  119. is maintained. When a pending component has been completely seen, it
  120. is removed from the pending set and its dependencies (following edges
  121. of the DAG) are added. A trivial component (those with one item) is
  122. complete as soon as its item is seen. A non-trivial component (one
  123. with more than one item; assumed to be static libraries) is complete
  124. when *all* its entries have been seen *twice* (all entries seen once,
  125. then all entries seen again, not just each entry twice). A pending
  126. component tracks which items have been seen and a count of how many
  127. times the component needs to be seen (once for trivial components,
  128. twice for non-trivial). If at any time another component finishes and
  129. re-adds an already pending component, the pending component is reset
  130. so that it needs to be seen in its entirety again. This ensures that
  131. all dependencies of a component are satisfied no matter where it
  132. appears.
  133. After the original link line has been completed, we append to it the
  134. remaining pending components and their dependencies. This is done by
  135. repeatedly emitting the first item from the first pending component
  136. and following the same update rules as when traversing the original
  137. link line. Since the pending components are kept in topological order
  138. they are emitted with minimal repeats (we do not want to emit a
  139. component just to have it added again when another component is
  140. completed later). This process continues until no pending components
  141. remain. We know it will terminate because the component graph is
  142. guaranteed to be acyclic.
  143. The final list of items produced by this procedure consists of the
  144. original user link line followed by minimal additional items needed to
  145. satisfy dependencies. The final list is then filtered to de-duplicate
  146. items that we know the linker will reuse automatically (shared libs).
  147. */
  148. namespace {
  149. // LINK_LIBRARY helpers
  150. bool IsFeatureSupported(cmMakefile* makefile, std::string const& linkLanguage,
  151. std::string const& feature)
  152. {
  153. auto featureSupported = cmStrCat(
  154. "CMAKE_", linkLanguage, "_LINK_LIBRARY_USING_", feature, "_SUPPORTED");
  155. if (makefile->GetDefinition(featureSupported).IsOn()) {
  156. return true;
  157. }
  158. featureSupported =
  159. cmStrCat("CMAKE_LINK_LIBRARY_USING_", feature, "_SUPPORTED");
  160. return makefile->GetDefinition(featureSupported).IsOn();
  161. }
  162. // LINK_LIBRARY feature attributes management
  163. struct LinkLibraryFeatureAttributeSet
  164. {
  165. std::set<cmStateEnums::TargetType> LibraryTypes = {
  166. cmStateEnums::EXECUTABLE, cmStateEnums::STATIC_LIBRARY,
  167. cmStateEnums::SHARED_LIBRARY, cmStateEnums::MODULE_LIBRARY,
  168. cmStateEnums::UNKNOWN_LIBRARY
  169. };
  170. std::set<std::string> Override;
  171. enum DeduplicationKind
  172. {
  173. Default,
  174. Yes,
  175. No
  176. };
  177. DeduplicationKind Deduplication = Default;
  178. };
  179. std::map<std::string, LinkLibraryFeatureAttributeSet>
  180. LinkLibraryFeatureAttributes;
  181. const LinkLibraryFeatureAttributeSet& GetLinkLibraryFeatureAttributes(
  182. cmMakefile* makefile, std::string const& linkLanguage,
  183. const std::string& feature)
  184. {
  185. auto it = LinkLibraryFeatureAttributes.find(feature);
  186. if (it != LinkLibraryFeatureAttributes.end()) {
  187. return it->second;
  188. }
  189. auto featureAttributesVariable =
  190. cmStrCat("CMAKE_", linkLanguage, "_LINK_LIBRARY_", feature, "_ATTRIBUTES");
  191. auto featureAttributesValues =
  192. makefile->GetDefinition(featureAttributesVariable);
  193. if (featureAttributesValues.IsEmpty()) {
  194. // try language agnostic definition
  195. featureAttributesVariable =
  196. cmStrCat("CMAKE_LINK_LIBRARY_", feature, "_ATTRIBUTES");
  197. featureAttributesValues =
  198. makefile->GetDefinition(featureAttributesVariable);
  199. }
  200. if (!featureAttributesValues.IsEmpty()) {
  201. LinkLibraryFeatureAttributeSet featureAttributes;
  202. cmsys::RegularExpression processingOption{
  203. "^(LIBRARY_TYPE|DEDUPLICATION|OVERRIDE)=((STATIC|SHARED|MODULE|"
  204. "EXECUTABLE)(,("
  205. "STATIC|"
  206. "SHARED|MODULE|EXECUTABLE)"
  207. ")*|YES|NO|DEFAULT|[A-Za-z0-9_]+(,[A-Za-z0-9_]+)*)$"
  208. };
  209. std::string errorMessage;
  210. for (auto const& option : cmList{ featureAttributesValues }) {
  211. if (processingOption.find(option)) {
  212. if (processingOption.match(1) == "LIBRARY_TYPE") {
  213. featureAttributes.LibraryTypes.clear();
  214. for (auto const& value :
  215. cmTokenize(processingOption.match(2), ',')) {
  216. if (value == "STATIC") {
  217. featureAttributes.LibraryTypes.emplace(
  218. cmStateEnums::STATIC_LIBRARY);
  219. } else if (value == "SHARED") {
  220. featureAttributes.LibraryTypes.emplace(
  221. cmStateEnums::SHARED_LIBRARY);
  222. } else if (value == "MODULE") {
  223. featureAttributes.LibraryTypes.emplace(
  224. cmStateEnums::MODULE_LIBRARY);
  225. } else if (value == "EXECUTABLE") {
  226. featureAttributes.LibraryTypes.emplace(cmStateEnums::EXECUTABLE);
  227. } else {
  228. errorMessage += cmStrCat(" ", option, '\n');
  229. break;
  230. }
  231. }
  232. // Always add UNKNOWN type
  233. featureAttributes.LibraryTypes.emplace(
  234. cmStateEnums::UNKNOWN_LIBRARY);
  235. } else if (processingOption.match(1) == "DEDUPLICATION") {
  236. if (processingOption.match(2) == "YES") {
  237. featureAttributes.Deduplication =
  238. LinkLibraryFeatureAttributeSet::Yes;
  239. } else if (processingOption.match(2) == "NO") {
  240. featureAttributes.Deduplication =
  241. LinkLibraryFeatureAttributeSet::No;
  242. } else if (processingOption.match(2) == "DEFAULT") {
  243. featureAttributes.Deduplication =
  244. LinkLibraryFeatureAttributeSet::Default;
  245. } else {
  246. errorMessage += cmStrCat(" ", option, '\n');
  247. }
  248. } else if (processingOption.match(1) == "OVERRIDE") {
  249. featureAttributes.Override.clear();
  250. std::vector<std::string> values =
  251. cmTokenize(processingOption.match(2), ',');
  252. featureAttributes.Override.insert(values.begin(), values.end());
  253. }
  254. } else {
  255. errorMessage += cmStrCat(" ", option, '\n');
  256. }
  257. }
  258. if (!errorMessage.empty()) {
  259. makefile->GetCMakeInstance()->IssueMessage(
  260. MessageType::FATAL_ERROR,
  261. cmStrCat("Erroneous option(s) for '", featureAttributesVariable,
  262. "':\n", errorMessage));
  263. }
  264. return LinkLibraryFeatureAttributes.emplace(feature, featureAttributes)
  265. .first->second;
  266. }
  267. return LinkLibraryFeatureAttributes
  268. .emplace(feature, LinkLibraryFeatureAttributeSet{})
  269. .first->second;
  270. }
  271. // LINK_GROUP helpers
  272. const cm::string_view LG_BEGIN = "<LINK_GROUP:"_s;
  273. const cm::string_view LG_END = "</LINK_GROUP:"_s;
  274. const cm::string_view LG_ITEM_BEGIN = "<LINK_GROUP>"_s;
  275. const cm::string_view LG_ITEM_END = "</LINK_GROUP>"_s;
  276. inline std::string ExtractGroupFeature(std::string const& item)
  277. {
  278. return item.substr(LG_BEGIN.length(),
  279. item.find(':', LG_BEGIN.length()) - LG_BEGIN.length());
  280. }
  281. bool IsGroupFeatureSupported(cmMakefile* makefile,
  282. std::string const& linkLanguage,
  283. std::string const& feature)
  284. {
  285. auto featureSupported = cmStrCat(
  286. "CMAKE_", linkLanguage, "_LINK_GROUP_USING_", feature, "_SUPPORTED");
  287. if (makefile->GetDefinition(featureSupported).IsOn()) {
  288. return true;
  289. }
  290. featureSupported =
  291. cmStrCat("CMAKE_LINK_GROUP_USING_", feature, "_SUPPORTED");
  292. return makefile->GetDefinition(featureSupported).IsOn();
  293. }
  294. class EntriesProcessing
  295. {
  296. public:
  297. using LinkEntry = cmComputeLinkDepends::LinkEntry;
  298. using EntryVector = cmComputeLinkDepends::EntryVector;
  299. EntriesProcessing(const cmGeneratorTarget* target,
  300. const std::string& linkLanguage, EntryVector& entries,
  301. EntryVector& finalEntries)
  302. : Target(target)
  303. , LinkLanguage(linkLanguage)
  304. , Entries(entries)
  305. , FinalEntries(finalEntries)
  306. {
  307. const auto* makefile = target->Makefile;
  308. switch (target->GetPolicyStatusCMP0156()) {
  309. case cmPolicies::WARN:
  310. if (!makefile->GetCMakeInstance()->GetIsInTryCompile() &&
  311. makefile->PolicyOptionalWarningEnabled(
  312. "CMAKE_POLICY_WARNING_CMP0156")) {
  313. makefile->GetCMakeInstance()->IssueMessage(
  314. MessageType::AUTHOR_WARNING,
  315. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0156),
  316. "\nSince the policy is not set, legacy libraries "
  317. "de-duplication strategy will be applied."),
  318. target->GetBacktrace());
  319. }
  320. CM_FALLTHROUGH;
  321. case cmPolicies::OLD:
  322. // rely on default initialization of the class
  323. break;
  324. case cmPolicies::REQUIRED_IF_USED:
  325. case cmPolicies::REQUIRED_ALWAYS:
  326. makefile->GetCMakeInstance()->IssueMessage(
  327. MessageType::FATAL_ERROR,
  328. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0156),
  329. target->GetBacktrace());
  330. CM_FALLTHROUGH;
  331. case cmPolicies::NEW: {
  332. // Policy 0179 applies only when policy 0156 is new
  333. switch (target->GetPolicyStatusCMP0179()) {
  334. case cmPolicies::WARN:
  335. if (!makefile->GetCMakeInstance()->GetIsInTryCompile() &&
  336. makefile->PolicyOptionalWarningEnabled(
  337. "CMAKE_POLICY_WARNING_CMP0179")) {
  338. makefile->GetCMakeInstance()->IssueMessage(
  339. MessageType::AUTHOR_WARNING,
  340. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0179),
  341. "\nSince the policy is not set, static libraries "
  342. "de-duplication will keep the last occurrence of the "
  343. "static libraries."),
  344. target->GetBacktrace());
  345. }
  346. CM_FALLTHROUGH;
  347. case cmPolicies::OLD:
  348. break;
  349. case cmPolicies::REQUIRED_IF_USED:
  350. case cmPolicies::REQUIRED_ALWAYS:
  351. makefile->GetCMakeInstance()->IssueMessage(
  352. MessageType::FATAL_ERROR,
  353. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0179),
  354. target->GetBacktrace());
  355. CM_FALLTHROUGH;
  356. case cmPolicies::NEW:
  357. break;
  358. }
  359. if (auto libProcessing = makefile->GetDefinition(cmStrCat(
  360. "CMAKE_", linkLanguage, "_LINK_LIBRARIES_PROCESSING"))) {
  361. // UNICITY keyword is just for compatibility with previous
  362. // implementation
  363. cmsys::RegularExpression processingOption{
  364. "^(ORDER|UNICITY|DEDUPLICATION)=(FORWARD|REVERSE|ALL|NONE|SHARED)$"
  365. };
  366. std::string errorMessage;
  367. for (auto const& option : cmList{ libProcessing }) {
  368. if (processingOption.find(option)) {
  369. if (processingOption.match(1) == "ORDER") {
  370. if (processingOption.match(2) == "FORWARD") {
  371. this->Order = Forward;
  372. } else if (processingOption.match(2) == "REVERSE") {
  373. this->Order = Reverse;
  374. } else {
  375. errorMessage += cmStrCat(" ", option, '\n');
  376. }
  377. } else if (processingOption.match(1) == "UNICITY" ||
  378. processingOption.match(1) == "DEDUPLICATION") {
  379. if (processingOption.match(2) == "ALL") {
  380. this->Deduplication = All;
  381. } else if (processingOption.match(2) == "NONE") {
  382. this->Deduplication = None;
  383. } else if (processingOption.match(2) == "SHARED") {
  384. this->Deduplication = Shared;
  385. } else {
  386. errorMessage += cmStrCat(" ", option, '\n');
  387. }
  388. }
  389. } else {
  390. errorMessage += cmStrCat(" ", option, '\n');
  391. }
  392. }
  393. if (!errorMessage.empty()) {
  394. makefile->GetCMakeInstance()->IssueMessage(
  395. MessageType::FATAL_ERROR,
  396. cmStrCat("Erroneous option(s) for 'CMAKE_", linkLanguage,
  397. "_LINK_LIBRARIES_PROCESSING':\n", errorMessage),
  398. target->GetBacktrace());
  399. }
  400. }
  401. }
  402. }
  403. }
  404. void AddGroups(const std::map<size_t, std::vector<size_t>>& groups)
  405. {
  406. if (!groups.empty()) {
  407. this->Groups = &groups;
  408. // record all libraries as part of groups to ensure correct
  409. // deduplication: libraries as part of groups are always kept.
  410. for (const auto& g : groups) {
  411. for (auto index : g.second) {
  412. this->Emitted.insert(index);
  413. }
  414. }
  415. }
  416. }
  417. void AddLibraries(const std::vector<size_t>& libEntries)
  418. {
  419. if (this->Order == Reverse) {
  420. std::vector<size_t> entries;
  421. if (this->Deduplication == All &&
  422. this->Target->GetPolicyStatusCMP0179() == cmPolicies::NEW) {
  423. // keep the first occurrence of the static libraries
  424. std::set<size_t> emitted{ this->Emitted };
  425. for (auto index : libEntries) {
  426. LinkEntry const& entry = this->Entries[index];
  427. if (!entry.Target ||
  428. entry.Target->GetType() != cmStateEnums::STATIC_LIBRARY) {
  429. entries.emplace_back(index);
  430. continue;
  431. }
  432. if (this->IncludeEntry(entry) || emitted.insert(index).second) {
  433. entries.emplace_back(index);
  434. }
  435. }
  436. } else {
  437. entries = libEntries;
  438. }
  439. // Iterate in reverse order so we can keep only the last occurrence
  440. // of the shared libraries.
  441. this->AddLibraries(cmReverseRange(entries));
  442. } else {
  443. this->AddLibraries(cmMakeRange(libEntries));
  444. }
  445. }
  446. void AddObjects(const std::vector<size_t>& objectEntries)
  447. {
  448. // Place explicitly linked object files in the front. The linker will
  449. // always use them anyway, and they may depend on symbols from libraries.
  450. if (this->Order == Reverse) {
  451. // Append in reverse order at the end since we reverse the final order.
  452. for (auto index : cmReverseRange(objectEntries)) {
  453. this->FinalEntries.emplace_back(this->Entries[index]);
  454. }
  455. } else {
  456. // Append in reverse order to ensure correct final order
  457. for (auto index : cmReverseRange(objectEntries)) {
  458. this->FinalEntries.emplace(this->FinalEntries.begin(),
  459. this->Entries[index]);
  460. }
  461. }
  462. }
  463. void Finalize()
  464. {
  465. if (this->Order == Reverse) {
  466. // Reverse the resulting order since we iterated in reverse.
  467. std::reverse(this->FinalEntries.begin(), this->FinalEntries.end());
  468. }
  469. // expand groups
  470. if (this->Groups) {
  471. for (const auto& g : *this->Groups) {
  472. const LinkEntry& groupEntry = this->Entries[g.first];
  473. auto it = this->FinalEntries.begin();
  474. while (true) {
  475. it = std::find_if(it, this->FinalEntries.end(),
  476. [&groupEntry](const LinkEntry& entry) -> bool {
  477. return groupEntry.Item == entry.Item;
  478. });
  479. if (it == this->FinalEntries.end()) {
  480. break;
  481. }
  482. it->Item.Value = std::string(LG_ITEM_END);
  483. for (auto index = g.second.rbegin(); index != g.second.rend();
  484. ++index) {
  485. it = this->FinalEntries.insert(it, this->Entries[*index]);
  486. }
  487. it = this->FinalEntries.insert(it, groupEntry);
  488. it->Item.Value = std::string(LG_ITEM_BEGIN);
  489. }
  490. }
  491. }
  492. }
  493. private:
  494. enum OrderKind
  495. {
  496. Forward,
  497. Reverse
  498. };
  499. enum DeduplicationKind
  500. {
  501. None,
  502. Shared,
  503. All
  504. };
  505. bool IncludeEntry(LinkEntry const& entry) const
  506. {
  507. if (entry.Feature != cmComputeLinkDepends::LinkEntry::DEFAULT) {
  508. auto const& featureAttributes = GetLinkLibraryFeatureAttributes(
  509. this->Target->Makefile, this->LinkLanguage, entry.Feature);
  510. if ((!entry.Target ||
  511. featureAttributes.LibraryTypes.find(entry.Target->GetType()) !=
  512. featureAttributes.LibraryTypes.end()) &&
  513. featureAttributes.Deduplication !=
  514. LinkLibraryFeatureAttributeSet::Default) {
  515. return featureAttributes.Deduplication ==
  516. LinkLibraryFeatureAttributeSet::No;
  517. }
  518. }
  519. return this->Deduplication == None ||
  520. (this->Deduplication == Shared &&
  521. (!entry.Target ||
  522. entry.Target->GetType() != cmStateEnums::SHARED_LIBRARY)) ||
  523. (this->Deduplication == All && entry.Kind != LinkEntry::Library);
  524. }
  525. template <typename Range>
  526. void AddLibraries(const Range& libEntries)
  527. {
  528. for (auto index : libEntries) {
  529. LinkEntry const& entry = this->Entries[index];
  530. if (this->IncludeEntry(entry) || this->Emitted.insert(index).second) {
  531. this->FinalEntries.emplace_back(entry);
  532. }
  533. }
  534. }
  535. OrderKind Order = Reverse;
  536. DeduplicationKind Deduplication = Shared;
  537. const cmGeneratorTarget* Target;
  538. const std::string& LinkLanguage;
  539. EntryVector& Entries;
  540. EntryVector& FinalEntries;
  541. std::set<size_t> Emitted;
  542. const std::map<size_t, std::vector<size_t>>* Groups = nullptr;
  543. };
  544. }
  545. std::string const& cmComputeLinkDepends::LinkEntry::DEFAULT =
  546. cmLinkItem::DEFAULT;
  547. cmComputeLinkDepends::cmComputeLinkDepends(const cmGeneratorTarget* target,
  548. const std::string& config,
  549. const std::string& linkLanguage,
  550. LinkLibrariesStrategy strategy)
  551. : Target(target)
  552. , Makefile(this->Target->Target->GetMakefile())
  553. , GlobalGenerator(this->Target->GetLocalGenerator()->GetGlobalGenerator())
  554. , CMakeInstance(this->GlobalGenerator->GetCMakeInstance())
  555. , Config(config)
  556. , DebugMode(this->Makefile->IsOn("CMAKE_LINK_DEPENDS_DEBUG_MODE") ||
  557. this->Target->GetProperty("LINK_DEPENDS_DEBUG_MODE").IsOn())
  558. , LinkLanguage(linkLanguage)
  559. , LinkType(CMP0003_ComputeLinkType(
  560. this->Config, this->Makefile->GetCMakeInstance()->GetDebugConfigs()))
  561. , Strategy(strategy)
  562. {
  563. // target oriented feature override property takes precedence over
  564. // global override property
  565. cm::string_view lloPrefix = "LINK_LIBRARY_OVERRIDE_"_s;
  566. auto const& keys = this->Target->GetPropertyKeys();
  567. std::for_each(
  568. keys.cbegin(), keys.cend(),
  569. [this, &lloPrefix, &config, &linkLanguage](std::string const& key) {
  570. if (cmHasPrefix(key, lloPrefix)) {
  571. if (cmValue feature = this->Target->GetProperty(key)) {
  572. if (!feature->empty() && key.length() > lloPrefix.length()) {
  573. auto item = key.substr(lloPrefix.length());
  574. cmGeneratorExpressionDAGChecker dag{
  575. this->Target->GetBacktrace(),
  576. this->Target,
  577. "LINK_LIBRARY_OVERRIDE",
  578. nullptr,
  579. nullptr,
  580. this->Target->GetLocalGenerator(),
  581. config
  582. };
  583. auto overrideFeature = cmGeneratorExpression::Evaluate(
  584. *feature, this->Target->GetLocalGenerator(), config,
  585. this->Target, &dag, this->Target, linkLanguage);
  586. this->LinkLibraryOverride.emplace(item, overrideFeature);
  587. }
  588. }
  589. }
  590. });
  591. // global override property
  592. if (cmValue linkLibraryOverride =
  593. this->Target->GetProperty("LINK_LIBRARY_OVERRIDE")) {
  594. cmGeneratorExpressionDAGChecker dag{ target->GetBacktrace(),
  595. target,
  596. "LINK_LIBRARY_OVERRIDE",
  597. nullptr,
  598. nullptr,
  599. target->GetLocalGenerator(),
  600. config };
  601. auto overrideValue = cmGeneratorExpression::Evaluate(
  602. *linkLibraryOverride, target->GetLocalGenerator(), config, target, &dag,
  603. target, linkLanguage);
  604. std::vector<std::string> overrideList =
  605. cmTokenize(overrideValue, ',', cmTokenizerMode::New);
  606. if (overrideList.size() >= 2) {
  607. auto const& feature = overrideList.front();
  608. std::for_each(overrideList.cbegin() + 1, overrideList.cend(),
  609. [this, &feature](std::string const& item) {
  610. this->LinkLibraryOverride.emplace(item, feature);
  611. });
  612. }
  613. }
  614. }
  615. cmComputeLinkDepends::~cmComputeLinkDepends() = default;
  616. void cmComputeLinkDepends::SetOldLinkDirMode(bool b)
  617. {
  618. this->OldLinkDirMode = b;
  619. }
  620. std::vector<cmComputeLinkDepends::LinkEntry> const&
  621. cmComputeLinkDepends::Compute()
  622. {
  623. // Follow the link dependencies of the target to be linked.
  624. this->AddDirectLinkEntries();
  625. // Complete the breadth-first search of dependencies.
  626. while (!this->BFSQueue.empty()) {
  627. // Get the next entry.
  628. BFSEntry qe = this->BFSQueue.front();
  629. this->BFSQueue.pop();
  630. // Follow the entry's dependencies.
  631. this->FollowLinkEntry(qe);
  632. }
  633. // Complete the search of shared library dependencies.
  634. while (!this->SharedDepQueue.empty()) {
  635. // Handle the next entry.
  636. this->HandleSharedDependency(this->SharedDepQueue.front());
  637. this->SharedDepQueue.pop();
  638. }
  639. // Infer dependencies of targets for which they were not known.
  640. this->InferDependencies();
  641. // finalize groups dependencies
  642. // All dependencies which are raw items must be replaced by the group
  643. // it belongs to, if any.
  644. this->UpdateGroupDependencies();
  645. // Cleanup the constraint graph.
  646. this->CleanConstraintGraph();
  647. // Display the constraint graph.
  648. if (this->DebugMode) {
  649. fprintf(stderr,
  650. "---------------------------------------"
  651. "---------------------------------------\n");
  652. fprintf(stderr, "Link dependency analysis for target %s, config %s\n",
  653. this->Target->GetName().c_str(),
  654. this->Config.empty() ? "noconfig" : this->Config.c_str());
  655. this->DisplayConstraintGraph();
  656. }
  657. // Compute the DAG of strongly connected components. The algorithm
  658. // used by cmComputeComponentGraph should identify the components in
  659. // the same order in which the items were originally discovered in
  660. // the BFS. This should preserve the original order when no
  661. // constraints disallow it.
  662. this->CCG =
  663. cm::make_unique<cmComputeComponentGraph>(this->EntryConstraintGraph);
  664. this->CCG->Compute();
  665. if (!this->CheckCircularDependencies()) {
  666. return this->FinalLinkEntries;
  667. }
  668. // Compute the final ordering.
  669. this->OrderLinkEntries();
  670. // Display the final ordering.
  671. if (this->DebugMode) {
  672. this->DisplayOrderedEntries();
  673. }
  674. // Compute the final set of link entries.
  675. EntriesProcessing entriesProcessing{ this->Target, this->LinkLanguage,
  676. this->EntryList,
  677. this->FinalLinkEntries };
  678. // Add groups first, to ensure that libraries of the groups are always kept.
  679. entriesProcessing.AddGroups(this->GroupItems);
  680. entriesProcessing.AddLibraries(this->FinalLinkOrder);
  681. entriesProcessing.AddObjects(this->ObjectEntries);
  682. entriesProcessing.Finalize();
  683. // Display the final set.
  684. if (this->DebugMode) {
  685. this->DisplayFinalEntries();
  686. }
  687. return this->FinalLinkEntries;
  688. }
  689. std::string const& cmComputeLinkDepends::GetCurrentFeature(
  690. std::string const& item, std::string const& defaultFeature) const
  691. {
  692. auto it = this->LinkLibraryOverride.find(item);
  693. return it == this->LinkLibraryOverride.end() ? defaultFeature : it->second;
  694. }
  695. std::pair<std::map<cmLinkItem, size_t>::iterator, bool>
  696. cmComputeLinkDepends::AllocateLinkEntry(cmLinkItem const& item)
  697. {
  698. std::map<cmLinkItem, size_t>::value_type index_entry(
  699. item, static_cast<size_t>(this->EntryList.size()));
  700. auto lei = this->LinkEntryIndex.insert(index_entry);
  701. if (lei.second) {
  702. this->EntryList.emplace_back();
  703. this->InferredDependSets.emplace_back();
  704. this->EntryConstraintGraph.emplace_back();
  705. }
  706. return lei;
  707. }
  708. std::pair<size_t, bool> cmComputeLinkDepends::AddLinkEntry(
  709. cmLinkItem const& item, cm::optional<size_t> const& groupIndex)
  710. {
  711. // Allocate a spot for the item entry.
  712. auto lei = this->AllocateLinkEntry(item);
  713. // Check if the item entry has already been added.
  714. if (!lei.second) {
  715. // Yes. We do not need to follow the item's dependencies again.
  716. return { lei.first->second, false };
  717. }
  718. // Initialize the item entry.
  719. size_t index = lei.first->second;
  720. LinkEntry& entry = this->EntryList[index];
  721. entry.Item = BT<std::string>(item.AsStr(), item.Backtrace);
  722. entry.Target = item.Target;
  723. entry.Feature = item.Feature;
  724. if (!entry.Target && entry.Item.Value[0] == '-' &&
  725. entry.Item.Value[1] != 'l' &&
  726. entry.Item.Value.substr(0, 10) != "-framework") {
  727. entry.Kind = LinkEntry::Flag;
  728. entry.Feature = LinkEntry::DEFAULT;
  729. } else if (cmHasPrefix(entry.Item.Value, LG_BEGIN) &&
  730. cmHasSuffix(entry.Item.Value, '>')) {
  731. entry.Kind = LinkEntry::Group;
  732. }
  733. if (entry.Kind != LinkEntry::Group) {
  734. // If the item has dependencies queue it to follow them.
  735. if (entry.Target) {
  736. // Target dependencies are always known. Follow them.
  737. BFSEntry qe = { index, groupIndex, nullptr };
  738. this->BFSQueue.push(qe);
  739. } else {
  740. // Look for an old-style <item>_LIB_DEPENDS variable.
  741. std::string var = cmStrCat(entry.Item.Value, "_LIB_DEPENDS");
  742. if (cmValue val = this->Makefile->GetDefinition(var)) {
  743. // The item dependencies are known. Follow them.
  744. BFSEntry qe = { index, groupIndex, val->c_str() };
  745. this->BFSQueue.push(qe);
  746. } else if (entry.Kind != LinkEntry::Flag) {
  747. // The item dependencies are not known. We need to infer them.
  748. this->InferredDependSets[index].Initialized = true;
  749. }
  750. }
  751. }
  752. return { index, true };
  753. }
  754. void cmComputeLinkDepends::AddLinkObject(cmLinkItem const& item)
  755. {
  756. assert(!item.Target); // The item is an object file, not its target.
  757. // Allocate a spot for the item entry.
  758. auto lei = this->AllocateLinkEntry(item);
  759. // Check if the item entry has already been added.
  760. if (!lei.second) {
  761. return;
  762. }
  763. // Initialize the item entry.
  764. size_t index = lei.first->second;
  765. LinkEntry& entry = this->EntryList[index];
  766. entry.Item = BT<std::string>(item.AsStr(), item.Backtrace);
  767. entry.Kind = LinkEntry::Object;
  768. entry.ObjectSource = item.ObjectSource;
  769. // Record explicitly linked object files separately.
  770. this->ObjectEntries.emplace_back(index);
  771. }
  772. void cmComputeLinkDepends::FollowLinkEntry(BFSEntry qe)
  773. {
  774. // Get this entry representation.
  775. size_t depender_index = qe.GroupIndex ? *qe.GroupIndex : qe.Index;
  776. LinkEntry const& entry = this->EntryList[qe.Index];
  777. // Follow the item's dependencies.
  778. if (entry.Target) {
  779. // Follow the target dependencies.
  780. if (cmLinkInterface const* iface =
  781. entry.Target->GetLinkInterface(this->Config, this->Target)) {
  782. const bool isIface =
  783. entry.Target->GetType() == cmStateEnums::INTERFACE_LIBRARY;
  784. // This target provides its own link interface information.
  785. this->AddLinkEntries(depender_index, iface->Libraries);
  786. this->AddLinkObjects(iface->Objects);
  787. for (auto const& language : iface->Languages) {
  788. auto runtimeEntries = iface->LanguageRuntimeLibraries.find(language);
  789. if (runtimeEntries != iface->LanguageRuntimeLibraries.end()) {
  790. this->AddLinkEntries(depender_index, runtimeEntries->second);
  791. }
  792. }
  793. if (isIface) {
  794. return;
  795. }
  796. // Handle dependent shared libraries.
  797. this->FollowSharedDeps(depender_index, iface);
  798. // Support for CMP0003.
  799. for (cmLinkItem const& oi : iface->WrongConfigLibraries) {
  800. this->CheckWrongConfigItem(oi);
  801. }
  802. }
  803. } else {
  804. // Follow the old-style dependency list.
  805. this->AddVarLinkEntries(depender_index, qe.LibDepends);
  806. }
  807. }
  808. void cmComputeLinkDepends::FollowSharedDeps(size_t depender_index,
  809. cmLinkInterface const* iface,
  810. bool follow_interface)
  811. {
  812. // Follow dependencies if we have not followed them already.
  813. if (this->SharedDepFollowed.insert(depender_index).second) {
  814. if (follow_interface) {
  815. this->QueueSharedDependencies(depender_index, iface->Libraries);
  816. }
  817. this->QueueSharedDependencies(depender_index, iface->SharedDeps);
  818. }
  819. }
  820. void cmComputeLinkDepends::QueueSharedDependencies(
  821. size_t depender_index, std::vector<cmLinkItem> const& deps)
  822. {
  823. for (cmLinkItem const& li : deps) {
  824. SharedDepEntry qe;
  825. qe.Item = li;
  826. qe.DependerIndex = depender_index;
  827. this->SharedDepQueue.push(qe);
  828. }
  829. }
  830. void cmComputeLinkDepends::HandleSharedDependency(SharedDepEntry const& dep)
  831. {
  832. // Allocate a spot for the item entry.
  833. auto lei = this->AllocateLinkEntry(dep.Item);
  834. size_t index = lei.first->second;
  835. // Check if the target does not already has an entry.
  836. if (lei.second) {
  837. // Initialize the item entry.
  838. LinkEntry& entry = this->EntryList[index];
  839. entry.Item = BT<std::string>(dep.Item.AsStr(), dep.Item.Backtrace);
  840. entry.Target = dep.Item.Target;
  841. // This item was added specifically because it is a dependent
  842. // shared library. It may get special treatment
  843. // in cmComputeLinkInformation.
  844. entry.Kind = LinkEntry::SharedDep;
  845. }
  846. // Get the link entry for this target.
  847. LinkEntry& entry = this->EntryList[index];
  848. // This shared library dependency must follow the item that listed
  849. // it.
  850. this->EntryConstraintGraph[dep.DependerIndex].emplace_back(
  851. index, true, false, cmListFileBacktrace());
  852. // Target items may have their own dependencies.
  853. if (entry.Target) {
  854. if (cmLinkInterface const* iface =
  855. entry.Target->GetLinkInterface(this->Config, this->Target)) {
  856. // Follow public and private dependencies transitively.
  857. this->FollowSharedDeps(index, iface, true);
  858. }
  859. }
  860. }
  861. void cmComputeLinkDepends::AddVarLinkEntries(
  862. cm::optional<size_t> const& depender_index, const char* value)
  863. {
  864. // This is called to add the dependencies named by
  865. // <item>_LIB_DEPENDS. The variable contains a semicolon-separated
  866. // list. The list contains link-type;item pairs and just items.
  867. cmList deplist{ value };
  868. // Look for entries meant for this configuration.
  869. std::vector<cmLinkItem> actual_libs;
  870. cmTargetLinkLibraryType llt = GENERAL_LibraryType;
  871. bool haveLLT = false;
  872. for (std::string const& d : deplist) {
  873. if (d == "debug") {
  874. llt = DEBUG_LibraryType;
  875. haveLLT = true;
  876. } else if (d == "optimized") {
  877. llt = OPTIMIZED_LibraryType;
  878. haveLLT = true;
  879. } else if (d == "general") {
  880. llt = GENERAL_LibraryType;
  881. haveLLT = true;
  882. } else if (!d.empty()) {
  883. // If no explicit link type was given prior to this entry then
  884. // check if the entry has its own link type variable. This is
  885. // needed for compatibility with dependency files generated by
  886. // the export_library_dependencies command from CMake 2.4 and
  887. // lower.
  888. if (!haveLLT) {
  889. std::string var = cmStrCat(d, "_LINK_TYPE");
  890. if (cmValue val = this->Makefile->GetDefinition(var)) {
  891. if (*val == "debug") {
  892. llt = DEBUG_LibraryType;
  893. } else if (*val == "optimized") {
  894. llt = OPTIMIZED_LibraryType;
  895. }
  896. }
  897. }
  898. // If the library is meant for this link type then use it.
  899. if (llt == GENERAL_LibraryType || llt == this->LinkType) {
  900. actual_libs.emplace_back(this->ResolveLinkItem(depender_index, d));
  901. } else if (this->OldLinkDirMode) {
  902. cmLinkItem item = this->ResolveLinkItem(depender_index, d);
  903. this->CheckWrongConfigItem(item);
  904. }
  905. // Reset the link type until another explicit type is given.
  906. llt = GENERAL_LibraryType;
  907. haveLLT = false;
  908. }
  909. }
  910. // Add the entries from this list.
  911. this->AddLinkEntries(depender_index, actual_libs);
  912. }
  913. void cmComputeLinkDepends::AddDirectLinkEntries()
  914. {
  915. // Add direct link dependencies in this configuration.
  916. cmLinkImplementation const* impl = this->Target->GetLinkImplementation(
  917. this->Config, cmGeneratorTarget::UseTo::Link);
  918. this->AddLinkEntries(cm::nullopt, impl->Libraries);
  919. this->AddLinkObjects(impl->Objects);
  920. for (auto const& language : impl->Languages) {
  921. auto runtimeEntries = impl->LanguageRuntimeLibraries.find(language);
  922. if (runtimeEntries != impl->LanguageRuntimeLibraries.end()) {
  923. this->AddLinkEntries(cm::nullopt, runtimeEntries->second);
  924. }
  925. }
  926. for (cmLinkItem const& wi : impl->WrongConfigLibraries) {
  927. this->CheckWrongConfigItem(wi);
  928. }
  929. }
  930. template <typename T>
  931. void cmComputeLinkDepends::AddLinkEntries(
  932. cm::optional<size_t> const& depender_index, std::vector<T> const& libs)
  933. {
  934. // Track inferred dependency sets implied by this list.
  935. std::map<size_t, DependSet> dependSets;
  936. cm::optional<std::pair<size_t, bool>> group;
  937. std::vector<size_t> groupItems;
  938. // Loop over the libraries linked directly by the depender.
  939. for (T const& l : libs) {
  940. // Skip entries that will resolve to the target getting linked or
  941. // are empty.
  942. cmLinkItem const& item = l;
  943. if (item.AsStr() == this->Target->GetName() || item.AsStr().empty()) {
  944. continue;
  945. }
  946. // emit a warning if an undefined feature is used as part of
  947. // an imported target
  948. if (item.Feature != LinkEntry::DEFAULT && depender_index) {
  949. const auto& depender = this->EntryList[*depender_index];
  950. if (depender.Target && depender.Target->IsImported() &&
  951. !IsFeatureSupported(this->Makefile, this->LinkLanguage,
  952. item.Feature)) {
  953. this->CMakeInstance->IssueMessage(
  954. MessageType::AUTHOR_ERROR,
  955. cmStrCat("The 'IMPORTED' target '", depender.Target->GetName(),
  956. "' uses the generator-expression '$<LINK_LIBRARY>' with "
  957. "the feature '",
  958. item.Feature,
  959. "', which is undefined or unsupported.\nDid you miss to "
  960. "define it by setting variables \"CMAKE_",
  961. this->LinkLanguage, "_LINK_LIBRARY_USING_", item.Feature,
  962. "\" and \"CMAKE_", this->LinkLanguage,
  963. "_LINK_LIBRARY_USING_", item.Feature, "_SUPPORTED\"?"),
  964. this->Target->GetBacktrace());
  965. }
  966. }
  967. if (cmHasPrefix(item.AsStr(), LG_BEGIN) &&
  968. cmHasSuffix(item.AsStr(), '>')) {
  969. group = this->AddLinkEntry(item, cm::nullopt);
  970. if (group->second) {
  971. LinkEntry& entry = this->EntryList[group->first];
  972. entry.Feature = ExtractGroupFeature(item.AsStr());
  973. }
  974. if (depender_index) {
  975. this->EntryConstraintGraph[*depender_index].emplace_back(
  976. group->first, false, false, cmListFileBacktrace());
  977. } else {
  978. // This is a direct dependency of the target being linked.
  979. this->OriginalEntries.push_back(group->first);
  980. }
  981. continue;
  982. }
  983. size_t dependee_index;
  984. if (cmHasPrefix(item.AsStr(), LG_END) && cmHasSuffix(item.AsStr(), '>')) {
  985. assert(group);
  986. dependee_index = group->first;
  987. if (group->second) {
  988. this->GroupItems.emplace(group->first, std::move(groupItems));
  989. }
  990. group = cm::nullopt;
  991. groupItems.clear();
  992. continue;
  993. }
  994. if (depender_index && group) {
  995. const auto& depender = this->EntryList[*depender_index];
  996. const auto& groupFeature = this->EntryList[group->first].Feature;
  997. if (depender.Target && depender.Target->IsImported() &&
  998. !IsGroupFeatureSupported(this->Makefile, this->LinkLanguage,
  999. groupFeature)) {
  1000. this->CMakeInstance->IssueMessage(
  1001. MessageType::AUTHOR_ERROR,
  1002. cmStrCat("The 'IMPORTED' target '", depender.Target->GetName(),
  1003. "' uses the generator-expression '$<LINK_GROUP>' with "
  1004. "the feature '",
  1005. groupFeature,
  1006. "', which is undefined or unsupported.\nDid you miss to "
  1007. "define it by setting variables \"CMAKE_",
  1008. this->LinkLanguage, "_LINK_GROUP_USING_", groupFeature,
  1009. "\" and \"CMAKE_", this->LinkLanguage, "_LINK_GROUP_USING_",
  1010. groupFeature, "_SUPPORTED\"?"),
  1011. this->Target->GetBacktrace());
  1012. }
  1013. }
  1014. // Add a link entry for this item.
  1015. auto ale = this->AddLinkEntry(
  1016. item, group ? cm::optional<size_t>(group->first) : cm::nullopt);
  1017. dependee_index = ale.first;
  1018. LinkEntry& entry = this->EntryList[dependee_index];
  1019. bool supportedItem = true;
  1020. auto const& itemFeature =
  1021. this->GetCurrentFeature(entry.Item.Value, item.Feature);
  1022. if (group && ale.second && entry.Target &&
  1023. (entry.Target->GetType() == cmStateEnums::TargetType::OBJECT_LIBRARY ||
  1024. entry.Target->GetType() ==
  1025. cmStateEnums::TargetType::INTERFACE_LIBRARY)) {
  1026. supportedItem = false;
  1027. const auto& groupFeature = this->EntryList[group->first].Feature;
  1028. this->CMakeInstance->IssueMessage(
  1029. MessageType::AUTHOR_WARNING,
  1030. cmStrCat(
  1031. "The feature '", groupFeature,
  1032. "', specified as part of a generator-expression "
  1033. "'$",
  1034. LG_BEGIN, groupFeature, ">', will not be applied to the ",
  1035. (entry.Target->GetType() == cmStateEnums::TargetType::OBJECT_LIBRARY
  1036. ? "OBJECT"
  1037. : "INTERFACE"),
  1038. " library '", entry.Item.Value, "'."),
  1039. this->Target->GetBacktrace());
  1040. }
  1041. // check if feature is applicable to this item
  1042. if (itemFeature != LinkEntry::DEFAULT && entry.Target) {
  1043. auto const& featureAttributes = GetLinkLibraryFeatureAttributes(
  1044. this->Makefile, this->LinkLanguage, itemFeature);
  1045. if (featureAttributes.LibraryTypes.find(entry.Target->GetType()) ==
  1046. featureAttributes.LibraryTypes.end()) {
  1047. supportedItem = false;
  1048. this->CMakeInstance->IssueMessage(
  1049. MessageType::AUTHOR_WARNING,
  1050. cmStrCat("The feature '", itemFeature,
  1051. "', specified as part of a generator-expression "
  1052. "'$<LINK_LIBRARY:",
  1053. itemFeature, ">', will not be applied to the ",
  1054. cmState::GetTargetTypeName(entry.Target->GetType()), " '",
  1055. entry.Item.Value, "'."),
  1056. this->Target->GetBacktrace());
  1057. }
  1058. }
  1059. if (ale.second) {
  1060. // current item not yet defined
  1061. entry.Feature = itemFeature;
  1062. if (!supportedItem) {
  1063. entry.Feature = LinkEntry::DEFAULT;
  1064. }
  1065. }
  1066. if (supportedItem) {
  1067. if (group) {
  1068. const auto& currentFeature = this->EntryList[group->first].Feature;
  1069. for (const auto& g : this->GroupItems) {
  1070. const auto& groupFeature = this->EntryList[g.first].Feature;
  1071. if (groupFeature == currentFeature) {
  1072. continue;
  1073. }
  1074. if (std::find(g.second.cbegin(), g.second.cend(), dependee_index) !=
  1075. g.second.cend()) {
  1076. this->CMakeInstance->IssueMessage(
  1077. MessageType::FATAL_ERROR,
  1078. cmStrCat("Impossible to link target '", this->Target->GetName(),
  1079. "' because the link item '", entry.Item.Value,
  1080. "', specified with the group feature '", currentFeature,
  1081. '\'', ", has already occurred with the feature '",
  1082. groupFeature, '\'', ", which is not allowed."),
  1083. this->Target->GetBacktrace());
  1084. continue;
  1085. }
  1086. }
  1087. }
  1088. if (entry.Feature != itemFeature) {
  1089. bool incompatibleFeatures = true;
  1090. // check if an override is possible
  1091. auto const& entryFeatureAttributes = GetLinkLibraryFeatureAttributes(
  1092. this->Makefile, this->LinkLanguage, entry.Feature);
  1093. auto const& itemFeatureAttributes = GetLinkLibraryFeatureAttributes(
  1094. this->Makefile, this->LinkLanguage, itemFeature);
  1095. if (itemFeatureAttributes.Override.find(entry.Feature) !=
  1096. itemFeatureAttributes.Override.end() &&
  1097. entryFeatureAttributes.Override.find(itemFeature) !=
  1098. entryFeatureAttributes.Override.end()) {
  1099. // features override each other
  1100. this->CMakeInstance->IssueMessage(
  1101. MessageType::FATAL_ERROR,
  1102. cmStrCat("Impossible to link target '", this->Target->GetName(),
  1103. "' because the link item '", entry.Item.Value,
  1104. "' is specified with the features '", itemFeature,
  1105. "' and '", entry.Feature, "'",
  1106. ", and both have an 'OVERRIDE' attribute that overrides "
  1107. "the other. Such cycles are not allowed."),
  1108. this->Target->GetBacktrace());
  1109. } else {
  1110. if (itemFeatureAttributes.Override.find(entry.Feature) !=
  1111. itemFeatureAttributes.Override.end()) {
  1112. entry.Feature = itemFeature;
  1113. incompatibleFeatures = false;
  1114. } else if (entryFeatureAttributes.Override.find(itemFeature) !=
  1115. entryFeatureAttributes.Override.end()) {
  1116. incompatibleFeatures = false;
  1117. }
  1118. if (incompatibleFeatures) {
  1119. // incompatibles features occurred
  1120. this->CMakeInstance->IssueMessage(
  1121. MessageType::FATAL_ERROR,
  1122. cmStrCat(
  1123. "Impossible to link target '", this->Target->GetName(),
  1124. "' because the link item '", entry.Item.Value, "', specified ",
  1125. (itemFeature == LinkEntry::DEFAULT
  1126. ? "without any feature or 'DEFAULT' feature"
  1127. : cmStrCat("with the feature '", itemFeature, '\'')),
  1128. ", has already occurred ",
  1129. (entry.Feature == LinkEntry::DEFAULT
  1130. ? "without any feature or 'DEFAULT' feature"
  1131. : cmStrCat("with the feature '", entry.Feature, '\'')),
  1132. ", which is not allowed."),
  1133. this->Target->GetBacktrace());
  1134. }
  1135. }
  1136. }
  1137. }
  1138. if (group) {
  1139. // store item index for dependencies handling
  1140. groupItems.push_back(dependee_index);
  1141. } else {
  1142. std::vector<size_t> indexes;
  1143. bool entryHandled = false;
  1144. // search any occurrence of the library in already defined groups
  1145. for (const auto& g : this->GroupItems) {
  1146. for (auto index : g.second) {
  1147. if (entry.Item.Value == this->EntryList[index].Item.Value) {
  1148. indexes.push_back(g.first);
  1149. entryHandled = true;
  1150. break;
  1151. }
  1152. }
  1153. }
  1154. if (!entryHandled) {
  1155. indexes.push_back(dependee_index);
  1156. }
  1157. for (auto index : indexes) {
  1158. // The dependee must come after the depender.
  1159. if (depender_index) {
  1160. this->EntryConstraintGraph[*depender_index].emplace_back(
  1161. index, false, false, cmListFileBacktrace());
  1162. } else {
  1163. // This is a direct dependency of the target being linked.
  1164. this->OriginalEntries.push_back(index);
  1165. }
  1166. // Update the inferred dependencies for earlier items.
  1167. for (auto& dependSet : dependSets) {
  1168. // Add this item to the inferred dependencies of other items.
  1169. // Target items are never inferred dependees because unknown
  1170. // items are outside libraries that should not be depending on
  1171. // targets.
  1172. if (!this->EntryList[index].Target &&
  1173. this->EntryList[index].Kind != LinkEntry::Flag &&
  1174. this->EntryList[index].Kind != LinkEntry::Group &&
  1175. dependee_index != dependSet.first) {
  1176. dependSet.second.insert(index);
  1177. }
  1178. }
  1179. // If this item needs to have dependencies inferred, do so.
  1180. if (this->InferredDependSets[index].Initialized) {
  1181. // Make sure an entry exists to hold the set for the item.
  1182. dependSets[index];
  1183. }
  1184. }
  1185. }
  1186. }
  1187. // Store the inferred dependency sets discovered for this list.
  1188. for (auto const& dependSet : dependSets) {
  1189. this->InferredDependSets[dependSet.first].push_back(dependSet.second);
  1190. }
  1191. }
  1192. void cmComputeLinkDepends::AddLinkObjects(std::vector<cmLinkItem> const& objs)
  1193. {
  1194. for (cmLinkItem const& obj : objs) {
  1195. this->AddLinkObject(obj);
  1196. }
  1197. }
  1198. cmLinkItem cmComputeLinkDepends::ResolveLinkItem(
  1199. cm::optional<size_t> const& depender_index, const std::string& name)
  1200. {
  1201. // Look for a target in the scope of the depender.
  1202. cmGeneratorTarget const* from = this->Target;
  1203. if (depender_index) {
  1204. if (cmGeneratorTarget const* depender =
  1205. this->EntryList[*depender_index].Target) {
  1206. from = depender;
  1207. }
  1208. }
  1209. return from->ResolveLinkItem(BT<std::string>(name));
  1210. }
  1211. void cmComputeLinkDepends::InferDependencies()
  1212. {
  1213. // The inferred dependency sets for each item list the possible
  1214. // dependencies. The intersection of the sets for one item form its
  1215. // inferred dependencies.
  1216. for (size_t depender_index = 0;
  1217. depender_index < this->InferredDependSets.size(); ++depender_index) {
  1218. // Skip items for which dependencies do not need to be inferred or
  1219. // for which the inferred dependency sets are empty.
  1220. DependSetList& sets = this->InferredDependSets[depender_index];
  1221. if (!sets.Initialized || sets.empty()) {
  1222. continue;
  1223. }
  1224. // Intersect the sets for this item.
  1225. DependSet common = sets.front();
  1226. for (DependSet const& i : cmMakeRange(sets).advance(1)) {
  1227. DependSet intersection;
  1228. std::set_intersection(common.begin(), common.end(), i.begin(), i.end(),
  1229. std::inserter(intersection, intersection.begin()));
  1230. common = intersection;
  1231. }
  1232. // Add the inferred dependencies to the graph.
  1233. cmGraphEdgeList& edges = this->EntryConstraintGraph[depender_index];
  1234. edges.reserve(edges.size() + common.size());
  1235. for (auto const& c : common) {
  1236. edges.emplace_back(c, true, false, cmListFileBacktrace());
  1237. }
  1238. }
  1239. }
  1240. void cmComputeLinkDepends::UpdateGroupDependencies()
  1241. {
  1242. if (this->GroupItems.empty()) {
  1243. return;
  1244. }
  1245. // Walks through all entries of the constraint graph to replace dependencies
  1246. // over raw items by the group it belongs to, if any.
  1247. for (auto& edgeList : this->EntryConstraintGraph) {
  1248. for (auto& edge : edgeList) {
  1249. size_t index = edge;
  1250. if (this->EntryList[index].Kind == LinkEntry::Group ||
  1251. this->EntryList[index].Kind == LinkEntry::Flag ||
  1252. this->EntryList[index].Kind == LinkEntry::Object) {
  1253. continue;
  1254. }
  1255. // search the item in the defined groups
  1256. for (const auto& groupItems : this->GroupItems) {
  1257. auto pos = std::find(groupItems.second.cbegin(),
  1258. groupItems.second.cend(), index);
  1259. if (pos != groupItems.second.cend()) {
  1260. // replace lib dependency by the group it belongs to
  1261. edge = cmGraphEdge{ groupItems.first, false, false,
  1262. cmListFileBacktrace() };
  1263. }
  1264. }
  1265. }
  1266. }
  1267. }
  1268. void cmComputeLinkDepends::CleanConstraintGraph()
  1269. {
  1270. for (cmGraphEdgeList& edgeList : this->EntryConstraintGraph) {
  1271. // Sort the outgoing edges for each graph node so that the
  1272. // original order will be preserved as much as possible.
  1273. std::sort(edgeList.begin(), edgeList.end());
  1274. // Make the edge list unique.
  1275. edgeList.erase(std::unique(edgeList.begin(), edgeList.end()),
  1276. edgeList.end());
  1277. }
  1278. }
  1279. bool cmComputeLinkDepends::CheckCircularDependencies() const
  1280. {
  1281. std::vector<NodeList> const& components = this->CCG->GetComponents();
  1282. size_t nc = components.size();
  1283. for (size_t c = 0; c < nc; ++c) {
  1284. // Get the current component.
  1285. NodeList const& nl = components[c];
  1286. // Skip trivial components.
  1287. if (nl.size() < 2) {
  1288. continue;
  1289. }
  1290. // no group must be evolved
  1291. bool cycleDetected = false;
  1292. for (size_t ni : nl) {
  1293. if (this->EntryList[ni].Kind == LinkEntry::Group) {
  1294. cycleDetected = true;
  1295. break;
  1296. }
  1297. }
  1298. if (!cycleDetected) {
  1299. continue;
  1300. }
  1301. // Construct the error message.
  1302. auto formatItem = [](LinkEntry const& entry) -> std::string {
  1303. if (entry.Kind == LinkEntry::Group) {
  1304. auto items =
  1305. entry.Item.Value.substr(entry.Item.Value.find(':', 12) + 1);
  1306. items.pop_back();
  1307. std::replace(items.begin(), items.end(), '|', ',');
  1308. return cmStrCat("group \"", ExtractGroupFeature(entry.Item.Value),
  1309. ":{", items, "}\"");
  1310. }
  1311. return cmStrCat('"', entry.Item.Value, '"');
  1312. };
  1313. std::ostringstream e;
  1314. e << "The inter-target dependency graph, for the target \""
  1315. << this->Target->GetName()
  1316. << "\", contains the following strongly connected component "
  1317. "(cycle):\n";
  1318. std::vector<size_t> const& cmap = this->CCG->GetComponentMap();
  1319. for (size_t i : nl) {
  1320. // Get the depender.
  1321. LinkEntry const& depender = this->EntryList[i];
  1322. // Describe the depender.
  1323. e << " " << formatItem(depender) << "\n";
  1324. // List its dependencies that are inside the component.
  1325. EdgeList const& el = this->EntryConstraintGraph[i];
  1326. for (cmGraphEdge const& ni : el) {
  1327. size_t j = ni;
  1328. if (cmap[j] == c) {
  1329. LinkEntry const& dependee = this->EntryList[j];
  1330. e << " depends on " << formatItem(dependee) << "\n";
  1331. }
  1332. }
  1333. }
  1334. this->CMakeInstance->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  1335. this->Target->GetBacktrace());
  1336. return false;
  1337. }
  1338. return true;
  1339. }
  1340. void cmComputeLinkDepends::DisplayConstraintGraph()
  1341. {
  1342. // Display the graph nodes and their edges.
  1343. std::ostringstream e;
  1344. for (size_t i = 0; i < this->EntryConstraintGraph.size(); ++i) {
  1345. EdgeList const& nl = this->EntryConstraintGraph[i];
  1346. e << "item " << i << " is [" << this->EntryList[i].Item << "]\n";
  1347. e << cmWrap(" item ", nl, " must follow it", "\n") << "\n";
  1348. }
  1349. fprintf(stderr, "%s\n", e.str().c_str());
  1350. }
  1351. void cmComputeLinkDepends::OrderLinkEntries()
  1352. {
  1353. // The component graph is guaranteed to be acyclic. Start a DFS
  1354. // from every entry to compute a topological order for the
  1355. // components.
  1356. Graph const& cgraph = this->CCG->GetComponentGraph();
  1357. size_t n = cgraph.size();
  1358. this->ComponentVisited.resize(cgraph.size(), 0);
  1359. this->ComponentOrder.resize(cgraph.size(), n);
  1360. this->ComponentOrderId = n;
  1361. // Run in reverse order so the topological order will preserve the
  1362. // original order where there are no constraints.
  1363. for (size_t c = n; c > 0; --c) {
  1364. this->VisitComponent(c - 1);
  1365. }
  1366. // Display the component graph.
  1367. if (this->DebugMode) {
  1368. this->DisplayComponents();
  1369. }
  1370. // Start with the original link line.
  1371. switch (this->Strategy) {
  1372. case LinkLibrariesStrategy::REORDER_MINIMALLY: {
  1373. // Emit the direct dependencies in their original order.
  1374. // This gives projects control over ordering.
  1375. for (size_t originalEntry : this->OriginalEntries) {
  1376. this->VisitEntry(originalEntry);
  1377. }
  1378. } break;
  1379. case LinkLibrariesStrategy::REORDER_FREELY: {
  1380. // Schedule the direct dependencies for emission in topo order.
  1381. // This may produce more efficient link lines.
  1382. for (size_t originalEntry : this->OriginalEntries) {
  1383. this->MakePendingComponent(
  1384. this->CCG->GetComponentMap()[originalEntry]);
  1385. }
  1386. } break;
  1387. }
  1388. // Now explore anything left pending. Since the component graph is
  1389. // guaranteed to be acyclic we know this will terminate.
  1390. while (!this->PendingComponents.empty()) {
  1391. // Visit one entry from the first pending component. The visit
  1392. // logic will update the pending components accordingly. Since
  1393. // the pending components are kept in topological order this will
  1394. // not repeat one.
  1395. size_t e = *this->PendingComponents.begin()->second.Entries.begin();
  1396. this->VisitEntry(e);
  1397. }
  1398. }
  1399. void cmComputeLinkDepends::DisplayComponents()
  1400. {
  1401. fprintf(stderr, "The strongly connected components are:\n");
  1402. std::vector<NodeList> const& components = this->CCG->GetComponents();
  1403. for (size_t c = 0; c < components.size(); ++c) {
  1404. fprintf(stderr, "Component (%zu):\n", c);
  1405. NodeList const& nl = components[c];
  1406. for (size_t i : nl) {
  1407. fprintf(stderr, " item %zu [%s]\n", i,
  1408. this->EntryList[i].Item.Value.c_str());
  1409. }
  1410. EdgeList const& ol = this->CCG->GetComponentGraphEdges(c);
  1411. for (cmGraphEdge const& oi : ol) {
  1412. size_t i = oi;
  1413. fprintf(stderr, " followed by Component (%zu)\n", i);
  1414. }
  1415. fprintf(stderr, " topo order index %zu\n", this->ComponentOrder[c]);
  1416. }
  1417. fprintf(stderr, "\n");
  1418. }
  1419. void cmComputeLinkDepends::VisitComponent(size_t c)
  1420. {
  1421. // Check if the node has already been visited.
  1422. if (this->ComponentVisited[c]) {
  1423. return;
  1424. }
  1425. // We are now visiting this component so mark it.
  1426. this->ComponentVisited[c] = 1;
  1427. // Visit the neighbors of the component first.
  1428. // Run in reverse order so the topological order will preserve the
  1429. // original order where there are no constraints.
  1430. EdgeList const& nl = this->CCG->GetComponentGraphEdges(c);
  1431. for (cmGraphEdge const& edge : cmReverseRange(nl)) {
  1432. this->VisitComponent(edge);
  1433. }
  1434. // Assign an ordering id to this component.
  1435. this->ComponentOrder[c] = --this->ComponentOrderId;
  1436. }
  1437. void cmComputeLinkDepends::VisitEntry(size_t index)
  1438. {
  1439. // Include this entry on the link line.
  1440. this->FinalLinkOrder.push_back(index);
  1441. // This entry has now been seen. Update its component.
  1442. bool completed = false;
  1443. size_t component = this->CCG->GetComponentMap()[index];
  1444. auto mi = this->PendingComponents.find(this->ComponentOrder[component]);
  1445. if (mi != this->PendingComponents.end()) {
  1446. // The entry is in an already pending component.
  1447. PendingComponent& pc = mi->second;
  1448. // Remove the entry from those pending in its component.
  1449. pc.Entries.erase(index);
  1450. if (pc.Entries.empty()) {
  1451. // The complete component has been seen since it was last needed.
  1452. --pc.Count;
  1453. if (pc.Count == 0) {
  1454. // The component has been completed.
  1455. this->PendingComponents.erase(mi);
  1456. completed = true;
  1457. } else {
  1458. // The whole component needs to be seen again.
  1459. NodeList const& nl = this->CCG->GetComponent(component);
  1460. assert(nl.size() > 1);
  1461. pc.Entries.insert(nl.begin(), nl.end());
  1462. }
  1463. }
  1464. } else {
  1465. // The entry is not in an already pending component.
  1466. NodeList const& nl = this->CCG->GetComponent(component);
  1467. if (nl.size() > 1) {
  1468. // This is a non-trivial component. It is now pending.
  1469. PendingComponent& pc = this->MakePendingComponent(component);
  1470. // The starting entry has already been seen.
  1471. pc.Entries.erase(index);
  1472. } else {
  1473. // This is a trivial component, so it is already complete.
  1474. completed = true;
  1475. }
  1476. }
  1477. // If the entry completed a component, the component's dependencies
  1478. // are now pending.
  1479. if (completed) {
  1480. EdgeList const& ol = this->CCG->GetComponentGraphEdges(component);
  1481. for (cmGraphEdge const& oi : ol) {
  1482. // This entire component is now pending no matter whether it has
  1483. // been partially seen already.
  1484. this->MakePendingComponent(oi);
  1485. }
  1486. }
  1487. }
  1488. cmComputeLinkDepends::PendingComponent&
  1489. cmComputeLinkDepends::MakePendingComponent(size_t component)
  1490. {
  1491. // Create an entry (in topological order) for the component.
  1492. PendingComponent& pc =
  1493. this->PendingComponents[this->ComponentOrder[component]];
  1494. pc.Id = component;
  1495. NodeList const& nl = this->CCG->GetComponent(component);
  1496. if (nl.size() == 1) {
  1497. // Trivial components need be seen only once.
  1498. pc.Count = 1;
  1499. } else {
  1500. // This is a non-trivial strongly connected component of the
  1501. // original graph. It consists of two or more libraries
  1502. // (archives) that mutually require objects from one another. In
  1503. // the worst case we may have to repeat the list of libraries as
  1504. // many times as there are object files in the biggest archive.
  1505. // For now we just list them twice.
  1506. //
  1507. // The list of items in the component has been sorted by the order
  1508. // of discovery in the original BFS of dependencies. This has the
  1509. // advantage that the item directly linked by a target requiring
  1510. // this component will come first which minimizes the number of
  1511. // repeats needed.
  1512. pc.Count = this->ComputeComponentCount(nl);
  1513. }
  1514. // Store the entries to be seen.
  1515. pc.Entries.insert(nl.begin(), nl.end());
  1516. return pc;
  1517. }
  1518. size_t cmComputeLinkDepends::ComputeComponentCount(NodeList const& nl)
  1519. {
  1520. size_t count = 2;
  1521. for (size_t ni : nl) {
  1522. if (cmGeneratorTarget const* target = this->EntryList[ni].Target) {
  1523. if (cmLinkInterface const* iface =
  1524. target->GetLinkInterface(this->Config, this->Target)) {
  1525. if (iface->Multiplicity > count) {
  1526. count = iface->Multiplicity;
  1527. }
  1528. }
  1529. }
  1530. }
  1531. return count;
  1532. }
  1533. namespace {
  1534. void DisplayLinkEntry(int& count, cmComputeLinkDepends::LinkEntry const& entry)
  1535. {
  1536. if (entry.Kind == cmComputeLinkDepends::LinkEntry::Group) {
  1537. if (entry.Item.Value == LG_ITEM_BEGIN) {
  1538. fprintf(stderr, " start group");
  1539. count = 4;
  1540. } else if (entry.Item.Value == LG_ITEM_END) {
  1541. fprintf(stderr, " end group");
  1542. count = 2;
  1543. } else {
  1544. fprintf(stderr, " group");
  1545. }
  1546. } else if (entry.Target) {
  1547. fprintf(stderr, "%*starget [%s]", count, "",
  1548. entry.Target->GetName().c_str());
  1549. } else {
  1550. fprintf(stderr, "%*sitem [%s]", count, "", entry.Item.Value.c_str());
  1551. }
  1552. if (entry.Feature != cmComputeLinkDepends::LinkEntry::DEFAULT) {
  1553. fprintf(stderr, ", feature [%s]", entry.Feature.c_str());
  1554. }
  1555. fprintf(stderr, "\n");
  1556. }
  1557. }
  1558. void cmComputeLinkDepends::DisplayOrderedEntries()
  1559. {
  1560. fprintf(stderr, "target [%s] link dependency ordering:\n",
  1561. this->Target->GetName().c_str());
  1562. int count = 2;
  1563. for (auto index : this->FinalLinkOrder) {
  1564. DisplayLinkEntry(count, this->EntryList[index]);
  1565. }
  1566. fprintf(stderr, "\n");
  1567. }
  1568. void cmComputeLinkDepends::DisplayFinalEntries()
  1569. {
  1570. fprintf(stderr, "target [%s] link line:\n", this->Target->GetName().c_str());
  1571. int count = 2;
  1572. for (LinkEntry const& entry : this->FinalLinkEntries) {
  1573. DisplayLinkEntry(count, entry);
  1574. }
  1575. fprintf(stderr, "\n");
  1576. }
  1577. void cmComputeLinkDepends::CheckWrongConfigItem(cmLinkItem const& item)
  1578. {
  1579. if (!this->OldLinkDirMode) {
  1580. return;
  1581. }
  1582. // For CMake 2.4 bug-compatibility we need to consider the output
  1583. // directories of targets linked in another configuration as link
  1584. // directories.
  1585. if (item.Target && !item.Target->IsImported()) {
  1586. this->OldWrongConfigItems.insert(item.Target);
  1587. }
  1588. }