cmGeneratorTarget_Link.cxx 53 KB

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