cmTargetLinkLibrariesCommand.cxx 19 KB

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