cmGeneratorTarget_Link.cxx 53 KB

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