cmGeneratorTarget_Link.cxx 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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(name);
  1229. if (target) {
  1230. name = target->GetName();
  1231. }
  1232. }
  1233. if (name == this->GetName() || name.empty()) {
  1234. if (name == this->GetName()) {
  1235. bool noMessage = false;
  1236. MessageType messageType = MessageType::FATAL_ERROR;
  1237. std::ostringstream e;
  1238. switch (this->GetPolicyStatusCMP0038()) {
  1239. case cmPolicies::WARN: {
  1240. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  1241. messageType = MessageType::AUTHOR_WARNING;
  1242. } break;
  1243. case cmPolicies::OLD:
  1244. noMessage = true;
  1245. break;
  1246. case cmPolicies::NEW:
  1247. // Issue the fatal message.
  1248. break;
  1249. }
  1250. if (!noMessage) {
  1251. e << "Target \"" << this->GetName() << "\" links to itself.";
  1252. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  1253. messageType, e.str(), this->GetBacktrace());
  1254. if (messageType == MessageType::FATAL_ERROR) {
  1255. return;
  1256. }
  1257. }
  1258. }
  1259. continue;
  1260. }
  1261. // The entry is meant for this configuration.
  1262. cmLinkItem item = this->ResolveLinkItem(
  1263. BT<std::string>(name, entry.Backtrace), lg, linkFeature);
  1264. if (item.Target) {
  1265. auto depsForTarget = synthTargetsForConfig.find(item.Target);
  1266. if (depsForTarget != synthTargetsForConfig.end()) {
  1267. for (auto const* depForTarget : depsForTarget->second) {
  1268. cmLinkItem synthItem(depForTarget, item.Cross, item.Backtrace);
  1269. impl.Libraries.emplace_back(std::move(synthItem), false);
  1270. }
  1271. }
  1272. } else {
  1273. // Report explicitly linked object files separately.
  1274. std::string const& maybeObj = item.AsStr();
  1275. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  1276. cmSourceFile const* sf =
  1277. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  1278. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  1279. item.ObjectSource = sf;
  1280. impl.Objects.emplace_back(std::move(item));
  1281. continue;
  1282. }
  1283. }
  1284. }
  1285. impl.Libraries.emplace_back(std::move(item), checkCMP0027);
  1286. }
  1287. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  1288. for (std::string const& sp : seenProps) {
  1289. if (!this->GetProperty(sp)) {
  1290. this->LinkImplicitNullProperties.insert(sp);
  1291. }
  1292. }
  1293. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  1294. }
  1295. // Update the list of direct link dependencies from usage requirements.
  1296. if (head == this) {
  1297. ComputeLinkImplTransitive(this, config, usage, impl);
  1298. }
  1299. // Get the list of configurations considered to be DEBUG.
  1300. std::vector<std::string> debugConfigs =
  1301. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  1302. cmTargetLinkLibraryType linkType = ComputeLinkType(config, debugConfigs);
  1303. cmTarget::LinkLibraryVectorType const& oldllibs =
  1304. this->Target->GetOriginalLinkLibraries();
  1305. auto linkFeature = cmLinkItem::DEFAULT;
  1306. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  1307. if (auto maybeLinkFeature = ParseLinkFeature(oldllib.first)) {
  1308. linkFeature = std::move(*maybeLinkFeature);
  1309. continue;
  1310. }
  1311. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  1312. std::string name = this->CheckCMP0004(oldllib.first);
  1313. if (name == this->GetName() || name.empty()) {
  1314. continue;
  1315. }
  1316. // Support OLD behavior for CMP0003.
  1317. impl.WrongConfigLibraries.push_back(
  1318. this->ResolveLinkItem(BT<std::string>(name), linkFeature));
  1319. }
  1320. }
  1321. }
  1322. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  1323. std::string const& name) const
  1324. {
  1325. return this->ResolveTargetReference(name, this->LocalGenerator);
  1326. }
  1327. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  1328. std::string const& name, cmLocalGenerator const* lg) const
  1329. {
  1330. TargetOrString resolved;
  1331. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  1332. resolved.Target = tgt;
  1333. } else {
  1334. resolved.String = name;
  1335. }
  1336. return resolved;
  1337. }
  1338. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  1339. BT<std::string> const& name, std::string const& linkFeature) const
  1340. {
  1341. return this->ResolveLinkItem(name, this->LocalGenerator, linkFeature);
  1342. }
  1343. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  1344. BT<std::string> const& name, cmLocalGenerator const* lg,
  1345. std::string const& linkFeature) const
  1346. {
  1347. auto bt = name.Backtrace;
  1348. TargetOrString resolved = this->ResolveTargetReference(name.Value, lg);
  1349. if (!resolved.Target) {
  1350. return cmLinkItem(resolved.String, false, bt, linkFeature);
  1351. }
  1352. // Check deprecation, issue message with `bt` backtrace.
  1353. if (resolved.Target->IsDeprecated()) {
  1354. std::ostringstream w;
  1355. /* clang-format off */
  1356. w <<
  1357. "The library that is being linked to, " << resolved.Target->GetName() <<
  1358. ", is marked as being deprecated by the owner. The message provided by "
  1359. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  1360. /* clang-format on */
  1361. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  1362. MessageType::AUTHOR_WARNING, w.str(), bt);
  1363. }
  1364. // Skip targets that will not really be linked. This is probably a
  1365. // name conflict between an external library and an executable
  1366. // within the project.
  1367. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  1368. !resolved.Target->IsExecutableWithExports()) {
  1369. return cmLinkItem(resolved.Target->GetName(), false, bt, linkFeature);
  1370. }
  1371. return cmLinkItem(resolved.Target, false, bt, linkFeature);
  1372. }