cmGeneratorTarget_Link.cxx 43 KB

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