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