cmGeneratorTarget_Link.cxx 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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 <unordered_map>
  14. #include <unordered_set>
  15. #include <utility>
  16. #include <vector>
  17. #include <cm/memory>
  18. #include <cm/optional>
  19. #include <cm/string_view>
  20. #include <cmext/algorithm>
  21. #include <cmext/string_view>
  22. #include "cmAlgorithms.h"
  23. #include "cmComputeLinkInformation.h"
  24. #include "cmGenExContext.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 (cmLinkItem 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 (cmLinkItem 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 (cmLinkItem 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 = cmStrCat(e, ":\n ", item.AsStr(), "\nbut the target was not found. ",
  406. missingTargetPossibleReasons);
  407. cmListFileBacktrace backtrace = item.Backtrace;
  408. if (backtrace.Empty()) {
  409. backtrace = this->GetBacktrace();
  410. }
  411. this->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  412. MessageType::FATAL_ERROR, e, backtrace);
  413. return false;
  414. }
  415. bool cmGeneratorTarget::VerifyLinkItemIsTarget(LinkItemRole role,
  416. cmLinkItem const& item) const
  417. {
  418. if (item.Target) {
  419. return true;
  420. }
  421. std::string const& str = item.AsStr();
  422. if (!str.empty() &&
  423. (str[0] == '-' || str[0] == '$' || str[0] == '`' ||
  424. str.find_first_of("/\\") != std::string::npos ||
  425. cmHasPrefix(str, "<LINK_LIBRARY:"_s) ||
  426. cmHasPrefix(str, "<LINK_GROUP:"_s))) {
  427. return true;
  428. }
  429. std::string e = cmStrCat("Target \"", this->GetName(),
  430. "\" has LINK_LIBRARIES_ONLY_TARGETS enabled, but ",
  431. role == LinkItemRole::Implementation
  432. ? "it links to"
  433. : "its link interface contains",
  434. ":\n ", item.AsStr(), "\nwhich is not a target. ",
  435. missingTargetPossibleReasons);
  436. cmListFileBacktrace backtrace = item.Backtrace;
  437. if (backtrace.Empty()) {
  438. backtrace = this->GetBacktrace();
  439. }
  440. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  441. MessageType::FATAL_ERROR, e, backtrace);
  442. return false;
  443. }
  444. bool cmGeneratorTarget::IsLinkLookupScope(std::string const& n,
  445. cmLocalGenerator const*& lg) const
  446. {
  447. if (cmHasLiteralPrefix(n, CMAKE_DIRECTORY_ID_SEP)) {
  448. cmDirectoryId const dirId = n.substr(cmStrLen(CMAKE_DIRECTORY_ID_SEP));
  449. if (dirId.String.empty()) {
  450. lg = this->LocalGenerator;
  451. return true;
  452. }
  453. if (cmLocalGenerator const* otherLG =
  454. this->GlobalGenerator->FindLocalGenerator(dirId)) {
  455. lg = otherLG;
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. cm::optional<cmLinkItem> cmGeneratorTarget::LookupLinkItem(
  462. std::string const& n, cmListFileBacktrace const& bt,
  463. std::string const& linkFeature, LookupLinkItemScope* scope,
  464. LookupSelf lookupSelf) const
  465. {
  466. cm::optional<cmLinkItem> maybeItem;
  467. if (this->IsLinkLookupScope(n, scope->LG)) {
  468. return maybeItem;
  469. }
  470. std::string name = this->CheckCMP0004(n);
  471. if (name.empty() ||
  472. (lookupSelf == LookupSelf::No && name == this->GetName())) {
  473. return maybeItem;
  474. }
  475. maybeItem =
  476. this->ResolveLinkItem(BT<std::string>(name, bt), scope->LG, linkFeature);
  477. return maybeItem;
  478. }
  479. void cmGeneratorTarget::ExpandLinkItems(std::string const& prop,
  480. cmBTStringRange entries,
  481. std::string const& config,
  482. cmGeneratorTarget const* headTarget,
  483. UseTo usage, LinkInterfaceField field,
  484. cmLinkInterface& iface) const
  485. {
  486. if (entries.empty()) {
  487. return;
  488. }
  489. // Keep this logic in sync with ComputeLinkImplementationLibraries.
  490. cm::GenEx::Context context(this->LocalGenerator, config,
  491. headTarget->LinkerLanguage);
  492. cmGeneratorExpressionDAGChecker dagChecker{
  493. this,
  494. prop,
  495. nullptr,
  496. nullptr,
  497. context,
  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(context, &dagChecker, headTarget, this) };
  516. auto linkFeature = cmLinkItem::DEFAULT;
  517. for (auto const& lib : libs) {
  518. if (auto maybeLinkFeature = ParseLinkFeature(lib)) {
  519. linkFeature = std::move(*maybeLinkFeature);
  520. continue;
  521. }
  522. if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem(
  523. lib, cge->GetBacktrace(), linkFeature, &scope,
  524. field == LinkInterfaceField::Libraries ? LookupSelf::No
  525. : LookupSelf::Yes)) {
  526. cmLinkItem item = std::move(*maybeItem);
  527. if (field == LinkInterfaceField::HeadInclude) {
  528. item.InterfaceDirectFrom = this;
  529. iface.HeadInclude.emplace_back(std::move(item));
  530. continue;
  531. }
  532. if (field == LinkInterfaceField::HeadExclude) {
  533. iface.HeadExclude.emplace_back(std::move(item));
  534. continue;
  535. }
  536. if (!item.Target) {
  537. // Report explicitly linked object files separately.
  538. std::string const& maybeObj = item.AsStr();
  539. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  540. cmSourceFile const* sf =
  541. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  542. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  543. item.ObjectSource = sf;
  544. iface.Objects.emplace_back(std::move(item));
  545. continue;
  546. }
  547. }
  548. }
  549. iface.Libraries.emplace_back(std::move(item));
  550. }
  551. }
  552. if (cge->GetHadHeadSensitiveCondition()) {
  553. iface.HadHeadSensitiveCondition = true;
  554. }
  555. if (cge->GetHadContextSensitiveCondition()) {
  556. iface.HadContextSensitiveCondition = true;
  557. }
  558. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  559. iface.HadLinkLanguageSensitiveCondition = true;
  560. }
  561. }
  562. }
  563. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  564. std::string const& config, cmGeneratorTarget const* head) const
  565. {
  566. return this->GetLinkInterface(config, head, false);
  567. }
  568. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  569. std::string const& config, cmGeneratorTarget const* head,
  570. bool secondPass) const
  571. {
  572. // Imported targets have their own link interface.
  573. if (this->IsImported()) {
  574. return this->GetImportLinkInterface(config, head, UseTo::Link, secondPass);
  575. }
  576. // Link interfaces are not supported for executables that do not
  577. // export symbols.
  578. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  579. !this->IsExecutableWithExports()) {
  580. return nullptr;
  581. }
  582. // Lookup any existing link interface for this configuration.
  583. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  584. // If the link interface does not depend on the head target
  585. // then reuse the one from the head we computed first.
  586. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  587. head = hm.begin()->first;
  588. }
  589. cmOptionalLinkInterface& iface = hm[head];
  590. if (secondPass) {
  591. iface = cmOptionalLinkInterface();
  592. }
  593. MaybeEnableCheckLinkLibraries(iface);
  594. if (!iface.LibrariesDone) {
  595. iface.LibrariesDone = true;
  596. this->ComputeLinkInterfaceLibraries(config, iface, head, UseTo::Link);
  597. }
  598. if (!iface.AllDone) {
  599. iface.AllDone = true;
  600. if (iface.Exists) {
  601. this->ComputeLinkInterface(config, iface, secondPass);
  602. this->ComputeLinkInterfaceRuntimeLibraries(config, iface);
  603. }
  604. }
  605. return iface.Exists ? &iface : nullptr;
  606. }
  607. void cmGeneratorTarget::ComputeLinkInterface(std::string const& config,
  608. cmOptionalLinkInterface& iface,
  609. bool secondPass) const
  610. {
  611. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  612. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  613. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  614. // Shared libraries may have runtime implementation dependencies
  615. // on other shared libraries that are not in the interface.
  616. std::set<cmLinkItem> emitted;
  617. for (cmLinkItem const& lib : iface.Libraries) {
  618. emitted.insert(lib);
  619. }
  620. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  621. cmLinkImplementation const* impl =
  622. this->GetLinkImplementation(config, UseTo::Link, secondPass);
  623. for (cmLinkItem const& lib : impl->Libraries) {
  624. if (emitted.insert(lib).second) {
  625. if (lib.Target) {
  626. // This is a runtime dependency on another shared library.
  627. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  628. iface.SharedDeps.push_back(lib);
  629. }
  630. } else {
  631. // TODO: Recognize shared library file names. Perhaps this
  632. // should be moved to cmComputeLinkInformation, but that
  633. // creates a chicken-and-egg problem since this list is needed
  634. // for its construction.
  635. }
  636. }
  637. }
  638. }
  639. }
  640. if (this->LinkLanguagePropagatesToDependents()) {
  641. // Targets using this archive need its language runtime libraries.
  642. if (cmLinkImplementation const* impl =
  643. this->GetLinkImplementation(config, UseTo::Link, secondPass)) {
  644. iface.Languages = impl->Languages;
  645. }
  646. }
  647. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  648. // Construct the property name suffix for this configuration.
  649. std::string suffix = "_";
  650. if (!config.empty()) {
  651. suffix += cmSystemTools::UpperCase(config);
  652. } else {
  653. suffix += "NOCONFIG";
  654. }
  655. // How many repetitions are needed if this library has cyclic
  656. // dependencies?
  657. std::string propName = cmStrCat("LINK_INTERFACE_MULTIPLICITY", suffix);
  658. if (cmValue config_reps = this->GetProperty(propName)) {
  659. sscanf(config_reps->c_str(), "%u", &iface.Multiplicity);
  660. } else if (cmValue reps =
  661. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  662. sscanf(reps->c_str(), "%u", &iface.Multiplicity);
  663. }
  664. }
  665. }
  666. cmLinkInterfaceLibraries const* cmGeneratorTarget::GetLinkInterfaceLibraries(
  667. std::string const& config, cmGeneratorTarget const* head, UseTo usage) const
  668. {
  669. // Imported targets have their own link interface.
  670. if (this->IsImported()) {
  671. return this->GetImportLinkInterface(config, head, usage);
  672. }
  673. // Link interfaces are not supported for executables that do not
  674. // export symbols.
  675. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  676. !this->IsExecutableWithExports()) {
  677. return nullptr;
  678. }
  679. // Lookup any existing link interface for this configuration.
  680. cmHeadToLinkInterfaceMap& hm =
  681. (usage == UseTo::Compile
  682. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  683. : this->GetHeadToLinkInterfaceMap(config));
  684. // If the link interface does not depend on the head target
  685. // then reuse the one from the head we computed first.
  686. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  687. head = hm.begin()->first;
  688. }
  689. cmOptionalLinkInterface& iface = hm[head];
  690. MaybeEnableCheckLinkLibraries(iface);
  691. if (!iface.LibrariesDone) {
  692. iface.LibrariesDone = true;
  693. this->ComputeLinkInterfaceLibraries(config, iface, head, usage);
  694. }
  695. return iface.Exists ? &iface : nullptr;
  696. }
  697. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  698. std::string const& config, cmOptionalLinkInterface& iface,
  699. cmGeneratorTarget const* headTarget, UseTo usage) const
  700. {
  701. // Construct the property name suffix for this configuration.
  702. std::string suffix = "_";
  703. if (!config.empty()) {
  704. suffix += cmSystemTools::UpperCase(config);
  705. } else {
  706. suffix += "NOCONFIG";
  707. }
  708. // An explicit list of interface libraries may be set for shared
  709. // libraries and executables that export symbols.
  710. bool const haveExplicitLibraries =
  711. !this->Target->GetLinkInterfaceEntries().empty() ||
  712. !this->Target->GetLinkInterfaceDirectEntries().empty() ||
  713. !this->Target->GetLinkInterfaceDirectExcludeEntries().empty();
  714. // There is no implicit link interface for executables or modules
  715. // so if none was explicitly set then there is no link interface.
  716. if (!haveExplicitLibraries &&
  717. (this->GetType() == cmStateEnums::EXECUTABLE ||
  718. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  719. return;
  720. }
  721. iface.Exists = true;
  722. // The interface libraries are specified by INTERFACE_LINK_LIBRARIES.
  723. // Use its special representation directly to get backtraces.
  724. this->ExpandLinkItems(
  725. kINTERFACE_LINK_LIBRARIES, this->Target->GetLinkInterfaceEntries(), config,
  726. headTarget, usage, LinkInterfaceField::Libraries, iface);
  727. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT,
  728. this->Target->GetLinkInterfaceDirectEntries(), config,
  729. headTarget, usage, LinkInterfaceField::HeadInclude,
  730. iface);
  731. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  732. this->Target->GetLinkInterfaceDirectExcludeEntries(),
  733. config, headTarget, usage,
  734. LinkInterfaceField::HeadExclude, iface);
  735. }
  736. std::vector<cmLinkItem> cmGeneratorTarget::ComputeImplicitLanguageTargets(
  737. std::string const& lang, std::string const& config) const
  738. {
  739. cmListFileBacktrace bt;
  740. std::vector<cmLinkItem> result;
  741. cmLocalGenerator* lg = this->GetLocalGenerator();
  742. std::string const& runtimeLibrary =
  743. this->GetRuntimeLinkLibrary(lang, config);
  744. if (cmValue runtimeLinkOptions = this->Makefile->GetDefinition(
  745. "CMAKE_" + lang + "_RUNTIME_LIBRARIES_" + runtimeLibrary)) {
  746. cmList libsList{ *runtimeLinkOptions };
  747. result.reserve(libsList.size());
  748. for (auto const& i : libsList) {
  749. cmGeneratorTarget::TargetOrString resolved =
  750. this->ResolveTargetReference(i, lg);
  751. if (resolved.Target) {
  752. result.emplace_back(resolved.Target, false, bt);
  753. }
  754. }
  755. }
  756. return result;
  757. }
  758. void cmGeneratorTarget::ComputeLinkInterfaceRuntimeLibraries(
  759. std::string const& config, cmOptionalLinkInterface& iface) const
  760. {
  761. for (std::string const& lang : iface.Languages) {
  762. if ((lang == "CUDA" || lang == "HIP") &&
  763. iface.LanguageRuntimeLibraries.find(lang) ==
  764. iface.LanguageRuntimeLibraries.end()) {
  765. iface.LanguageRuntimeLibraries[lang] =
  766. this->ComputeImplicitLanguageTargets(lang, config);
  767. }
  768. }
  769. }
  770. void cmGeneratorTarget::ComputeLinkImplementationRuntimeLibraries(
  771. std::string const& config, cmOptionalLinkImplementation& impl) const
  772. {
  773. for (std::string const& lang : impl.Languages) {
  774. if ((lang == "CUDA" || lang == "HIP") &&
  775. impl.LanguageRuntimeLibraries.find(lang) ==
  776. impl.LanguageRuntimeLibraries.end()) {
  777. impl.LanguageRuntimeLibraries[lang] =
  778. this->ComputeImplicitLanguageTargets(lang, config);
  779. }
  780. }
  781. }
  782. cmLinkInterface const* cmGeneratorTarget::GetImportLinkInterface(
  783. std::string const& config, cmGeneratorTarget const* headTarget, UseTo usage,
  784. bool secondPass) const
  785. {
  786. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  787. if (!info) {
  788. return nullptr;
  789. }
  790. cmHeadToLinkInterfaceMap& hm =
  791. (usage == UseTo::Compile
  792. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  793. : this->GetHeadToLinkInterfaceMap(config));
  794. // If the link interface does not depend on the head target
  795. // then reuse the one from the head we computed first.
  796. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  797. headTarget = hm.begin()->first;
  798. }
  799. cmOptionalLinkInterface& iface = hm[headTarget];
  800. if (secondPass) {
  801. iface = cmOptionalLinkInterface();
  802. }
  803. MaybeEnableCheckLinkLibraries(iface);
  804. if (!iface.AllDone) {
  805. iface.AllDone = true;
  806. iface.LibrariesDone = true;
  807. iface.Multiplicity = info->Multiplicity;
  808. cmExpandList(info->Languages, iface.Languages);
  809. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT,
  810. cmMakeRange(info->LibrariesHeadInclude), config,
  811. headTarget, usage, LinkInterfaceField::HeadInclude,
  812. iface);
  813. this->ExpandLinkItems(kINTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE,
  814. cmMakeRange(info->LibrariesHeadExclude), config,
  815. headTarget, usage, LinkInterfaceField::HeadExclude,
  816. iface);
  817. this->ExpandLinkItems(info->LibrariesProp, cmMakeRange(info->Libraries),
  818. config, headTarget, usage,
  819. LinkInterfaceField::Libraries, iface);
  820. cmList deps{ info->SharedDeps };
  821. LookupLinkItemScope scope{ this->LocalGenerator };
  822. auto linkFeature = cmLinkItem::DEFAULT;
  823. for (auto const& dep : deps) {
  824. if (auto maybeLinkFeature = ParseLinkFeature(dep)) {
  825. linkFeature = std::move(*maybeLinkFeature);
  826. continue;
  827. }
  828. if (cm::optional<cmLinkItem> maybeItem = this->LookupLinkItem(
  829. dep, cmListFileBacktrace(), linkFeature, &scope, LookupSelf::No)) {
  830. iface.SharedDeps.emplace_back(std::move(*maybeItem));
  831. }
  832. }
  833. }
  834. return &iface;
  835. }
  836. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  837. std::string const& config) const
  838. {
  839. return this->LinkInterfaceMap[cmSystemTools::UpperCase(config)];
  840. }
  841. cmHeadToLinkInterfaceMap&
  842. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  843. std::string const& config) const
  844. {
  845. return this
  846. ->LinkInterfaceUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  847. }
  848. cmLinkImplementation const* cmGeneratorTarget::GetLinkImplementation(
  849. std::string const& config, UseTo usage) const
  850. {
  851. return this->GetLinkImplementation(config, usage, false);
  852. }
  853. cmLinkImplementation const* cmGeneratorTarget::GetLinkImplementation(
  854. std::string const& config, UseTo usage, bool secondPass) const
  855. {
  856. // There is no link implementation for targets that cannot compile sources.
  857. if (!this->CanCompileSources()) {
  858. return nullptr;
  859. }
  860. cmOptionalLinkImplementation& impl =
  861. (usage == UseTo::Compile
  862. ? this
  863. ->LinkImplUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)]
  864. : this->LinkImplMap[cmSystemTools::UpperCase(config)]);
  865. if (secondPass) {
  866. impl = cmOptionalLinkImplementation();
  867. }
  868. MaybeEnableCheckLinkLibraries(impl);
  869. if (!impl.LibrariesDone) {
  870. impl.LibrariesDone = true;
  871. this->ComputeLinkImplementationLibraries(config, impl, usage);
  872. }
  873. if (!impl.LanguagesDone) {
  874. impl.LanguagesDone = true;
  875. this->ComputeLinkImplementationLanguages(config, impl);
  876. this->ComputeLinkImplementationRuntimeLibraries(config, impl);
  877. }
  878. return &impl;
  879. }
  880. cmLinkImplementationLibraries const*
  881. cmGeneratorTarget::GetLinkImplementationLibraries(std::string const& config,
  882. UseTo usage) const
  883. {
  884. // There is no link implementation for targets that cannot compile sources.
  885. if (!this->CanCompileSources()) {
  886. return nullptr;
  887. }
  888. // Populate the link implementation libraries for this configuration.
  889. cmOptionalLinkImplementation& impl =
  890. (usage == UseTo::Compile
  891. ? this
  892. ->LinkImplUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)]
  893. : this->LinkImplMap[cmSystemTools::UpperCase(config)]);
  894. MaybeEnableCheckLinkLibraries(impl);
  895. if (!impl.LibrariesDone) {
  896. impl.LibrariesDone = true;
  897. this->ComputeLinkImplementationLibraries(config, impl, usage);
  898. }
  899. return &impl;
  900. }
  901. namespace {
  902. class TransitiveLinkImpl
  903. {
  904. cmGeneratorTarget const* Self;
  905. std::string const& Config;
  906. UseTo ImplFor;
  907. cmLinkImplementation& Impl;
  908. std::set<cmLinkItem> Emitted;
  909. std::set<cmLinkItem> Excluded;
  910. std::unordered_set<cmGeneratorTarget const*> Followed;
  911. void Follow(cmGeneratorTarget const* target);
  912. public:
  913. TransitiveLinkImpl(cmGeneratorTarget const* self, std::string const& config,
  914. UseTo usage, cmLinkImplementation& impl)
  915. : Self(self)
  916. , Config(config)
  917. , ImplFor(usage)
  918. , Impl(impl)
  919. {
  920. }
  921. void Compute();
  922. };
  923. void TransitiveLinkImpl::Follow(cmGeneratorTarget const* target)
  924. {
  925. if (!target || !this->Followed.insert(target).second) {
  926. return;
  927. }
  928. // Get this target's usage requirements.
  929. cmLinkInterfaceLibraries const* iface =
  930. target->GetLinkInterfaceLibraries(this->Config, this->Self, this->ImplFor);
  931. if (!iface) {
  932. return;
  933. }
  934. if (iface->HadContextSensitiveCondition) {
  935. this->Impl.HadContextSensitiveCondition = true;
  936. }
  937. // Process 'INTERFACE_LINK_LIBRARIES_DIRECT' usage requirements.
  938. for (cmLinkItem const& item : iface->HeadInclude) {
  939. // Inject direct dependencies from the item's usage requirements
  940. // before the item itself.
  941. this->Follow(item.Target);
  942. // Add the item itself, but at most once.
  943. if (this->Emitted.insert(item).second) {
  944. this->Impl.Libraries.emplace_back(item);
  945. }
  946. }
  947. // Follow transitive dependencies.
  948. for (cmLinkItem const& item : iface->Libraries) {
  949. this->Follow(item.Target);
  950. }
  951. // Record exclusions from 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE'
  952. // usage requirements.
  953. for (cmLinkItem const& item : iface->HeadExclude) {
  954. this->Excluded.insert(item);
  955. }
  956. }
  957. void TransitiveLinkImpl::Compute()
  958. {
  959. // Save the original items and start with an empty list.
  960. std::vector<cmLinkItem> original = std::move(this->Impl.Libraries);
  961. // Avoid injecting any original items as usage requirements.
  962. // This gives LINK_LIBRARIES final control over the order
  963. // if it explicitly lists everything.
  964. this->Emitted.insert(original.cbegin(), original.cend());
  965. // Process each original item.
  966. for (cmLinkItem& item : original) {
  967. // Inject direct dependencies listed in 'INTERFACE_LINK_LIBRARIES_DIRECT'
  968. // usage requirements before the item itself.
  969. this->Follow(item.Target);
  970. // Add the item itself.
  971. this->Impl.Libraries.emplace_back(std::move(item));
  972. }
  973. // Remove items listed in 'INTERFACE_LINK_LIBRARIES_DIRECT_EXCLUDE'
  974. // usage requirements found through any dependency above.
  975. this->Impl.Libraries.erase(
  976. std::remove_if(this->Impl.Libraries.begin(), this->Impl.Libraries.end(),
  977. [this](cmLinkItem const& item) {
  978. return this->Excluded.find(item) != this->Excluded.end();
  979. }),
  980. this->Impl.Libraries.end());
  981. }
  982. void ComputeLinkImplTransitive(cmGeneratorTarget const* self,
  983. std::string const& config, UseTo usage,
  984. cmLinkImplementation& impl)
  985. {
  986. TransitiveLinkImpl transitiveLinkImpl(self, config, usage, impl);
  987. transitiveLinkImpl.Compute();
  988. }
  989. }
  990. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  991. std::string const& config, cmOptionalLinkImplementation& impl,
  992. UseTo usage) const
  993. {
  994. cm::GenEx::Context context(this->LocalGenerator, config,
  995. this->LinkerLanguage);
  996. cmLocalGenerator const* lg = this->LocalGenerator;
  997. cmMakefile const* mf = lg->GetMakefile();
  998. cmBTStringRange entryRange = this->Target->GetLinkImplementationEntries();
  999. auto const& synthTargetsForConfig = this->Configs[config].SyntheticDeps;
  1000. // Collect libraries directly linked in this configuration.
  1001. for (auto const& entry : entryRange) {
  1002. // Keep this logic in sync with ExpandLinkItems.
  1003. cmGeneratorExpressionDAGChecker dagChecker{
  1004. this,
  1005. "LINK_LIBRARIES",
  1006. nullptr,
  1007. nullptr,
  1008. context,
  1009. cmListFileBacktrace(),
  1010. cmGeneratorExpressionDAGChecker::ComputingLinkLibraries::Yes,
  1011. };
  1012. // The $<LINK_ONLY> expression may be used to specify link dependencies
  1013. // that are otherwise excluded from usage requirements.
  1014. if (usage == UseTo::Compile) {
  1015. dagChecker.SetTransitivePropertiesOnly();
  1016. switch (this->GetPolicyStatusCMP0131()) {
  1017. case cmPolicies::WARN:
  1018. case cmPolicies::OLD:
  1019. break;
  1020. case cmPolicies::NEW:
  1021. dagChecker.SetTransitivePropertiesOnlyCMP0131();
  1022. break;
  1023. }
  1024. }
  1025. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance(),
  1026. entry.Backtrace);
  1027. std::unique_ptr<cmCompiledGeneratorExpression> const cge =
  1028. ge.Parse(entry.Value);
  1029. cge->SetEvaluateForBuildsystem(true);
  1030. std::string const& evaluated = cge->Evaluate(context, &dagChecker, this);
  1031. cmList llibs(evaluated);
  1032. if (cge->GetHadHeadSensitiveCondition()) {
  1033. impl.HadHeadSensitiveCondition = true;
  1034. }
  1035. if (cge->GetHadContextSensitiveCondition()) {
  1036. impl.HadContextSensitiveCondition = true;
  1037. }
  1038. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  1039. impl.HadLinkLanguageSensitiveCondition = true;
  1040. }
  1041. auto linkFeature = cmLinkItem::DEFAULT;
  1042. for (auto const& lib : llibs) {
  1043. if (auto maybeLinkFeature = ParseLinkFeature(lib)) {
  1044. linkFeature = std::move(*maybeLinkFeature);
  1045. continue;
  1046. }
  1047. if (this->IsLinkLookupScope(lib, lg)) {
  1048. continue;
  1049. }
  1050. // Skip entries that resolve to the target itself or are empty.
  1051. std::string name = this->CheckCMP0004(lib);
  1052. if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) {
  1053. // resolve alias name
  1054. auto* target = this->Makefile->FindTargetToUse(
  1055. name, cmStateEnums::AllTargetDomains);
  1056. if (target) {
  1057. name = target->GetName();
  1058. }
  1059. }
  1060. if (name == this->GetName() || name.empty()) {
  1061. if (name == this->GetName()) {
  1062. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  1063. MessageType::FATAL_ERROR,
  1064. cmStrCat("Target \"", this->GetName(), "\" links to itself."),
  1065. this->GetBacktrace());
  1066. return;
  1067. }
  1068. continue;
  1069. }
  1070. // The entry is meant for this configuration.
  1071. cmLinkItem item = this->ResolveLinkItem(
  1072. BT<std::string>(name, entry.Backtrace), lg, linkFeature);
  1073. if (item.Target) {
  1074. auto depsForTarget = synthTargetsForConfig.find(item.Target);
  1075. if (depsForTarget != synthTargetsForConfig.end()) {
  1076. for (auto const* depForTarget : depsForTarget->second) {
  1077. cmLinkItem synthItem(depForTarget, item.Cross, item.Backtrace);
  1078. impl.Libraries.emplace_back(std::move(synthItem));
  1079. }
  1080. }
  1081. } else {
  1082. // Report explicitly linked object files separately.
  1083. std::string const& maybeObj = item.AsStr();
  1084. if (cmSystemTools::FileIsFullPath(maybeObj)) {
  1085. cmSourceFile const* sf =
  1086. mf->GetSource(maybeObj, cmSourceFileLocationKind::Known);
  1087. if (sf && sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  1088. item.ObjectSource = sf;
  1089. impl.Objects.emplace_back(std::move(item));
  1090. continue;
  1091. }
  1092. }
  1093. }
  1094. impl.Libraries.emplace_back(std::move(item));
  1095. }
  1096. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  1097. for (std::string const& sp : seenProps) {
  1098. if (!this->GetProperty(sp)) {
  1099. this->LinkImplicitNullProperties.insert(sp);
  1100. }
  1101. }
  1102. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  1103. }
  1104. // Update the list of direct link dependencies from usage requirements.
  1105. ComputeLinkImplTransitive(this, config, usage, impl);
  1106. // Get the list of configurations considered to be DEBUG.
  1107. std::vector<std::string> debugConfigs =
  1108. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  1109. cmTargetLinkLibraryType linkType = ComputeLinkType(config, debugConfigs);
  1110. cmTarget::LinkLibraryVectorType const& oldllibs =
  1111. this->Target->GetOriginalLinkLibraries();
  1112. auto linkFeature = cmLinkItem::DEFAULT;
  1113. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  1114. if (auto maybeLinkFeature = ParseLinkFeature(oldllib.first)) {
  1115. linkFeature = std::move(*maybeLinkFeature);
  1116. continue;
  1117. }
  1118. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  1119. std::string name = this->CheckCMP0004(oldllib.first);
  1120. if (name == this->GetName() || name.empty()) {
  1121. continue;
  1122. }
  1123. }
  1124. }
  1125. }
  1126. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  1127. std::string const& name) const
  1128. {
  1129. return this->ResolveTargetReference(name, this->LocalGenerator);
  1130. }
  1131. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  1132. std::string const& name, cmLocalGenerator const* lg) const
  1133. {
  1134. TargetOrString resolved;
  1135. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  1136. resolved.Target = tgt;
  1137. } else {
  1138. resolved.String = name;
  1139. }
  1140. return resolved;
  1141. }
  1142. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  1143. BT<std::string> const& name, std::string const& linkFeature) const
  1144. {
  1145. return this->ResolveLinkItem(name, this->LocalGenerator, linkFeature);
  1146. }
  1147. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  1148. BT<std::string> const& name, cmLocalGenerator const* lg,
  1149. std::string const& linkFeature) const
  1150. {
  1151. auto bt = name.Backtrace;
  1152. TargetOrString resolved = this->ResolveTargetReference(name.Value, lg);
  1153. if (!resolved.Target) {
  1154. return cmLinkItem(resolved.String, false, bt, linkFeature);
  1155. }
  1156. // Check deprecation, issue message with `bt` backtrace.
  1157. if (resolved.Target->IsDeprecated()) {
  1158. std::ostringstream w;
  1159. /* clang-format off */
  1160. w <<
  1161. "The library that is being linked to, " << resolved.Target->GetName() <<
  1162. ", is marked as being deprecated by the owner. The message provided by "
  1163. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  1164. /* clang-format on */
  1165. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  1166. MessageType::AUTHOR_WARNING, w.str(), bt);
  1167. }
  1168. // Skip targets that will not really be linked. This is probably a
  1169. // name conflict between an external library and an executable
  1170. // within the project.
  1171. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  1172. !resolved.Target->IsExecutableWithExports()) {
  1173. return cmLinkItem(resolved.Target->GetName(), false, bt, linkFeature);
  1174. }
  1175. return cmLinkItem(resolved.Target, false, bt, linkFeature);
  1176. }