cmGeneratorTarget_Link.cxx 47 KB

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