cmTargetLinkLibrariesCommand.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmTargetLinkLibrariesCommand.h"
  4. #include <cassert>
  5. #include <cstddef>
  6. #include <memory>
  7. #include <sstream>
  8. #include <unordered_set>
  9. #include <utility>
  10. #include <cm/optional>
  11. #include <cm/string_view>
  12. #include "cmExecutionStatus.h"
  13. #include "cmGeneratorExpression.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmListFileCache.h"
  16. #include "cmMakefile.h"
  17. #include "cmMessageType.h"
  18. #include "cmPolicies.h"
  19. #include "cmState.h"
  20. #include "cmStateTypes.h"
  21. #include "cmStringAlgorithms.h"
  22. #include "cmSystemTools.h"
  23. #include "cmTarget.h"
  24. #include "cmTargetLinkLibraryType.h"
  25. #include "cmValue.h"
  26. #include "cmake.h"
  27. namespace {
  28. enum ProcessingState
  29. {
  30. ProcessingLinkLibraries,
  31. ProcessingPlainLinkInterface,
  32. ProcessingKeywordLinkInterface,
  33. ProcessingPlainPublicInterface,
  34. ProcessingKeywordPublicInterface,
  35. ProcessingPlainPrivateInterface,
  36. ProcessingKeywordPrivateInterface
  37. };
  38. const char* LinkLibraryTypeNames[3] = { "general", "debug", "optimized" };
  39. struct TLL
  40. {
  41. cmMakefile& Makefile;
  42. cmTarget* Target;
  43. bool WarnRemoteInterface = false;
  44. bool RejectRemoteLinking = false;
  45. bool EncodeRemoteReference = false;
  46. std::string DirectoryId;
  47. std::unordered_set<std::string> Props;
  48. TLL(cmMakefile& mf, cmTarget* target);
  49. ~TLL();
  50. bool HandleLibrary(ProcessingState currentProcessingState,
  51. const std::string& lib, cmTargetLinkLibraryType llt);
  52. void AppendProperty(std::string const& prop, std::string const& value);
  53. void AffectsProperty(std::string const& prop);
  54. };
  55. } // namespace
  56. static void LinkLibraryTypeSpecifierWarning(cmMakefile& mf, int left,
  57. int right);
  58. bool cmTargetLinkLibrariesCommand(std::vector<std::string> const& args,
  59. cmExecutionStatus& status)
  60. {
  61. // Must have at least one argument.
  62. if (args.empty()) {
  63. status.SetError("called with incorrect number of arguments");
  64. return false;
  65. }
  66. cmMakefile& mf = status.GetMakefile();
  67. // Alias targets cannot be on the LHS of this command.
  68. if (mf.IsAlias(args[0])) {
  69. status.SetError("can not be used on an ALIAS target.");
  70. return false;
  71. }
  72. // Lookup the target for which libraries are specified.
  73. cmTarget* target = mf.GetGlobalGenerator()->FindTarget(args[0]);
  74. if (!target) {
  75. for (const auto& importedTarget : mf.GetOwnedImportedTargets()) {
  76. if (importedTarget->GetName() == args[0] &&
  77. !importedTarget->IsForeign()) {
  78. target = importedTarget.get();
  79. break;
  80. }
  81. }
  82. }
  83. if (!target) {
  84. MessageType t = MessageType::FATAL_ERROR; // fail by default
  85. std::ostringstream e;
  86. e << "Cannot specify link libraries for target \"" << args[0]
  87. << "\" which is not built by this project.";
  88. // The bad target is the only argument. Check how policy CMP0016 is set,
  89. // and accept, warn or fail respectively:
  90. if (args.size() < 2) {
  91. switch (mf.GetPolicyStatus(cmPolicies::CMP0016)) {
  92. case cmPolicies::WARN:
  93. t = MessageType::AUTHOR_WARNING;
  94. // Print the warning.
  95. e << "\n"
  96. "CMake does not support this but it used to work accidentally "
  97. "and is being allowed for compatibility."
  98. "\n"
  99. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0016);
  100. break;
  101. case cmPolicies::OLD: // OLD behavior does not warn.
  102. t = MessageType::MESSAGE;
  103. break;
  104. case cmPolicies::NEW: // NEW behavior prints the error.
  105. break;
  106. }
  107. }
  108. // Now actually print the message.
  109. switch (t) {
  110. case MessageType::AUTHOR_WARNING:
  111. mf.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  112. break;
  113. case MessageType::FATAL_ERROR:
  114. mf.IssueMessage(MessageType::FATAL_ERROR, e.str());
  115. cmSystemTools::SetFatalErrorOccurred();
  116. break;
  117. default:
  118. break;
  119. }
  120. return true;
  121. }
  122. // Having a UTILITY library on the LHS is a bug.
  123. if (target->GetType() == cmStateEnums::UTILITY) {
  124. std::ostringstream e;
  125. const char* modal = nullptr;
  126. MessageType messageType = MessageType::AUTHOR_WARNING;
  127. switch (mf.GetPolicyStatus(cmPolicies::CMP0039)) {
  128. case cmPolicies::WARN:
  129. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0039) << '\n';
  130. modal = "should";
  131. CM_FALLTHROUGH;
  132. case cmPolicies::OLD:
  133. break;
  134. case cmPolicies::NEW:
  135. modal = "must";
  136. messageType = MessageType::FATAL_ERROR;
  137. break;
  138. }
  139. if (modal) {
  140. e << "Utility target \"" << target->GetName() << "\" " << modal
  141. << " not be used as the target of a target_link_libraries call.";
  142. mf.IssueMessage(messageType, e.str());
  143. if (messageType == MessageType::FATAL_ERROR) {
  144. return false;
  145. }
  146. }
  147. }
  148. // But we might not have any libs after variable expansion.
  149. if (args.size() < 2) {
  150. return true;
  151. }
  152. TLL tll(mf, target);
  153. // Keep track of link configuration specifiers.
  154. cmTargetLinkLibraryType llt = GENERAL_LibraryType;
  155. bool haveLLT = false;
  156. // Start with primary linking and switch to link interface
  157. // specification if the keyword is encountered as the first argument.
  158. ProcessingState currentProcessingState = ProcessingLinkLibraries;
  159. // Accumulate consecutive non-keyword arguments into one entry in
  160. // order to handle unquoted generator expressions containing ';'.
  161. std::size_t genexNesting = 0;
  162. cm::optional<std::string> currentEntry;
  163. auto processCurrentEntry = [&]() -> bool {
  164. // FIXME: Warn about partial genex if genexNesting > 0?
  165. genexNesting = 0;
  166. if (currentEntry) {
  167. assert(!haveLLT);
  168. if (!tll.HandleLibrary(currentProcessingState, *currentEntry,
  169. GENERAL_LibraryType)) {
  170. return false;
  171. }
  172. currentEntry = cm::nullopt;
  173. }
  174. return true;
  175. };
  176. auto extendCurrentEntry = [&currentEntry](std::string const& arg) {
  177. if (currentEntry) {
  178. currentEntry = cmStrCat(*currentEntry, ';', arg);
  179. } else {
  180. currentEntry = arg;
  181. }
  182. };
  183. // Keep this list in sync with the keyword dispatch below.
  184. static std::unordered_set<std::string> const keywords{
  185. "LINK_INTERFACE_LIBRARIES",
  186. "INTERFACE",
  187. "LINK_PUBLIC",
  188. "PUBLIC",
  189. "LINK_PRIVATE",
  190. "PRIVATE",
  191. "debug",
  192. "optimized",
  193. "general",
  194. };
  195. // Add libraries, note that there is an optional prefix
  196. // of debug and optimized that can be used.
  197. for (unsigned int i = 1; i < args.size(); ++i) {
  198. if (keywords.count(args[i])) {
  199. // A keyword argument terminates any accumulated partial genex.
  200. if (!processCurrentEntry()) {
  201. return false;
  202. }
  203. // Process this keyword argument.
  204. if (args[i] == "LINK_INTERFACE_LIBRARIES") {
  205. currentProcessingState = ProcessingPlainLinkInterface;
  206. if (i != 1) {
  207. mf.IssueMessage(
  208. MessageType::FATAL_ERROR,
  209. "The LINK_INTERFACE_LIBRARIES option must appear as the "
  210. "second argument, just after the target name.");
  211. return true;
  212. }
  213. } else if (args[i] == "INTERFACE") {
  214. if (i != 1 &&
  215. currentProcessingState != ProcessingKeywordPrivateInterface &&
  216. currentProcessingState != ProcessingKeywordPublicInterface &&
  217. currentProcessingState != ProcessingKeywordLinkInterface) {
  218. mf.IssueMessage(MessageType::FATAL_ERROR,
  219. "The INTERFACE, PUBLIC or PRIVATE option must "
  220. "appear as the second argument, just after the "
  221. "target name.");
  222. return true;
  223. }
  224. currentProcessingState = ProcessingKeywordLinkInterface;
  225. } else if (args[i] == "LINK_PUBLIC") {
  226. if (i != 1 &&
  227. currentProcessingState != ProcessingPlainPrivateInterface &&
  228. currentProcessingState != ProcessingPlainPublicInterface) {
  229. mf.IssueMessage(
  230. MessageType::FATAL_ERROR,
  231. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the "
  232. "second argument, just after the target name.");
  233. return true;
  234. }
  235. currentProcessingState = ProcessingPlainPublicInterface;
  236. } else if (args[i] == "PUBLIC") {
  237. if (i != 1 &&
  238. currentProcessingState != ProcessingKeywordPrivateInterface &&
  239. currentProcessingState != ProcessingKeywordPublicInterface &&
  240. currentProcessingState != ProcessingKeywordLinkInterface) {
  241. mf.IssueMessage(MessageType::FATAL_ERROR,
  242. "The INTERFACE, PUBLIC or PRIVATE option must "
  243. "appear as the second argument, just after the "
  244. "target name.");
  245. return true;
  246. }
  247. currentProcessingState = ProcessingKeywordPublicInterface;
  248. } else if (args[i] == "LINK_PRIVATE") {
  249. if (i != 1 &&
  250. currentProcessingState != ProcessingPlainPublicInterface &&
  251. currentProcessingState != ProcessingPlainPrivateInterface) {
  252. mf.IssueMessage(
  253. MessageType::FATAL_ERROR,
  254. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the "
  255. "second argument, just after the target name.");
  256. return true;
  257. }
  258. currentProcessingState = ProcessingPlainPrivateInterface;
  259. } else if (args[i] == "PRIVATE") {
  260. if (i != 1 &&
  261. currentProcessingState != ProcessingKeywordPrivateInterface &&
  262. currentProcessingState != ProcessingKeywordPublicInterface &&
  263. currentProcessingState != ProcessingKeywordLinkInterface) {
  264. mf.IssueMessage(MessageType::FATAL_ERROR,
  265. "The INTERFACE, PUBLIC or PRIVATE option must "
  266. "appear as the second argument, just after the "
  267. "target name.");
  268. return true;
  269. }
  270. currentProcessingState = ProcessingKeywordPrivateInterface;
  271. } else if (args[i] == "debug") {
  272. if (haveLLT) {
  273. LinkLibraryTypeSpecifierWarning(mf, llt, DEBUG_LibraryType);
  274. }
  275. llt = DEBUG_LibraryType;
  276. haveLLT = true;
  277. } else if (args[i] == "optimized") {
  278. if (haveLLT) {
  279. LinkLibraryTypeSpecifierWarning(mf, llt, OPTIMIZED_LibraryType);
  280. }
  281. llt = OPTIMIZED_LibraryType;
  282. haveLLT = true;
  283. } else if (args[i] == "general") {
  284. if (haveLLT) {
  285. LinkLibraryTypeSpecifierWarning(mf, llt, GENERAL_LibraryType);
  286. }
  287. llt = GENERAL_LibraryType;
  288. haveLLT = true;
  289. }
  290. } else if (haveLLT) {
  291. // The link type was specified by the previous argument.
  292. haveLLT = false;
  293. assert(!currentEntry);
  294. if (!tll.HandleLibrary(currentProcessingState, args[i], llt)) {
  295. return false;
  296. }
  297. llt = GENERAL_LibraryType;
  298. } else {
  299. // Track the genex nesting level.
  300. {
  301. cm::string_view arg = args[i];
  302. for (std::string::size_type pos = 0; pos < arg.size(); ++pos) {
  303. cm::string_view cur = arg.substr(pos);
  304. if (cmHasLiteralPrefix(cur, "$<")) {
  305. ++genexNesting;
  306. ++pos;
  307. } else if (genexNesting > 0 && cmHasLiteralPrefix(cur, ">")) {
  308. --genexNesting;
  309. }
  310. }
  311. }
  312. // Accumulate this argument in the current entry.
  313. extendCurrentEntry(args[i]);
  314. // Process this entry if it does not end inside a genex.
  315. if (genexNesting == 0) {
  316. if (!processCurrentEntry()) {
  317. return false;
  318. }
  319. }
  320. }
  321. }
  322. // Process the last accumulated partial genex, if any.
  323. if (!processCurrentEntry()) {
  324. return false;
  325. }
  326. // Make sure the last argument was not a library type specifier.
  327. if (haveLLT) {
  328. mf.IssueMessage(MessageType::FATAL_ERROR,
  329. cmStrCat("The \"", LinkLibraryTypeNames[llt],
  330. "\" argument must be followed by a library."));
  331. cmSystemTools::SetFatalErrorOccurred();
  332. }
  333. const cmPolicies::PolicyStatus policy22Status =
  334. target->GetPolicyStatusCMP0022();
  335. // If any of the LINK_ options were given, make sure the
  336. // LINK_INTERFACE_LIBRARIES target property exists.
  337. // Use of any of the new keywords implies awareness of
  338. // this property. And if no libraries are named, it should
  339. // result in an empty link interface.
  340. if ((policy22Status == cmPolicies::OLD ||
  341. policy22Status == cmPolicies::WARN) &&
  342. currentProcessingState != ProcessingLinkLibraries &&
  343. !target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  344. target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  345. }
  346. return true;
  347. }
  348. static void LinkLibraryTypeSpecifierWarning(cmMakefile& mf, int left,
  349. int right)
  350. {
  351. mf.IssueMessage(
  352. MessageType::AUTHOR_WARNING,
  353. cmStrCat(
  354. "Link library type specifier \"", LinkLibraryTypeNames[left],
  355. "\" is followed by specifier \"", LinkLibraryTypeNames[right],
  356. "\" instead of a library name. The first specifier will be ignored."));
  357. }
  358. namespace {
  359. TLL::TLL(cmMakefile& mf, cmTarget* target)
  360. : Makefile(mf)
  361. , Target(target)
  362. {
  363. if (&this->Makefile != this->Target->GetMakefile()) {
  364. // The LHS target was created in another directory.
  365. switch (this->Makefile.GetPolicyStatus(cmPolicies::CMP0079)) {
  366. case cmPolicies::WARN:
  367. this->WarnRemoteInterface = true;
  368. CM_FALLTHROUGH;
  369. case cmPolicies::OLD:
  370. this->RejectRemoteLinking = true;
  371. break;
  372. case cmPolicies::NEW:
  373. this->EncodeRemoteReference = true;
  374. break;
  375. }
  376. }
  377. if (this->EncodeRemoteReference) {
  378. cmDirectoryId const dirId = this->Makefile.GetDirectoryId();
  379. this->DirectoryId = cmStrCat(CMAKE_DIRECTORY_ID_SEP, dirId.String);
  380. }
  381. }
  382. bool TLL::HandleLibrary(ProcessingState currentProcessingState,
  383. const std::string& lib, cmTargetLinkLibraryType llt)
  384. {
  385. if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
  386. currentProcessingState != ProcessingKeywordLinkInterface) {
  387. this->Makefile.IssueMessage(
  388. MessageType::FATAL_ERROR,
  389. "INTERFACE library can only be used with the INTERFACE keyword of "
  390. "target_link_libraries");
  391. return false;
  392. }
  393. if (this->Target->IsImported() &&
  394. currentProcessingState != ProcessingKeywordLinkInterface) {
  395. this->Makefile.IssueMessage(
  396. MessageType::FATAL_ERROR,
  397. "IMPORTED library can only be used with the INTERFACE keyword of "
  398. "target_link_libraries");
  399. return false;
  400. }
  401. cmTarget::TLLSignature sig =
  402. (currentProcessingState == ProcessingPlainPrivateInterface ||
  403. currentProcessingState == ProcessingPlainPublicInterface ||
  404. currentProcessingState == ProcessingKeywordPrivateInterface ||
  405. currentProcessingState == ProcessingKeywordPublicInterface ||
  406. currentProcessingState == ProcessingKeywordLinkInterface)
  407. ? cmTarget::KeywordTLLSignature
  408. : cmTarget::PlainTLLSignature;
  409. if (!this->Target->PushTLLCommandTrace(
  410. sig, this->Makefile.GetBacktrace().Top())) {
  411. std::ostringstream e;
  412. const char* modal = nullptr;
  413. MessageType messageType = MessageType::AUTHOR_WARNING;
  414. switch (this->Makefile.GetPolicyStatus(cmPolicies::CMP0023)) {
  415. case cmPolicies::WARN:
  416. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0023) << '\n';
  417. modal = "should";
  418. CM_FALLTHROUGH;
  419. case cmPolicies::OLD:
  420. break;
  421. case cmPolicies::NEW:
  422. modal = "must";
  423. messageType = MessageType::FATAL_ERROR;
  424. break;
  425. }
  426. if (modal) {
  427. // If the sig is a keyword form and there is a conflict, the existing
  428. // form must be the plain form.
  429. const char* existingSig =
  430. (sig == cmTarget::KeywordTLLSignature ? "plain" : "keyword");
  431. e << "The " << existingSig
  432. << " signature for target_link_libraries has "
  433. "already been used with the target \""
  434. << this->Target->GetName()
  435. << "\". All uses of target_link_libraries with a target " << modal
  436. << " be either all-keyword or all-plain.\n";
  437. this->Target->GetTllSignatureTraces(e,
  438. sig == cmTarget::KeywordTLLSignature
  439. ? cmTarget::PlainTLLSignature
  440. : cmTarget::KeywordTLLSignature);
  441. this->Makefile.IssueMessage(messageType, e.str());
  442. if (messageType == MessageType::FATAL_ERROR) {
  443. return false;
  444. }
  445. }
  446. }
  447. // Handle normal case where the command was called with another keyword than
  448. // INTERFACE / LINK_INTERFACE_LIBRARIES or none at all. (The "LINK_LIBRARIES"
  449. // property of the target on the LHS shall be populated.)
  450. if (currentProcessingState != ProcessingKeywordLinkInterface &&
  451. currentProcessingState != ProcessingPlainLinkInterface) {
  452. if (this->RejectRemoteLinking) {
  453. this->Makefile.IssueMessage(
  454. MessageType::FATAL_ERROR,
  455. cmStrCat("Attempt to add link library \"", lib, "\" to target \"",
  456. this->Target->GetName(),
  457. "\" which is not built in this "
  458. "directory.\nThis is allowed only when policy CMP0079 "
  459. "is set to NEW."));
  460. return false;
  461. }
  462. cmTarget* tgt = this->Makefile.GetGlobalGenerator()->FindTarget(lib);
  463. if (tgt && (tgt->GetType() != cmStateEnums::STATIC_LIBRARY) &&
  464. (tgt->GetType() != cmStateEnums::SHARED_LIBRARY) &&
  465. (tgt->GetType() != cmStateEnums::UNKNOWN_LIBRARY) &&
  466. (tgt->GetType() != cmStateEnums::OBJECT_LIBRARY) &&
  467. (tgt->GetType() != cmStateEnums::INTERFACE_LIBRARY) &&
  468. !tgt->IsExecutableWithExports()) {
  469. this->Makefile.IssueMessage(
  470. MessageType::FATAL_ERROR,
  471. cmStrCat(
  472. "Target \"", lib, "\" of type ",
  473. cmState::GetTargetTypeName(tgt->GetType()),
  474. " may not be linked into another target. One may link only to "
  475. "INTERFACE, OBJECT, STATIC or SHARED libraries, or to ",
  476. "executables with the ENABLE_EXPORTS property set."));
  477. }
  478. this->AffectsProperty("LINK_LIBRARIES");
  479. this->Target->AddLinkLibrary(this->Makefile, lib, llt);
  480. }
  481. if (this->WarnRemoteInterface) {
  482. this->Makefile.IssueMessage(
  483. MessageType::AUTHOR_WARNING,
  484. cmStrCat(
  485. cmPolicies::GetPolicyWarning(cmPolicies::CMP0079), "\nTarget\n ",
  486. this->Target->GetName(),
  487. "\nis not created in this "
  488. "directory. For compatibility with older versions of CMake, link "
  489. "library\n ",
  490. lib,
  491. "\nwill be looked up in the directory in which "
  492. "the target was created rather than in this calling directory."));
  493. }
  494. // Handle (additional) case where the command was called with PRIVATE /
  495. // LINK_PRIVATE and stop its processing. (The "INTERFACE_LINK_LIBRARIES"
  496. // property of the target on the LHS shall only be populated if it is a
  497. // STATIC library.)
  498. if (currentProcessingState == ProcessingKeywordPrivateInterface ||
  499. currentProcessingState == ProcessingPlainPrivateInterface) {
  500. if (this->Target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  501. this->Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  502. // TODO: Detect and no-op `$<COMPILE_ONLY>` genexes here.
  503. std::string configLib =
  504. this->Target->GetDebugGeneratorExpressions(lib, llt);
  505. if (cmGeneratorExpression::IsValidTargetName(lib) ||
  506. cmGeneratorExpression::Find(lib) != std::string::npos) {
  507. configLib = "$<LINK_ONLY:" + configLib + ">";
  508. }
  509. this->AppendProperty("INTERFACE_LINK_LIBRARIES", configLib);
  510. }
  511. return true;
  512. }
  513. // Handle general case where the command was called with another keyword than
  514. // PRIVATE / LINK_PRIVATE or none at all. (The "INTERFACE_LINK_LIBRARIES"
  515. // property of the target on the LHS shall be populated.)
  516. this->AppendProperty("INTERFACE_LINK_LIBRARIES",
  517. this->Target->GetDebugGeneratorExpressions(lib, llt));
  518. // Stop processing if called without any keyword.
  519. if (currentProcessingState == ProcessingLinkLibraries) {
  520. return true;
  521. }
  522. // Stop processing if policy CMP0022 is set to NEW.
  523. const cmPolicies::PolicyStatus policy22Status =
  524. this->Target->GetPolicyStatusCMP0022();
  525. if (policy22Status != cmPolicies::OLD &&
  526. policy22Status != cmPolicies::WARN) {
  527. return true;
  528. }
  529. // Stop processing if called with an INTERFACE library on the LHS.
  530. if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  531. return true;
  532. }
  533. // Handle (additional) backward-compatibility case where the command was
  534. // called with PUBLIC / INTERFACE / LINK_PUBLIC / LINK_INTERFACE_LIBRARIES.
  535. // (The policy CMP0022 is not set to NEW.)
  536. {
  537. // Get the list of configurations considered to be DEBUG.
  538. std::vector<std::string> debugConfigs =
  539. this->Makefile.GetCMakeInstance()->GetDebugConfigs();
  540. std::string prop;
  541. // Include this library in the link interface for the target.
  542. if (llt == DEBUG_LibraryType || llt == GENERAL_LibraryType) {
  543. // Put in the DEBUG configuration interfaces.
  544. for (std::string const& dc : debugConfigs) {
  545. prop = cmStrCat("LINK_INTERFACE_LIBRARIES_", dc);
  546. this->AppendProperty(prop, lib);
  547. }
  548. }
  549. if (llt == OPTIMIZED_LibraryType || llt == GENERAL_LibraryType) {
  550. // Put in the non-DEBUG configuration interfaces.
  551. this->AppendProperty("LINK_INTERFACE_LIBRARIES", lib);
  552. // Make sure the DEBUG configuration interfaces exist so that the
  553. // general one will not be used as a fall-back.
  554. for (std::string const& dc : debugConfigs) {
  555. prop = cmStrCat("LINK_INTERFACE_LIBRARIES_", dc);
  556. if (!this->Target->GetProperty(prop)) {
  557. this->Target->SetProperty(prop, "");
  558. }
  559. }
  560. }
  561. }
  562. return true;
  563. }
  564. void TLL::AppendProperty(std::string const& prop, std::string const& value)
  565. {
  566. this->AffectsProperty(prop);
  567. this->Target->AppendProperty(prop, value, this->Makefile.GetBacktrace());
  568. }
  569. void TLL::AffectsProperty(std::string const& prop)
  570. {
  571. if (!this->EncodeRemoteReference) {
  572. return;
  573. }
  574. // Add a wrapper to the expression to tell LookupLinkItem to look up
  575. // names in the caller's directory.
  576. if (this->Props.insert(prop).second) {
  577. this->Target->AppendProperty(prop, this->DirectoryId,
  578. this->Makefile.GetBacktrace());
  579. }
  580. }
  581. TLL::~TLL()
  582. {
  583. for (std::string const& prop : this->Props) {
  584. this->Target->AppendProperty(prop, CMAKE_DIRECTORY_ID_SEP,
  585. this->Makefile.GetBacktrace());
  586. }
  587. }
  588. } // namespace