cmGeneratorTarget_Link.cxx 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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& impl : this->LinkImplMap) {
  343. for (cmLinkImplItem const& item : impl.second.Libraries) {
  344. if (!this->VerifyLinkItemColons(LinkItemRole::Implementation, item)) {
  345. return;
  346. }
  347. if (linkLibrariesOnlyTargets &&
  348. !this->VerifyLinkItemIsTarget(LinkItemRole::Implementation, item)) {
  349. return;
  350. }
  351. }
  352. }
  353. // Check link the interface for each generated combination of
  354. // configuration and consuming head target. We should not need to
  355. // consider LinkInterfaceUsageRequirementsOnlyMap because its entries
  356. // should be a subset of LinkInterfaceMap (with LINK_ONLY left out).
  357. for (auto const& hmp : this->LinkInterfaceMap) {
  358. for (auto const& hmi : hmp.second) {
  359. if (!hmi.second.LibrariesDone || !hmi.second.CheckLinkLibraries) {
  360. continue;
  361. }
  362. for (cmLinkItem const& item : hmi.second.Libraries) {
  363. if (!this->VerifyLinkItemColons(LinkItemRole::Interface, item)) {
  364. return;
  365. }
  366. if (linkLibrariesOnlyTargets &&
  367. !this->VerifyLinkItemIsTarget(LinkItemRole::Interface, item)) {
  368. return;
  369. }
  370. }
  371. }
  372. }
  373. }
  374. cmGeneratorTarget::CheckLinkLibrariesSuppressionRAII::
  375. CheckLinkLibrariesSuppressionRAII()
  376. {
  377. ++CheckLinkLibrariesSuppressionRAIICount;
  378. }
  379. cmGeneratorTarget::CheckLinkLibrariesSuppressionRAII::
  380. ~CheckLinkLibrariesSuppressionRAII()
  381. {
  382. --CheckLinkLibrariesSuppressionRAIICount;
  383. }
  384. namespace {
  385. cm::string_view missingTargetPossibleReasons =
  386. "Possible reasons include:\n"
  387. " * There is a typo in the target name.\n"
  388. " * A find_package call is missing for an IMPORTED target.\n"
  389. " * An ALIAS target is missing.\n"_s;
  390. }
  391. bool cmGeneratorTarget::VerifyLinkItemColons(LinkItemRole role,
  392. cmLinkItem const& item) const
  393. {
  394. if (item.Target || cmHasPrefix(item.AsStr(), "<LINK_GROUP:"_s) ||
  395. item.AsStr().find("::") == std::string::npos) {
  396. return true;
  397. }
  398. MessageType messageType = MessageType::FATAL_ERROR;
  399. std::string e;
  400. switch (this->GetLocalGenerator()->GetPolicyStatus(cmPolicies::CMP0028)) {
  401. case cmPolicies::WARN: {
  402. e = cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0028), "\n");
  403. messageType = MessageType::AUTHOR_WARNING;
  404. } break;
  405. case cmPolicies::OLD:
  406. return true;
  407. case cmPolicies::NEW:
  408. // Issue the fatal message.
  409. break;
  410. }
  411. if (role == LinkItemRole::Implementation) {
  412. e = cmStrCat(e, "Target \"", this->GetName(), "\" links to");
  413. } else {
  414. e = cmStrCat(e, "The link interface of target \"", this->GetName(),
  415. "\" contains");
  416. }
  417. e =
  418. cmStrCat(e, ":\n ", item.AsStr(), "\n", "but the target was not found. ",
  419. missingTargetPossibleReasons);
  420. cmListFileBacktrace backtrace = item.Backtrace;
  421. if (backtrace.Empty()) {
  422. backtrace = this->GetBacktrace();
  423. }
  424. this->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(messageType, e,
  425. backtrace);
  426. return false;
  427. }
  428. bool cmGeneratorTarget::VerifyLinkItemIsTarget(LinkItemRole role,
  429. cmLinkItem const& item) const
  430. {
  431. if (item.Target) {
  432. return true;
  433. }
  434. std::string const& str = item.AsStr();
  435. if (!str.empty() &&
  436. (str[0] == '-' || str[0] == '$' || str[0] == '`' ||
  437. str.find_first_of("/\\") != std::string::npos ||
  438. cmHasPrefix(str, "<LINK_LIBRARY:"_s) ||
  439. cmHasPrefix(str, "<LINK_GROUP:"_s))) {
  440. return true;
  441. }
  442. std::string e = cmStrCat("Target \"", this->GetName(),
  443. "\" has LINK_LIBRARIES_ONLY_TARGETS enabled, but ",
  444. role == LinkItemRole::Implementation
  445. ? "it links to"
  446. : "its link interface contains",
  447. ":\n ", item.AsStr(), "\nwhich is not a target. ",
  448. missingTargetPossibleReasons);
  449. cmListFileBacktrace backtrace = item.Backtrace;
  450. if (backtrace.Empty()) {
  451. backtrace = this->GetBacktrace();
  452. }
  453. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  454. MessageType::FATAL_ERROR, e, backtrace);
  455. return false;
  456. }
  457. bool cmGeneratorTarget::IsLinkLookupScope(std::string const& n,
  458. cmLocalGenerator const*& lg) const
  459. {
  460. if (cmHasLiteralPrefix(n, CMAKE_DIRECTORY_ID_SEP)) {
  461. cmDirectoryId const dirId = n.substr(cmStrLen(CMAKE_DIRECTORY_ID_SEP));
  462. if (dirId.String.empty()) {
  463. lg = this->LocalGenerator;
  464. return true;
  465. }
  466. if (cmLocalGenerator const* otherLG =
  467. this->GlobalGenerator->FindLocalGenerator(dirId)) {
  468. lg = otherLG;
  469. return true;
  470. }
  471. }
  472. return false;
  473. }
  474. cm::optional<cmLinkItem> cmGeneratorTarget::LookupLinkItem(
  475. std::string const& n, cmListFileBacktrace const& bt,
  476. std::string const& linkFeature, LookupLinkItemScope* scope,
  477. LookupSelf lookupSelf) const
  478. {
  479. cm::optional<cmLinkItem> maybeItem;
  480. if (this->IsLinkLookupScope(n, scope->LG)) {
  481. return maybeItem;
  482. }
  483. std::string name = this->CheckCMP0004(n);
  484. if (name.empty() ||
  485. (lookupSelf == LookupSelf::No && name == this->GetName())) {
  486. return maybeItem;
  487. }
  488. maybeItem =
  489. this->ResolveLinkItem(BT<std::string>(name, bt), scope->LG, linkFeature);
  490. return maybeItem;
  491. }
  492. void cmGeneratorTarget::ExpandLinkItems(std::string const& prop,
  493. cmBTStringRange entries,
  494. std::string const& config,
  495. cmGeneratorTarget const* headTarget,
  496. UseTo usage, LinkInterfaceField field,
  497. cmLinkInterface& iface) const
  498. {
  499. if (entries.empty()) {
  500. return;
  501. }
  502. // Keep this logic in sync with ComputeLinkImplementationLibraries.
  503. cmGeneratorExpressionDAGChecker dagChecker(this, prop, nullptr, nullptr,
  504. this->LocalGenerator, config);
  505. // The $<LINK_ONLY> expression may be in a link interface to specify
  506. // private link dependencies that are otherwise excluded from usage
  507. // requirements.
  508. if (usage == UseTo::Compile) {
  509. dagChecker.SetTransitivePropertiesOnly();
  510. dagChecker.SetTransitivePropertiesOnlyCMP0131();
  511. }
  512. cmMakefile const* mf = this->LocalGenerator->GetMakefile();
  513. LookupLinkItemScope scope{ this->LocalGenerator };
  514. for (BT<std::string> const& entry : entries) {
  515. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(),
  516. entry.Backtrace);
  517. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(entry.Value);
  518. cge->SetEvaluateForBuildsystem(true);
  519. cmList libs{ cge->Evaluate(this->LocalGenerator, config, headTarget,
  520. &dagChecker, this,
  521. headTarget->LinkerLanguage) };
  522. auto linkFeature = cmLinkItem::DEFAULT;
  523. for (auto const& lib : libs) {
  524. if (auto maybeLinkFeature = ParseLinkFeature(lib)) {
  525. linkFeature = std::move(*maybeLinkFeature);
  526. continue;
  527. }
  528. if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem(
  529. lib, cge->GetBacktrace(), linkFeature, &scope,
  530. field == LinkInterfaceField::Libraries ? LookupSelf::No
  531. : LookupSelf::Yes)) {
  532. cmLinkItem item = std::move(*maybeItem);
  533. if (field == LinkInterfaceField::HeadInclude) {
  534. iface.HeadInclude.emplace_back(std::move(item));
  535. continue;
  536. }
  537. if (field == LinkInterfaceField::HeadExclude) {
  538. iface.HeadExclude.emplace_back(std::move(item));
  539. continue;
  540. }
  541. if (!item.Target) {
  542. // Report explicitly linked object files separately.
  543. std::string const& maybeObj = item.AsStr();
  544. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  545. cmSourceFile const* sf =
  546. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  547. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  548. item.ObjectSource = sf;
  549. iface.Objects.emplace_back(std::move(item));
  550. continue;
  551. }
  552. }
  553. }
  554. iface.Libraries.emplace_back(std::move(item));
  555. }
  556. }
  557. if (cge->GetHadHeadSensitiveCondition()) {
  558. iface.HadHeadSensitiveCondition = true;
  559. }
  560. if (cge->GetHadContextSensitiveCondition()) {
  561. iface.HadContextSensitiveCondition = true;
  562. }
  563. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  564. iface.HadLinkLanguageSensitiveCondition = true;
  565. }
  566. }
  567. }
  568. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  569. const std::string& config, cmGeneratorTarget const* head) const
  570. {
  571. return this->GetLinkInterface(config, head, false);
  572. }
  573. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  574. const std::string& config, cmGeneratorTarget const* head,
  575. bool secondPass) const
  576. {
  577. // Imported targets have their own link interface.
  578. if (this->IsImported()) {
  579. return this->GetImportLinkInterface(config, head, UseTo::Link, secondPass);
  580. }
  581. // Link interfaces are not supported for executables that do not
  582. // export symbols.
  583. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  584. !this->IsExecutableWithExports()) {
  585. return nullptr;
  586. }
  587. // Lookup any existing link interface for this configuration.
  588. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  589. // If the link interface does not depend on the head target
  590. // then reuse the one from the head we computed first.
  591. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  592. head = hm.begin()->first;
  593. }
  594. cmOptionalLinkInterface& iface = hm[head];
  595. if (secondPass) {
  596. iface = cmOptionalLinkInterface();
  597. }
  598. MaybeEnableCheckLinkLibraries(iface);
  599. if (!iface.LibrariesDone) {
  600. iface.LibrariesDone = true;
  601. this->ComputeLinkInterfaceLibraries(config, iface, head, UseTo::Link);
  602. }
  603. if (!iface.AllDone) {
  604. iface.AllDone = true;
  605. if (iface.Exists) {
  606. this->ComputeLinkInterface(config, iface, secondPass);
  607. this->ComputeLinkInterfaceRuntimeLibraries(config, iface);
  608. }
  609. }
  610. return iface.Exists ? &iface : nullptr;
  611. }
  612. void cmGeneratorTarget::ComputeLinkInterface(const std::string& config,
  613. cmOptionalLinkInterface& iface,
  614. bool secondPass) const
  615. {
  616. if (iface.Explicit) {
  617. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  618. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  619. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  620. // Shared libraries may have runtime implementation dependencies
  621. // on other shared libraries that are not in the interface.
  622. std::set<cmLinkItem> emitted;
  623. for (cmLinkItem const& lib : iface.Libraries) {
  624. emitted.insert(lib);
  625. }
  626. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  627. cmLinkImplementation const* impl =
  628. this->GetLinkImplementation(config, UseTo::Link, secondPass);
  629. for (cmLinkImplItem const& lib : impl->Libraries) {
  630. if (emitted.insert(lib).second) {
  631. if (lib.Target) {
  632. // This is a runtime dependency on another shared library.
  633. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  634. iface.SharedDeps.push_back(lib);
  635. }
  636. } else {
  637. // TODO: Recognize shared library file names. Perhaps this
  638. // should be moved to cmComputeLinkInformation, but that
  639. // creates a chicken-and-egg problem since this list is needed
  640. // for its construction.
  641. }
  642. }
  643. }
  644. }
  645. }
  646. }
  647. if (this->LinkLanguagePropagatesToDependents()) {
  648. // Targets using this archive need its language runtime libraries.
  649. if (cmLinkImplementation const* impl =
  650. this->GetLinkImplementation(config, UseTo::Link, secondPass)) {
  651. iface.Languages = impl->Languages;
  652. }
  653. }
  654. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  655. // Construct the property name suffix for this configuration.
  656. std::string suffix = "_";
  657. if (!config.empty()) {
  658. suffix += cmSystemTools::UpperCase(config);
  659. } else {
  660. suffix += "NOCONFIG";
  661. }
  662. // How many repetitions are needed if this library has cyclic
  663. // dependencies?
  664. std::string propName = cmStrCat("LINK_INTERFACE_MULTIPLICITY", suffix);
  665. if (cmValue config_reps = this->GetProperty(propName)) {
  666. sscanf(config_reps->c_str(), "%u", &iface.Multiplicity);
  667. } else if (cmValue reps =
  668. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  669. sscanf(reps->c_str(), "%u", &iface.Multiplicity);
  670. }
  671. }
  672. }
  673. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  674. const std::string& config, cmGeneratorTarget const* head, UseTo usage) const
  675. {
  676. // Imported targets have their own link interface.
  677. if (this->IsImported()) {
  678. return this->GetImportLinkInterface(config, head, usage);
  679. }
  680. // Link interfaces are not supported for executables that do not
  681. // export symbols.
  682. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  683. !this->IsExecutableWithExports()) {
  684. return nullptr;
  685. }
  686. // Lookup any existing link interface for this configuration.
  687. cmHeadToLinkInterfaceMap& hm =
  688. (usage == UseTo::Compile
  689. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  690. : this->GetHeadToLinkInterfaceMap(config));
  691. // If the link interface does not depend on the head target
  692. // then reuse the one from the head we computed first.
  693. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  694. head = hm.begin()->first;
  695. }
  696. cmOptionalLinkInterface& iface = hm[head];
  697. MaybeEnableCheckLinkLibraries(iface);
  698. if (!iface.LibrariesDone) {
  699. iface.LibrariesDone = true;
  700. this->ComputeLinkInterfaceLibraries(config, iface, head, usage);
  701. }
  702. return iface.Exists ? &iface : nullptr;
  703. }
  704. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  705. const std::string& config, cmOptionalLinkInterface& iface,
  706. cmGeneratorTarget const* headTarget, UseTo usage) const
  707. {
  708. // Construct the property name suffix for this configuration.
  709. std::string suffix = "_";
  710. if (!config.empty()) {
  711. suffix += cmSystemTools::UpperCase(config);
  712. } else {
  713. suffix += "NOCONFIG";
  714. }
  715. // An explicit list of interface libraries may be set for shared
  716. // libraries and executables that export symbols.
  717. bool const haveExplicitLibraries =
  718. !this->Target->GetLinkInterfaceEntries().empty() ||
  719. !this->Target->GetLinkInterfaceDirectEntries().empty() ||
  720. !this->Target->GetLinkInterfaceDirectExcludeEntries().empty();
  721. // There is no implicit link interface for executables or modules
  722. // so if none was explicitly set then there is no link interface.
  723. if (!haveExplicitLibraries &&
  724. (this->GetType() == cmStateEnums::EXECUTABLE ||
  725. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  726. return;
  727. }
  728. iface.Exists = true;
  729. iface.Explicit = true;
  730. // The interface libraries are specified by INTERFACE_LINK_LIBRARIES.
  731. // Use its special representation directly to get backtraces.
  732. this->ExpandLinkItems(
  733. kINTERFACE_LINK_LIBRARIES, this->Target->GetLinkInterfaceEntries(), config,
  734. headTarget, usage, LinkInterfaceField::Libraries, iface);
  735. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT,
  736. this->Target->GetLinkInterfaceDirectEntries(), config,
  737. headTarget, usage, LinkInterfaceField::HeadInclude,
  738. iface);
  739. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  740. this->Target->GetLinkInterfaceDirectExcludeEntries(),
  741. config, headTarget, usage,
  742. LinkInterfaceField::HeadExclude, iface);
  743. }
  744. namespace {
  745. template <typename ReturnType>
  746. ReturnType constructItem(cmGeneratorTarget* target,
  747. cmListFileBacktrace const& bt);
  748. template <>
  749. inline cmLinkImplItem constructItem(cmGeneratorTarget* target,
  750. cmListFileBacktrace const& bt)
  751. {
  752. return cmLinkImplItem(cmLinkItem(target, false, bt), false);
  753. }
  754. template <>
  755. inline cmLinkItem constructItem(cmGeneratorTarget* target,
  756. cmListFileBacktrace const& bt)
  757. {
  758. return cmLinkItem(target, false, bt);
  759. }
  760. template <typename ValueType>
  761. std::vector<ValueType> computeImplicitLanguageTargets(
  762. std::string const& lang, std::string const& config,
  763. cmGeneratorTarget const* currentTarget)
  764. {
  765. cmListFileBacktrace bt;
  766. std::vector<ValueType> result;
  767. cmLocalGenerator* lg = currentTarget->GetLocalGenerator();
  768. std::string const& runtimeLibrary =
  769. currentTarget->GetRuntimeLinkLibrary(lang, config);
  770. if (cmValue runtimeLinkOptions = currentTarget->Makefile->GetDefinition(
  771. "CMAKE_" + lang + "_RUNTIME_LIBRARIES_" + runtimeLibrary)) {
  772. cmList libsList{ *runtimeLinkOptions };
  773. result.reserve(libsList.size());
  774. for (auto const& i : libsList) {
  775. cmGeneratorTarget::TargetOrString resolved =
  776. currentTarget->ResolveTargetReference(i, lg);
  777. if (resolved.Target) {
  778. result.emplace_back(constructItem<ValueType>(resolved.Target, bt));
  779. }
  780. }
  781. }
  782. return result;
  783. }
  784. }
  785. void cmGeneratorTarget::ComputeLinkInterfaceRuntimeLibraries(
  786. const std::string& config, cmOptionalLinkInterface& iface) const
  787. {
  788. for (std::string const& lang : iface.Languages) {
  789. if ((lang == "CUDA" || lang == "HIP") &&
  790. iface.LanguageRuntimeLibraries.find(lang) ==
  791. iface.LanguageRuntimeLibraries.end()) {
  792. auto implicitTargets =
  793. computeImplicitLanguageTargets<cmLinkItem>(lang, config, this);
  794. iface.LanguageRuntimeLibraries[lang] = std::move(implicitTargets);
  795. }
  796. }
  797. }
  798. void cmGeneratorTarget::ComputeLinkImplementationRuntimeLibraries(
  799. const std::string& config, cmOptionalLinkImplementation& impl) const
  800. {
  801. for (std::string const& lang : impl.Languages) {
  802. if ((lang == "CUDA" || lang == "HIP") &&
  803. impl.LanguageRuntimeLibraries.find(lang) ==
  804. impl.LanguageRuntimeLibraries.end()) {
  805. auto implicitTargets =
  806. computeImplicitLanguageTargets<cmLinkImplItem>(lang, config, this);
  807. impl.LanguageRuntimeLibraries[lang] = std::move(implicitTargets);
  808. }
  809. }
  810. }
  811. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  812. const std::string& config, cmGeneratorTarget const* headTarget, UseTo usage,
  813. bool secondPass) const
  814. {
  815. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  816. if (!info) {
  817. return nullptr;
  818. }
  819. cmHeadToLinkInterfaceMap& hm =
  820. (usage == UseTo::Compile
  821. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  822. : this->GetHeadToLinkInterfaceMap(config));
  823. // If the link interface does not depend on the head target
  824. // then reuse the one from the head we computed first.
  825. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  826. headTarget = hm.begin()->first;
  827. }
  828. cmOptionalLinkInterface& iface = hm[headTarget];
  829. if (secondPass) {
  830. iface = cmOptionalLinkInterface();
  831. }
  832. MaybeEnableCheckLinkLibraries(iface);
  833. if (!iface.AllDone) {
  834. iface.AllDone = true;
  835. iface.LibrariesDone = true;
  836. iface.Multiplicity = info->Multiplicity;
  837. cmExpandList(info->Languages, iface.Languages);
  838. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT,
  839. cmMakeRange(info->LibrariesHeadInclude), config,
  840. headTarget, usage, LinkInterfaceField::HeadInclude,
  841. iface);
  842. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  843. cmMakeRange(info->LibrariesHeadExclude), config,
  844. headTarget, usage, LinkInterfaceField::HeadExclude,
  845. iface);
  846. this->ExpandLinkItems(info->LibrariesProp, cmMakeRange(info->Libraries),
  847. config, headTarget, usage,
  848. LinkInterfaceField::Libraries, iface);
  849. cmList deps{ info->SharedDeps };
  850. LookupLinkItemScope scope{ this->LocalGenerator };
  851. auto linkFeature = cmLinkItem::DEFAULT;
  852. for (auto const& dep : deps) {
  853. if (auto maybeLinkFeature = ParseLinkFeature(dep)) {
  854. linkFeature = std::move(*maybeLinkFeature);
  855. continue;
  856. }
  857. if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem(
  858. dep, cmListFileBacktrace(), linkFeature, &scope, LookupSelf::No)) {
  859. iface.SharedDeps.emplace_back(std::move(*maybeItem));
  860. }
  861. }
  862. }
  863. return &iface;
  864. }
  865. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  866. const std::string& config) const
  867. {
  868. return this->LinkInterfaceMap[cmSystemTools::UpperCase(config)];
  869. }
  870. cmHeadToLinkInterfaceMap&
  871. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  872. const std::string& config) const
  873. {
  874. return this
  875. ->LinkInterfaceUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  876. }
  877. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  878. const std::string& config, UseTo usage) const
  879. {
  880. return this->GetLinkImplementation(config, usage, false);
  881. }
  882. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  883. const std::string& config, UseTo usage, bool secondPass) const
  884. {
  885. // There is no link implementation for targets that cannot compile sources.
  886. if (!this->CanCompileSources()) {
  887. return nullptr;
  888. }
  889. cmOptionalLinkImplementation& impl =
  890. (usage == UseTo::Compile
  891. ? this
  892. ->LinkImplUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)]
  893. : this->LinkImplMap[cmSystemTools::UpperCase(config)]);
  894. if (secondPass) {
  895. impl = cmOptionalLinkImplementation();
  896. }
  897. MaybeEnableCheckLinkLibraries(impl);
  898. if (!impl.LibrariesDone) {
  899. impl.LibrariesDone = true;
  900. this->ComputeLinkImplementationLibraries(config, impl, usage);
  901. }
  902. if (!impl.LanguagesDone) {
  903. impl.LanguagesDone = true;
  904. this->ComputeLinkImplementationLanguages(config, impl);
  905. this->ComputeLinkImplementationRuntimeLibraries(config, impl);
  906. }
  907. return &impl;
  908. }
  909. cmLinkImplementationLibraries const*
  910. cmGeneratorTarget::GetLinkImplementationLibraries(const std::string& config,
  911. UseTo usage) const
  912. {
  913. // There is no link implementation for targets that cannot compile sources.
  914. if (!this->CanCompileSources()) {
  915. return nullptr;
  916. }
  917. // Populate the link implementation libraries for this configuration.
  918. cmOptionalLinkImplementation& impl =
  919. (usage == UseTo::Compile
  920. ? this
  921. ->LinkImplUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)]
  922. : this->LinkImplMap[cmSystemTools::UpperCase(config)]);
  923. MaybeEnableCheckLinkLibraries(impl);
  924. if (!impl.LibrariesDone) {
  925. impl.LibrariesDone = true;
  926. this->ComputeLinkImplementationLibraries(config, impl, usage);
  927. }
  928. return &impl;
  929. }
  930. namespace {
  931. class TransitiveLinkImpl
  932. {
  933. cmGeneratorTarget const* Self;
  934. std::string const& Config;
  935. UseTo ImplFor;
  936. cmLinkImplementation& Impl;
  937. std::set<cmLinkItem> Emitted;
  938. std::set<cmLinkItem> Excluded;
  939. std::unordered_set<cmGeneratorTarget const*> Followed;
  940. void Follow(cmGeneratorTarget const* target);
  941. public:
  942. TransitiveLinkImpl(cmGeneratorTarget const* self, std::string const& config,
  943. UseTo usage, cmLinkImplementation& impl)
  944. : Self(self)
  945. , Config(config)
  946. , ImplFor(usage)
  947. , Impl(impl)
  948. {
  949. }
  950. void Compute();
  951. };
  952. void TransitiveLinkImpl::Follow(cmGeneratorTarget const* target)
  953. {
  954. if (!target || !this->Followed.insert(target).second) {
  955. return;
  956. }
  957. // Get this target's usage requirements.
  958. cmLinkInterfaceLibraries const* iface =
  959. target->GetLinkInterfaceLibraries(this->Config, this->Self, this->ImplFor);
  960. if (!iface) {
  961. return;
  962. }
  963. if (iface->HadContextSensitiveCondition) {
  964. this->Impl.HadContextSensitiveCondition = true;
  965. }
  966. // Process 'INTERFACE_LINK_LIBRARIES_DIRECT' usage requirements.
  967. for (cmLinkItem const& item : iface->HeadInclude) {
  968. // Inject direct dependencies from the item's usage requirements
  969. // before the item itself.
  970. this->Follow(item.Target);
  971. // Add the item itself, but at most once.
  972. if (this->Emitted.insert(item).second) {
  973. this->Impl.Libraries.emplace_back(item, /* checkCMP0027= */ false);
  974. }
  975. }
  976. // Follow transitive dependencies.
  977. for (cmLinkItem const& item : iface->Libraries) {
  978. this->Follow(item.Target);
  979. }
  980. // Record exclusions from 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE'
  981. // usage requirements.
  982. for (cmLinkItem const& item : iface->HeadExclude) {
  983. this->Excluded.insert(item);
  984. }
  985. }
  986. void TransitiveLinkImpl::Compute()
  987. {
  988. // Save the original items and start with an empty list.
  989. std::vector<cmLinkImplItem> original = std::move(this->Impl.Libraries);
  990. // Avoid injecting any original items as usage requirements.
  991. // This gives LINK_LIBRARIES final control over the order
  992. // if it explicitly lists everything.
  993. this->Emitted.insert(original.cbegin(), original.cend());
  994. // Process each original item.
  995. for (cmLinkImplItem& item : original) {
  996. // Inject direct dependencies listed in 'INTERFACE_LINK_LIBRARIES_DIRECT'
  997. // usage requirements before the item itself.
  998. this->Follow(item.Target);
  999. // Add the item itself.
  1000. this->Impl.Libraries.emplace_back(std::move(item));
  1001. }
  1002. // Remove items listed in 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE'
  1003. // usage requirements found through any dependency above.
  1004. this->Impl.Libraries.erase(
  1005. std::remove_if(this->Impl.Libraries.begin(), this->Impl.Libraries.end(),
  1006. [this](cmLinkImplItem const& item) {
  1007. return this->Excluded.find(item) != this->Excluded.end();
  1008. }),
  1009. this->Impl.Libraries.end());
  1010. }
  1011. void ComputeLinkImplTransitive(cmGeneratorTarget const* self,
  1012. std::string const& config, UseTo usage,
  1013. cmLinkImplementation& impl)
  1014. {
  1015. TransitiveLinkImpl transitiveLinkImpl(self, config, usage, impl);
  1016. transitiveLinkImpl.Compute();
  1017. }
  1018. }
  1019. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  1020. const std::string& config, cmOptionalLinkImplementation& impl,
  1021. UseTo usage) const
  1022. {
  1023. cmLocalGenerator const* lg = this->LocalGenerator;
  1024. cmMakefile const* mf = lg->GetMakefile();
  1025. cmBTStringRange entryRange = this->Target->GetLinkImplementationEntries();
  1026. auto const& synthTargetsForConfig = this->Configs[config].SyntheticDeps;
  1027. // Collect libraries directly linked in this configuration.
  1028. for (auto const& entry : entryRange) {
  1029. // Keep this logic in sync with ExpandLinkItems.
  1030. cmGeneratorExpressionDAGChecker dagChecker(
  1031. this, "LINK_LIBRARIES", nullptr, nullptr, this->LocalGenerator, config);
  1032. // The $<LINK_ONLY> expression may be used to specify link dependencies
  1033. // that are otherwise excluded from usage requirements.
  1034. if (usage == UseTo::Compile) {
  1035. dagChecker.SetTransitivePropertiesOnly();
  1036. switch (this->GetPolicyStatusCMP0131()) {
  1037. case cmPolicies::WARN:
  1038. case cmPolicies::OLD:
  1039. break;
  1040. case cmPolicies::NEW:
  1041. dagChecker.SetTransitivePropertiesOnlyCMP0131();
  1042. break;
  1043. }
  1044. }
  1045. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(),
  1046. entry.Backtrace);
  1047. std::unique_ptr<cmCompiledGeneratorExpression> const cge =
  1048. ge.Parse(entry.Value);
  1049. cge->SetEvaluateForBuildsystem(true);
  1050. std::string const& evaluated =
  1051. cge->Evaluate(this->LocalGenerator, config, this, &dagChecker, nullptr,
  1052. this->LinkerLanguage);
  1053. bool const checkCMP0027 = evaluated != entry.Value;
  1054. cmList llibs(evaluated);
  1055. if (cge->GetHadHeadSensitiveCondition()) {
  1056. impl.HadHeadSensitiveCondition = true;
  1057. }
  1058. if (cge->GetHadContextSensitiveCondition()) {
  1059. impl.HadContextSensitiveCondition = true;
  1060. }
  1061. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  1062. impl.HadLinkLanguageSensitiveCondition = true;
  1063. }
  1064. auto linkFeature = cmLinkItem::DEFAULT;
  1065. for (auto const& lib : llibs) {
  1066. if (auto maybeLinkFeature = ParseLinkFeature(lib)) {
  1067. linkFeature = std::move(*maybeLinkFeature);
  1068. continue;
  1069. }
  1070. if (this->IsLinkLookupScope(lib, lg)) {
  1071. continue;
  1072. }
  1073. // Skip entries that resolve to the target itself or are empty.
  1074. std::string name = this->CheckCMP0004(lib);
  1075. if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) {
  1076. // resolve alias name
  1077. auto* target = this->Makefile->FindTargetToUse(name);
  1078. if (target) {
  1079. name = target->GetName();
  1080. }
  1081. }
  1082. if (name == this->GetName() || name.empty()) {
  1083. if (name == this->GetName()) {
  1084. bool noMessage = false;
  1085. MessageType messageType = MessageType::FATAL_ERROR;
  1086. std::ostringstream e;
  1087. switch (this->GetPolicyStatusCMP0038()) {
  1088. case cmPolicies::WARN: {
  1089. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  1090. messageType = MessageType::AUTHOR_WARNING;
  1091. } break;
  1092. case cmPolicies::OLD:
  1093. noMessage = true;
  1094. break;
  1095. case cmPolicies::NEW:
  1096. // Issue the fatal message.
  1097. break;
  1098. }
  1099. if (!noMessage) {
  1100. e << "Target \"" << this->GetName() << "\" links to itself.";
  1101. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  1102. messageType, e.str(), this->GetBacktrace());
  1103. if (messageType == MessageType::FATAL_ERROR) {
  1104. return;
  1105. }
  1106. }
  1107. }
  1108. continue;
  1109. }
  1110. // The entry is meant for this configuration.
  1111. cmLinkItem item = this->ResolveLinkItem(
  1112. BT<std::string>(name, entry.Backtrace), lg, linkFeature);
  1113. if (item.Target) {
  1114. auto depsForTarget = synthTargetsForConfig.find(item.Target);
  1115. if (depsForTarget != synthTargetsForConfig.end()) {
  1116. for (auto const* depForTarget : depsForTarget->second) {
  1117. cmLinkItem synthItem(depForTarget, item.Cross, item.Backtrace);
  1118. impl.Libraries.emplace_back(std::move(synthItem), false);
  1119. }
  1120. }
  1121. } else {
  1122. // Report explicitly linked object files separately.
  1123. std::string const& maybeObj = item.AsStr();
  1124. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  1125. cmSourceFile const* sf =
  1126. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  1127. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  1128. item.ObjectSource = sf;
  1129. impl.Objects.emplace_back(std::move(item));
  1130. continue;
  1131. }
  1132. }
  1133. }
  1134. impl.Libraries.emplace_back(std::move(item), checkCMP0027);
  1135. }
  1136. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  1137. for (std::string const& sp : seenProps) {
  1138. if (!this->GetProperty(sp)) {
  1139. this->LinkImplicitNullProperties.insert(sp);
  1140. }
  1141. }
  1142. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  1143. }
  1144. // Update the list of direct link dependencies from usage requirements.
  1145. ComputeLinkImplTransitive(this, config, usage, impl);
  1146. // Get the list of configurations considered to be DEBUG.
  1147. std::vector<std::string> debugConfigs =
  1148. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  1149. cmTargetLinkLibraryType linkType = ComputeLinkType(config, debugConfigs);
  1150. cmTarget::LinkLibraryVectorType const& oldllibs =
  1151. this->Target->GetOriginalLinkLibraries();
  1152. auto linkFeature = cmLinkItem::DEFAULT;
  1153. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  1154. if (auto maybeLinkFeature = ParseLinkFeature(oldllib.first)) {
  1155. linkFeature = std::move(*maybeLinkFeature);
  1156. continue;
  1157. }
  1158. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  1159. std::string name = this->CheckCMP0004(oldllib.first);
  1160. if (name == this->GetName() || name.empty()) {
  1161. continue;
  1162. }
  1163. }
  1164. }
  1165. }
  1166. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  1167. std::string const& name) const
  1168. {
  1169. return this->ResolveTargetReference(name, this->LocalGenerator);
  1170. }
  1171. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  1172. std::string const& name, cmLocalGenerator const* lg) const
  1173. {
  1174. TargetOrString resolved;
  1175. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  1176. resolved.Target = tgt;
  1177. } else {
  1178. resolved.String = name;
  1179. }
  1180. return resolved;
  1181. }
  1182. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  1183. BT<std::string> const& name, std::string const& linkFeature) const
  1184. {
  1185. return this->ResolveLinkItem(name, this->LocalGenerator, linkFeature);
  1186. }
  1187. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  1188. BT<std::string> const& name, cmLocalGenerator const* lg,
  1189. std::string const& linkFeature) const
  1190. {
  1191. auto bt = name.Backtrace;
  1192. TargetOrString resolved = this->ResolveTargetReference(name.Value, lg);
  1193. if (!resolved.Target) {
  1194. return cmLinkItem(resolved.String, false, bt, linkFeature);
  1195. }
  1196. // Check deprecation, issue message with `bt` backtrace.
  1197. if (resolved.Target->IsDeprecated()) {
  1198. std::ostringstream w;
  1199. /* clang-format off */
  1200. w <<
  1201. "The library that is being linked to, " << resolved.Target->GetName() <<
  1202. ", is marked as being deprecated by the owner. The message provided by "
  1203. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  1204. /* clang-format on */
  1205. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  1206. MessageType::AUTHOR_WARNING, w.str(), bt);
  1207. }
  1208. // Skip targets that will not really be linked. This is probably a
  1209. // name conflict between an external library and an executable
  1210. // within the project.
  1211. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  1212. !resolved.Target->IsExecutableWithExports()) {
  1213. return cmLinkItem(resolved.Target->GetName(), false, bt, linkFeature);
  1214. }
  1215. return cmLinkItem(resolved.Target, false, bt, linkFeature);
  1216. }