cmGeneratorTarget_Link.cxx 44 KB

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