cmGeneratorTarget_Link.cxx 51 KB

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