cmTargetLinkLibrariesCommand.cxx 16 KB

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