cmTargetLinkLibrariesCommand.cxx 23 KB

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