/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->IssueDiagnostic( |
367 | 0 | cmDiagnostics::CMD_AUTHOR, |
368 | 0 | cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0156), |
369 | 0 | "\nSince the policy is not set, legacy libraries " |
370 | 0 | "de-duplication strategy will be applied."), |
371 | 0 | target->GetBacktrace()); |
372 | 0 | } |
373 | 0 | CM_FALLTHROUGH; |
374 | 0 | case cmPolicies::OLD: |
375 | | // rely on default initialization of the class |
376 | 0 | break; |
377 | 0 | case cmPolicies::NEW: { |
378 | | // Policy 0179 applies only when policy 0156 is new |
379 | 0 | if (target->GetPolicyStatusCMP0179() == cmPolicies::WARN && |
380 | 0 | !makefile->GetCMakeInstance()->GetIsInTryCompile() && |
381 | 0 | makefile->PolicyOptionalWarningEnabled( |
382 | 0 | "CMAKE_POLICY_WARNING_CMP0179")) { |
383 | 0 | makefile->IssueDiagnostic( |
384 | 0 | cmDiagnostics::CMD_AUTHOR, |
385 | 0 | cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0179), |
386 | 0 | "\nSince the policy is not set, static libraries " |
387 | 0 | "de-duplication will keep the last occurrence of the " |
388 | 0 | "static libraries."), |
389 | 0 | target->GetBacktrace()); |
390 | 0 | } |
391 | |
|
392 | 0 | if (auto libProcessing = makefile->GetDefinition(cmStrCat( |
393 | 0 | "CMAKE_", linkLanguage, "_LINK_LIBRARIES_PROCESSING"))) { |
394 | | // UNICITY keyword is just for compatibility with previous |
395 | | // implementation |
396 | 0 | cmsys::RegularExpression processingOption{ |
397 | 0 | "^(ORDER|UNICITY|DEDUPLICATION)=(FORWARD|REVERSE|ALL|NONE|SHARED)$" |
398 | 0 | }; |
399 | 0 | std::string errorMessage; |
400 | 0 | for (auto const& option : cmList{ libProcessing }) { |
401 | 0 | if (processingOption.find(option)) { |
402 | 0 | if (processingOption.match(1) == "ORDER") { |
403 | 0 | if (processingOption.match(2) == "FORWARD") { |
404 | 0 | this->Order = Forward; |
405 | 0 | } else if (processingOption.match(2) == "REVERSE") { |
406 | 0 | this->Order = Reverse; |
407 | 0 | } else { |
408 | 0 | errorMessage += cmStrCat(" ", option, '\n'); |
409 | 0 | } |
410 | 0 | } else if (processingOption.match(1) == "UNICITY" || |
411 | 0 | processingOption.match(1) == "DEDUPLICATION") { |
412 | 0 | if (processingOption.match(2) == "ALL") { |
413 | 0 | this->Deduplication = All; |
414 | 0 | } else if (processingOption.match(2) == "NONE") { |
415 | 0 | this->Deduplication = None; |
416 | 0 | } else if (processingOption.match(2) == "SHARED") { |
417 | 0 | this->Deduplication = Shared; |
418 | 0 | } else { |
419 | 0 | errorMessage += cmStrCat(" ", option, '\n'); |
420 | 0 | } |
421 | 0 | } |
422 | 0 | } else { |
423 | 0 | errorMessage += cmStrCat(" ", option, '\n'); |
424 | 0 | } |
425 | 0 | } |
426 | 0 | if (!errorMessage.empty()) { |
427 | 0 | makefile->GetCMakeInstance()->IssueMessage( |
428 | 0 | MessageType::FATAL_ERROR, |
429 | 0 | cmStrCat("Erroneous option(s) for 'CMAKE_", linkLanguage, |
430 | 0 | "_LINK_LIBRARIES_PROCESSING':\n", errorMessage), |
431 | 0 | target->GetBacktrace()); |
432 | 0 | } |
433 | | // For some environments, deduplication should be activated only if |
434 | | // both policies CMP0156 and CMP0179 are NEW |
435 | 0 | if (makefile->GetDefinition(cmStrCat( |
436 | 0 | "CMAKE_", linkLanguage, "_PLATFORM_LINKER_ID")) == "LLD"_s && |
437 | 0 | makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT") == "ELF"_s && |
438 | 0 | target->GetPolicyStatusCMP0179() != cmPolicies::NEW && |
439 | 0 | this->Deduplication == All) { |
440 | 0 | this->Deduplication = Shared; |
441 | 0 | } |
442 | 0 | } |
443 | 0 | } |
444 | 0 | } |
445 | 0 | } |
446 | | |
447 | | void AddGroups(std::map<size_t, std::vector<size_t>> const& groups) |
448 | 0 | { |
449 | 0 | if (!groups.empty()) { |
450 | 0 | this->Groups = &groups; |
451 | | // record all libraries as part of groups to ensure correct |
452 | | // deduplication: libraries as part of groups are always kept. |
453 | 0 | for (auto const& g : groups) { |
454 | 0 | for (auto index : g.second) { |
455 | 0 | this->Emitted.insert(index); |
456 | 0 | } |
457 | 0 | } |
458 | 0 | } |
459 | 0 | } |
460 | | |
461 | | void AddLibraries(std::vector<size_t> const& libEntries) |
462 | 0 | { |
463 | 0 | if (this->Order == Reverse) { |
464 | 0 | std::vector<size_t> entries; |
465 | 0 | if (this->Deduplication == All && |
466 | 0 | this->Target->GetPolicyStatusCMP0179() == cmPolicies::NEW) { |
467 | | // keep the first occurrence of the static libraries |
468 | 0 | std::set<size_t> emitted{ this->Emitted }; |
469 | 0 | for (auto index : libEntries) { |
470 | 0 | LinkEntry const& entry = this->Entries[index]; |
471 | 0 | if (!entry.Target || |
472 | 0 | entry.Target->GetType() != cmStateEnums::STATIC_LIBRARY) { |
473 | 0 | entries.emplace_back(index); |
474 | 0 | continue; |
475 | 0 | } |
476 | 0 | if (this->IncludeEntry(entry) || emitted.insert(index).second) { |
477 | 0 | entries.emplace_back(index); |
478 | 0 | } |
479 | 0 | } |
480 | 0 | } else { |
481 | 0 | entries = libEntries; |
482 | 0 | } |
483 | | // Iterate in reverse order so we can keep only the last occurrence |
484 | | // of the shared libraries. |
485 | 0 | this->AddLibraries(cmReverseRange(entries)); |
486 | 0 | } else { |
487 | 0 | this->AddLibraries(cmMakeRange(libEntries)); |
488 | 0 | } |
489 | 0 | } |
490 | | |
491 | | void AddObjects(std::vector<size_t> const& objectEntries) |
492 | 0 | { |
493 | | // Place explicitly linked object files in the front. The linker will |
494 | | // always use them anyway, and they may depend on symbols from libraries. |
495 | 0 | if (this->Order == Reverse) { |
496 | | // Append in reverse order at the end since we reverse the final order. |
497 | 0 | for (auto index : cmReverseRange(objectEntries)) { |
498 | 0 | this->FinalEntries.emplace_back(this->Entries[index]); |
499 | 0 | } |
500 | 0 | } else { |
501 | | // Append in reverse order to ensure correct final order |
502 | 0 | for (auto index : cmReverseRange(objectEntries)) { |
503 | 0 | this->FinalEntries.emplace(this->FinalEntries.begin(), |
504 | 0 | this->Entries[index]); |
505 | 0 | } |
506 | 0 | } |
507 | 0 | } |
508 | | |
509 | | void Finalize() |
510 | 0 | { |
511 | 0 | if (this->Order == Reverse) { |
512 | | // Reverse the resulting order since we iterated in reverse. |
513 | 0 | std::reverse(this->FinalEntries.begin(), this->FinalEntries.end()); |
514 | 0 | } |
515 | | |
516 | | // expand groups |
517 | 0 | if (this->Groups) { |
518 | 0 | for (auto const& g : *this->Groups) { |
519 | 0 | LinkEntry const& groupEntry = this->Entries[g.first]; |
520 | 0 | auto it = this->FinalEntries.begin(); |
521 | 0 | while (true) { |
522 | 0 | it = std::find_if(it, this->FinalEntries.end(), |
523 | 0 | [&groupEntry](LinkEntry const& entry) -> bool { |
524 | 0 | return groupEntry.Item == entry.Item; |
525 | 0 | }); |
526 | 0 | if (it == this->FinalEntries.end()) { |
527 | 0 | break; |
528 | 0 | } |
529 | 0 | it->Item.Value = std::string(LG_ITEM_END); |
530 | 0 | for (auto index = g.second.rbegin(); index != g.second.rend(); |
531 | 0 | ++index) { |
532 | 0 | it = this->FinalEntries.insert(it, this->Entries[*index]); |
533 | 0 | } |
534 | 0 | it = this->FinalEntries.insert(it, groupEntry); |
535 | 0 | it->Item.Value = std::string(LG_ITEM_BEGIN); |
536 | 0 | } |
537 | 0 | } |
538 | 0 | } |
539 | 0 | } |
540 | | |
541 | | private: |
542 | | enum OrderKind |
543 | | { |
544 | | Forward, |
545 | | Reverse |
546 | | }; |
547 | | |
548 | | enum DeduplicationKind |
549 | | { |
550 | | None, |
551 | | Shared, |
552 | | All |
553 | | }; |
554 | | |
555 | | bool IncludeEntry(LinkEntry const& entry) const |
556 | 0 | { |
557 | 0 | if (entry.Feature != cmComputeLinkDepends::LinkEntry::DEFAULT) { |
558 | 0 | auto const& featureAttributes = GetLinkLibraryFeatureAttributes( |
559 | 0 | this->Target->Makefile, this->LinkLanguage, entry.Feature); |
560 | 0 | if ((!entry.Target || |
561 | 0 | featureAttributes.LibraryTypes.find(entry.Target->GetType()) != |
562 | 0 | featureAttributes.LibraryTypes.end()) && |
563 | 0 | featureAttributes.Deduplication != |
564 | 0 | LinkLibraryFeatureAttributeSet::Default) { |
565 | 0 | return featureAttributes.Deduplication == |
566 | 0 | LinkLibraryFeatureAttributeSet::No; |
567 | 0 | } |
568 | 0 | } |
569 | | |
570 | 0 | return this->Deduplication == None || |
571 | 0 | (this->Deduplication == Shared && |
572 | 0 | (!entry.Target || |
573 | 0 | entry.Target->GetType() != cmStateEnums::SHARED_LIBRARY)) || |
574 | 0 | (this->Deduplication == All && entry.Kind != LinkEntry::Library); |
575 | 0 | } |
576 | | |
577 | | template <typename Range> |
578 | | void AddLibraries(Range const& libEntries) |
579 | 0 | { |
580 | 0 | for (auto index : libEntries) { |
581 | 0 | LinkEntry const& entry = this->Entries[index]; |
582 | 0 | if (this->IncludeEntry(entry) || this->Emitted.insert(index).second) { |
583 | 0 | this->FinalEntries.emplace_back(entry); |
584 | 0 | } |
585 | 0 | } |
586 | 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&) |
587 | | |
588 | | OrderKind Order = Reverse; |
589 | | DeduplicationKind Deduplication = Shared; |
590 | | cmGeneratorTarget const* Target; |
591 | | std::string const& LinkLanguage; |
592 | | EntryVector& Entries; |
593 | | EntryVector& FinalEntries; |
594 | | std::set<size_t> Emitted; |
595 | | std::map<size_t, std::vector<size_t>> const* Groups = nullptr; |
596 | | }; |
597 | | } |
598 | | |
599 | | std::string const& cmComputeLinkDepends::LinkEntry::DEFAULT = |
600 | | cmLinkItem::DEFAULT; |
601 | | |
602 | | cmComputeLinkDepends::cmComputeLinkDepends(cmGeneratorTarget const* target, |
603 | | std::string config, |
604 | | std::string linkLanguage, |
605 | | LinkLibrariesStrategy strategy) |
606 | 0 | : Target(target) |
607 | 0 | , Makefile(this->Target->Target->GetMakefile()) |
608 | 0 | , GlobalGenerator(this->Target->GetLocalGenerator()->GetGlobalGenerator()) |
609 | 0 | , CMakeInstance(this->GlobalGenerator->GetCMakeInstance()) |
610 | 0 | , Config(std::move(config)) |
611 | 0 | , DebugMode(this->Makefile->IsOn("CMAKE_LINK_DEPENDS_DEBUG_MODE") || |
612 | 0 | this->Target->GetProperty("LINK_DEPENDS_DEBUG_MODE").IsOn()) |
613 | 0 | , LinkLanguage(std::move(linkLanguage)) |
614 | 0 | , LinkType(ComputeLinkType( |
615 | 0 | this->Config, this->Makefile->GetCMakeInstance()->GetDebugConfigs())) |
616 | 0 | , Strategy(strategy) |
617 | | |
618 | 0 | { |
619 | 0 | cm::GenEx::Context context(this->Target->LocalGenerator, this->Config, |
620 | 0 | this->LinkLanguage); |
621 | | // target oriented feature override property takes precedence over |
622 | | // global override property |
623 | 0 | cm::string_view lloPrefix = "LINK_LIBRARY_OVERRIDE_"_s; |
624 | 0 | auto const& keys = this->Target->GetPropertyKeys(); |
625 | 0 | std::for_each( |
626 | 0 | keys.cbegin(), keys.cend(), |
627 | 0 | [this, &lloPrefix, &context](std::string const& key) { |
628 | 0 | if (cmHasPrefix(key, lloPrefix)) { |
629 | 0 | if (cmValue feature = this->Target->GetProperty(key)) { |
630 | 0 | if (!feature->empty() && key.length() > lloPrefix.length()) { |
631 | 0 | auto item = key.substr(lloPrefix.length()); |
632 | 0 | cmGeneratorExpressionDAGChecker dagChecker{ |
633 | 0 | this->Target, "LINK_LIBRARY_OVERRIDE", nullptr, nullptr, |
634 | 0 | context, this->Target->GetBacktrace(), |
635 | 0 | }; |
636 | 0 | auto overrideFeature = cmGeneratorExpression::Evaluate( |
637 | 0 | *feature, context.LG, context.Config, this->Target, &dagChecker, |
638 | 0 | this->Target, context.Language); |
639 | 0 | this->LinkLibraryOverride.emplace(item, overrideFeature); |
640 | 0 | } |
641 | 0 | } |
642 | 0 | } |
643 | 0 | }); |
644 | | // global override property |
645 | 0 | if (cmValue linkLibraryOverride = |
646 | 0 | this->Target->GetProperty("LINK_LIBRARY_OVERRIDE")) { |
647 | 0 | cmGeneratorExpressionDAGChecker dagChecker{ |
648 | 0 | this->Target, "LINK_LIBRARY_OVERRIDE", nullptr, nullptr, |
649 | 0 | context, this->Target->GetBacktrace(), |
650 | 0 | }; |
651 | 0 | auto overrideValue = cmGeneratorExpression::Evaluate( |
652 | 0 | *linkLibraryOverride, context.LG, context.Config, this->Target, |
653 | 0 | &dagChecker, this->Target, context.Language); |
654 | |
|
655 | 0 | std::vector<std::string> overrideList = |
656 | 0 | cmTokenize(overrideValue, ',', cmTokenizerMode::New); |
657 | 0 | if (overrideList.size() >= 2) { |
658 | 0 | auto const& feature = overrideList.front(); |
659 | 0 | std::for_each(overrideList.cbegin() + 1, overrideList.cend(), |
660 | 0 | [this, &feature](std::string const& item) { |
661 | 0 | this->LinkLibraryOverride.emplace(item, feature); |
662 | 0 | }); |
663 | 0 | } |
664 | 0 | } |
665 | 0 | } |
666 | | |
667 | 0 | cmComputeLinkDepends::~cmComputeLinkDepends() = default; |
668 | | |
669 | | std::vector<cmComputeLinkDepends::LinkEntry> const& |
670 | | cmComputeLinkDepends::Compute() |
671 | 0 | { |
672 | | // Follow the link dependencies of the target to be linked. |
673 | 0 | this->AddDirectLinkEntries(); |
674 | | |
675 | | // Complete the breadth-first search of dependencies. |
676 | 0 | while (!this->BFSQueue.empty()) { |
677 | | // Get the next entry. |
678 | 0 | BFSEntry qe = this->BFSQueue.front(); |
679 | 0 | this->BFSQueue.pop(); |
680 | | |
681 | | // Follow the entry's dependencies. |
682 | 0 | this->FollowLinkEntry(qe); |
683 | 0 | } |
684 | | |
685 | | // Complete the search of shared library dependencies. |
686 | 0 | while (!this->SharedDepQueue.empty()) { |
687 | | // Handle the next entry. |
688 | 0 | this->HandleSharedDependency(this->SharedDepQueue.front()); |
689 | 0 | this->SharedDepQueue.pop(); |
690 | 0 | } |
691 | | |
692 | | // Infer dependencies of targets for which they were not known. |
693 | 0 | this->InferDependencies(); |
694 | | |
695 | | // finalize groups dependencies |
696 | | // All dependencies which are raw items must be replaced by the group |
697 | | // it belongs to, if any. |
698 | 0 | this->UpdateGroupDependencies(); |
699 | | |
700 | | // Cleanup the constraint graph. |
701 | 0 | this->CleanConstraintGraph(); |
702 | | |
703 | | // Display the constraint graph. |
704 | 0 | if (this->DebugMode) { |
705 | 0 | fprintf(stderr, |
706 | 0 | "---------------------------------------" |
707 | 0 | "---------------------------------------\n"); |
708 | 0 | fprintf(stderr, "Link dependency analysis for target %s, config %s\n", |
709 | 0 | this->Target->GetName().c_str(), |
710 | 0 | this->Config.empty() ? "noconfig" : this->Config.c_str()); |
711 | 0 | this->DisplayConstraintGraph(); |
712 | 0 | } |
713 | | |
714 | | // Compute the DAG of strongly connected components. The algorithm |
715 | | // used by cmComputeComponentGraph should identify the components in |
716 | | // the same order in which the items were originally discovered in |
717 | | // the BFS. This should preserve the original order when no |
718 | | // constraints disallow it. |
719 | 0 | this->CCG = |
720 | 0 | cm::make_unique<cmComputeComponentGraph>(this->EntryConstraintGraph); |
721 | 0 | this->CCG->Compute(); |
722 | |
|
723 | 0 | if (!this->CheckCircularDependencies()) { |
724 | 0 | return this->FinalLinkEntries; |
725 | 0 | } |
726 | | |
727 | | // Compute the final ordering. |
728 | 0 | this->OrderLinkEntries(); |
729 | | |
730 | | // Display the final ordering. |
731 | 0 | if (this->DebugMode) { |
732 | 0 | this->DisplayOrderedEntries(); |
733 | 0 | } |
734 | | |
735 | | // Compute the final set of link entries. |
736 | 0 | EntriesProcessing entriesProcessing{ this->Target, this->LinkLanguage, |
737 | 0 | this->EntryList, |
738 | 0 | this->FinalLinkEntries }; |
739 | | // Add groups first, to ensure that libraries of the groups are always kept. |
740 | 0 | entriesProcessing.AddGroups(this->GroupItems); |
741 | 0 | entriesProcessing.AddLibraries(this->FinalLinkOrder); |
742 | 0 | entriesProcessing.AddObjects(this->ObjectEntries); |
743 | 0 | entriesProcessing.Finalize(); |
744 | | |
745 | | // Display the final set. |
746 | 0 | if (this->DebugMode) { |
747 | 0 | this->DisplayFinalEntries(); |
748 | 0 | } |
749 | |
|
750 | 0 | return this->FinalLinkEntries; |
751 | 0 | } |
752 | | |
753 | | std::string const& cmComputeLinkDepends::GetCurrentFeature( |
754 | | std::string const& item, std::string const& defaultFeature) const |
755 | 0 | { |
756 | 0 | auto it = this->LinkLibraryOverride.find(item); |
757 | 0 | return it == this->LinkLibraryOverride.end() ? defaultFeature : it->second; |
758 | 0 | } |
759 | | |
760 | | std::pair<std::map<cmLinkItem, size_t>::iterator, bool> |
761 | | cmComputeLinkDepends::AllocateLinkEntry(cmLinkItem const& item) |
762 | 0 | { |
763 | 0 | std::map<cmLinkItem, size_t>::value_type index_entry( |
764 | 0 | item, static_cast<size_t>(this->EntryList.size())); |
765 | 0 | auto lei = this->LinkEntryIndex.insert(index_entry); |
766 | 0 | if (lei.second) { |
767 | 0 | this->EntryList.emplace_back(); |
768 | 0 | this->InferredDependSets.emplace_back(); |
769 | 0 | this->EntryConstraintGraph.emplace_back(); |
770 | 0 | } |
771 | 0 | return lei; |
772 | 0 | } |
773 | | |
774 | | std::pair<size_t, bool> cmComputeLinkDepends::AddLinkEntry( |
775 | | cmLinkItem const& item, cm::optional<size_t> groupIndex) |
776 | 0 | { |
777 | | // Allocate a spot for the item entry. |
778 | 0 | auto lei = this->AllocateLinkEntry(item); |
779 | | |
780 | | // Check if the item entry has already been added. |
781 | 0 | if (!lei.second) { |
782 | | // Yes. We do not need to follow the item's dependencies again. |
783 | 0 | return { lei.first->second, false }; |
784 | 0 | } |
785 | | |
786 | | // Initialize the item entry. |
787 | 0 | size_t index = lei.first->second; |
788 | 0 | LinkEntry& entry = this->EntryList[index]; |
789 | 0 | entry.Item = BT<std::string>(item.AsStr(), item.Backtrace); |
790 | 0 | entry.Target = item.Target; |
791 | 0 | entry.Feature = item.Feature; |
792 | 0 | if (!entry.Target && entry.Item.Value[0] == '-' && |
793 | 0 | entry.Item.Value[1] != 'l' && |
794 | 0 | entry.Item.Value.substr(0, 10) != "-framework") { |
795 | 0 | entry.Kind = LinkEntry::Flag; |
796 | 0 | entry.Feature = LinkEntry::DEFAULT; |
797 | 0 | } else if (cmHasPrefix(entry.Item.Value, LG_BEGIN) && |
798 | 0 | cmHasSuffix(entry.Item.Value, '>')) { |
799 | 0 | entry.Kind = LinkEntry::Group; |
800 | 0 | } |
801 | |
|
802 | 0 | if (entry.Kind != LinkEntry::Group) { |
803 | | // If the item has dependencies queue it to follow them. |
804 | 0 | if (entry.Target) { |
805 | | // Target dependencies are always known. Follow them. |
806 | 0 | BFSEntry qe = { index, groupIndex, nullptr }; |
807 | 0 | this->BFSQueue.push(qe); |
808 | 0 | } else { |
809 | | // Look for an old-style <item>_LIB_DEPENDS variable. |
810 | 0 | std::string var = cmStrCat(entry.Item.Value, "_LIB_DEPENDS"); |
811 | 0 | if (cmValue val = this->Makefile->GetDefinition(var)) { |
812 | | // The item dependencies are known. Follow them. |
813 | 0 | BFSEntry qe = { index, groupIndex, val->c_str() }; |
814 | 0 | this->BFSQueue.push(qe); |
815 | 0 | } else if (entry.Kind != LinkEntry::Flag) { |
816 | | // The item dependencies are not known. We need to infer them. |
817 | 0 | this->InferredDependSets[index].Initialized = true; |
818 | 0 | } |
819 | 0 | } |
820 | 0 | } |
821 | |
|
822 | 0 | return { index, true }; |
823 | 0 | } |
824 | | |
825 | | void cmComputeLinkDepends::AddLinkObject(cmLinkItem const& item) |
826 | 0 | { |
827 | 0 | assert(!item.Target); // The item is an object file, not its target. |
828 | | |
829 | | // Allocate a spot for the item entry. |
830 | 0 | auto lei = this->AllocateLinkEntry(item); |
831 | | |
832 | | // Check if the item entry has already been added. |
833 | 0 | if (!lei.second) { |
834 | 0 | return; |
835 | 0 | } |
836 | | |
837 | | // Initialize the item entry. |
838 | 0 | size_t index = lei.first->second; |
839 | 0 | LinkEntry& entry = this->EntryList[index]; |
840 | 0 | entry.Item = BT<std::string>(item.AsStr(), item.Backtrace); |
841 | 0 | entry.Kind = LinkEntry::Object; |
842 | 0 | entry.ObjectSource = item.ObjectSource; |
843 | | |
844 | | // Record explicitly linked object files separately. |
845 | 0 | this->ObjectEntries.emplace_back(index); |
846 | 0 | } |
847 | | |
848 | | void cmComputeLinkDepends::FollowLinkEntry(BFSEntry qe) |
849 | 0 | { |
850 | | // Get this entry representation. |
851 | 0 | size_t depender_index = qe.GroupIndex ? *qe.GroupIndex : qe.Index; |
852 | 0 | LinkEntry const& entry = this->EntryList[qe.Index]; |
853 | | |
854 | | // Follow the item's dependencies. |
855 | 0 | if (entry.Target) { |
856 | | // Follow the target dependencies. |
857 | 0 | if (cmLinkInterface const* iface = |
858 | 0 | entry.Target->GetLinkInterface(this->Config, this->Target)) { |
859 | 0 | bool const isIface = |
860 | 0 | entry.Target->GetType() == cmStateEnums::INTERFACE_LIBRARY; |
861 | | // This target provides its own link interface information. |
862 | 0 | this->AddLinkEntries(depender_index, iface->Libraries); |
863 | 0 | this->AddLinkObjects(iface->Objects); |
864 | 0 | for (auto const& language : iface->Languages) { |
865 | 0 | auto runtimeEntries = iface->LanguageRuntimeLibraries.find(language); |
866 | 0 | if (runtimeEntries != iface->LanguageRuntimeLibraries.end()) { |
867 | 0 | this->AddLinkEntries(depender_index, runtimeEntries->second); |
868 | 0 | } |
869 | 0 | } |
870 | |
|
871 | 0 | if (isIface) { |
872 | 0 | return; |
873 | 0 | } |
874 | | |
875 | | // Handle dependent shared libraries. |
876 | 0 | this->FollowSharedDeps(depender_index, iface); |
877 | 0 | } |
878 | 0 | } else { |
879 | | // Follow the old-style dependency list. |
880 | 0 | this->AddVarLinkEntries(depender_index, qe.LibDepends); |
881 | 0 | } |
882 | 0 | } |
883 | | |
884 | | void cmComputeLinkDepends::FollowSharedDeps(size_t depender_index, |
885 | | cmLinkInterface const* iface, |
886 | | bool follow_interface) |
887 | 0 | { |
888 | | // Follow dependencies if we have not followed them already. |
889 | 0 | if (this->SharedDepFollowed.insert(depender_index).second) { |
890 | 0 | if (follow_interface) { |
891 | 0 | this->QueueSharedDependencies(depender_index, iface->Libraries); |
892 | 0 | } |
893 | 0 | this->QueueSharedDependencies(depender_index, iface->SharedDeps); |
894 | 0 | } |
895 | 0 | } |
896 | | |
897 | | void cmComputeLinkDepends::QueueSharedDependencies( |
898 | | size_t depender_index, std::vector<cmLinkItem> const& deps) |
899 | 0 | { |
900 | 0 | for (cmLinkItem const& li : deps) { |
901 | 0 | SharedDepEntry qe; |
902 | 0 | qe.Item = li; |
903 | 0 | qe.DependerIndex = depender_index; |
904 | 0 | this->SharedDepQueue.push(qe); |
905 | 0 | } |
906 | 0 | } |
907 | | |
908 | | void cmComputeLinkDepends::HandleSharedDependency(SharedDepEntry const& dep) |
909 | 0 | { |
910 | | // Allocate a spot for the item entry. |
911 | 0 | auto lei = this->AllocateLinkEntry(dep.Item); |
912 | 0 | size_t index = lei.first->second; |
913 | | |
914 | | // Check if the target does not already has an entry. |
915 | 0 | if (lei.second) { |
916 | | // Initialize the item entry. |
917 | 0 | LinkEntry& entry = this->EntryList[index]; |
918 | 0 | entry.Item = BT<std::string>(dep.Item.AsStr(), dep.Item.Backtrace); |
919 | 0 | entry.Target = dep.Item.Target; |
920 | | |
921 | | // This item was added specifically because it is a dependent |
922 | | // shared library. It may get special treatment |
923 | | // in cmComputeLinkInformation. |
924 | 0 | entry.Kind = LinkEntry::SharedDep; |
925 | 0 | } |
926 | | |
927 | | // Get the link entry for this target. |
928 | 0 | LinkEntry& entry = this->EntryList[index]; |
929 | | |
930 | | // This shared library dependency must follow the item that listed |
931 | | // it. |
932 | 0 | this->EntryConstraintGraph[dep.DependerIndex].emplace_back( |
933 | 0 | index, true, false, cmListFileBacktrace()); |
934 | | |
935 | | // Target items may have their own dependencies. |
936 | 0 | if (entry.Target) { |
937 | 0 | if (cmLinkInterface const* iface = |
938 | 0 | entry.Target->GetLinkInterface(this->Config, this->Target)) { |
939 | | // Follow public and private dependencies transitively. |
940 | 0 | this->FollowSharedDeps(index, iface, true); |
941 | 0 | } |
942 | 0 | } |
943 | 0 | } |
944 | | |
945 | | void cmComputeLinkDepends::AddVarLinkEntries( |
946 | | cm::optional<size_t> depender_index, char const* value) |
947 | 0 | { |
948 | | // This is called to add the dependencies named by |
949 | | // <item>_LIB_DEPENDS. The variable contains a semicolon-separated |
950 | | // list. The list contains link-type;item pairs and just items. |
951 | 0 | cmList deplist{ value }; |
952 | | |
953 | | // Look for entries meant for this configuration. |
954 | 0 | std::vector<cmLinkItem> actual_libs; |
955 | 0 | cmTargetLinkLibraryType llt = GENERAL_LibraryType; |
956 | 0 | bool haveLLT = false; |
957 | 0 | for (std::string const& d : deplist) { |
958 | 0 | if (d == "debug") { |
959 | 0 | llt = DEBUG_LibraryType; |
960 | 0 | haveLLT = true; |
961 | 0 | } else if (d == "optimized") { |
962 | 0 | llt = OPTIMIZED_LibraryType; |
963 | 0 | haveLLT = true; |
964 | 0 | } else if (d == "general") { |
965 | 0 | llt = GENERAL_LibraryType; |
966 | 0 | haveLLT = true; |
967 | 0 | } else if (!d.empty()) { |
968 | | // If no explicit link type was given prior to this entry then |
969 | | // check if the entry has its own link type variable. This is |
970 | | // needed for compatibility with dependency files generated by |
971 | | // the export_library_dependencies command from CMake 2.4 and |
972 | | // lower. |
973 | 0 | if (!haveLLT) { |
974 | 0 | std::string var = cmStrCat(d, "_LINK_TYPE"); |
975 | 0 | if (cmValue val = this->Makefile->GetDefinition(var)) { |
976 | 0 | if (*val == "debug") { |
977 | 0 | llt = DEBUG_LibraryType; |
978 | 0 | } else if (*val == "optimized") { |
979 | 0 | llt = OPTIMIZED_LibraryType; |
980 | 0 | } |
981 | 0 | } |
982 | 0 | } |
983 | | |
984 | | // If the library is meant for this link type then use it. |
985 | 0 | if (llt == GENERAL_LibraryType || llt == this->LinkType) { |
986 | 0 | actual_libs.emplace_back(this->ResolveLinkItem(depender_index, d)); |
987 | 0 | } |
988 | | |
989 | | // Reset the link type until another explicit type is given. |
990 | 0 | llt = GENERAL_LibraryType; |
991 | 0 | haveLLT = false; |
992 | 0 | } |
993 | 0 | } |
994 | | |
995 | | // Add the entries from this list. |
996 | 0 | this->AddLinkEntries(depender_index, actual_libs); |
997 | 0 | } |
998 | | |
999 | | void cmComputeLinkDepends::AddDirectLinkEntries() |
1000 | 0 | { |
1001 | | // Add direct link dependencies in this configuration. |
1002 | 0 | cmLinkImplementation const* impl = this->Target->GetLinkImplementation( |
1003 | 0 | this->Config, cmGeneratorTarget::UseTo::Link); |
1004 | 0 | this->AddLinkEntries(cm::nullopt, impl->Libraries); |
1005 | 0 | this->AddLinkObjects(impl->Objects); |
1006 | |
|
1007 | 0 | for (auto const& language : impl->Languages) { |
1008 | 0 | auto runtimeEntries = impl->LanguageRuntimeLibraries.find(language); |
1009 | 0 | if (runtimeEntries != impl->LanguageRuntimeLibraries.end()) { |
1010 | 0 | this->AddLinkEntries(cm::nullopt, runtimeEntries->second); |
1011 | 0 | } |
1012 | 0 | } |
1013 | 0 | } |
1014 | | |
1015 | | template <typename T> |
1016 | | void cmComputeLinkDepends::AddLinkEntries(cm::optional<size_t> depender_index, |
1017 | | std::vector<T> const& libs) |
1018 | 0 | { |
1019 | | // Track inferred dependency sets implied by this list. |
1020 | 0 | std::map<size_t, DependSet> dependSets; |
1021 | |
|
1022 | 0 | cm::optional<std::pair<size_t, bool>> group; |
1023 | 0 | std::vector<size_t> groupItems; |
1024 | | |
1025 | | // Loop over the libraries linked directly by the depender. |
1026 | 0 | for (T const& l : libs) { |
1027 | | // Skip entries that will resolve to the target getting linked or |
1028 | | // are empty. |
1029 | 0 | cmLinkItem const& item = l; |
1030 | 0 | if (item.AsStr() == this->Target->GetName() || item.AsStr().empty()) { |
1031 | 0 | continue; |
1032 | 0 | } |
1033 | | |
1034 | | // emit a warning if an undefined feature is used as part of |
1035 | | // an imported target |
1036 | 0 | if (item.Feature != LinkEntry::DEFAULT && depender_index) { |
1037 | 0 | auto const& depender = this->EntryList[*depender_index]; |
1038 | 0 | if (depender.Target && depender.Target->IsImported() && |
1039 | 0 | !IsFeatureSupported(this->Makefile, this->LinkLanguage, |
1040 | 0 | item.Feature)) { |
1041 | 0 | this->CMakeInstance->IssueDiagnostic( |
1042 | 0 | cmDiagnostics::CMD_AUTHOR, |
1043 | 0 | cmStrCat("The 'IMPORTED' target '", depender.Target->GetName(), |
1044 | 0 | "' uses the generator-expression '$<LINK_LIBRARY>' with " |
1045 | 0 | "the feature '", |
1046 | 0 | item.Feature, |
1047 | 0 | "', which is undefined or unsupported.\nDid you miss to " |
1048 | 0 | "define it by setting variables \"CMAKE_", |
1049 | 0 | this->LinkLanguage, "_LINK_LIBRARY_USING_", item.Feature, |
1050 | 0 | "\" and \"CMAKE_", this->LinkLanguage, |
1051 | 0 | "_LINK_LIBRARY_USING_", item.Feature, "_SUPPORTED\"?"), |
1052 | 0 | this->Target->GetBacktrace()); |
1053 | 0 | } |
1054 | 0 | } |
1055 | |
|
1056 | 0 | if (cmHasPrefix(item.AsStr(), LG_BEGIN) && |
1057 | 0 | cmHasSuffix(item.AsStr(), '>')) { |
1058 | 0 | group = this->AddLinkEntry(item, cm::nullopt); |
1059 | 0 | if (group->second) { |
1060 | 0 | LinkEntry& entry = this->EntryList[group->first]; |
1061 | 0 | entry.Feature = ExtractGroupFeature(item.AsStr()); |
1062 | 0 | } |
1063 | 0 | if (depender_index) { |
1064 | 0 | this->EntryConstraintGraph[*depender_index].emplace_back( |
1065 | 0 | group->first, false, false, cmListFileBacktrace()); |
1066 | 0 | } else { |
1067 | | // This is a direct dependency of the target being linked. |
1068 | 0 | this->OriginalEntries.push_back(group->first); |
1069 | 0 | } |
1070 | 0 | continue; |
1071 | 0 | } |
1072 | | |
1073 | 0 | size_t dependee_index; |
1074 | |
|
1075 | 0 | if (cmHasPrefix(item.AsStr(), LG_END) && cmHasSuffix(item.AsStr(), '>')) { |
1076 | 0 | assert(group); |
1077 | 0 | dependee_index = group->first; |
1078 | 0 | if (group->second) { |
1079 | 0 | this->GroupItems.emplace(group->first, std::move(groupItems)); |
1080 | 0 | } |
1081 | 0 | group = cm::nullopt; |
1082 | 0 | groupItems.clear(); |
1083 | 0 | continue; |
1084 | 0 | } |
1085 | | |
1086 | 0 | if (depender_index && group) { |
1087 | 0 | auto const& depender = this->EntryList[*depender_index]; |
1088 | 0 | auto const& groupFeature = this->EntryList[group->first].Feature; |
1089 | 0 | if (depender.Target && depender.Target->IsImported() && |
1090 | 0 | !IsGroupFeatureSupported(this->Makefile, this->LinkLanguage, |
1091 | 0 | groupFeature)) { |
1092 | 0 | this->CMakeInstance->IssueDiagnostic( |
1093 | 0 | cmDiagnostics::CMD_AUTHOR, |
1094 | 0 | cmStrCat("The 'IMPORTED' target '", depender.Target->GetName(), |
1095 | 0 | "' uses the generator-expression '$<LINK_GROUP>' with " |
1096 | 0 | "the feature '", |
1097 | 0 | groupFeature, |
1098 | 0 | "', which is undefined or unsupported.\nDid you miss to " |
1099 | 0 | "define it by setting variables \"CMAKE_", |
1100 | 0 | this->LinkLanguage, "_LINK_GROUP_USING_", groupFeature, |
1101 | 0 | "\" and \"CMAKE_", this->LinkLanguage, "_LINK_GROUP_USING_", |
1102 | 0 | groupFeature, "_SUPPORTED\"?"), |
1103 | 0 | this->Target->GetBacktrace()); |
1104 | 0 | } |
1105 | 0 | } |
1106 | | |
1107 | | // Add a link entry for this item. |
1108 | 0 | auto ale = this->AddLinkEntry( |
1109 | 0 | item, group ? cm::optional<size_t>(group->first) : cm::nullopt); |
1110 | 0 | dependee_index = ale.first; |
1111 | 0 | LinkEntry& entry = this->EntryList[dependee_index]; |
1112 | 0 | bool supportedItem = true; |
1113 | 0 | auto const& itemFeature = |
1114 | 0 | this->GetCurrentFeature(entry.Item.Value, item.Feature); |
1115 | 0 | if (group && ale.second && entry.Target && |
1116 | 0 | (entry.Target->GetType() == cmStateEnums::TargetType::OBJECT_LIBRARY || |
1117 | 0 | entry.Target->GetType() == |
1118 | 0 | cmStateEnums::TargetType::INTERFACE_LIBRARY)) { |
1119 | 0 | supportedItem = false; |
1120 | 0 | auto const& groupFeature = this->EntryList[group->first].Feature; |
1121 | 0 | this->CMakeInstance->IssueDiagnostic( |
1122 | 0 | cmDiagnostics::CMD_AUTHOR, |
1123 | 0 | cmStrCat( |
1124 | 0 | "The feature '", groupFeature, |
1125 | 0 | "', specified as part of a generator-expression " |
1126 | 0 | "'$", |
1127 | 0 | LG_BEGIN, groupFeature, ">', will not be applied to the ", |
1128 | 0 | (entry.Target->GetType() == cmStateEnums::TargetType::OBJECT_LIBRARY |
1129 | 0 | ? "OBJECT" |
1130 | 0 | : "INTERFACE"), |
1131 | 0 | " library '", entry.Item.Value, "'."), |
1132 | 0 | this->Target->GetBacktrace()); |
1133 | 0 | } |
1134 | | // check if feature is applicable to this item |
1135 | 0 | if (itemFeature != LinkEntry::DEFAULT && entry.Target) { |
1136 | 0 | auto const& featureAttributes = GetLinkLibraryFeatureAttributes( |
1137 | 0 | this->Makefile, this->LinkLanguage, itemFeature); |
1138 | 0 | if (featureAttributes.LibraryTypes.find(entry.Target->GetType()) == |
1139 | 0 | featureAttributes.LibraryTypes.end()) { |
1140 | 0 | supportedItem = false; |
1141 | 0 | this->CMakeInstance->IssueDiagnostic( |
1142 | 0 | cmDiagnostics::CMD_AUTHOR, |
1143 | 0 | cmStrCat("The feature '", itemFeature, |
1144 | 0 | "', specified as part of a generator-expression " |
1145 | 0 | "'$<LINK_LIBRARY:", |
1146 | 0 | itemFeature, ">', will not be applied to the ", |
1147 | 0 | cmState::GetTargetTypeName(entry.Target->GetType()), " '", |
1148 | 0 | entry.Item.Value, "'."), |
1149 | 0 | this->Target->GetBacktrace()); |
1150 | 0 | } |
1151 | 0 | } |
1152 | 0 | if (ale.second) { |
1153 | | // current item not yet defined |
1154 | 0 | entry.Feature = itemFeature; |
1155 | 0 | if (!supportedItem) { |
1156 | 0 | entry.Feature = LinkEntry::DEFAULT; |
1157 | 0 | } |
1158 | 0 | } |
1159 | |
|
1160 | 0 | if (supportedItem) { |
1161 | 0 | if (group) { |
1162 | 0 | auto const& currentFeature = this->EntryList[group->first].Feature; |
1163 | 0 | for (auto const& g : this->GroupItems) { |
1164 | 0 | auto const& groupFeature = this->EntryList[g.first].Feature; |
1165 | 0 | if (groupFeature == currentFeature) { |
1166 | 0 | continue; |
1167 | 0 | } |
1168 | 0 | if (std::find(g.second.cbegin(), g.second.cend(), dependee_index) != |
1169 | 0 | g.second.cend()) { |
1170 | 0 | this->CMakeInstance->IssueMessage( |
1171 | 0 | MessageType::FATAL_ERROR, |
1172 | 0 | cmStrCat("Impossible to link target '", this->Target->GetName(), |
1173 | 0 | "' because the link item '", entry.Item.Value, |
1174 | 0 | "', specified with the group feature '", currentFeature, |
1175 | 0 | "', has already occurred with the feature '", |
1176 | 0 | groupFeature, "', which is not allowed."), |
1177 | 0 | this->Target->GetBacktrace()); |
1178 | 0 | continue; |
1179 | 0 | } |
1180 | 0 | } |
1181 | 0 | } |
1182 | 0 | if (entry.Feature != itemFeature) { |
1183 | 0 | bool incompatibleFeatures = true; |
1184 | | // check if an override is possible |
1185 | 0 | auto const& entryFeatureAttributes = GetLinkLibraryFeatureAttributes( |
1186 | 0 | this->Makefile, this->LinkLanguage, entry.Feature); |
1187 | 0 | auto const& itemFeatureAttributes = GetLinkLibraryFeatureAttributes( |
1188 | 0 | this->Makefile, this->LinkLanguage, itemFeature); |
1189 | 0 | if (itemFeatureAttributes.Override.find(entry.Feature) != |
1190 | 0 | itemFeatureAttributes.Override.end() && |
1191 | 0 | entryFeatureAttributes.Override.find(itemFeature) != |
1192 | 0 | entryFeatureAttributes.Override.end()) { |
1193 | | // features override each other |
1194 | 0 | this->CMakeInstance->IssueMessage( |
1195 | 0 | MessageType::FATAL_ERROR, |
1196 | 0 | cmStrCat("Impossible to link target '", this->Target->GetName(), |
1197 | 0 | "' because the link item '", entry.Item.Value, |
1198 | 0 | "' is specified with the features '", itemFeature, |
1199 | 0 | "' and '", entry.Feature, |
1200 | 0 | "'" |
1201 | 0 | ", and both have an 'OVERRIDE' attribute that overrides " |
1202 | 0 | "the other. Such cycles are not allowed."), |
1203 | 0 | this->Target->GetBacktrace()); |
1204 | 0 | } else { |
1205 | 0 | if (itemFeatureAttributes.Override.find(entry.Feature) != |
1206 | 0 | itemFeatureAttributes.Override.end()) { |
1207 | 0 | entry.Feature = itemFeature; |
1208 | 0 | incompatibleFeatures = false; |
1209 | 0 | } else if (entryFeatureAttributes.Override.find(itemFeature) != |
1210 | 0 | entryFeatureAttributes.Override.end()) { |
1211 | 0 | incompatibleFeatures = false; |
1212 | 0 | } |
1213 | 0 | if (incompatibleFeatures) { |
1214 | | // incompatibles features occurred |
1215 | 0 | this->CMakeInstance->IssueMessage( |
1216 | 0 | MessageType::FATAL_ERROR, |
1217 | 0 | cmStrCat( |
1218 | 0 | "Impossible to link target '", this->Target->GetName(), |
1219 | 0 | "' because the link item '", entry.Item.Value, "', specified ", |
1220 | 0 | (itemFeature == LinkEntry::DEFAULT |
1221 | 0 | ? "without any feature or 'DEFAULT' feature" |
1222 | 0 | : cmStrCat("with the feature '", itemFeature, '\'')), |
1223 | 0 | ", has already occurred ", |
1224 | 0 | (entry.Feature == LinkEntry::DEFAULT |
1225 | 0 | ? "without any feature or 'DEFAULT' feature" |
1226 | 0 | : cmStrCat("with the feature '", entry.Feature, '\'')), |
1227 | 0 | ", which is not allowed."), |
1228 | 0 | this->Target->GetBacktrace()); |
1229 | 0 | } |
1230 | 0 | } |
1231 | 0 | } |
1232 | 0 | } |
1233 | |
|
1234 | 0 | if (group) { |
1235 | | // store item index for dependencies handling |
1236 | 0 | groupItems.push_back(dependee_index); |
1237 | 0 | } else { |
1238 | 0 | std::vector<size_t> indexes; |
1239 | 0 | bool entryHandled = false; |
1240 | | // search any occurrence of the library in already defined groups |
1241 | 0 | for (auto const& g : this->GroupItems) { |
1242 | 0 | for (auto index : g.second) { |
1243 | 0 | if (entry.Item.Value == this->EntryList[index].Item.Value) { |
1244 | 0 | indexes.push_back(g.first); |
1245 | 0 | entryHandled = true; |
1246 | 0 | break; |
1247 | 0 | } |
1248 | 0 | } |
1249 | 0 | } |
1250 | 0 | if (!entryHandled) { |
1251 | 0 | indexes.push_back(dependee_index); |
1252 | 0 | } |
1253 | |
|
1254 | 0 | for (auto index : indexes) { |
1255 | | // The dependee must come after the depender. |
1256 | 0 | if (depender_index) { |
1257 | 0 | this->EntryConstraintGraph[*depender_index].emplace_back( |
1258 | 0 | index, false, false, cmListFileBacktrace()); |
1259 | 0 | } else { |
1260 | | // This is a direct dependency of the target being linked. |
1261 | 0 | this->OriginalEntries.push_back(index); |
1262 | 0 | } |
1263 | | |
1264 | | // Update the inferred dependencies for earlier items. |
1265 | 0 | for (auto& dependSet : dependSets) { |
1266 | | // Add this item to the inferred dependencies of other items. |
1267 | | // Target items are never inferred dependees because unknown |
1268 | | // items are outside libraries that should not be depending on |
1269 | | // targets. |
1270 | 0 | if (!this->EntryList[index].Target && |
1271 | 0 | this->EntryList[index].Kind != LinkEntry::Flag && |
1272 | 0 | this->EntryList[index].Kind != LinkEntry::Group && |
1273 | 0 | dependee_index != dependSet.first) { |
1274 | 0 | dependSet.second.insert(index); |
1275 | 0 | } |
1276 | 0 | } |
1277 | | |
1278 | | // If this item needs to have dependencies inferred, do so. |
1279 | 0 | if (this->InferredDependSets[index].Initialized) { |
1280 | | // Make sure an entry exists to hold the set for the item. |
1281 | 0 | dependSets[index]; |
1282 | 0 | } |
1283 | 0 | } |
1284 | 0 | } |
1285 | 0 | } |
1286 | | |
1287 | | // Store the inferred dependency sets discovered for this list. |
1288 | 0 | for (auto const& dependSet : dependSets) { |
1289 | 0 | this->InferredDependSets[dependSet.first].push_back(dependSet.second); |
1290 | 0 | } |
1291 | 0 | } |
1292 | | |
1293 | | void cmComputeLinkDepends::AddLinkObjects(std::vector<cmLinkItem> const& objs) |
1294 | 0 | { |
1295 | 0 | for (cmLinkItem const& obj : objs) { |
1296 | 0 | this->AddLinkObject(obj); |
1297 | 0 | } |
1298 | 0 | } |
1299 | | |
1300 | | cmLinkItem cmComputeLinkDepends::ResolveLinkItem( |
1301 | | cm::optional<size_t> depender_index, std::string const& name) |
1302 | 0 | { |
1303 | | // Look for a target in the scope of the depender. |
1304 | 0 | cmGeneratorTarget const* from = this->Target; |
1305 | 0 | if (depender_index) { |
1306 | 0 | if (cmGeneratorTarget const* depender = |
1307 | 0 | this->EntryList[*depender_index].Target) { |
1308 | 0 | from = depender; |
1309 | 0 | } |
1310 | 0 | } |
1311 | 0 | return from->ResolveLinkItem(BT<std::string>(name)); |
1312 | 0 | } |
1313 | | |
1314 | | void cmComputeLinkDepends::InferDependencies() |
1315 | 0 | { |
1316 | | // The inferred dependency sets for each item list the possible |
1317 | | // dependencies. The intersection of the sets for one item form its |
1318 | | // inferred dependencies. |
1319 | 0 | for (size_t depender_index = 0; |
1320 | 0 | depender_index < this->InferredDependSets.size(); ++depender_index) { |
1321 | | // Skip items for which dependencies do not need to be inferred or |
1322 | | // for which the inferred dependency sets are empty. |
1323 | 0 | DependSetList& sets = this->InferredDependSets[depender_index]; |
1324 | 0 | if (!sets.Initialized || sets.empty()) { |
1325 | 0 | continue; |
1326 | 0 | } |
1327 | | |
1328 | | // Intersect the sets for this item. |
1329 | 0 | DependSet common = sets.front(); |
1330 | 0 | for (DependSet const& i : cmMakeRange(sets).advance(1)) { |
1331 | 0 | DependSet intersection; |
1332 | 0 | std::set_intersection(common.begin(), common.end(), i.begin(), i.end(), |
1333 | 0 | std::inserter(intersection, intersection.begin())); |
1334 | 0 | common = intersection; |
1335 | 0 | } |
1336 | | |
1337 | | // Add the inferred dependencies to the graph. |
1338 | 0 | cmGraphEdgeList& edges = this->EntryConstraintGraph[depender_index]; |
1339 | 0 | edges.reserve(edges.size() + common.size()); |
1340 | 0 | for (auto const& c : common) { |
1341 | 0 | edges.emplace_back(c, true, false, cmListFileBacktrace()); |
1342 | 0 | } |
1343 | 0 | } |
1344 | 0 | } |
1345 | | |
1346 | | void cmComputeLinkDepends::UpdateGroupDependencies() |
1347 | 0 | { |
1348 | 0 | if (this->GroupItems.empty()) { |
1349 | 0 | return; |
1350 | 0 | } |
1351 | | |
1352 | | // Walks through all entries of the constraint graph to replace dependencies |
1353 | | // over raw items by the group it belongs to, if any. |
1354 | 0 | for (auto& edgeList : this->EntryConstraintGraph) { |
1355 | 0 | for (auto& edge : edgeList) { |
1356 | 0 | size_t index = edge; |
1357 | 0 | if (this->EntryList[index].Kind == LinkEntry::Group || |
1358 | 0 | this->EntryList[index].Kind == LinkEntry::Flag || |
1359 | 0 | this->EntryList[index].Kind == LinkEntry::Object) { |
1360 | 0 | continue; |
1361 | 0 | } |
1362 | | // search the item in the defined groups |
1363 | 0 | for (auto const& groupItems : this->GroupItems) { |
1364 | 0 | auto pos = std::find(groupItems.second.cbegin(), |
1365 | 0 | groupItems.second.cend(), index); |
1366 | 0 | if (pos != groupItems.second.cend()) { |
1367 | | // replace lib dependency by the group it belongs to |
1368 | 0 | edge = cmGraphEdge{ groupItems.first, false, false, |
1369 | 0 | cmListFileBacktrace() }; |
1370 | 0 | } |
1371 | 0 | } |
1372 | 0 | } |
1373 | 0 | } |
1374 | 0 | } |
1375 | | |
1376 | | void cmComputeLinkDepends::CleanConstraintGraph() |
1377 | 0 | { |
1378 | 0 | for (cmGraphEdgeList& edgeList : this->EntryConstraintGraph) { |
1379 | | // Sort the outgoing edges for each graph node so that the |
1380 | | // original order will be preserved as much as possible. |
1381 | 0 | std::sort(edgeList.begin(), edgeList.end()); |
1382 | | |
1383 | | // Make the edge list unique. |
1384 | 0 | edgeList.erase(std::unique(edgeList.begin(), edgeList.end()), |
1385 | 0 | edgeList.end()); |
1386 | 0 | } |
1387 | 0 | } |
1388 | | |
1389 | | bool cmComputeLinkDepends::CheckCircularDependencies() const |
1390 | 0 | { |
1391 | 0 | std::vector<NodeList> const& components = this->CCG->GetComponents(); |
1392 | 0 | size_t nc = components.size(); |
1393 | 0 | for (size_t c = 0; c < nc; ++c) { |
1394 | | // Get the current component. |
1395 | 0 | NodeList const& nl = components[c]; |
1396 | | |
1397 | | // Skip trivial components. |
1398 | 0 | if (nl.size() < 2) { |
1399 | 0 | continue; |
1400 | 0 | } |
1401 | | |
1402 | | // no group must be evolved |
1403 | 0 | bool cycleDetected = false; |
1404 | 0 | for (size_t ni : nl) { |
1405 | 0 | if (this->EntryList[ni].Kind == LinkEntry::Group) { |
1406 | 0 | cycleDetected = true; |
1407 | 0 | break; |
1408 | 0 | } |
1409 | 0 | } |
1410 | 0 | if (!cycleDetected) { |
1411 | 0 | continue; |
1412 | 0 | } |
1413 | | |
1414 | | // Construct the error message. |
1415 | 0 | auto formatItem = [](LinkEntry const& entry) -> std::string { |
1416 | 0 | if (entry.Kind == LinkEntry::Group) { |
1417 | 0 | auto items = |
1418 | 0 | entry.Item.Value.substr(entry.Item.Value.find(':', 12) + 1); |
1419 | 0 | items.pop_back(); |
1420 | 0 | std::replace(items.begin(), items.end(), '|', ','); |
1421 | 0 | return cmStrCat("group \"", ExtractGroupFeature(entry.Item.Value), |
1422 | 0 | ":{", items, "}\""); |
1423 | 0 | } |
1424 | 0 | return cmStrCat('"', entry.Item.Value, '"'); |
1425 | 0 | }; |
1426 | |
|
1427 | 0 | std::ostringstream e; |
1428 | 0 | e << "The inter-target dependency graph, for the target \"" |
1429 | 0 | << this->Target->GetName() |
1430 | 0 | << "\", contains the following strongly connected component " |
1431 | 0 | "(cycle):\n"; |
1432 | 0 | std::vector<size_t> const& cmap = this->CCG->GetComponentMap(); |
1433 | 0 | for (size_t i : nl) { |
1434 | | // Get the depender. |
1435 | 0 | LinkEntry const& depender = this->EntryList[i]; |
1436 | | |
1437 | | // Describe the depender. |
1438 | 0 | e << " " << formatItem(depender) << "\n"; |
1439 | | |
1440 | | // List its dependencies that are inside the component. |
1441 | 0 | EdgeList const& el = this->EntryConstraintGraph[i]; |
1442 | 0 | for (cmGraphEdge const& ni : el) { |
1443 | 0 | size_t j = ni; |
1444 | 0 | if (cmap[j] == c) { |
1445 | 0 | LinkEntry const& dependee = this->EntryList[j]; |
1446 | 0 | e << " depends on " << formatItem(dependee) << "\n"; |
1447 | 0 | } |
1448 | 0 | } |
1449 | 0 | } |
1450 | 0 | this->CMakeInstance->IssueMessage(MessageType::FATAL_ERROR, e.str(), |
1451 | 0 | this->Target->GetBacktrace()); |
1452 | |
|
1453 | 0 | return false; |
1454 | 0 | } |
1455 | | |
1456 | 0 | return true; |
1457 | 0 | } |
1458 | | |
1459 | | void cmComputeLinkDepends::DisplayConstraintGraph() |
1460 | 0 | { |
1461 | | // Display the graph nodes and their edges. |
1462 | 0 | std::ostringstream e; |
1463 | 0 | for (size_t i = 0; i < this->EntryConstraintGraph.size(); ++i) { |
1464 | 0 | EdgeList const& nl = this->EntryConstraintGraph[i]; |
1465 | 0 | e << "item " << i << " is [" << this->EntryList[i].Item << "]\n"; |
1466 | 0 | e << cmWrap(" item ", nl, " must follow it", "\n") << "\n"; |
1467 | 0 | } |
1468 | 0 | fprintf(stderr, "%s\n", e.str().c_str()); |
1469 | 0 | } |
1470 | | |
1471 | | void cmComputeLinkDepends::OrderLinkEntries() |
1472 | 0 | { |
1473 | | // The component graph is guaranteed to be acyclic. Start a DFS |
1474 | | // from every entry to compute a topological order for the |
1475 | | // components. |
1476 | 0 | Graph const& cgraph = this->CCG->GetComponentGraph(); |
1477 | 0 | size_t n = cgraph.size(); |
1478 | 0 | this->ComponentVisited.resize(cgraph.size(), 0); |
1479 | 0 | this->ComponentOrder.resize(cgraph.size(), n); |
1480 | 0 | this->ComponentOrderId = n; |
1481 | | // Run in reverse order so the topological order will preserve the |
1482 | | // original order where there are no constraints. |
1483 | 0 | for (size_t c = n; c > 0; --c) { |
1484 | 0 | this->VisitComponent(c - 1); |
1485 | 0 | } |
1486 | | |
1487 | | // Display the component graph. |
1488 | 0 | if (this->DebugMode) { |
1489 | 0 | this->DisplayComponents(); |
1490 | 0 | } |
1491 | | |
1492 | | // Start with the original link line. |
1493 | 0 | switch (this->Strategy) { |
1494 | 0 | case LinkLibrariesStrategy::REORDER_MINIMALLY: { |
1495 | | // Emit the direct dependencies in their original order. |
1496 | | // This gives projects control over ordering. |
1497 | 0 | for (size_t originalEntry : this->OriginalEntries) { |
1498 | 0 | this->VisitEntry(originalEntry); |
1499 | 0 | } |
1500 | 0 | } break; |
1501 | 0 | case LinkLibrariesStrategy::REORDER_FREELY: { |
1502 | | // Schedule the direct dependencies for emission in topo order. |
1503 | | // This may produce more efficient link lines. |
1504 | 0 | for (size_t originalEntry : this->OriginalEntries) { |
1505 | 0 | this->MakePendingComponent( |
1506 | 0 | this->CCG->GetComponentMap()[originalEntry]); |
1507 | 0 | } |
1508 | 0 | } break; |
1509 | 0 | } |
1510 | | |
1511 | | // Now explore anything left pending. Since the component graph is |
1512 | | // guaranteed to be acyclic we know this will terminate. |
1513 | 0 | while (!this->PendingComponents.empty()) { |
1514 | | // Visit one entry from the first pending component. The visit |
1515 | | // logic will update the pending components accordingly. Since |
1516 | | // the pending components are kept in topological order this will |
1517 | | // not repeat one. |
1518 | 0 | size_t e = *this->PendingComponents.begin()->second.Entries.begin(); |
1519 | 0 | this->VisitEntry(e); |
1520 | 0 | } |
1521 | 0 | } |
1522 | | |
1523 | | void cmComputeLinkDepends::DisplayComponents() |
1524 | 0 | { |
1525 | 0 | fprintf(stderr, "The strongly connected components are:\n"); |
1526 | 0 | std::vector<NodeList> const& components = this->CCG->GetComponents(); |
1527 | 0 | for (size_t c = 0; c < components.size(); ++c) { |
1528 | 0 | fprintf(stderr, "Component (%zu):\n", c); |
1529 | 0 | NodeList const& nl = components[c]; |
1530 | 0 | for (size_t i : nl) { |
1531 | 0 | fprintf(stderr, " item %zu [%s]\n", i, |
1532 | 0 | this->EntryList[i].Item.Value.c_str()); |
1533 | 0 | } |
1534 | 0 | EdgeList const& ol = this->CCG->GetComponentGraphEdges(c); |
1535 | 0 | for (cmGraphEdge const& oi : ol) { |
1536 | 0 | size_t i = oi; |
1537 | 0 | fprintf(stderr, " followed by Component (%zu)\n", i); |
1538 | 0 | } |
1539 | 0 | fprintf(stderr, " topo order index %zu\n", this->ComponentOrder[c]); |
1540 | 0 | } |
1541 | 0 | fprintf(stderr, "\n"); |
1542 | 0 | } |
1543 | | |
1544 | | void cmComputeLinkDepends::VisitComponent(size_t c) |
1545 | 0 | { |
1546 | | // Check if the node has already been visited. |
1547 | 0 | if (this->ComponentVisited[c]) { |
1548 | 0 | return; |
1549 | 0 | } |
1550 | | |
1551 | | // We are now visiting this component so mark it. |
1552 | 0 | this->ComponentVisited[c] = 1; |
1553 | | |
1554 | | // Visit the neighbors of the component first. |
1555 | | // Run in reverse order so the topological order will preserve the |
1556 | | // original order where there are no constraints. |
1557 | 0 | EdgeList const& nl = this->CCG->GetComponentGraphEdges(c); |
1558 | 0 | for (cmGraphEdge const& edge : cmReverseRange(nl)) { |
1559 | 0 | this->VisitComponent(edge); |
1560 | 0 | } |
1561 | | |
1562 | | // Assign an ordering id to this component. |
1563 | 0 | this->ComponentOrder[c] = --this->ComponentOrderId; |
1564 | 0 | } |
1565 | | |
1566 | | void cmComputeLinkDepends::VisitEntry(size_t index) |
1567 | 0 | { |
1568 | | // Include this entry on the link line. |
1569 | 0 | this->FinalLinkOrder.push_back(index); |
1570 | | |
1571 | | // This entry has now been seen. Update its component. |
1572 | 0 | bool completed = false; |
1573 | 0 | size_t component = this->CCG->GetComponentMap()[index]; |
1574 | 0 | auto mi = this->PendingComponents.find(this->ComponentOrder[component]); |
1575 | 0 | if (mi != this->PendingComponents.end()) { |
1576 | | // The entry is in an already pending component. |
1577 | 0 | PendingComponent& pc = mi->second; |
1578 | | |
1579 | | // Remove the entry from those pending in its component. |
1580 | 0 | pc.Entries.erase(index); |
1581 | 0 | if (pc.Entries.empty()) { |
1582 | | // The complete component has been seen since it was last needed. |
1583 | 0 | --pc.Count; |
1584 | |
|
1585 | 0 | if (pc.Count == 0) { |
1586 | | // The component has been completed. |
1587 | 0 | this->PendingComponents.erase(mi); |
1588 | 0 | completed = true; |
1589 | 0 | } else { |
1590 | | // The whole component needs to be seen again. |
1591 | 0 | NodeList const& nl = this->CCG->GetComponent(component); |
1592 | 0 | assert(nl.size() > 1); |
1593 | 0 | pc.Entries.insert(nl.begin(), nl.end()); |
1594 | 0 | } |
1595 | 0 | } |
1596 | 0 | } else { |
1597 | | // The entry is not in an already pending component. |
1598 | 0 | NodeList const& nl = this->CCG->GetComponent(component); |
1599 | 0 | if (nl.size() > 1) { |
1600 | | // This is a non-trivial component. It is now pending. |
1601 | 0 | PendingComponent& pc = this->MakePendingComponent(component); |
1602 | | |
1603 | | // The starting entry has already been seen. |
1604 | 0 | pc.Entries.erase(index); |
1605 | 0 | } else { |
1606 | | // This is a trivial component, so it is already complete. |
1607 | 0 | completed = true; |
1608 | 0 | } |
1609 | 0 | } |
1610 | | |
1611 | | // If the entry completed a component, the component's dependencies |
1612 | | // are now pending. |
1613 | 0 | if (completed) { |
1614 | 0 | EdgeList const& ol = this->CCG->GetComponentGraphEdges(component); |
1615 | 0 | for (cmGraphEdge const& oi : ol) { |
1616 | | // This entire component is now pending no matter whether it has |
1617 | | // been partially seen already. |
1618 | 0 | this->MakePendingComponent(oi); |
1619 | 0 | } |
1620 | 0 | } |
1621 | 0 | } |
1622 | | |
1623 | | cmComputeLinkDepends::PendingComponent& |
1624 | | cmComputeLinkDepends::MakePendingComponent(size_t component) |
1625 | 0 | { |
1626 | | // Create an entry (in topological order) for the component. |
1627 | 0 | PendingComponent& pc = |
1628 | 0 | this->PendingComponents[this->ComponentOrder[component]]; |
1629 | 0 | pc.Id = component; |
1630 | 0 | NodeList const& nl = this->CCG->GetComponent(component); |
1631 | |
|
1632 | 0 | if (nl.size() == 1) { |
1633 | | // Trivial components need be seen only once. |
1634 | 0 | pc.Count = 1; |
1635 | 0 | } else { |
1636 | | // This is a non-trivial strongly connected component of the |
1637 | | // original graph. It consists of two or more libraries |
1638 | | // (archives) that mutually require objects from one another. In |
1639 | | // the worst case we may have to repeat the list of libraries as |
1640 | | // many times as there are object files in the biggest archive. |
1641 | | // For now we just list them twice. |
1642 | | // |
1643 | | // The list of items in the component has been sorted by the order |
1644 | | // of discovery in the original BFS of dependencies. This has the |
1645 | | // advantage that the item directly linked by a target requiring |
1646 | | // this component will come first which minimizes the number of |
1647 | | // repeats needed. |
1648 | 0 | pc.Count = this->ComputeComponentCount(nl); |
1649 | 0 | } |
1650 | | |
1651 | | // Store the entries to be seen. |
1652 | 0 | pc.Entries.insert(nl.begin(), nl.end()); |
1653 | |
|
1654 | 0 | return pc; |
1655 | 0 | } |
1656 | | |
1657 | | size_t cmComputeLinkDepends::ComputeComponentCount(NodeList const& nl) |
1658 | 0 | { |
1659 | 0 | size_t count = 2; |
1660 | 0 | for (size_t ni : nl) { |
1661 | 0 | if (cmGeneratorTarget const* target = this->EntryList[ni].Target) { |
1662 | 0 | if (cmLinkInterface const* iface = |
1663 | 0 | target->GetLinkInterface(this->Config, this->Target)) { |
1664 | 0 | if (iface->Multiplicity > count) { |
1665 | 0 | count = iface->Multiplicity; |
1666 | 0 | } |
1667 | 0 | } |
1668 | 0 | } |
1669 | 0 | } |
1670 | 0 | return count; |
1671 | 0 | } |
1672 | | |
1673 | | namespace { |
1674 | | void DisplayLinkEntry(int& count, cmComputeLinkDepends::LinkEntry const& entry) |
1675 | 0 | { |
1676 | 0 | if (entry.Kind == cmComputeLinkDepends::LinkEntry::Group) { |
1677 | 0 | if (entry.Item.Value == LG_ITEM_BEGIN) { |
1678 | 0 | fprintf(stderr, " start group"); |
1679 | 0 | count = 4; |
1680 | 0 | } else if (entry.Item.Value == LG_ITEM_END) { |
1681 | 0 | fprintf(stderr, " end group"); |
1682 | 0 | count = 2; |
1683 | 0 | } else { |
1684 | 0 | fprintf(stderr, " group"); |
1685 | 0 | } |
1686 | 0 | } else if (entry.Target) { |
1687 | 0 | fprintf(stderr, "%*starget [%s]", count, "", |
1688 | 0 | entry.Target->GetName().c_str()); |
1689 | 0 | } else { |
1690 | 0 | fprintf(stderr, "%*sitem [%s]", count, "", entry.Item.Value.c_str()); |
1691 | 0 | } |
1692 | 0 | if (entry.Feature != cmComputeLinkDepends::LinkEntry::DEFAULT) { |
1693 | 0 | fprintf(stderr, ", feature [%s]", entry.Feature.c_str()); |
1694 | 0 | } |
1695 | 0 | fprintf(stderr, "\n"); |
1696 | 0 | } |
1697 | | } |
1698 | | |
1699 | | void cmComputeLinkDepends::DisplayOrderedEntries() |
1700 | 0 | { |
1701 | 0 | fprintf(stderr, "target [%s] link dependency ordering:\n", |
1702 | 0 | this->Target->GetName().c_str()); |
1703 | 0 | int count = 2; |
1704 | 0 | for (auto index : this->FinalLinkOrder) { |
1705 | 0 | DisplayLinkEntry(count, this->EntryList[index]); |
1706 | 0 | } |
1707 | 0 | fprintf(stderr, "\n"); |
1708 | 0 | } |
1709 | | |
1710 | | void cmComputeLinkDepends::DisplayFinalEntries() |
1711 | 0 | { |
1712 | 0 | fprintf(stderr, "target [%s] link line:\n", this->Target->GetName().c_str()); |
1713 | 0 | int count = 2; |
1714 | 0 | for (LinkEntry const& entry : this->FinalLinkEntries) { |
1715 | 0 | DisplayLinkEntry(count, entry); |
1716 | 0 | } |
1717 | | fprintf(stderr, "\n"); |
1718 | 0 | } |