cmTargetLinkLibrariesCommand.cxx 19 KB

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