cmGeneratorTarget_Link.cxx 51 KB

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