/src/CMake/Source/cmGeneratorTarget_Link.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 | | /* clang-format off */ |
4 | | #include "cmGeneratorTarget.h" |
5 | | /* clang-format on */ |
6 | | |
7 | | #include <algorithm> |
8 | | #include <cassert> |
9 | | #include <cstdio> |
10 | | #include <map> |
11 | | #include <set> |
12 | | #include <sstream> |
13 | | #include <string> |
14 | | #include <unordered_map> |
15 | | #include <unordered_set> |
16 | | #include <utility> |
17 | | #include <vector> |
18 | | |
19 | | #include <cm/memory> |
20 | | #include <cm/optional> |
21 | | #include <cm/string_view> |
22 | | #include <cmext/algorithm> |
23 | | #include <cmext/string_view> |
24 | | |
25 | | #include "cmAlgorithms.h" |
26 | | #include "cmComputeLinkInformation.h" |
27 | | #include "cmGenExContext.h" |
28 | | #include "cmGeneratorExpression.h" |
29 | | #include "cmGeneratorExpressionDAGChecker.h" |
30 | | #include "cmGlobalGenerator.h" |
31 | | #include "cmLinkItem.h" |
32 | | #include "cmList.h" |
33 | | #include "cmListFileCache.h" |
34 | | #include "cmLocalGenerator.h" |
35 | | #include "cmMakefile.h" |
36 | | #include "cmMessageType.h" |
37 | | #include "cmPolicies.h" |
38 | | #include "cmRange.h" |
39 | | #include "cmSourceFile.h" |
40 | | #include "cmSourceFileLocationKind.h" |
41 | | #include "cmStateTypes.h" |
42 | | #include "cmStringAlgorithms.h" |
43 | | #include "cmSystemTools.h" |
44 | | #include "cmTarget.h" |
45 | | #include "cmTargetLinkLibraryType.h" |
46 | | #include "cmValue.h" |
47 | | #include "cmake.h" |
48 | | |
49 | | namespace { |
50 | | using UseTo = cmGeneratorTarget::UseTo; |
51 | | |
52 | | std::string const kINTERFACE_LINK_LIBRARIES = "INTERFACE_LINK_LIBRARIES"; |
53 | | std::string const kINTERFACE_LINK_LIBRARIES_DIRECT = |
54 | | "INTERFACE_LINK_LIBRARIES_DIRECT"; |
55 | | std::string const kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE = |
56 | | "INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE"; |
57 | | |
58 | | unsigned int CheckLinkLibrariesSuppressionRAIICount; |
59 | | void MaybeEnableCheckLinkLibraries(cmOptionalLinkImplementation& impl) |
60 | 0 | { |
61 | 0 | if (CheckLinkLibrariesSuppressionRAIICount == 0) { |
62 | 0 | impl.CheckLinkLibraries = true; |
63 | 0 | } |
64 | 0 | } |
65 | | void MaybeEnableCheckLinkLibraries(cmOptionalLinkInterface& iface) |
66 | 0 | { |
67 | 0 | if (CheckLinkLibrariesSuppressionRAIICount == 0) { |
68 | 0 | iface.CheckLinkLibraries = true; |
69 | 0 | } |
70 | 0 | } |
71 | | } |
72 | | |
73 | | class cmTargetCollectLinkLanguages |
74 | | { |
75 | | public: |
76 | | cmTargetCollectLinkLanguages(cmGeneratorTarget const* target, |
77 | | std::string config, |
78 | | std::unordered_set<std::string>& languages, |
79 | | cmGeneratorTarget const* head, bool secondPass) |
80 | 0 | : Config(std::move(config)) |
81 | 0 | , Languages(languages) |
82 | 0 | , HeadTarget(head) |
83 | 0 | , SecondPass(secondPass) |
84 | 0 | { |
85 | 0 | this->Visited.insert(target); |
86 | 0 | } |
87 | | |
88 | | void Visit(cmLinkItem const& item) |
89 | 0 | { |
90 | 0 | if (!item.Target) { |
91 | 0 | return; |
92 | 0 | } |
93 | 0 | if (!this->Visited.insert(item.Target).second) { |
94 | 0 | return; |
95 | 0 | } |
96 | 0 | cmLinkInterface const* iface = item.Target->GetLinkInterface( |
97 | 0 | this->Config, this->HeadTarget, this->SecondPass); |
98 | 0 | if (!iface) { |
99 | 0 | return; |
100 | 0 | } |
101 | 0 | if (iface->HadLinkLanguageSensitiveCondition) { |
102 | 0 | this->HadLinkLanguageSensitiveCondition = true; |
103 | 0 | } |
104 | |
|
105 | 0 | for (std::string const& language : iface->Languages) { |
106 | 0 | this->Languages.insert(language); |
107 | 0 | } |
108 | |
|
109 | 0 | for (cmLinkItem const& lib : iface->Libraries) { |
110 | 0 | this->Visit(lib); |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | bool GetHadLinkLanguageSensitiveCondition() const |
115 | 0 | { |
116 | 0 | return this->HadLinkLanguageSensitiveCondition; |
117 | 0 | } |
118 | | |
119 | | private: |
120 | | std::string Config; |
121 | | std::unordered_set<std::string>& Languages; |
122 | | cmGeneratorTarget const* HeadTarget; |
123 | | std::set<cmGeneratorTarget const*> Visited; |
124 | | bool SecondPass; |
125 | | bool HadLinkLanguageSensitiveCondition = false; |
126 | | }; |
127 | | |
128 | | cmGeneratorTarget::LinkClosure const* cmGeneratorTarget::GetLinkClosure( |
129 | | std::string const& config) const |
130 | 0 | { |
131 | | // There is no link implementation for targets that cannot compile sources. |
132 | 0 | if (!this->CanCompileSources()) { |
133 | 0 | static LinkClosure const empty = { {}, {} }; |
134 | 0 | return ∅ |
135 | 0 | } |
136 | | |
137 | 0 | std::string key(cmSystemTools::UpperCase(config)); |
138 | 0 | auto i = this->LinkClosureMap.find(key); |
139 | 0 | if (i == this->LinkClosureMap.end()) { |
140 | 0 | LinkClosure lc; |
141 | 0 | this->ComputeLinkClosure(config, lc); |
142 | 0 | LinkClosureMapType::value_type entry(key, lc); |
143 | 0 | i = this->LinkClosureMap.insert(entry).first; |
144 | 0 | } |
145 | 0 | return &i->second; |
146 | 0 | } |
147 | | |
148 | | class cmTargetSelectLinker |
149 | | { |
150 | | int Preference = 0; |
151 | | cmGeneratorTarget const* Target; |
152 | | cmGlobalGenerator* GG; |
153 | | std::set<std::string> Preferred; |
154 | | |
155 | | public: |
156 | | cmTargetSelectLinker(cmGeneratorTarget const* target) |
157 | 0 | : Target(target) |
158 | 0 | { |
159 | 0 | this->GG = this->Target->GetLocalGenerator()->GetGlobalGenerator(); |
160 | 0 | } |
161 | | void Consider(std::string const& lang) |
162 | 0 | { |
163 | 0 | int preference = this->GG->GetLinkerPreference(lang); |
164 | 0 | if (preference > this->Preference) { |
165 | 0 | this->Preference = preference; |
166 | 0 | this->Preferred.clear(); |
167 | 0 | } |
168 | 0 | if (preference == this->Preference) { |
169 | 0 | this->Preferred.insert(lang); |
170 | 0 | } |
171 | 0 | } |
172 | | std::string Choose() |
173 | 0 | { |
174 | 0 | if (this->Preferred.empty()) { |
175 | 0 | return ""; |
176 | 0 | } |
177 | 0 | if (this->Preferred.size() > 1) { |
178 | 0 | std::ostringstream e; |
179 | 0 | e << "Target " << this->Target->GetName() |
180 | 0 | << " contains multiple languages with the highest linker preference" |
181 | 0 | << " (" << this->Preference << "):\n"; |
182 | 0 | for (std::string const& li : this->Preferred) { |
183 | 0 | e << " " << li << "\n"; |
184 | 0 | } |
185 | 0 | e << "Set the LINKER_LANGUAGE property for this target."; |
186 | 0 | cmake* cm = this->Target->GetLocalGenerator()->GetCMakeInstance(); |
187 | 0 | cm->IssueMessage(MessageType::FATAL_ERROR, e.str(), |
188 | 0 | this->Target->GetBacktrace()); |
189 | 0 | } |
190 | 0 | return *this->Preferred.begin(); |
191 | 0 | } |
192 | | }; |
193 | | |
194 | | bool cmGeneratorTarget::ComputeLinkClosure(std::string const& config, |
195 | | LinkClosure& lc, |
196 | | bool secondPass) const |
197 | 0 | { |
198 | | // Get languages built in this target. |
199 | 0 | std::unordered_set<std::string> languages; |
200 | 0 | cmLinkImplementation const* impl = |
201 | 0 | this->GetLinkImplementation(config, UseTo::Link, secondPass); |
202 | 0 | assert(impl); |
203 | 0 | languages.insert(impl->Languages.cbegin(), impl->Languages.cend()); |
204 | | |
205 | | // Add interface languages from linked targets. |
206 | | // cmTargetCollectLinkLanguages linkLangs(this, config, languages, this, |
207 | | // secondPass); |
208 | 0 | cmTargetCollectLinkLanguages linkLangs(this, config, languages, this, |
209 | 0 | secondPass); |
210 | 0 | for (cmLinkItem const& lib : impl->Libraries) { |
211 | 0 | linkLangs.Visit(lib); |
212 | 0 | } |
213 | | |
214 | | // Store the transitive closure of languages. |
215 | 0 | cm::append(lc.Languages, languages); |
216 | | |
217 | | // Choose the language whose linker should be used. |
218 | 0 | if (secondPass || lc.LinkerLanguage.empty()) { |
219 | | // Find the language with the highest preference value. |
220 | 0 | cmTargetSelectLinker tsl(this); |
221 | | |
222 | | // First select from the languages compiled directly in this target. |
223 | 0 | for (std::string const& l : impl->Languages) { |
224 | 0 | tsl.Consider(l); |
225 | 0 | } |
226 | | |
227 | | // Now consider languages that propagate from linked targets. |
228 | 0 | for (std::string const& lang : languages) { |
229 | 0 | std::string propagates = |
230 | 0 | cmStrCat("CMAKE_", lang, "_LINKER_PREFERENCE_PROPAGATES"); |
231 | 0 | if (this->Makefile->IsOn(propagates)) { |
232 | 0 | tsl.Consider(lang); |
233 | 0 | } |
234 | 0 | } |
235 | |
|
236 | 0 | lc.LinkerLanguage = tsl.Choose(); |
237 | 0 | } |
238 | |
|
239 | 0 | return impl->HadLinkLanguageSensitiveCondition || |
240 | 0 | linkLangs.GetHadLinkLanguageSensitiveCondition(); |
241 | 0 | } |
242 | | |
243 | | void cmGeneratorTarget::ComputeLinkClosure(std::string const& config, |
244 | | LinkClosure& lc) const |
245 | 0 | { |
246 | 0 | bool secondPass = false; |
247 | |
|
248 | 0 | { |
249 | 0 | LinkClosure linkClosure; |
250 | 0 | linkClosure.LinkerLanguage = this->LinkerLanguage; |
251 | |
|
252 | 0 | bool hasHardCodedLinkerLanguage = this->Target->GetProperty("HAS_CXX") || |
253 | 0 | !this->Target->GetSafeProperty("LINKER_LANGUAGE").empty(); |
254 | | |
255 | | // Get languages built in this target. |
256 | 0 | secondPass = this->ComputeLinkClosure(config, linkClosure, false) && |
257 | 0 | !hasHardCodedLinkerLanguage; |
258 | 0 | this->LinkerLanguage = linkClosure.LinkerLanguage; |
259 | 0 | if (!secondPass) { |
260 | 0 | lc = std::move(linkClosure); |
261 | 0 | } |
262 | 0 | } |
263 | |
|
264 | 0 | if (secondPass) { |
265 | 0 | LinkClosure linkClosure; |
266 | |
|
267 | 0 | this->ComputeLinkClosure(config, linkClosure, secondPass); |
268 | 0 | lc = std::move(linkClosure); |
269 | | |
270 | | // linker language must not be changed between the two passes |
271 | 0 | if (this->LinkerLanguage != lc.LinkerLanguage) { |
272 | 0 | std::ostringstream e; |
273 | 0 | e << "Evaluation of $<LINK_LANGUAGE:...> or $<LINK_LAND_AND_ID:...> " |
274 | 0 | "changes\nthe linker language for target \"" |
275 | 0 | << this->GetName() << "\" (from '" << this->LinkerLanguage << "' to '" |
276 | 0 | << lc.LinkerLanguage << "') which is invalid."; |
277 | 0 | cmSystemTools::Error(e.str()); |
278 | 0 | } |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | | static void processILibs(std::string const& config, |
283 | | cmGeneratorTarget const* headTarget, |
284 | | cmLinkItem const& item, cmGlobalGenerator* gg, |
285 | | std::vector<cmGeneratorTarget const*>& tgts, |
286 | | std::set<cmGeneratorTarget const*>& emitted, |
287 | | UseTo usage) |
288 | 0 | { |
289 | 0 | if (item.Target && emitted.insert(item.Target).second) { |
290 | 0 | tgts.push_back(item.Target); |
291 | 0 | if (cmLinkInterfaceLibraries const* iface = |
292 | 0 | item.Target->GetLinkInterfaceLibraries(config, headTarget, usage)) { |
293 | 0 | for (cmLinkItem const& lib : iface->Libraries) { |
294 | 0 | processILibs(config, headTarget, lib, gg, tgts, emitted, usage); |
295 | 0 | } |
296 | 0 | } |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | std::vector<cmGeneratorTarget const*> |
301 | | cmGeneratorTarget::GetLinkInterfaceClosure(std::string const& config, |
302 | | cmGeneratorTarget const* headTarget, |
303 | | UseTo usage) const |
304 | 0 | { |
305 | 0 | cmGlobalGenerator* gg = this->GetLocalGenerator()->GetGlobalGenerator(); |
306 | 0 | std::vector<cmGeneratorTarget const*> tgts; |
307 | 0 | std::set<cmGeneratorTarget const*> emitted; |
308 | 0 | if (cmLinkInterfaceLibraries const* iface = |
309 | 0 | this->GetLinkInterfaceLibraries(config, headTarget, usage)) { |
310 | 0 | for (cmLinkItem const& lib : iface->Libraries) { |
311 | 0 | processILibs(config, headTarget, lib, gg, tgts, emitted, usage); |
312 | 0 | } |
313 | 0 | } |
314 | 0 | return tgts; |
315 | 0 | } |
316 | | |
317 | | std::vector<cmGeneratorTarget const*> const& |
318 | | cmGeneratorTarget::GetLinkImplementationClosure(std::string const& config, |
319 | | UseTo usage) const |
320 | 0 | { |
321 | | // There is no link implementation for targets that cannot compile sources. |
322 | 0 | if (!this->CanCompileSources()) { |
323 | 0 | static std::vector<cmGeneratorTarget const*> const empty; |
324 | 0 | return empty; |
325 | 0 | } |
326 | | |
327 | 0 | LinkImplClosure& tgts = |
328 | 0 | (usage == UseTo::Compile ? this->LinkImplClosureForUsageMap[config] |
329 | 0 | : this->LinkImplClosureForLinkMap[config]); |
330 | 0 | if (!tgts.Done) { |
331 | 0 | tgts.Done = true; |
332 | 0 | std::set<cmGeneratorTarget const*> emitted; |
333 | |
|
334 | 0 | cmLinkImplementationLibraries const* impl = |
335 | 0 | this->GetLinkImplementationLibraries(config, usage); |
336 | 0 | assert(impl); |
337 | |
|
338 | 0 | for (cmLinkItem const& lib : impl->Libraries) { |
339 | 0 | processILibs(config, this, lib, |
340 | 0 | this->LocalGenerator->GetGlobalGenerator(), tgts, emitted, |
341 | 0 | usage); |
342 | 0 | } |
343 | 0 | } |
344 | 0 | return tgts; |
345 | 0 | } |
346 | | |
347 | | cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation( |
348 | | std::string const& config) const |
349 | 0 | { |
350 | | // Lookup any existing information for this configuration. |
351 | 0 | std::string key(cmSystemTools::UpperCase(config)); |
352 | 0 | auto i = this->LinkInformation.find(key); |
353 | 0 | if (i == this->LinkInformation.end()) { |
354 | | // Compute information for this configuration. |
355 | 0 | auto info = cm::make_unique<cmComputeLinkInformation>(this, config); |
356 | 0 | if (info && !info->Compute()) { |
357 | 0 | info.reset(); |
358 | 0 | } |
359 | | |
360 | | // Store the information for this configuration. |
361 | 0 | i = this->LinkInformation.emplace(key, std::move(info)).first; |
362 | |
|
363 | 0 | if (i->second) { |
364 | 0 | this->CheckPropertyCompatibility(*i->second, config); |
365 | 0 | } |
366 | 0 | } |
367 | 0 | return i->second.get(); |
368 | 0 | } |
369 | | |
370 | | void cmGeneratorTarget::CheckLinkLibraries() const |
371 | 0 | { |
372 | 0 | bool linkLibrariesOnlyTargets = |
373 | 0 | this->GetPropertyAsBool("LINK_LIBRARIES_ONLY_TARGETS"); |
374 | | |
375 | | // Evaluate the link interface of this target if needed for extra checks. |
376 | 0 | if (linkLibrariesOnlyTargets) { |
377 | 0 | std::vector<std::string> const& configs = |
378 | 0 | this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); |
379 | 0 | for (std::string const& config : configs) { |
380 | 0 | this->GetLinkInterfaceLibraries(config, this, UseTo::Link); |
381 | 0 | } |
382 | 0 | } |
383 | | |
384 | | // Check link the implementation for each generated configuration. |
385 | 0 | for (auto const& impl : this->LinkImplMap) { |
386 | 0 | for (cmLinkItem const& item : impl.second.Libraries) { |
387 | 0 | if (!this->VerifyLinkItemColons(LinkItemRole::Implementation, item)) { |
388 | 0 | return; |
389 | 0 | } |
390 | 0 | if (linkLibrariesOnlyTargets && |
391 | 0 | !this->VerifyLinkItemIsTarget(LinkItemRole::Implementation, item)) { |
392 | 0 | return; |
393 | 0 | } |
394 | 0 | } |
395 | 0 | } |
396 | | |
397 | | // Check link the interface for each generated combination of |
398 | | // configuration and consuming head target. We should not need to |
399 | | // consider LinkInterfaceUsageRequirementsOnlyMap because its entries |
400 | | // should be a subset of LinkInterfaceMap (with LINK_ONLY left out). |
401 | 0 | for (auto const& hmp : this->LinkInterfaceMap) { |
402 | 0 | for (auto const& hmi : hmp.second) { |
403 | 0 | if (!hmi.second.LibrariesDone || !hmi.second.CheckLinkLibraries) { |
404 | 0 | continue; |
405 | 0 | } |
406 | 0 | for (cmLinkItem const& item : hmi.second.Libraries) { |
407 | 0 | if (!this->VerifyLinkItemColons(LinkItemRole::Interface, item)) { |
408 | 0 | return; |
409 | 0 | } |
410 | 0 | if (linkLibrariesOnlyTargets && |
411 | 0 | !this->VerifyLinkItemIsTarget(LinkItemRole::Interface, item)) { |
412 | 0 | return; |
413 | 0 | } |
414 | 0 | } |
415 | 0 | } |
416 | 0 | } |
417 | 0 | } |
418 | | |
419 | | cmGeneratorTarget::CheckLinkLibrariesSuppressionRAII:: |
420 | | CheckLinkLibrariesSuppressionRAII() |
421 | 0 | { |
422 | 0 | ++CheckLinkLibrariesSuppressionRAIICount; |
423 | 0 | } |
424 | | |
425 | | cmGeneratorTarget::CheckLinkLibrariesSuppressionRAII:: |
426 | | ~CheckLinkLibrariesSuppressionRAII() |
427 | 0 | { |
428 | 0 | --CheckLinkLibrariesSuppressionRAIICount; |
429 | 0 | } |
430 | | |
431 | | namespace { |
432 | | cm::string_view missingTargetPossibleReasons = |
433 | | "Possible reasons include:\n" |
434 | | " * There is a typo in the target name.\n" |
435 | | " * A find_package call is missing for an IMPORTED target.\n" |
436 | | " * An ALIAS target is missing.\n"_s; |
437 | | } |
438 | | |
439 | | bool cmGeneratorTarget::VerifyLinkItemColons(LinkItemRole role, |
440 | | cmLinkItem const& item) const |
441 | 0 | { |
442 | 0 | if (item.Target || cmHasPrefix(item.AsStr(), "<LINK_GROUP:"_s) || |
443 | 0 | item.AsStr().find("::") == std::string::npos) { |
444 | 0 | return true; |
445 | 0 | } |
446 | 0 | std::string e; |
447 | 0 | if (role == LinkItemRole::Implementation) { |
448 | 0 | e = cmStrCat(e, "Target \"", this->GetName(), "\" links to"); |
449 | 0 | } else { |
450 | 0 | e = cmStrCat(e, "The link interface of target \"", this->GetName(), |
451 | 0 | "\" contains"); |
452 | 0 | } |
453 | 0 | e = cmStrCat(e, ":\n ", item.AsStr(), "\nbut the target was not found. ", |
454 | 0 | missingTargetPossibleReasons); |
455 | 0 | cmListFileBacktrace backtrace = item.Backtrace; |
456 | 0 | if (backtrace.Empty()) { |
457 | 0 | backtrace = this->GetBacktrace(); |
458 | 0 | } |
459 | 0 | this->GetLocalGenerator()->GetCMakeInstance()->IssueMessage( |
460 | 0 | MessageType::FATAL_ERROR, e, backtrace); |
461 | 0 | return false; |
462 | 0 | } |
463 | | |
464 | | bool cmGeneratorTarget::VerifyLinkItemIsTarget(LinkItemRole role, |
465 | | cmLinkItem const& item) const |
466 | 0 | { |
467 | 0 | if (item.Target) { |
468 | 0 | return true; |
469 | 0 | } |
470 | 0 | std::string const& str = item.AsStr(); |
471 | 0 | if (!str.empty() && |
472 | 0 | (str[0] == '-' || str[0] == '$' || str[0] == '`' || |
473 | 0 | str.find_first_of("/\\") != std::string::npos || |
474 | 0 | cmHasPrefix(str, "<LINK_LIBRARY:"_s) || |
475 | 0 | cmHasPrefix(str, "<LINK_GROUP:"_s))) { |
476 | 0 | return true; |
477 | 0 | } |
478 | | |
479 | 0 | std::string e = cmStrCat("Target \"", this->GetName(), |
480 | 0 | "\" has LINK_LIBRARIES_ONLY_TARGETS enabled, but ", |
481 | 0 | role == LinkItemRole::Implementation |
482 | 0 | ? "it links to" |
483 | 0 | : "its link interface contains", |
484 | 0 | ":\n ", item.AsStr(), "\nwhich is not a target. ", |
485 | 0 | missingTargetPossibleReasons); |
486 | 0 | cmListFileBacktrace backtrace = item.Backtrace; |
487 | 0 | if (backtrace.Empty()) { |
488 | 0 | backtrace = this->GetBacktrace(); |
489 | 0 | } |
490 | 0 | this->LocalGenerator->GetCMakeInstance()->IssueMessage( |
491 | 0 | MessageType::FATAL_ERROR, e, backtrace); |
492 | 0 | return false; |
493 | 0 | } |
494 | | |
495 | | bool cmGeneratorTarget::IsLinkLookupScope(std::string const& n, |
496 | | cmLocalGenerator const*& lg) const |
497 | 0 | { |
498 | 0 | if (cmHasLiteralPrefix(n, CMAKE_DIRECTORY_ID_SEP)) { |
499 | 0 | cmDirectoryId const dirId = n.substr(cmStrLen(CMAKE_DIRECTORY_ID_SEP)); |
500 | 0 | if (dirId.String.empty()) { |
501 | 0 | lg = this->LocalGenerator; |
502 | 0 | return true; |
503 | 0 | } |
504 | 0 | if (cmLocalGenerator const* otherLG = |
505 | 0 | this->GlobalGenerator->FindLocalGenerator(dirId)) { |
506 | 0 | lg = otherLG; |
507 | 0 | return true; |
508 | 0 | } |
509 | 0 | } |
510 | 0 | return false; |
511 | 0 | } |
512 | | |
513 | | cm::optional<cmLinkItem> cmGeneratorTarget::LookupLinkItem( |
514 | | std::string const& n, cmListFileBacktrace const& bt, |
515 | | std::string const& linkFeature, LookupLinkItemScope* scope, |
516 | | LookupSelf lookupSelf) const |
517 | 0 | { |
518 | 0 | cm::optional<cmLinkItem> maybeItem; |
519 | 0 | if (this->IsLinkLookupScope(n, scope->LG)) { |
520 | 0 | return maybeItem; |
521 | 0 | } |
522 | | |
523 | 0 | std::string name = this->CheckCMP0004(n); |
524 | 0 | if (name.empty() || |
525 | 0 | (lookupSelf == LookupSelf::No && name == this->GetName())) { |
526 | 0 | return maybeItem; |
527 | 0 | } |
528 | 0 | maybeItem = |
529 | 0 | this->ResolveLinkItem(BT<std::string>(name, bt), scope->LG, linkFeature); |
530 | 0 | return maybeItem; |
531 | 0 | } |
532 | | |
533 | | void cmGeneratorTarget::ExpandLinkItems(std::string const& prop, |
534 | | cmBTStringRange entries, |
535 | | std::string const& config, |
536 | | cmGeneratorTarget const* headTarget, |
537 | | UseTo usage, LinkInterfaceField field, |
538 | | cmLinkInterface& iface) const |
539 | 0 | { |
540 | 0 | if (entries.empty()) { |
541 | 0 | return; |
542 | 0 | } |
543 | | // Keep this logic in sync with ComputeLinkImplementationLibraries. |
544 | 0 | cm::GenEx::Context context(this->LocalGenerator, config, |
545 | 0 | headTarget->LinkerLanguage); |
546 | 0 | cmGeneratorExpressionDAGChecker dagChecker{ |
547 | 0 | this, |
548 | 0 | prop, |
549 | 0 | nullptr, |
550 | 0 | nullptr, |
551 | 0 | context, |
552 | 0 | cmListFileBacktrace(), |
553 | 0 | cmGeneratorExpressionDAGChecker::ComputingLinkLibraries::Yes, |
554 | 0 | }; |
555 | | // The $<LINK_ONLY> expression may be in a link interface to specify |
556 | | // private link dependencies that are otherwise excluded from usage |
557 | | // requirements. |
558 | 0 | if (usage == UseTo::Compile) { |
559 | 0 | dagChecker.SetTransitivePropertiesOnly(); |
560 | 0 | dagChecker.SetTransitivePropertiesOnlyCMP0131(); |
561 | 0 | } |
562 | 0 | cmMakefile const* mf = this->LocalGenerator->GetMakefile(); |
563 | 0 | LookupLinkItemScope scope{ this->LocalGenerator }; |
564 | 0 | for (BT<std::string> const& entry : entries) { |
565 | 0 | cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(), |
566 | 0 | entry.Backtrace); |
567 | 0 | std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(entry.Value); |
568 | 0 | cge->SetEvaluateForBuildsystem(true); |
569 | 0 | cmList libs{ cge->Evaluate(context, &dagChecker, headTarget, this) }; |
570 | |
|
571 | 0 | auto linkFeature = cmLinkItem::DEFAULT; |
572 | 0 | for (auto const& lib : libs) { |
573 | 0 | if (auto maybeLinkFeature = ParseLinkFeature(lib)) { |
574 | 0 | linkFeature = std::move(*maybeLinkFeature); |
575 | 0 | continue; |
576 | 0 | } |
577 | | |
578 | 0 | if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem( |
579 | 0 | lib, cge->GetBacktrace(), linkFeature, &scope, |
580 | 0 | field == LinkInterfaceField::Libraries ? LookupSelf::No |
581 | 0 | : LookupSelf::Yes)) { |
582 | 0 | cmLinkItem item = std::move(*maybeItem); |
583 | |
|
584 | 0 | if (field == LinkInterfaceField::HeadInclude) { |
585 | 0 | item.InterfaceDirectFrom = this; |
586 | 0 | iface.HeadInclude.emplace_back(std::move(item)); |
587 | 0 | continue; |
588 | 0 | } |
589 | 0 | if (field == LinkInterfaceField::HeadExclude) { |
590 | 0 | iface.HeadExclude.emplace_back(std::move(item)); |
591 | 0 | continue; |
592 | 0 | } |
593 | 0 | if (!item.Target) { |
594 | | // Report explicitly linked object files separately. |
595 | 0 | std::string const& maybeObj = item.AsStr(); |
596 | 0 | if (cmSystemTools::FileIsFullPath(maybeObj)) { |
597 | 0 | cmSourceFile const* sf = |
598 | 0 | mf->GetSource(maybeObj, cmSourceFileLocationKind::Known); |
599 | 0 | if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) { |
600 | 0 | item.ObjectSource = sf; |
601 | 0 | iface.Objects.emplace_back(std::move(item)); |
602 | 0 | continue; |
603 | 0 | } |
604 | 0 | } |
605 | 0 | } |
606 | | |
607 | 0 | iface.Libraries.emplace_back(std::move(item)); |
608 | 0 | } |
609 | 0 | } |
610 | 0 | if (cge->GetHadHeadSensitiveCondition()) { |
611 | 0 | iface.HadHeadSensitiveCondition = true; |
612 | 0 | } |
613 | 0 | if (cge->GetHadContextSensitiveCondition()) { |
614 | 0 | iface.HadContextSensitiveCondition = true; |
615 | 0 | } |
616 | 0 | if (cge->GetHadLinkLanguageSensitiveCondition()) { |
617 | 0 | iface.HadLinkLanguageSensitiveCondition = true; |
618 | 0 | } |
619 | 0 | } |
620 | 0 | } |
621 | | |
622 | | cmLinkInterface const* cmGeneratorTarget::GetLinkInterface( |
623 | | std::string const& config, cmGeneratorTarget const* head) const |
624 | 0 | { |
625 | 0 | return this->GetLinkInterface(config, head, false); |
626 | 0 | } |
627 | | |
628 | | cmLinkInterface const* cmGeneratorTarget::GetLinkInterface( |
629 | | std::string const& config, cmGeneratorTarget const* head, |
630 | | bool secondPass) const |
631 | 0 | { |
632 | | // Imported targets have their own link interface. |
633 | 0 | if (this->IsImported()) { |
634 | 0 | return this->GetImportLinkInterface(config, head, UseTo::Link, secondPass); |
635 | 0 | } |
636 | | |
637 | | // Link interfaces are not supported for executables that do not |
638 | | // export symbols. |
639 | 0 | if (this->GetType() == cmStateEnums::EXECUTABLE && |
640 | 0 | !this->IsExecutableWithExports()) { |
641 | 0 | return nullptr; |
642 | 0 | } |
643 | | |
644 | | // Lookup any existing link interface for this configuration. |
645 | 0 | cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config); |
646 | | |
647 | | // If the link interface does not depend on the head target |
648 | | // then reuse the one from the head we computed first. |
649 | 0 | if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) { |
650 | 0 | head = hm.begin()->first; |
651 | 0 | } |
652 | |
|
653 | 0 | cmOptionalLinkInterface& iface = hm[head]; |
654 | 0 | if (secondPass) { |
655 | 0 | iface = cmOptionalLinkInterface(); |
656 | 0 | } |
657 | 0 | MaybeEnableCheckLinkLibraries(iface); |
658 | 0 | if (!iface.LibrariesDone) { |
659 | 0 | iface.LibrariesDone = true; |
660 | 0 | this->ComputeLinkInterfaceLibraries(config, iface, head, UseTo::Link); |
661 | 0 | } |
662 | 0 | if (!iface.AllDone) { |
663 | 0 | iface.AllDone = true; |
664 | 0 | if (iface.Exists) { |
665 | 0 | this->ComputeLinkInterface(config, iface, secondPass); |
666 | 0 | this->ComputeLinkInterfaceRuntimeLibraries(config, iface); |
667 | 0 | } |
668 | 0 | } |
669 | |
|
670 | 0 | return iface.Exists ? &iface : nullptr; |
671 | 0 | } |
672 | | |
673 | | void cmGeneratorTarget::ComputeLinkInterface(std::string const& config, |
674 | | cmOptionalLinkInterface& iface, |
675 | | bool secondPass) const |
676 | 0 | { |
677 | 0 | if (this->GetType() == cmStateEnums::SHARED_LIBRARY || |
678 | 0 | this->GetType() == cmStateEnums::STATIC_LIBRARY || |
679 | 0 | this->GetType() == cmStateEnums::INTERFACE_LIBRARY) { |
680 | | // Shared libraries may have runtime implementation dependencies |
681 | | // on other shared libraries that are not in the interface. |
682 | 0 | std::set<cmLinkItem> emitted; |
683 | 0 | for (cmLinkItem const& lib : iface.Libraries) { |
684 | 0 | emitted.insert(lib); |
685 | 0 | } |
686 | 0 | if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) { |
687 | 0 | cmLinkImplementation const* impl = |
688 | 0 | this->GetLinkImplementation(config, UseTo::Link, secondPass); |
689 | 0 | for (cmLinkItem const& lib : impl->Libraries) { |
690 | 0 | if (emitted.insert(lib).second) { |
691 | 0 | if (lib.Target) { |
692 | | // This is a runtime dependency on another shared library. |
693 | 0 | if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) { |
694 | 0 | iface.SharedDeps.push_back(lib); |
695 | 0 | } |
696 | 0 | } else { |
697 | | // TODO: Recognize shared library file names. Perhaps this |
698 | | // should be moved to cmComputeLinkInformation, but that |
699 | | // creates a chicken-and-egg problem since this list is needed |
700 | | // for its construction. |
701 | 0 | } |
702 | 0 | } |
703 | 0 | } |
704 | 0 | } |
705 | 0 | } |
706 | |
|
707 | 0 | if (this->LinkLanguagePropagatesToDependents()) { |
708 | | // Targets using this archive need its language runtime libraries. |
709 | 0 | if (cmLinkImplementation const* impl = |
710 | 0 | this->GetLinkImplementation(config, UseTo::Link, secondPass)) { |
711 | 0 | iface.Languages = impl->Languages; |
712 | 0 | } |
713 | 0 | } |
714 | |
|
715 | 0 | if (this->GetType() == cmStateEnums::STATIC_LIBRARY) { |
716 | | // Construct the property name suffix for this configuration. |
717 | 0 | std::string suffix = "_"; |
718 | 0 | if (!config.empty()) { |
719 | 0 | suffix += cmSystemTools::UpperCase(config); |
720 | 0 | } else { |
721 | 0 | suffix += "NOCONFIG"; |
722 | 0 | } |
723 | | |
724 | | // How many repetitions are needed if this library has cyclic |
725 | | // dependencies? |
726 | 0 | std::string propName = cmStrCat("LINK_INTERFACE_MULTIPLICITY", suffix); |
727 | 0 | if (cmValue config_reps = this->GetProperty(propName)) { |
728 | 0 | sscanf(config_reps->c_str(), "%u", &iface.Multiplicity); |
729 | 0 | } else if (cmValue reps = |
730 | 0 | this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) { |
731 | 0 | sscanf(reps->c_str(), "%u", &iface.Multiplicity); |
732 | 0 | } |
733 | 0 | } |
734 | 0 | } |
735 | | |
736 | | cmLinkInterfaceLibraries const* cmGeneratorTarget::GetLinkInterfaceLibraries( |
737 | | std::string const& config, cmGeneratorTarget const* head, UseTo usage) const |
738 | 0 | { |
739 | | // Imported targets have their own link interface. |
740 | 0 | if (this->IsImported()) { |
741 | 0 | return this->GetImportLinkInterface(config, head, usage); |
742 | 0 | } |
743 | | |
744 | | // Link interfaces are not supported for executables that do not |
745 | | // export symbols. |
746 | 0 | if (this->GetType() == cmStateEnums::EXECUTABLE && |
747 | 0 | !this->IsExecutableWithExports()) { |
748 | 0 | return nullptr; |
749 | 0 | } |
750 | | |
751 | | // Lookup any existing link interface for this configuration. |
752 | 0 | cmHeadToLinkInterfaceMap& hm = |
753 | 0 | (usage == UseTo::Compile |
754 | 0 | ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config) |
755 | 0 | : this->GetHeadToLinkInterfaceMap(config)); |
756 | | |
757 | | // If the link interface does not depend on the head target |
758 | | // then reuse the one from the head we computed first. |
759 | 0 | if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) { |
760 | 0 | head = hm.begin()->first; |
761 | 0 | } |
762 | |
|
763 | 0 | cmOptionalLinkInterface& iface = hm[head]; |
764 | 0 | MaybeEnableCheckLinkLibraries(iface); |
765 | 0 | if (!iface.LibrariesDone) { |
766 | 0 | iface.LibrariesDone = true; |
767 | 0 | this->ComputeLinkInterfaceLibraries(config, iface, head, usage); |
768 | 0 | } |
769 | |
|
770 | 0 | return iface.Exists ? &iface : nullptr; |
771 | 0 | } |
772 | | |
773 | | void cmGeneratorTarget::ComputeLinkInterfaceLibraries( |
774 | | std::string const& config, cmOptionalLinkInterface& iface, |
775 | | cmGeneratorTarget const* headTarget, UseTo usage) const |
776 | 0 | { |
777 | | // Construct the property name suffix for this configuration. |
778 | 0 | std::string suffix = "_"; |
779 | 0 | if (!config.empty()) { |
780 | 0 | suffix += cmSystemTools::UpperCase(config); |
781 | 0 | } else { |
782 | 0 | suffix += "NOCONFIG"; |
783 | 0 | } |
784 | | |
785 | | // An explicit list of interface libraries may be set for shared |
786 | | // libraries and executables that export symbols. |
787 | 0 | bool const haveExplicitLibraries = |
788 | 0 | !this->Target->GetLinkInterfaceEntries().empty() || |
789 | 0 | !this->Target->GetLinkInterfaceDirectEntries().empty() || |
790 | 0 | !this->Target->GetLinkInterfaceDirectExcludeEntries().empty(); |
791 | | |
792 | | // There is no implicit link interface for executables or modules |
793 | | // so if none was explicitly set then there is no link interface. |
794 | 0 | if (!haveExplicitLibraries && |
795 | 0 | (this->GetType() == cmStateEnums::EXECUTABLE || |
796 | 0 | (this->GetType() == cmStateEnums::MODULE_LIBRARY))) { |
797 | 0 | return; |
798 | 0 | } |
799 | 0 | iface.Exists = true; |
800 | | |
801 | | // The interface libraries are specified by INTERFACE_LINK_LIBRARIES. |
802 | | // Use its special representation directly to get backtraces. |
803 | 0 | this->ExpandLinkItems( |
804 | 0 | kINTERFACE_LINK_LIBRARIES, this->Target->GetLinkInterfaceEntries(), config, |
805 | 0 | headTarget, usage, LinkInterfaceField::Libraries, iface); |
806 | 0 | this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT, |
807 | 0 | this->Target->GetLinkInterfaceDirectEntries(), config, |
808 | 0 | headTarget, usage, LinkInterfaceField::HeadInclude, |
809 | 0 | iface); |
810 | 0 | this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE, |
811 | 0 | this->Target->GetLinkInterfaceDirectExcludeEntries(), |
812 | 0 | config, headTarget, usage, |
813 | 0 | LinkInterfaceField::HeadExclude, iface); |
814 | 0 | } |
815 | | |
816 | | std::vector<cmLinkItem> cmGeneratorTarget::ComputeImplicitLanguageTargets( |
817 | | std::string const& lang, std::string const& config) const |
818 | 0 | { |
819 | 0 | cmListFileBacktrace bt; |
820 | 0 | std::vector<cmLinkItem> result; |
821 | 0 | cmLocalGenerator* lg = this->GetLocalGenerator(); |
822 | |
|
823 | 0 | std::string const& runtimeLibrary = |
824 | 0 | this->GetRuntimeLinkLibrary(lang, config); |
825 | 0 | if (cmValue runtimeLinkOptions = this->Makefile->GetDefinition( |
826 | 0 | cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARIES_", runtimeLibrary))) { |
827 | 0 | cmList libsList{ *runtimeLinkOptions }; |
828 | 0 | result.reserve(libsList.size()); |
829 | |
|
830 | 0 | for (auto const& i : libsList) { |
831 | 0 | cmGeneratorTarget::TargetOrString resolved = |
832 | 0 | this->ResolveTargetReference(i, lg); |
833 | 0 | if (resolved.Target) { |
834 | 0 | result.emplace_back(resolved.Target, false, bt); |
835 | 0 | } |
836 | 0 | } |
837 | 0 | } |
838 | |
|
839 | 0 | return result; |
840 | 0 | } |
841 | | |
842 | | void cmGeneratorTarget::ComputeLinkInterfaceRuntimeLibraries( |
843 | | std::string const& config, cmOptionalLinkInterface& iface) const |
844 | 0 | { |
845 | 0 | for (std::string const& lang : iface.Languages) { |
846 | 0 | if ((lang == "CUDA" || lang == "HIP") && |
847 | 0 | iface.LanguageRuntimeLibraries.find(lang) == |
848 | 0 | iface.LanguageRuntimeLibraries.end()) { |
849 | 0 | iface.LanguageRuntimeLibraries[lang] = |
850 | 0 | this->ComputeImplicitLanguageTargets(lang, config); |
851 | 0 | } |
852 | 0 | } |
853 | 0 | } |
854 | | |
855 | | void cmGeneratorTarget::ComputeLinkImplementationRuntimeLibraries( |
856 | | std::string const& config, cmOptionalLinkImplementation& impl) const |
857 | 0 | { |
858 | 0 | for (std::string const& lang : impl.Languages) { |
859 | 0 | if ((lang == "CUDA" || lang == "HIP") && |
860 | 0 | impl.LanguageRuntimeLibraries.find(lang) == |
861 | 0 | impl.LanguageRuntimeLibraries.end()) { |
862 | 0 | impl.LanguageRuntimeLibraries[lang] = |
863 | 0 | this->ComputeImplicitLanguageTargets(lang, config); |
864 | 0 | } |
865 | 0 | } |
866 | 0 | } |
867 | | |
868 | | cmLinkInterface const* cmGeneratorTarget::GetImportLinkInterface( |
869 | | std::string const& config, cmGeneratorTarget const* headTarget, UseTo usage, |
870 | | bool secondPass) const |
871 | 0 | { |
872 | 0 | cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config); |
873 | 0 | if (!info) { |
874 | 0 | return nullptr; |
875 | 0 | } |
876 | | |
877 | 0 | cmHeadToLinkInterfaceMap& hm = |
878 | 0 | (usage == UseTo::Compile |
879 | 0 | ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config) |
880 | 0 | : this->GetHeadToLinkInterfaceMap(config)); |
881 | | |
882 | | // If the link interface does not depend on the head target |
883 | | // then reuse the one from the head we computed first. |
884 | 0 | if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) { |
885 | 0 | headTarget = hm.begin()->first; |
886 | 0 | } |
887 | |
|
888 | 0 | cmOptionalLinkInterface& iface = hm[headTarget]; |
889 | 0 | if (secondPass) { |
890 | 0 | iface = cmOptionalLinkInterface(); |
891 | 0 | } |
892 | 0 | MaybeEnableCheckLinkLibraries(iface); |
893 | 0 | if (!iface.AllDone) { |
894 | 0 | iface.AllDone = true; |
895 | 0 | iface.LibrariesDone = true; |
896 | 0 | iface.Multiplicity = info->Multiplicity; |
897 | 0 | cmExpandList(info->Languages, iface.Languages); |
898 | 0 | this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT, |
899 | 0 | cmMakeRange(info->LibrariesHeadInclude), config, |
900 | 0 | headTarget, usage, LinkInterfaceField::HeadInclude, |
901 | 0 | iface); |
902 | 0 | this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE, |
903 | 0 | cmMakeRange(info->LibrariesHeadExclude), config, |
904 | 0 | headTarget, usage, LinkInterfaceField::HeadExclude, |
905 | 0 | iface); |
906 | 0 | this->ExpandLinkItems(info->LibrariesProp, cmMakeRange(info->Libraries), |
907 | 0 | config, headTarget, usage, |
908 | 0 | LinkInterfaceField::Libraries, iface); |
909 | 0 | cmList deps{ info->SharedDeps }; |
910 | 0 | LookupLinkItemScope scope{ this->LocalGenerator }; |
911 | |
|
912 | 0 | auto linkFeature = cmLinkItem::DEFAULT; |
913 | 0 | for (auto const& dep : deps) { |
914 | 0 | if (auto maybeLinkFeature = ParseLinkFeature(dep)) { |
915 | 0 | linkFeature = std::move(*maybeLinkFeature); |
916 | 0 | continue; |
917 | 0 | } |
918 | | |
919 | 0 | if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem( |
920 | 0 | dep, cmListFileBacktrace(), linkFeature, &scope, LookupSelf::No)) { |
921 | 0 | iface.SharedDeps.emplace_back(std::move(*maybeItem)); |
922 | 0 | } |
923 | 0 | } |
924 | 0 | } |
925 | |
|
926 | 0 | return &iface; |
927 | 0 | } |
928 | | |
929 | | cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap( |
930 | | std::string const& config) const |
931 | 0 | { |
932 | 0 | return this->LinkInterfaceMap[cmSystemTools::UpperCase(config)]; |
933 | 0 | } |
934 | | |
935 | | cmHeadToLinkInterfaceMap& |
936 | | cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap( |
937 | | std::string const& config) const |
938 | 0 | { |
939 | 0 | return this |
940 | 0 | ->LinkInterfaceUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)]; |
941 | 0 | } |
942 | | |
943 | | cmLinkImplementation const* cmGeneratorTarget::GetLinkImplementation( |
944 | | std::string const& config, UseTo usage) const |
945 | 0 | { |
946 | 0 | return this->GetLinkImplementation(config, usage, false); |
947 | 0 | } |
948 | | |
949 | | cmLinkImplementation const* cmGeneratorTarget::GetLinkImplementation( |
950 | | std::string const& config, UseTo usage, bool secondPass) const |
951 | 0 | { |
952 | | // There is no link implementation for targets that cannot compile sources. |
953 | 0 | if (!this->CanCompileSources()) { |
954 | 0 | return nullptr; |
955 | 0 | } |
956 | | |
957 | 0 | cmOptionalLinkImplementation& impl = |
958 | 0 | (usage == UseTo::Compile |
959 | 0 | ? this |
960 | 0 | ->LinkImplUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)] |
961 | 0 | : this->LinkImplMap[cmSystemTools::UpperCase(config)]); |
962 | 0 | if (secondPass) { |
963 | 0 | impl = cmOptionalLinkImplementation(); |
964 | 0 | } |
965 | 0 | MaybeEnableCheckLinkLibraries(impl); |
966 | 0 | if (!impl.LibrariesDone) { |
967 | 0 | impl.LibrariesDone = true; |
968 | 0 | this->ComputeLinkImplementationLibraries(config, impl, usage); |
969 | 0 | } |
970 | 0 | if (!impl.LanguagesDone) { |
971 | 0 | impl.LanguagesDone = true; |
972 | 0 | this->ComputeLinkImplementationLanguages(config, impl); |
973 | 0 | this->ComputeLinkImplementationRuntimeLibraries(config, impl); |
974 | 0 | } |
975 | 0 | return &impl; |
976 | 0 | } |
977 | | |
978 | | cmLinkImplementationLibraries const* |
979 | | cmGeneratorTarget::GetLinkImplementationLibraries(std::string const& config, |
980 | | UseTo usage) const |
981 | 0 | { |
982 | | // There is no link implementation for targets that cannot compile sources. |
983 | 0 | if (!this->CanCompileSources()) { |
984 | 0 | return nullptr; |
985 | 0 | } |
986 | | |
987 | | // Populate the link implementation libraries for this configuration. |
988 | 0 | cmOptionalLinkImplementation& impl = |
989 | 0 | (usage == UseTo::Compile |
990 | 0 | ? this |
991 | 0 | ->LinkImplUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)] |
992 | 0 | : this->LinkImplMap[cmSystemTools::UpperCase(config)]); |
993 | 0 | MaybeEnableCheckLinkLibraries(impl); |
994 | 0 | if (!impl.LibrariesDone) { |
995 | 0 | impl.LibrariesDone = true; |
996 | 0 | this->ComputeLinkImplementationLibraries(config, impl, usage); |
997 | 0 | } |
998 | 0 | return &impl; |
999 | 0 | } |
1000 | | |
1001 | | namespace { |
1002 | | class TransitiveLinkImpl |
1003 | | { |
1004 | | cmGeneratorTarget const* Self; |
1005 | | std::string const& Config; |
1006 | | UseTo ImplFor; |
1007 | | cmLinkImplementation& Impl; |
1008 | | |
1009 | | std::set<cmLinkItem> Emitted; |
1010 | | std::set<cmLinkItem> Excluded; |
1011 | | std::unordered_set<cmGeneratorTarget const*> Followed; |
1012 | | |
1013 | | void Follow(cmGeneratorTarget const* target); |
1014 | | |
1015 | | public: |
1016 | | TransitiveLinkImpl(cmGeneratorTarget const* self, std::string const& config, |
1017 | | UseTo usage, cmLinkImplementation& impl) |
1018 | 0 | : Self(self) |
1019 | 0 | , Config(config) |
1020 | 0 | , ImplFor(usage) |
1021 | 0 | , Impl(impl) |
1022 | 0 | { |
1023 | 0 | } |
1024 | | |
1025 | | void Compute(); |
1026 | | }; |
1027 | | |
1028 | | void TransitiveLinkImpl::Follow(cmGeneratorTarget const* target) |
1029 | 0 | { |
1030 | 0 | if (!target || !this->Followed.insert(target).second) { |
1031 | 0 | return; |
1032 | 0 | } |
1033 | | |
1034 | | // Get this target's usage requirements. |
1035 | 0 | cmLinkInterfaceLibraries const* iface = |
1036 | 0 | target->GetLinkInterfaceLibraries(this->Config, this->Self, this->ImplFor); |
1037 | 0 | if (!iface) { |
1038 | 0 | return; |
1039 | 0 | } |
1040 | 0 | if (iface->HadContextSensitiveCondition) { |
1041 | 0 | this->Impl.HadContextSensitiveCondition = true; |
1042 | 0 | } |
1043 | | |
1044 | | // Process 'INTERFACE_LINK_LIBRARIES_DIRECT' usage requirements. |
1045 | 0 | for (cmLinkItem const& item : iface->HeadInclude) { |
1046 | | // Inject direct dependencies from the item's usage requirements |
1047 | | // before the item itself. |
1048 | 0 | this->Follow(item.Target); |
1049 | | |
1050 | | // Add the item itself, but at most once. |
1051 | 0 | if (this->Emitted.insert(item).second) { |
1052 | 0 | this->Impl.Libraries.emplace_back(item); |
1053 | 0 | } |
1054 | 0 | } |
1055 | | |
1056 | | // Follow transitive dependencies. |
1057 | 0 | for (cmLinkItem const& item : iface->Libraries) { |
1058 | 0 | this->Follow(item.Target); |
1059 | 0 | } |
1060 | | |
1061 | | // Record exclusions from 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE' |
1062 | | // usage requirements. |
1063 | 0 | for (cmLinkItem const& item : iface->HeadExclude) { |
1064 | 0 | this->Excluded.insert(item); |
1065 | 0 | } |
1066 | 0 | } |
1067 | | |
1068 | | void TransitiveLinkImpl::Compute() |
1069 | 0 | { |
1070 | | // Save the original items and start with an empty list. |
1071 | 0 | std::vector<cmLinkItem> original = std::move(this->Impl.Libraries); |
1072 | | |
1073 | | // Avoid injecting any original items as usage requirements. |
1074 | | // This gives LINK_LIBRARIES final control over the order |
1075 | | // if it explicitly lists everything. |
1076 | 0 | this->Emitted.insert(original.cbegin(), original.cend()); |
1077 | | |
1078 | | // Process each original item. |
1079 | 0 | for (cmLinkItem& item : original) { |
1080 | | // Inject direct dependencies listed in 'INTERFACE_LINK_LIBRARIES_DIRECT' |
1081 | | // usage requirements before the item itself. |
1082 | 0 | this->Follow(item.Target); |
1083 | | |
1084 | | // Add the item itself. |
1085 | 0 | this->Impl.Libraries.emplace_back(std::move(item)); |
1086 | 0 | } |
1087 | | |
1088 | | // Remove items listed in 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE' |
1089 | | // usage requirements found through any dependency above. |
1090 | 0 | this->Impl.Libraries.erase( |
1091 | 0 | std::remove_if(this->Impl.Libraries.begin(), this->Impl.Libraries.end(), |
1092 | 0 | [this](cmLinkItem const& item) { |
1093 | 0 | return this->Excluded.find(item) != this->Excluded.end(); |
1094 | 0 | }), |
1095 | 0 | this->Impl.Libraries.end()); |
1096 | 0 | } |
1097 | | |
1098 | | void ComputeLinkImplTransitive(cmGeneratorTarget const* self, |
1099 | | std::string const& config, UseTo usage, |
1100 | | cmLinkImplementation& impl) |
1101 | 0 | { |
1102 | 0 | TransitiveLinkImpl transitiveLinkImpl(self, config, usage, impl); |
1103 | 0 | transitiveLinkImpl.Compute(); |
1104 | 0 | } |
1105 | | } |
1106 | | |
1107 | | void cmGeneratorTarget::ComputeLinkImplementationLibraries( |
1108 | | std::string const& config, cmOptionalLinkImplementation& impl, |
1109 | | UseTo usage) const |
1110 | 0 | { |
1111 | 0 | cm::GenEx::Context context(this->LocalGenerator, config, |
1112 | 0 | this->LinkerLanguage); |
1113 | 0 | cmLocalGenerator const* lg = this->LocalGenerator; |
1114 | 0 | cmMakefile const* mf = lg->GetMakefile(); |
1115 | 0 | cmBTStringRange entryRange = this->Target->GetLinkImplementationEntries(); |
1116 | 0 | auto const& synthTargetsForConfig = this->Configs[config].SyntheticDeps; |
1117 | | // Collect libraries directly linked in this configuration. |
1118 | 0 | for (auto const& entry : entryRange) { |
1119 | | // Keep this logic in sync with ExpandLinkItems. |
1120 | 0 | cmGeneratorExpressionDAGChecker dagChecker{ |
1121 | 0 | this, |
1122 | 0 | "LINK_LIBRARIES", |
1123 | 0 | nullptr, |
1124 | 0 | nullptr, |
1125 | 0 | context, |
1126 | 0 | cmListFileBacktrace(), |
1127 | 0 | cmGeneratorExpressionDAGChecker::ComputingLinkLibraries::Yes, |
1128 | 0 | }; |
1129 | | // The $<LINK_ONLY> expression may be used to specify link dependencies |
1130 | | // that are otherwise excluded from usage requirements. |
1131 | 0 | if (usage == UseTo::Compile) { |
1132 | 0 | dagChecker.SetTransitivePropertiesOnly(); |
1133 | 0 | switch (this->GetPolicyStatusCMP0131()) { |
1134 | 0 | case cmPolicies::WARN: |
1135 | 0 | case cmPolicies::OLD: |
1136 | 0 | break; |
1137 | 0 | case cmPolicies::NEW: |
1138 | 0 | dagChecker.SetTransitivePropertiesOnlyCMP0131(); |
1139 | 0 | break; |
1140 | 0 | } |
1141 | 0 | } |
1142 | 0 | cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(), |
1143 | 0 | entry.Backtrace); |
1144 | 0 | std::unique_ptr<cmCompiledGeneratorExpression> const cge = |
1145 | 0 | ge.Parse(entry.Value); |
1146 | 0 | cge->SetEvaluateForBuildsystem(true); |
1147 | 0 | std::string const& evaluated = cge->Evaluate(context, &dagChecker, this); |
1148 | 0 | cmList llibs(evaluated); |
1149 | 0 | if (cge->GetHadHeadSensitiveCondition()) { |
1150 | 0 | impl.HadHeadSensitiveCondition = true; |
1151 | 0 | } |
1152 | 0 | if (cge->GetHadContextSensitiveCondition()) { |
1153 | 0 | impl.HadContextSensitiveCondition = true; |
1154 | 0 | } |
1155 | 0 | if (cge->GetHadLinkLanguageSensitiveCondition()) { |
1156 | 0 | impl.HadLinkLanguageSensitiveCondition = true; |
1157 | 0 | } |
1158 | |
|
1159 | 0 | auto linkFeature = cmLinkItem::DEFAULT; |
1160 | 0 | for (auto const& lib : llibs) { |
1161 | 0 | if (auto maybeLinkFeature = ParseLinkFeature(lib)) { |
1162 | 0 | linkFeature = std::move(*maybeLinkFeature); |
1163 | 0 | continue; |
1164 | 0 | } |
1165 | | |
1166 | 0 | if (this->IsLinkLookupScope(lib, lg)) { |
1167 | 0 | continue; |
1168 | 0 | } |
1169 | | |
1170 | | // Skip entries that resolve to the target itself or are empty. |
1171 | 0 | std::string name = this->CheckCMP0004(lib); |
1172 | 0 | if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) { |
1173 | | // resolve alias name |
1174 | 0 | auto* target = this->Makefile->FindTargetToUse( |
1175 | 0 | name, cmStateEnums::AllTargetDomains); |
1176 | 0 | if (target) { |
1177 | 0 | name = target->GetName(); |
1178 | 0 | } |
1179 | 0 | } |
1180 | 0 | if (name == this->GetName() || name.empty()) { |
1181 | 0 | if (name == this->GetName()) { |
1182 | 0 | this->LocalGenerator->GetCMakeInstance()->IssueMessage( |
1183 | 0 | MessageType::FATAL_ERROR, |
1184 | 0 | cmStrCat("Target \"", this->GetName(), "\" links to itself."), |
1185 | 0 | this->GetBacktrace()); |
1186 | 0 | return; |
1187 | 0 | } |
1188 | 0 | continue; |
1189 | 0 | } |
1190 | | |
1191 | | // The entry is meant for this configuration. |
1192 | 0 | cmLinkItem item = this->ResolveLinkItem( |
1193 | 0 | BT<std::string>(name, entry.Backtrace), lg, linkFeature); |
1194 | 0 | if (item.Target) { |
1195 | 0 | auto depsForTarget = synthTargetsForConfig.find(item.Target); |
1196 | 0 | if (depsForTarget != synthTargetsForConfig.end()) { |
1197 | 0 | for (auto const* depForTarget : depsForTarget->second) { |
1198 | 0 | cmLinkItem synthItem(depForTarget, item.Cross, item.Backtrace); |
1199 | 0 | impl.Libraries.emplace_back(std::move(synthItem)); |
1200 | 0 | } |
1201 | 0 | } |
1202 | 0 | } else { |
1203 | | // Report explicitly linked object files separately. |
1204 | 0 | std::string const& maybeObj = item.AsStr(); |
1205 | 0 | if (cmSystemTools::FileIsFullPath(maybeObj)) { |
1206 | 0 | cmSourceFile const* sf = |
1207 | 0 | mf->GetSource(maybeObj, cmSourceFileLocationKind::Known); |
1208 | 0 | if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) { |
1209 | 0 | item.ObjectSource = sf; |
1210 | 0 | impl.Objects.emplace_back(std::move(item)); |
1211 | 0 | continue; |
1212 | 0 | } |
1213 | 0 | } |
1214 | 0 | } |
1215 | | |
1216 | 0 | impl.Libraries.emplace_back(std::move(item)); |
1217 | 0 | } |
1218 | | |
1219 | 0 | std::set<std::string> const& seenProps = cge->GetSeenTargetProperties(); |
1220 | 0 | for (std::string const& sp : seenProps) { |
1221 | 0 | if (!this->GetProperty(sp)) { |
1222 | 0 | this->LinkImplicitNullProperties.insert(sp); |
1223 | 0 | } |
1224 | 0 | } |
1225 | 0 | cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards); |
1226 | 0 | } |
1227 | | |
1228 | | // Update the list of direct link dependencies from usage requirements. |
1229 | 0 | ComputeLinkImplTransitive(this, config, usage, impl); |
1230 | | |
1231 | | // Get the list of configurations considered to be DEBUG. |
1232 | 0 | std::vector<std::string> debugConfigs = |
1233 | 0 | this->Makefile->GetCMakeInstance()->GetDebugConfigs(); |
1234 | |
|
1235 | 0 | cmTargetLinkLibraryType linkType = ComputeLinkType(config, debugConfigs); |
1236 | 0 | cmTarget::LinkLibraryVectorType const& oldllibs = |
1237 | 0 | this->Target->GetOriginalLinkLibraries(); |
1238 | |
|
1239 | 0 | auto linkFeature = cmLinkItem::DEFAULT; |
1240 | 0 | for (cmTarget::LibraryID const& oldllib : oldllibs) { |
1241 | 0 | if (auto maybeLinkFeature = ParseLinkFeature(oldllib.first)) { |
1242 | 0 | linkFeature = std::move(*maybeLinkFeature); |
1243 | 0 | continue; |
1244 | 0 | } |
1245 | | |
1246 | 0 | if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) { |
1247 | 0 | std::string name = this->CheckCMP0004(oldllib.first); |
1248 | 0 | if (name == this->GetName() || name.empty()) { |
1249 | 0 | continue; |
1250 | 0 | } |
1251 | 0 | } |
1252 | 0 | } |
1253 | 0 | } |
1254 | | |
1255 | | cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference( |
1256 | | std::string const& name) const |
1257 | 0 | { |
1258 | 0 | return this->ResolveTargetReference(name, this->LocalGenerator); |
1259 | 0 | } |
1260 | | |
1261 | | cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference( |
1262 | | std::string const& name, cmLocalGenerator const* lg) const |
1263 | 0 | { |
1264 | 0 | TargetOrString resolved; |
1265 | |
|
1266 | 0 | if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) { |
1267 | 0 | resolved.Target = tgt; |
1268 | 0 | } else { |
1269 | 0 | resolved.String = name; |
1270 | 0 | } |
1271 | |
|
1272 | 0 | return resolved; |
1273 | 0 | } |
1274 | | |
1275 | | cmLinkItem cmGeneratorTarget::ResolveLinkItem( |
1276 | | BT<std::string> const& name, std::string const& linkFeature) const |
1277 | 0 | { |
1278 | 0 | return this->ResolveLinkItem(name, this->LocalGenerator, linkFeature); |
1279 | 0 | } |
1280 | | |
1281 | | cmLinkItem cmGeneratorTarget::ResolveLinkItem( |
1282 | | BT<std::string> const& name, cmLocalGenerator const* lg, |
1283 | | std::string const& linkFeature) const |
1284 | 0 | { |
1285 | 0 | auto bt = name.Backtrace; |
1286 | 0 | TargetOrString resolved = this->ResolveTargetReference(name.Value, lg); |
1287 | |
|
1288 | 0 | if (!resolved.Target) { |
1289 | 0 | return cmLinkItem(resolved.String, false, bt, linkFeature); |
1290 | 0 | } |
1291 | | |
1292 | | // Check deprecation, issue message with `bt` backtrace. |
1293 | 0 | if (resolved.Target->IsDeprecated()) { |
1294 | 0 | std::ostringstream w; |
1295 | | /* clang-format off */ |
1296 | 0 | w << |
1297 | 0 | "The library that is being linked to, " << resolved.Target->GetName() << |
1298 | 0 | ", is marked as being deprecated by the owner. The message provided by " |
1299 | 0 | "the developer is: \n" << resolved.Target->GetDeprecation() << "\n"; |
1300 | | /* clang-format on */ |
1301 | 0 | this->LocalGenerator->GetCMakeInstance()->IssueMessage( |
1302 | 0 | MessageType::AUTHOR_WARNING, w.str(), bt); |
1303 | 0 | } |
1304 | | |
1305 | | // Skip targets that will not really be linked. This is probably a |
1306 | | // name conflict between an external library and an executable |
1307 | | // within the project. |
1308 | 0 | if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE && |
1309 | 0 | !resolved.Target->IsExecutableWithExports()) { |
1310 | 0 | return cmLinkItem(resolved.Target->GetName(), false, bt, linkFeature); |
1311 | 0 | } |
1312 | | |
1313 | 0 | return cmLinkItem(resolved.Target, false, bt, linkFeature); |
1314 | 0 | } |