cmTargetLinkLibrariesCommand.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. {
  144. this->Makefile->IssueMessage(
  145. cmake::FATAL_ERROR,
  146. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  147. "argument, just after the target name."
  148. );
  149. return true;
  150. }
  151. this->CurrentProcessingState = ProcessingPlainPublicInterface;
  152. }
  153. else if(args[i] == "PUBLIC")
  154. {
  155. if(i != 1
  156. && this->CurrentProcessingState != ProcessingKeywordPrivateInterface
  157. && this->CurrentProcessingState != ProcessingKeywordPublicInterface
  158. && this->CurrentProcessingState != ProcessingKeywordLinkInterface)
  159. {
  160. this->Makefile->IssueMessage(
  161. cmake::FATAL_ERROR,
  162. "The PUBLIC or PRIVATE option must appear as the second "
  163. "argument, just after the target name."
  164. );
  165. return true;
  166. }
  167. this->CurrentProcessingState = ProcessingKeywordPublicInterface;
  168. }
  169. else if(args[i] == "LINK_PRIVATE")
  170. {
  171. if(i != 1
  172. && this->CurrentProcessingState != ProcessingPlainPublicInterface)
  173. {
  174. this->Makefile->IssueMessage(
  175. cmake::FATAL_ERROR,
  176. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  177. "argument, just after the target name."
  178. );
  179. return true;
  180. }
  181. this->CurrentProcessingState = ProcessingPlainPrivateInterface;
  182. }
  183. else if(args[i] == "PRIVATE")
  184. {
  185. if(i != 1
  186. && this->CurrentProcessingState != ProcessingKeywordPrivateInterface
  187. && this->CurrentProcessingState != ProcessingKeywordPublicInterface
  188. && this->CurrentProcessingState != ProcessingKeywordLinkInterface)
  189. {
  190. this->Makefile->IssueMessage(
  191. cmake::FATAL_ERROR,
  192. "The PUBLIC or PRIVATE option must appear as the second "
  193. "argument, just after the target name."
  194. );
  195. return true;
  196. }
  197. this->CurrentProcessingState = ProcessingKeywordPrivateInterface;
  198. }
  199. else if(args[i] == "debug")
  200. {
  201. if(haveLLT)
  202. {
  203. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::DEBUG);
  204. }
  205. llt = cmTarget::DEBUG;
  206. haveLLT = true;
  207. }
  208. else if(args[i] == "optimized")
  209. {
  210. if(haveLLT)
  211. {
  212. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::OPTIMIZED);
  213. }
  214. llt = cmTarget::OPTIMIZED;
  215. haveLLT = true;
  216. }
  217. else if(args[i] == "general")
  218. {
  219. if(haveLLT)
  220. {
  221. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::GENERAL);
  222. }
  223. llt = cmTarget::GENERAL;
  224. haveLLT = true;
  225. }
  226. else if(haveLLT)
  227. {
  228. // The link type was specified by the previous argument.
  229. haveLLT = false;
  230. if (!this->HandleLibrary(args[i].c_str(), llt))
  231. {
  232. return false;
  233. }
  234. }
  235. else
  236. {
  237. // Lookup old-style cache entry if type is unspecified. So if you
  238. // do a target_link_libraries(foo optimized bar) it will stay optimized
  239. // and not use the lookup. As there maybe the case where someone has
  240. // specifed that a library is both debug and optimized. (this check is
  241. // only there for backwards compatibility when mixing projects built
  242. // with old versions of CMake and new)
  243. llt = cmTarget::GENERAL;
  244. std::string linkType = args[0];
  245. linkType += "_LINK_TYPE";
  246. const char* linkTypeString =
  247. this->Makefile->GetDefinition( linkType.c_str() );
  248. if(linkTypeString)
  249. {
  250. if(strcmp(linkTypeString, "debug") == 0)
  251. {
  252. llt = cmTarget::DEBUG;
  253. }
  254. if(strcmp(linkTypeString, "optimized") == 0)
  255. {
  256. llt = cmTarget::OPTIMIZED;
  257. }
  258. }
  259. if (!this->HandleLibrary(args[i].c_str(), llt))
  260. {
  261. return false;
  262. }
  263. }
  264. }
  265. // Make sure the last argument was not a library type specifier.
  266. if(haveLLT)
  267. {
  268. cmOStringStream e;
  269. e << "The \"" << this->LinkLibraryTypeNames[llt]
  270. << "\" argument must be followed by a library.";
  271. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  272. cmSystemTools::SetFatalErrorOccured();
  273. }
  274. const cmPolicies::PolicyStatus policy22Status
  275. = this->Target->GetPolicyStatusCMP0022();
  276. // If any of the LINK_ options were given, make sure the
  277. // LINK_INTERFACE_LIBRARIES target property exists.
  278. // Use of any of the new keywords implies awareness of
  279. // this property. And if no libraries are named, it should
  280. // result in an empty link interface.
  281. if((policy22Status == cmPolicies::OLD ||
  282. policy22Status == cmPolicies::WARN) &&
  283. this->CurrentProcessingState != ProcessingLinkLibraries &&
  284. !this->Target->GetProperty("LINK_INTERFACE_LIBRARIES"))
  285. {
  286. this->Target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  287. }
  288. return true;
  289. }
  290. //----------------------------------------------------------------------------
  291. void
  292. cmTargetLinkLibrariesCommand
  293. ::LinkLibraryTypeSpecifierWarning(int left, int right)
  294. {
  295. cmOStringStream w;
  296. w << "Link library type specifier \""
  297. << this->LinkLibraryTypeNames[left] << "\" is followed by specifier \""
  298. << this->LinkLibraryTypeNames[right] << "\" instead of a library name. "
  299. << "The first specifier will be ignored.";
  300. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  301. }
  302. //----------------------------------------------------------------------------
  303. bool
  304. cmTargetLinkLibrariesCommand::HandleLibrary(const char* lib,
  305. cmTarget::LinkLibraryType llt)
  306. {
  307. cmTarget::TLLSignature sig =
  308. (this->CurrentProcessingState == ProcessingPlainPrivateInterface
  309. || this->CurrentProcessingState == ProcessingPlainPublicInterface
  310. || this->CurrentProcessingState == ProcessingKeywordPrivateInterface
  311. || this->CurrentProcessingState == ProcessingKeywordPublicInterface
  312. || this->CurrentProcessingState == ProcessingKeywordLinkInterface)
  313. ? cmTarget::KeywordTLLSignature : cmTarget::PlainTLLSignature;
  314. if (!this->Target->PushTLLCommandTrace(sig))
  315. {
  316. const char *modal = 0;
  317. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  318. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0023))
  319. {
  320. case cmPolicies::WARN:
  321. modal = "should";
  322. case cmPolicies::OLD:
  323. break;
  324. case cmPolicies::REQUIRED_ALWAYS:
  325. case cmPolicies::REQUIRED_IF_USED:
  326. case cmPolicies::NEW:
  327. modal = "must";
  328. messageType = cmake::FATAL_ERROR;
  329. }
  330. if(modal)
  331. {
  332. cmOStringStream e;
  333. // If the sig is a keyword form and there is a conflict, the existing
  334. // form must be the plain form.
  335. const char *existingSig
  336. = (sig == cmTarget::KeywordTLLSignature ? "plain"
  337. : "keyword");
  338. e << this->Makefile->GetPolicies()
  339. ->GetPolicyWarning(cmPolicies::CMP0023) << "\n"
  340. "The " << existingSig << " signature for target_link_libraries "
  341. "has already been used with the target \""
  342. << this->Target->GetName() << "\". All uses of "
  343. "target_link_libraries with a target " << modal << " be either "
  344. "all-keyword or all-plain.\n";
  345. this->Target->GetTllSignatureTraces(e,
  346. sig == cmTarget::KeywordTLLSignature
  347. ? cmTarget::PlainTLLSignature
  348. : cmTarget::KeywordTLLSignature);
  349. this->Makefile->IssueMessage(messageType, e.str().c_str());
  350. if(messageType == cmake::FATAL_ERROR)
  351. {
  352. return false;
  353. }
  354. }
  355. }
  356. // Handle normal case first.
  357. if(this->CurrentProcessingState != ProcessingKeywordLinkInterface
  358. && this->CurrentProcessingState != ProcessingPlainLinkInterface)
  359. {
  360. this->Makefile
  361. ->AddLinkLibraryForTarget(this->Target->GetName(), lib, llt);
  362. if(this->CurrentProcessingState == ProcessingLinkLibraries)
  363. {
  364. this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
  365. this->Target->GetDebugGeneratorExpressions(lib, llt).c_str());
  366. return true;
  367. }
  368. else if(this->CurrentProcessingState != ProcessingKeywordPublicInterface
  369. && this->CurrentProcessingState != ProcessingPlainPublicInterface)
  370. {
  371. if (this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  372. {
  373. std::string configLib = this->Target
  374. ->GetDebugGeneratorExpressions(lib, llt);
  375. if (cmGeneratorExpression::IsValidTargetName(lib)
  376. || cmGeneratorExpression::Find(lib) != std::string::npos)
  377. {
  378. configLib = "$<LINK_ONLY:" + configLib + ">";
  379. }
  380. this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
  381. configLib.c_str());
  382. }
  383. // Not a 'public' or 'interface' library. Do not add to interface
  384. // property.
  385. return true;
  386. }
  387. }
  388. this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
  389. this->Target->GetDebugGeneratorExpressions(lib, llt).c_str());
  390. const cmPolicies::PolicyStatus policy22Status
  391. = this->Target->GetPolicyStatusCMP0022();
  392. if (policy22Status != cmPolicies::OLD
  393. && policy22Status != cmPolicies::WARN)
  394. {
  395. return true;
  396. }
  397. // Get the list of configurations considered to be DEBUG.
  398. std::vector<std::string> const& debugConfigs =
  399. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  400. std::string prop;
  401. // Include this library in the link interface for the target.
  402. if(llt == cmTarget::DEBUG || llt == cmTarget::GENERAL)
  403. {
  404. // Put in the DEBUG configuration interfaces.
  405. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  406. i != debugConfigs.end(); ++i)
  407. {
  408. prop = "LINK_INTERFACE_LIBRARIES_";
  409. prop += *i;
  410. this->Target->AppendProperty(prop.c_str(), lib);
  411. }
  412. }
  413. if(llt == cmTarget::OPTIMIZED || llt == cmTarget::GENERAL)
  414. {
  415. // Put in the non-DEBUG configuration interfaces.
  416. this->Target->AppendProperty("LINK_INTERFACE_LIBRARIES", lib);
  417. // Make sure the DEBUG configuration interfaces exist so that the
  418. // general one will not be used as a fall-back.
  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. if(!this->Target->GetProperty(prop.c_str()))
  425. {
  426. this->Target->SetProperty(prop.c_str(), "");
  427. }
  428. }
  429. }
  430. return true;
  431. }