cmGeneratorTarget_Link.cxx 53 KB

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