cmGeneratorTarget_Link.cxx 44 KB

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