1
0

cmTargetLinkLibrariesCommand.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 <cstring>
  5. #include <sstream>
  6. #include "cmExecutionStatus.h"
  7. #include "cmGeneratorExpression.h"
  8. #include "cmGlobalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmMessageType.h"
  11. #include "cmPolicies.h"
  12. #include "cmState.h"
  13. #include "cmStateTypes.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include "cmTargetLinkLibraryType.h"
  18. #include "cmake.h"
  19. namespace {
  20. enum ProcessingState
  21. {
  22. ProcessingLinkLibraries,
  23. ProcessingPlainLinkInterface,
  24. ProcessingKeywordLinkInterface,
  25. ProcessingPlainPublicInterface,
  26. ProcessingKeywordPublicInterface,
  27. ProcessingPlainPrivateInterface,
  28. ProcessingKeywordPrivateInterface
  29. };
  30. const char* LinkLibraryTypeNames[3] = { "general", "debug", "optimized" };
  31. } // namespace
  32. static void LinkLibraryTypeSpecifierWarning(cmMakefile& mf, int left,
  33. int right);
  34. static bool HandleLibrary(cmMakefile& mf, cmTarget* target,
  35. ProcessingState currentProcessingState,
  36. const std::string& lib, cmTargetLinkLibraryType llt);
  37. bool cmTargetLinkLibrariesCommand(std::vector<std::string> const& args,
  38. cmExecutionStatus& status)
  39. {
  40. // Must have at least one argument.
  41. if (args.empty()) {
  42. status.SetError("called with incorrect number of arguments");
  43. return false;
  44. }
  45. cmMakefile& mf = status.GetMakefile();
  46. // Alias targets cannot be on the LHS of this command.
  47. if (mf.IsAlias(args[0])) {
  48. status.SetError("can not be used on an ALIAS target.");
  49. return false;
  50. }
  51. // Lookup the target for which libraries are specified.
  52. cmTarget* target =
  53. mf.GetCMakeInstance()->GetGlobalGenerator()->FindTarget(args[0]);
  54. if (!target) {
  55. const std::vector<cmTarget*>& importedTargets =
  56. mf.GetOwnedImportedTargets();
  57. for (cmTarget* importedTarget : importedTargets) {
  58. if (importedTarget->GetName() == args[0]) {
  59. target = importedTarget;
  60. break;
  61. }
  62. }
  63. }
  64. if (!target) {
  65. MessageType t = MessageType::FATAL_ERROR; // fail by default
  66. std::ostringstream e;
  67. e << "Cannot specify link libraries for target \"" << args[0] << "\" "
  68. << "which is not built by this project.";
  69. // The bad target is the only argument. Check how policy CMP0016 is set,
  70. // and accept, warn or fail respectively:
  71. if (args.size() < 2) {
  72. switch (mf.GetPolicyStatus(cmPolicies::CMP0016)) {
  73. case cmPolicies::WARN:
  74. t = MessageType::AUTHOR_WARNING;
  75. // Print the warning.
  76. e << "\n"
  77. << "CMake does not support this but it used to work accidentally "
  78. << "and is being allowed for compatibility."
  79. << "\n"
  80. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0016);
  81. break;
  82. case cmPolicies::OLD: // OLD behavior does not warn.
  83. t = MessageType::MESSAGE;
  84. break;
  85. case cmPolicies::REQUIRED_IF_USED:
  86. case cmPolicies::REQUIRED_ALWAYS:
  87. e << "\n" << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0016);
  88. break;
  89. case cmPolicies::NEW: // NEW behavior prints the error.
  90. break;
  91. }
  92. }
  93. // Now actually print the message.
  94. switch (t) {
  95. case MessageType::AUTHOR_WARNING:
  96. mf.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  97. break;
  98. case MessageType::FATAL_ERROR:
  99. mf.IssueMessage(MessageType::FATAL_ERROR, e.str());
  100. cmSystemTools::SetFatalErrorOccured();
  101. break;
  102. default:
  103. break;
  104. }
  105. return true;
  106. }
  107. // Having a UTILITY library on the LHS is a bug.
  108. if (target->GetType() == cmStateEnums::UTILITY) {
  109. std::ostringstream e;
  110. const char* modal = nullptr;
  111. MessageType messageType = MessageType::AUTHOR_WARNING;
  112. switch (mf.GetPolicyStatus(cmPolicies::CMP0039)) {
  113. case cmPolicies::WARN:
  114. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0039) << "\n";
  115. modal = "should";
  116. case cmPolicies::OLD:
  117. break;
  118. case cmPolicies::REQUIRED_ALWAYS:
  119. case cmPolicies::REQUIRED_IF_USED:
  120. case cmPolicies::NEW:
  121. modal = "must";
  122. messageType = MessageType::FATAL_ERROR;
  123. }
  124. if (modal) {
  125. e << "Utility target \"" << target->GetName() << "\" " << modal
  126. << " not be used as the target of a target_link_libraries call.";
  127. mf.IssueMessage(messageType, e.str());
  128. if (messageType == MessageType::FATAL_ERROR) {
  129. return false;
  130. }
  131. }
  132. }
  133. // But we might not have any libs after variable expansion.
  134. if (args.size() < 2) {
  135. return true;
  136. }
  137. // Keep track of link configuration specifiers.
  138. cmTargetLinkLibraryType llt = GENERAL_LibraryType;
  139. bool haveLLT = false;
  140. // Start with primary linking and switch to link interface
  141. // specification if the keyword is encountered as the first argument.
  142. ProcessingState currentProcessingState = ProcessingLinkLibraries;
  143. // Add libraries, note that there is an optional prefix
  144. // of debug and optimized that can be used.
  145. for (unsigned int i = 1; i < args.size(); ++i) {
  146. if (args[i] == "LINK_INTERFACE_LIBRARIES") {
  147. currentProcessingState = ProcessingPlainLinkInterface;
  148. if (i != 1) {
  149. mf.IssueMessage(
  150. MessageType::FATAL_ERROR,
  151. "The LINK_INTERFACE_LIBRARIES option must appear as the second "
  152. "argument, just after the target name.");
  153. return true;
  154. }
  155. } else if (args[i] == "INTERFACE") {
  156. if (i != 1 &&
  157. currentProcessingState != ProcessingKeywordPrivateInterface &&
  158. currentProcessingState != ProcessingKeywordPublicInterface &&
  159. currentProcessingState != ProcessingKeywordLinkInterface) {
  160. mf.IssueMessage(
  161. MessageType::FATAL_ERROR,
  162. "The INTERFACE, PUBLIC or PRIVATE option must appear as the second "
  163. "argument, just after the target name.");
  164. return true;
  165. }
  166. currentProcessingState = ProcessingKeywordLinkInterface;
  167. } else if (args[i] == "LINK_PUBLIC") {
  168. if (i != 1 &&
  169. currentProcessingState != ProcessingPlainPrivateInterface &&
  170. currentProcessingState != ProcessingPlainPublicInterface) {
  171. mf.IssueMessage(
  172. MessageType::FATAL_ERROR,
  173. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  174. "argument, just after the target name.");
  175. return true;
  176. }
  177. currentProcessingState = ProcessingPlainPublicInterface;
  178. } else if (args[i] == "PUBLIC") {
  179. if (i != 1 &&
  180. currentProcessingState != ProcessingKeywordPrivateInterface &&
  181. currentProcessingState != ProcessingKeywordPublicInterface &&
  182. currentProcessingState != ProcessingKeywordLinkInterface) {
  183. mf.IssueMessage(
  184. MessageType::FATAL_ERROR,
  185. "The INTERFACE, PUBLIC or PRIVATE option must appear as the second "
  186. "argument, just after the target name.");
  187. return true;
  188. }
  189. currentProcessingState = ProcessingKeywordPublicInterface;
  190. } else if (args[i] == "LINK_PRIVATE") {
  191. if (i != 1 && currentProcessingState != ProcessingPlainPublicInterface &&
  192. currentProcessingState != ProcessingPlainPrivateInterface) {
  193. mf.IssueMessage(
  194. MessageType::FATAL_ERROR,
  195. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  196. "argument, just after the target name.");
  197. return true;
  198. }
  199. currentProcessingState = ProcessingPlainPrivateInterface;
  200. } else if (args[i] == "PRIVATE") {
  201. if (i != 1 &&
  202. currentProcessingState != ProcessingKeywordPrivateInterface &&
  203. currentProcessingState != ProcessingKeywordPublicInterface &&
  204. currentProcessingState != ProcessingKeywordLinkInterface) {
  205. mf.IssueMessage(
  206. MessageType::FATAL_ERROR,
  207. "The INTERFACE, PUBLIC or PRIVATE option must appear as the second "
  208. "argument, just after the target name.");
  209. return true;
  210. }
  211. currentProcessingState = ProcessingKeywordPrivateInterface;
  212. } else if (args[i] == "debug") {
  213. if (haveLLT) {
  214. LinkLibraryTypeSpecifierWarning(mf, llt, DEBUG_LibraryType);
  215. }
  216. llt = DEBUG_LibraryType;
  217. haveLLT = true;
  218. } else if (args[i] == "optimized") {
  219. if (haveLLT) {
  220. LinkLibraryTypeSpecifierWarning(mf, llt, OPTIMIZED_LibraryType);
  221. }
  222. llt = OPTIMIZED_LibraryType;
  223. haveLLT = true;
  224. } else if (args[i] == "general") {
  225. if (haveLLT) {
  226. LinkLibraryTypeSpecifierWarning(mf, llt, GENERAL_LibraryType);
  227. }
  228. llt = GENERAL_LibraryType;
  229. haveLLT = true;
  230. } else if (haveLLT) {
  231. // The link type was specified by the previous argument.
  232. haveLLT = false;
  233. if (!HandleLibrary(mf, target, currentProcessingState, args[i], llt)) {
  234. return false;
  235. }
  236. } else {
  237. // Lookup old-style cache entry if type is unspecified. So if you
  238. // do a target_link_libraries(foo optimized bar) it will stay optimized
  239. // and not use the lookup. As there may be the case where someone has
  240. // specified that a library is both debug and optimized. (this check is
  241. // only there for backwards compatibility when mixing projects built
  242. // with old versions of CMake and new)
  243. llt = GENERAL_LibraryType;
  244. std::string linkType = cmStrCat(args[0], "_LINK_TYPE");
  245. const char* linkTypeString = mf.GetDefinition(linkType);
  246. if (linkTypeString) {
  247. if (strcmp(linkTypeString, "debug") == 0) {
  248. llt = DEBUG_LibraryType;
  249. }
  250. if (strcmp(linkTypeString, "optimized") == 0) {
  251. llt = OPTIMIZED_LibraryType;
  252. }
  253. }
  254. if (!HandleLibrary(mf, target, currentProcessingState, args[i], llt)) {
  255. return false;
  256. }
  257. }
  258. }
  259. // Make sure the last argument was not a library type specifier.
  260. if (haveLLT) {
  261. mf.IssueMessage(MessageType::FATAL_ERROR,
  262. cmStrCat("The \"", LinkLibraryTypeNames[llt],
  263. "\" argument must be followed by a library."));
  264. cmSystemTools::SetFatalErrorOccured();
  265. }
  266. const cmPolicies::PolicyStatus policy22Status =
  267. target->GetPolicyStatusCMP0022();
  268. // If any of the LINK_ options were given, make sure the
  269. // LINK_INTERFACE_LIBRARIES target property exists.
  270. // Use of any of the new keywords implies awareness of
  271. // this property. And if no libraries are named, it should
  272. // result in an empty link interface.
  273. if ((policy22Status == cmPolicies::OLD ||
  274. policy22Status == cmPolicies::WARN) &&
  275. currentProcessingState != ProcessingLinkLibraries &&
  276. !target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  277. target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  278. }
  279. return true;
  280. }
  281. static void LinkLibraryTypeSpecifierWarning(cmMakefile& mf, int left,
  282. int right)
  283. {
  284. mf.IssueMessage(
  285. MessageType::AUTHOR_WARNING,
  286. cmStrCat(
  287. "Link library type specifier \"", LinkLibraryTypeNames[left],
  288. "\" is followed by specifier \"", LinkLibraryTypeNames[right],
  289. "\" instead of a library name. The first specifier will be ignored."));
  290. }
  291. static bool HandleLibrary(cmMakefile& mf, cmTarget* target,
  292. ProcessingState currentProcessingState,
  293. const std::string& lib, cmTargetLinkLibraryType llt)
  294. {
  295. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
  296. currentProcessingState != ProcessingKeywordLinkInterface) {
  297. mf.IssueMessage(
  298. MessageType::FATAL_ERROR,
  299. "INTERFACE library can only be used with the INTERFACE keyword of "
  300. "target_link_libraries");
  301. return false;
  302. }
  303. if (target->IsImported() &&
  304. currentProcessingState != ProcessingKeywordLinkInterface) {
  305. mf.IssueMessage(
  306. MessageType::FATAL_ERROR,
  307. "IMPORTED library can only be used with the INTERFACE keyword of "
  308. "target_link_libraries");
  309. return false;
  310. }
  311. cmTarget::TLLSignature sig =
  312. (currentProcessingState == ProcessingPlainPrivateInterface ||
  313. currentProcessingState == ProcessingPlainPublicInterface ||
  314. currentProcessingState == ProcessingKeywordPrivateInterface ||
  315. currentProcessingState == ProcessingKeywordPublicInterface ||
  316. currentProcessingState == ProcessingKeywordLinkInterface)
  317. ? cmTarget::KeywordTLLSignature
  318. : cmTarget::PlainTLLSignature;
  319. if (!target->PushTLLCommandTrace(sig, mf.GetExecutionContext())) {
  320. std::ostringstream e;
  321. const char* modal = nullptr;
  322. MessageType messageType = MessageType::AUTHOR_WARNING;
  323. switch (mf.GetPolicyStatus(cmPolicies::CMP0023)) {
  324. case cmPolicies::WARN:
  325. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0023) << "\n";
  326. modal = "should";
  327. case cmPolicies::OLD:
  328. break;
  329. case cmPolicies::REQUIRED_ALWAYS:
  330. case cmPolicies::REQUIRED_IF_USED:
  331. case cmPolicies::NEW:
  332. modal = "must";
  333. messageType = MessageType::FATAL_ERROR;
  334. }
  335. if (modal) {
  336. // If the sig is a keyword form and there is a conflict, the existing
  337. // form must be the plain form.
  338. const char* existingSig =
  339. (sig == cmTarget::KeywordTLLSignature ? "plain" : "keyword");
  340. e << "The " << existingSig
  341. << " signature for target_link_libraries has "
  342. "already been used with the target \""
  343. << target->GetName()
  344. << "\". All uses of target_link_libraries with a target " << modal
  345. << " be either all-keyword or all-plain.\n";
  346. target->GetTllSignatureTraces(e,
  347. sig == cmTarget::KeywordTLLSignature
  348. ? cmTarget::PlainTLLSignature
  349. : cmTarget::KeywordTLLSignature);
  350. mf.IssueMessage(messageType, e.str());
  351. if (messageType == MessageType::FATAL_ERROR) {
  352. return false;
  353. }
  354. }
  355. }
  356. bool warnRemoteInterface = false;
  357. bool rejectRemoteLinking = false;
  358. bool encodeRemoteReference = false;
  359. if (&mf != target->GetMakefile()) {
  360. // The LHS target was created in another directory.
  361. switch (mf.GetPolicyStatus(cmPolicies::CMP0079)) {
  362. case cmPolicies::WARN:
  363. warnRemoteInterface = true;
  364. CM_FALLTHROUGH;
  365. case cmPolicies::OLD:
  366. rejectRemoteLinking = true;
  367. break;
  368. case cmPolicies::REQUIRED_ALWAYS:
  369. case cmPolicies::REQUIRED_IF_USED:
  370. case cmPolicies::NEW:
  371. encodeRemoteReference = true;
  372. break;
  373. }
  374. }
  375. std::string libRef;
  376. if (encodeRemoteReference && !cmSystemTools::FileIsFullPath(lib)) {
  377. // This is a library name added by a caller that is not in the
  378. // same directory as the target was created. Add a suffix to
  379. // the name to tell ResolveLinkItem to look up the name in the
  380. // caller's directory.
  381. cmDirectoryId const dirId = mf.GetDirectoryId();
  382. libRef = lib + CMAKE_DIRECTORY_ID_SEP + dirId.String;
  383. } else {
  384. // This is an absolute path or a library name added by a caller
  385. // in the same directory as the target was created. We can use
  386. // the original name directly.
  387. libRef = lib;
  388. }
  389. // Handle normal case where the command was called with another keyword than
  390. // INTERFACE / LINK_INTERFACE_LIBRARIES or none at all. (The "LINK_LIBRARIES"
  391. // property of the target on the LHS shall be populated.)
  392. if (currentProcessingState != ProcessingKeywordLinkInterface &&
  393. currentProcessingState != ProcessingPlainLinkInterface) {
  394. if (rejectRemoteLinking) {
  395. mf.IssueMessage(
  396. MessageType::FATAL_ERROR,
  397. cmStrCat("Attempt to add link library \"", lib, "\" to target \"",
  398. target->GetName(),
  399. "\" which is not built in this "
  400. "directory.\nThis is allowed only when policy CMP0079 "
  401. "is set to NEW."));
  402. return false;
  403. }
  404. cmTarget* tgt = mf.GetGlobalGenerator()->FindTarget(lib);
  405. if (tgt && (tgt->GetType() != cmStateEnums::STATIC_LIBRARY) &&
  406. (tgt->GetType() != cmStateEnums::SHARED_LIBRARY) &&
  407. (tgt->GetType() != cmStateEnums::UNKNOWN_LIBRARY) &&
  408. (tgt->GetType() != cmStateEnums::OBJECT_LIBRARY) &&
  409. (tgt->GetType() != cmStateEnums::INTERFACE_LIBRARY) &&
  410. !tgt->IsExecutableWithExports()) {
  411. mf.IssueMessage(
  412. MessageType::FATAL_ERROR,
  413. cmStrCat(
  414. "Target \"", lib, "\" of type ",
  415. cmState::GetTargetTypeName(tgt->GetType()),
  416. " may not be linked into another target. One may link only to "
  417. "INTERFACE, OBJECT, STATIC or SHARED libraries, or to ",
  418. "executables with the ENABLE_EXPORTS property set."));
  419. }
  420. target->AddLinkLibrary(mf, lib, libRef, llt);
  421. }
  422. if (warnRemoteInterface) {
  423. mf.IssueMessage(
  424. MessageType::AUTHOR_WARNING,
  425. cmStrCat(
  426. cmPolicies::GetPolicyWarning(cmPolicies::CMP0079), "\nTarget\n ",
  427. target->GetName(),
  428. "\nis not created in this "
  429. "directory. For compatibility with older versions of CMake, link "
  430. "library\n ",
  431. lib,
  432. "\nwill be looked up in the directory in which "
  433. "the target was created rather than in this calling directory."));
  434. }
  435. // Handle (additional) case where the command was called with PRIVATE /
  436. // LINK_PRIVATE and stop its processing. (The "INTERFACE_LINK_LIBRARIES"
  437. // property of the target on the LHS shall only be populated if it is a
  438. // STATIC library.)
  439. if (currentProcessingState == ProcessingKeywordPrivateInterface ||
  440. currentProcessingState == ProcessingPlainPrivateInterface) {
  441. if (target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  442. target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  443. std::string configLib =
  444. target->GetDebugGeneratorExpressions(libRef, llt);
  445. if (cmGeneratorExpression::IsValidTargetName(lib) ||
  446. cmGeneratorExpression::Find(lib) != std::string::npos) {
  447. configLib = "$<LINK_ONLY:" + configLib + ">";
  448. }
  449. target->AppendProperty("INTERFACE_LINK_LIBRARIES", configLib.c_str());
  450. }
  451. return true;
  452. }
  453. // Handle general case where the command was called with another keyword than
  454. // PRIVATE / LINK_PRIVATE or none at all. (The "INTERFACE_LINK_LIBRARIES"
  455. // property of the target on the LHS shall be populated.)
  456. target->AppendProperty(
  457. "INTERFACE_LINK_LIBRARIES",
  458. target->GetDebugGeneratorExpressions(libRef, llt).c_str());
  459. // Stop processing if called without any keyword.
  460. if (currentProcessingState == ProcessingLinkLibraries) {
  461. return true;
  462. }
  463. // Stop processing if policy CMP0022 is set to NEW.
  464. const cmPolicies::PolicyStatus policy22Status =
  465. target->GetPolicyStatusCMP0022();
  466. if (policy22Status != cmPolicies::OLD &&
  467. policy22Status != cmPolicies::WARN) {
  468. return true;
  469. }
  470. // Stop processing if called with an INTERFACE library on the LHS.
  471. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  472. return true;
  473. }
  474. // Handle (additional) backward-compatibility case where the command was
  475. // called with PUBLIC / INTERFACE / LINK_PUBLIC / LINK_INTERFACE_LIBRARIES.
  476. // (The policy CMP0022 is not set to NEW.)
  477. {
  478. // Get the list of configurations considered to be DEBUG.
  479. std::vector<std::string> debugConfigs =
  480. mf.GetCMakeInstance()->GetDebugConfigs();
  481. std::string prop;
  482. // Include this library in the link interface for the target.
  483. if (llt == DEBUG_LibraryType || llt == GENERAL_LibraryType) {
  484. // Put in the DEBUG configuration interfaces.
  485. for (std::string const& dc : debugConfigs) {
  486. prop = cmStrCat("LINK_INTERFACE_LIBRARIES_", dc);
  487. target->AppendProperty(prop, libRef.c_str());
  488. }
  489. }
  490. if (llt == OPTIMIZED_LibraryType || llt == GENERAL_LibraryType) {
  491. // Put in the non-DEBUG configuration interfaces.
  492. target->AppendProperty("LINK_INTERFACE_LIBRARIES", libRef.c_str());
  493. // Make sure the DEBUG configuration interfaces exist so that the
  494. // general one will not be used as a fall-back.
  495. for (std::string const& dc : debugConfigs) {
  496. prop = cmStrCat("LINK_INTERFACE_LIBRARIES_", dc);
  497. if (!target->GetProperty(prop)) {
  498. target->SetProperty(prop, "");
  499. }
  500. }
  501. }
  502. }
  503. return true;
  504. }