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