cmGeneratorTarget_Link.cxx 52 KB

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