cmTargetLinkLibrariesCommand.cxx 17 KB

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