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, "The INTERFACE option must appear as the second "
  151. "argument, just after the target name.");
  152. return true;
  153. }
  154. this->CurrentProcessingState = ProcessingKeywordLinkInterface;
  155. } else if (args[i] == "LINK_PUBLIC") {
  156. if (i != 1 &&
  157. this->CurrentProcessingState != ProcessingPlainPrivateInterface &&
  158. this->CurrentProcessingState != ProcessingPlainPublicInterface) {
  159. this->Makefile->IssueMessage(
  160. cmake::FATAL_ERROR,
  161. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  162. "argument, just after the target name.");
  163. return true;
  164. }
  165. this->CurrentProcessingState = ProcessingPlainPublicInterface;
  166. } else if (args[i] == "PUBLIC") {
  167. if (i != 1 &&
  168. this->CurrentProcessingState != ProcessingKeywordPrivateInterface &&
  169. this->CurrentProcessingState != ProcessingKeywordPublicInterface &&
  170. this->CurrentProcessingState != ProcessingKeywordLinkInterface) {
  171. this->Makefile->IssueMessage(
  172. cmake::FATAL_ERROR,
  173. "The PUBLIC or PRIVATE option must appear as the second "
  174. "argument, just after the target name.");
  175. return true;
  176. }
  177. this->CurrentProcessingState = ProcessingKeywordPublicInterface;
  178. } else if (args[i] == "LINK_PRIVATE") {
  179. if (i != 1 &&
  180. this->CurrentProcessingState != ProcessingPlainPublicInterface &&
  181. this->CurrentProcessingState != ProcessingPlainPrivateInterface) {
  182. this->Makefile->IssueMessage(
  183. cmake::FATAL_ERROR,
  184. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  185. "argument, just after the target name.");
  186. return true;
  187. }
  188. this->CurrentProcessingState = ProcessingPlainPrivateInterface;
  189. } else if (args[i] == "PRIVATE") {
  190. if (i != 1 &&
  191. this->CurrentProcessingState != ProcessingKeywordPrivateInterface &&
  192. this->CurrentProcessingState != ProcessingKeywordPublicInterface &&
  193. this->CurrentProcessingState != ProcessingKeywordLinkInterface) {
  194. this->Makefile->IssueMessage(
  195. cmake::FATAL_ERROR,
  196. "The PUBLIC or PRIVATE option must appear as the second "
  197. "argument, just after the target name.");
  198. return true;
  199. }
  200. this->CurrentProcessingState = ProcessingKeywordPrivateInterface;
  201. } else if (args[i] == "debug") {
  202. if (haveLLT) {
  203. this->LinkLibraryTypeSpecifierWarning(llt, DEBUG_LibraryType);
  204. }
  205. llt = DEBUG_LibraryType;
  206. haveLLT = true;
  207. } else if (args[i] == "optimized") {
  208. if (haveLLT) {
  209. this->LinkLibraryTypeSpecifierWarning(llt, OPTIMIZED_LibraryType);
  210. }
  211. llt = OPTIMIZED_LibraryType;
  212. haveLLT = true;
  213. } else if (args[i] == "general") {
  214. if (haveLLT) {
  215. this->LinkLibraryTypeSpecifierWarning(llt, GENERAL_LibraryType);
  216. }
  217. llt = GENERAL_LibraryType;
  218. haveLLT = true;
  219. } else if (haveLLT) {
  220. // The link type was specified by the previous argument.
  221. haveLLT = false;
  222. if (!this->HandleLibrary(args[i], llt)) {
  223. return false;
  224. }
  225. } else {
  226. // Lookup old-style cache entry if type is unspecified. So if you
  227. // do a target_link_libraries(foo optimized bar) it will stay optimized
  228. // and not use the lookup. As there may be the case where someone has
  229. // specifed that a library is both debug and optimized. (this check is
  230. // only there for backwards compatibility when mixing projects built
  231. // with old versions of CMake and new)
  232. llt = GENERAL_LibraryType;
  233. std::string linkType = args[0];
  234. linkType += "_LINK_TYPE";
  235. const char* linkTypeString = this->Makefile->GetDefinition(linkType);
  236. if (linkTypeString) {
  237. if (strcmp(linkTypeString, "debug") == 0) {
  238. llt = DEBUG_LibraryType;
  239. }
  240. if (strcmp(linkTypeString, "optimized") == 0) {
  241. llt = OPTIMIZED_LibraryType;
  242. }
  243. }
  244. if (!this->HandleLibrary(args[i], llt)) {
  245. return false;
  246. }
  247. }
  248. }
  249. // Make sure the last argument was not a library type specifier.
  250. if (haveLLT) {
  251. std::ostringstream e;
  252. e << "The \"" << this->LinkLibraryTypeNames[llt]
  253. << "\" argument must be followed by a library.";
  254. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  255. cmSystemTools::SetFatalErrorOccured();
  256. }
  257. const cmPolicies::PolicyStatus policy22Status =
  258. this->Target->GetPolicyStatusCMP0022();
  259. // If any of the LINK_ options were given, make sure the
  260. // LINK_INTERFACE_LIBRARIES target property exists.
  261. // Use of any of the new keywords implies awareness of
  262. // this property. And if no libraries are named, it should
  263. // result in an empty link interface.
  264. if ((policy22Status == cmPolicies::OLD ||
  265. policy22Status == cmPolicies::WARN) &&
  266. this->CurrentProcessingState != ProcessingLinkLibraries &&
  267. !this->Target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  268. this->Target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  269. }
  270. return true;
  271. }
  272. void cmTargetLinkLibrariesCommand::LinkLibraryTypeSpecifierWarning(int left,
  273. int right)
  274. {
  275. std::ostringstream w;
  276. w << "Link library type specifier \"" << this->LinkLibraryTypeNames[left]
  277. << "\" is followed by specifier \"" << this->LinkLibraryTypeNames[right]
  278. << "\" instead of a library name. "
  279. << "The first specifier will be ignored.";
  280. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  281. }
  282. bool cmTargetLinkLibrariesCommand::HandleLibrary(const std::string& lib,
  283. cmTargetLinkLibraryType llt)
  284. {
  285. if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
  286. this->CurrentProcessingState != ProcessingKeywordLinkInterface) {
  287. this->Makefile->IssueMessage(
  288. cmake::FATAL_ERROR,
  289. "INTERFACE library can only be used with the INTERFACE keyword of "
  290. "target_link_libraries");
  291. return false;
  292. }
  293. if (this->Target->IsImported() &&
  294. this->CurrentProcessingState != ProcessingKeywordLinkInterface) {
  295. this->Makefile->IssueMessage(
  296. cmake::FATAL_ERROR,
  297. "IMPORTED library can only be used with the INTERFACE keyword of "
  298. "target_link_libraries");
  299. return false;
  300. }
  301. cmTarget::TLLSignature sig =
  302. (this->CurrentProcessingState == ProcessingPlainPrivateInterface ||
  303. this->CurrentProcessingState == ProcessingPlainPublicInterface ||
  304. this->CurrentProcessingState == ProcessingKeywordPrivateInterface ||
  305. this->CurrentProcessingState == ProcessingKeywordPublicInterface ||
  306. this->CurrentProcessingState == ProcessingKeywordLinkInterface)
  307. ? cmTarget::KeywordTLLSignature
  308. : cmTarget::PlainTLLSignature;
  309. if (!this->Target->PushTLLCommandTrace(
  310. sig, this->Makefile->GetExecutionContext())) {
  311. std::ostringstream e;
  312. const char* modal = nullptr;
  313. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  314. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0023)) {
  315. case cmPolicies::WARN:
  316. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0023) << "\n";
  317. modal = "should";
  318. case cmPolicies::OLD:
  319. break;
  320. case cmPolicies::REQUIRED_ALWAYS:
  321. case cmPolicies::REQUIRED_IF_USED:
  322. case cmPolicies::NEW:
  323. modal = "must";
  324. messageType = cmake::FATAL_ERROR;
  325. }
  326. if (modal) {
  327. // If the sig is a keyword form and there is a conflict, the existing
  328. // form must be the plain form.
  329. const char* existingSig =
  330. (sig == cmTarget::KeywordTLLSignature ? "plain" : "keyword");
  331. e << "The " << existingSig << " signature for target_link_libraries "
  332. "has already been used with the target \""
  333. << this->Target->GetName() << "\". All uses of "
  334. "target_link_libraries with a target "
  335. << modal << " be either "
  336. "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. } else {
  369. cmTarget* tgt = this->Makefile->GetGlobalGenerator()->FindTarget(lib);
  370. if (tgt && (tgt->GetType() != cmStateEnums::STATIC_LIBRARY) &&
  371. (tgt->GetType() != cmStateEnums::SHARED_LIBRARY) &&
  372. (tgt->GetType() != cmStateEnums::UNKNOWN_LIBRARY) &&
  373. (tgt->GetType() != cmStateEnums::INTERFACE_LIBRARY) &&
  374. !tgt->IsExecutableWithExports()) {
  375. std::ostringstream e;
  376. e << "Target \"" << lib << "\" of type "
  377. << cmState::GetTargetTypeName(tgt->GetType())
  378. << " may not be linked into another target. "
  379. << "One may link only to STATIC or SHARED libraries, or "
  380. << "to executables with the ENABLE_EXPORTS property set.";
  381. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  382. }
  383. this->Target->AddLinkLibrary(*this->Makefile, lib, llt);
  384. }
  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. }