1
0

cmTargetLinkLibrariesCommand.cxx 18 KB

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