cmTargetLinkLibrariesCommand.cxx 16 KB

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