cmTargetLinkLibrariesCommand.cxx 9.8 KB

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