cmTargetLinkLibrariesCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. // Lookup the target for which libraries are specified.
  29. this->Target =
  30. this->Makefile->GetCMakeInstance()
  31. ->GetGlobalGenerator()->FindTarget(0, args[0].c_str());
  32. if(!this->Target)
  33. {
  34. cmake::MessageType t = cmake::FATAL_ERROR; // fail by default
  35. cmOStringStream e;
  36. e << "Cannot specify link libraries for target \"" << args[0] << "\" "
  37. << "which is not built by this project.";
  38. // The bad target is the only argument. Check how policy CMP0016 is set,
  39. // and accept, warn or fail respectively:
  40. if (args.size() < 2)
  41. {
  42. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0016))
  43. {
  44. case cmPolicies::WARN:
  45. t = cmake::AUTHOR_WARNING;
  46. // Print the warning.
  47. e << "\n"
  48. << "CMake does not support this but it used to work accidentally "
  49. << "and is being allowed for compatibility."
  50. << "\n" << this->Makefile->GetPolicies()->
  51. GetPolicyWarning(cmPolicies::CMP0016);
  52. break;
  53. case cmPolicies::OLD: // OLD behavior does not warn.
  54. t = cmake::MESSAGE;
  55. break;
  56. case cmPolicies::REQUIRED_IF_USED:
  57. case cmPolicies::REQUIRED_ALWAYS:
  58. e << "\n" << this->Makefile->GetPolicies()->
  59. GetRequiredPolicyError(cmPolicies::CMP0016);
  60. break;
  61. case cmPolicies::NEW: // NEW behavior prints the error.
  62. default:
  63. break;
  64. }
  65. }
  66. // now actually print the message
  67. switch(t)
  68. {
  69. case cmake::AUTHOR_WARNING:
  70. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  71. break;
  72. case cmake::FATAL_ERROR:
  73. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  74. cmSystemTools::SetFatalErrorOccured();
  75. break;
  76. default:
  77. break;
  78. }
  79. return true;
  80. }
  81. if(this->Target->GetType() == cmTarget::OBJECT_LIBRARY)
  82. {
  83. cmOStringStream e;
  84. e << "Object library target \"" << args[0] << "\" "
  85. << "may not link to anything.";
  86. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  87. cmSystemTools::SetFatalErrorOccured();
  88. return true;
  89. }
  90. // but we might not have any libs after variable expansion
  91. if(args.size() < 2)
  92. {
  93. return true;
  94. }
  95. // Keep track of link configuration specifiers.
  96. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  97. bool haveLLT = false;
  98. // Start with primary linking and switch to link interface
  99. // specification if the keyword is encountered as the first argument.
  100. this->CurrentProcessingState = ProcessingLinkLibraries;
  101. // add libraries, note that there is an optional prefix
  102. // of debug and optimized that can be used
  103. for(unsigned int i=1; i < args.size(); ++i)
  104. {
  105. if(args[i] == "LINK_INTERFACE_LIBRARIES")
  106. {
  107. this->CurrentProcessingState = ProcessingLinkInterface;
  108. if(i != 1)
  109. {
  110. this->Makefile->IssueMessage(
  111. cmake::FATAL_ERROR,
  112. "The LINK_INTERFACE_LIBRARIES option must appear as the second "
  113. "argument, just after the target name."
  114. );
  115. return true;
  116. }
  117. }
  118. else if(args[i] == "LINK_PUBLIC")
  119. {
  120. if(i != 1 && this->CurrentProcessingState != ProcessingPrivateInterface)
  121. {
  122. this->Makefile->IssueMessage(
  123. cmake::FATAL_ERROR,
  124. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  125. "argument, just after the target name."
  126. );
  127. return true;
  128. }
  129. this->CurrentProcessingState = ProcessingPublicInterface;
  130. }
  131. else if(args[i] == "LINK_PRIVATE")
  132. {
  133. if(i != 1 && this->CurrentProcessingState != ProcessingPublicInterface)
  134. {
  135. this->Makefile->IssueMessage(
  136. cmake::FATAL_ERROR,
  137. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  138. "argument, just after the target name."
  139. );
  140. return true;
  141. }
  142. this->CurrentProcessingState = ProcessingPrivateInterface;
  143. }
  144. else if(args[i] == "debug")
  145. {
  146. if(haveLLT)
  147. {
  148. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::DEBUG);
  149. }
  150. llt = cmTarget::DEBUG;
  151. haveLLT = true;
  152. }
  153. else if(args[i] == "optimized")
  154. {
  155. if(haveLLT)
  156. {
  157. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::OPTIMIZED);
  158. }
  159. llt = cmTarget::OPTIMIZED;
  160. haveLLT = true;
  161. }
  162. else if(args[i] == "general")
  163. {
  164. if(haveLLT)
  165. {
  166. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::GENERAL);
  167. }
  168. llt = cmTarget::GENERAL;
  169. haveLLT = true;
  170. }
  171. else if(haveLLT)
  172. {
  173. // The link type was specified by the previous argument.
  174. haveLLT = false;
  175. this->HandleLibrary(args[i].c_str(), llt);
  176. }
  177. else
  178. {
  179. // Lookup old-style cache entry if type is unspecified. So if you
  180. // do a target_link_libraries(foo optimized bar) it will stay optimized
  181. // and not use the lookup. As there maybe the case where someone has
  182. // specifed that a library is both debug and optimized. (this check is
  183. // only there for backwards compatibility when mixing projects built
  184. // with old versions of CMake and new)
  185. llt = cmTarget::GENERAL;
  186. std::string linkType = args[0];
  187. linkType += "_LINK_TYPE";
  188. const char* linkTypeString =
  189. this->Makefile->GetDefinition( linkType.c_str() );
  190. if(linkTypeString)
  191. {
  192. if(strcmp(linkTypeString, "debug") == 0)
  193. {
  194. llt = cmTarget::DEBUG;
  195. }
  196. if(strcmp(linkTypeString, "optimized") == 0)
  197. {
  198. llt = cmTarget::OPTIMIZED;
  199. }
  200. }
  201. this->HandleLibrary(args[i].c_str(), llt);
  202. }
  203. }
  204. // Make sure the last argument was not a library type specifier.
  205. if(haveLLT)
  206. {
  207. cmOStringStream e;
  208. e << "The \"" << this->LinkLibraryTypeNames[llt]
  209. << "\" argument must be followed by a library.";
  210. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  211. cmSystemTools::SetFatalErrorOccured();
  212. }
  213. // If any of the LINK_ options were given, make sure the
  214. // LINK_INTERFACE_LIBRARIES target property exists.
  215. // Use of any of the new keywords implies awareness of
  216. // this property. And if no libraries are named, it should
  217. // result in an empty link interface.
  218. if(this->CurrentProcessingState != ProcessingLinkLibraries &&
  219. !this->Target->GetProperty("LINK_INTERFACE_LIBRARIES"))
  220. {
  221. this->Target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  222. }
  223. return true;
  224. }
  225. //----------------------------------------------------------------------------
  226. void
  227. cmTargetLinkLibrariesCommand
  228. ::LinkLibraryTypeSpecifierWarning(int left, int right)
  229. {
  230. cmOStringStream w;
  231. w << "Link library type specifier \""
  232. << this->LinkLibraryTypeNames[left] << "\" is followed by specifier \""
  233. << this->LinkLibraryTypeNames[right] << "\" instead of a library name. "
  234. << "The first specifier will be ignored.";
  235. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  236. }
  237. //----------------------------------------------------------------------------
  238. static std::string compileProperty(cmTarget *tgt, const std::string &lib,
  239. bool isGenex,
  240. const std::string &property,
  241. cmTarget::LinkLibraryType llt)
  242. {
  243. std::string value = !isGenex ? "$<LINKED:" + lib + ">"
  244. : "$<$<TARGET_DEFINED:" + lib + ">:" +
  245. "$<TARGET_PROPERTY:" + lib +
  246. ",INTERFACE_" + property + ">"
  247. ">";
  248. return tgt->GetDebugGeneratorExpressions(value, llt);
  249. }
  250. //----------------------------------------------------------------------------
  251. void
  252. cmTargetLinkLibrariesCommand::HandleLibrary(const char* lib,
  253. cmTarget::LinkLibraryType llt)
  254. {
  255. const bool isGenex = cmGeneratorExpression::Find(lib) != std::string::npos;
  256. const bool potentialTargetName
  257. = cmGeneratorExpression::IsValidTargetName(lib);
  258. if (potentialTargetName || isGenex)
  259. {
  260. this->Target->AppendProperty("INCLUDE_DIRECTORIES",
  261. compileProperty(this->Target, lib,
  262. isGenex,
  263. "INCLUDE_DIRECTORIES", llt).c_str());
  264. this->Target->AppendProperty("COMPILE_DEFINITIONS",
  265. compileProperty(this->Target, lib,
  266. isGenex,
  267. "COMPILE_DEFINITIONS", llt).c_str());
  268. }
  269. // Handle normal case first.
  270. if(this->CurrentProcessingState != ProcessingLinkInterface)
  271. {
  272. this->Makefile
  273. ->AddLinkLibraryForTarget(this->Target->GetName(), lib, llt);
  274. if (this->CurrentProcessingState != ProcessingPublicInterface)
  275. {
  276. // Not LINK_INTERFACE_LIBRARIES or LINK_PUBLIC, do not add to interface.
  277. return;
  278. }
  279. }
  280. if (potentialTargetName || isGenex)
  281. {
  282. this->Target->AppendProperty("INTERFACE_COMPILE_DEFINITIONS",
  283. compileProperty(this->Target, lib,
  284. isGenex,
  285. "COMPILE_DEFINITIONS", llt).c_str());
  286. this->Target->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
  287. compileProperty(this->Target, lib,
  288. isGenex,
  289. "INCLUDE_DIRECTORIES", llt).c_str());
  290. }
  291. // Get the list of configurations considered to be DEBUG.
  292. std::vector<std::string> const& debugConfigs =
  293. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  294. std::string prop;
  295. // Include this library in the link interface for the target.
  296. if(llt == cmTarget::DEBUG || llt == cmTarget::GENERAL)
  297. {
  298. // Put in the DEBUG configuration interfaces.
  299. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  300. i != debugConfigs.end(); ++i)
  301. {
  302. prop = "LINK_INTERFACE_LIBRARIES_";
  303. prop += *i;
  304. this->Target->AppendProperty(prop.c_str(), lib);
  305. }
  306. }
  307. if(llt == cmTarget::OPTIMIZED || llt == cmTarget::GENERAL)
  308. {
  309. // Put in the non-DEBUG configuration interfaces.
  310. this->Target->AppendProperty("LINK_INTERFACE_LIBRARIES", lib);
  311. // Make sure the DEBUG configuration interfaces exist so that the
  312. // general one will not be used as a fall-back.
  313. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  314. i != debugConfigs.end(); ++i)
  315. {
  316. prop = "LINK_INTERFACE_LIBRARIES_";
  317. prop += *i;
  318. if(!this->Target->GetProperty(prop.c_str()))
  319. {
  320. this->Target->SetProperty(prop.c_str(), "");
  321. }
  322. }
  323. }
  324. }