cmTargetLinkLibrariesCommand.cxx 22 KB

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