cmGeneratorTarget_Link.cxx 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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. bool noMessage = false;
  1069. MessageType messageType = MessageType::FATAL_ERROR;
  1070. std::ostringstream e;
  1071. switch (this->GetPolicyStatusCMP0038()) {
  1072. case cmPolicies::WARN: {
  1073. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  1074. messageType = MessageType::AUTHOR_WARNING;
  1075. } break;
  1076. case cmPolicies::OLD:
  1077. noMessage = true;
  1078. break;
  1079. case cmPolicies::NEW:
  1080. // Issue the fatal message.
  1081. break;
  1082. }
  1083. if (!noMessage) {
  1084. e << "Target \"" << this->GetName() << "\" links to itself.";
  1085. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  1086. messageType, e.str(), this->GetBacktrace());
  1087. if (messageType == MessageType::FATAL_ERROR) {
  1088. return;
  1089. }
  1090. }
  1091. }
  1092. continue;
  1093. }
  1094. // The entry is meant for this configuration.
  1095. cmLinkItem item = this->ResolveLinkItem(
  1096. BT<std::string>(name, entry.Backtrace), lg, linkFeature);
  1097. if (item.Target) {
  1098. auto depsForTarget = synthTargetsForConfig.find(item.Target);
  1099. if (depsForTarget != synthTargetsForConfig.end()) {
  1100. for (auto const* depForTarget : depsForTarget->second) {
  1101. cmLinkItem synthItem(depForTarget, item.Cross, item.Backtrace);
  1102. impl.Libraries.emplace_back(std::move(synthItem));
  1103. }
  1104. }
  1105. } else {
  1106. // Report explicitly linked object files separately.
  1107. std::string const& maybeObj = item.AsStr();
  1108. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  1109. cmSourceFile const* sf =
  1110. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  1111. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  1112. item.ObjectSource = sf;
  1113. impl.Objects.emplace_back(std::move(item));
  1114. continue;
  1115. }
  1116. }
  1117. }
  1118. impl.Libraries.emplace_back(std::move(item));
  1119. }
  1120. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  1121. for (std::string const& sp : seenProps) {
  1122. if (!this->GetProperty(sp)) {
  1123. this->LinkImplicitNullProperties.insert(sp);
  1124. }
  1125. }
  1126. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  1127. }
  1128. // Update the list of direct link dependencies from usage requirements.
  1129. ComputeLinkImplTransitive(this, config, usage, impl);
  1130. // Get the list of configurations considered to be DEBUG.
  1131. std::vector<std::string> debugConfigs =
  1132. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  1133. cmTargetLinkLibraryType linkType = ComputeLinkType(config, debugConfigs);
  1134. cmTarget::LinkLibraryVectorType const& oldllibs =
  1135. this->Target->GetOriginalLinkLibraries();
  1136. auto linkFeature = cmLinkItem::DEFAULT;
  1137. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  1138. if (auto maybeLinkFeature = ParseLinkFeature(oldllib.first)) {
  1139. linkFeature = std::move(*maybeLinkFeature);
  1140. continue;
  1141. }
  1142. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  1143. std::string name = this->CheckCMP0004(oldllib.first);
  1144. if (name == this->GetName() || name.empty()) {
  1145. continue;
  1146. }
  1147. }
  1148. }
  1149. }
  1150. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  1151. std::string const& name) const
  1152. {
  1153. return this->ResolveTargetReference(name, this->LocalGenerator);
  1154. }
  1155. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  1156. std::string const& name, cmLocalGenerator const* lg) const
  1157. {
  1158. TargetOrString resolved;
  1159. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  1160. resolved.Target = tgt;
  1161. } else {
  1162. resolved.String = name;
  1163. }
  1164. return resolved;
  1165. }
  1166. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  1167. BT<std::string> const& name, std::string const& linkFeature) const
  1168. {
  1169. return this->ResolveLinkItem(name, this->LocalGenerator, linkFeature);
  1170. }
  1171. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  1172. BT<std::string> const& name, cmLocalGenerator const* lg,
  1173. std::string const& linkFeature) const
  1174. {
  1175. auto bt = name.Backtrace;
  1176. TargetOrString resolved = this->ResolveTargetReference(name.Value, lg);
  1177. if (!resolved.Target) {
  1178. return cmLinkItem(resolved.String, false, bt, linkFeature);
  1179. }
  1180. // Check deprecation, issue message with `bt` backtrace.
  1181. if (resolved.Target->IsDeprecated()) {
  1182. std::ostringstream w;
  1183. /* clang-format off */
  1184. w <<
  1185. "The library that is being linked to, " << resolved.Target->GetName() <<
  1186. ", is marked as being deprecated by the owner. The message provided by "
  1187. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  1188. /* clang-format on */
  1189. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  1190. MessageType::AUTHOR_WARNING, w.str(), bt);
  1191. }
  1192. // Skip targets that will not really be linked. This is probably a
  1193. // name conflict between an external library and an executable
  1194. // within the project.
  1195. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  1196. !resolved.Target->IsExecutableWithExports()) {
  1197. return cmLinkItem(resolved.Target->GetName(), false, bt, linkFeature);
  1198. }
  1199. return cmLinkItem(resolved.Target, false, bt, linkFeature);
  1200. }