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