cmTargetLinkLibrariesCommand.cxx 17 KB

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