cmTargetLinkLibrariesCommand.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. // but we might not have any libs after variable expansion
  81. if(args.size() < 2)
  82. {
  83. return true;
  84. }
  85. // Keep track of link configuration specifiers.
  86. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  87. bool haveLLT = false;
  88. // Start with primary linking and switch to link interface
  89. // specification when the keyword is encountered.
  90. this->DoingInterface = false;
  91. // add libraries, nothe that there is an optional prefix
  92. // of debug and optimized than can be used
  93. for(unsigned int i=1; i < args.size(); ++i)
  94. {
  95. if(args[i] == "LINK_INTERFACE_LIBRARIES")
  96. {
  97. this->DoingInterface = true;
  98. if(i != 1)
  99. {
  100. this->Makefile->IssueMessage(
  101. cmake::FATAL_ERROR,
  102. "The LINK_INTERFACE_LIBRARIES option must appear as the second "
  103. "argument, just after the target name."
  104. );
  105. return true;
  106. }
  107. }
  108. else if(args[i] == "debug")
  109. {
  110. if(haveLLT)
  111. {
  112. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::DEBUG);
  113. }
  114. llt = cmTarget::DEBUG;
  115. haveLLT = true;
  116. }
  117. else if(args[i] == "optimized")
  118. {
  119. if(haveLLT)
  120. {
  121. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::OPTIMIZED);
  122. }
  123. llt = cmTarget::OPTIMIZED;
  124. haveLLT = true;
  125. }
  126. else if(args[i] == "general")
  127. {
  128. if(haveLLT)
  129. {
  130. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::GENERAL);
  131. }
  132. llt = cmTarget::GENERAL;
  133. haveLLT = true;
  134. }
  135. else if(haveLLT)
  136. {
  137. // The link type was specified by the previous argument.
  138. haveLLT = false;
  139. this->HandleLibrary(args[i].c_str(), llt);
  140. }
  141. else
  142. {
  143. // Lookup old-style cache entry if type is unspecified. So if you
  144. // do a target_link_libraries(foo optimized bar) it will stay optimized
  145. // and not use the lookup. As there maybe the case where someone has
  146. // specifed that a library is both debug and optimized. (this check is
  147. // only there for backwards compatibility when mixing projects built
  148. // with old versions of CMake and new)
  149. llt = cmTarget::GENERAL;
  150. std::string linkType = args[0];
  151. linkType += "_LINK_TYPE";
  152. const char* linkTypeString =
  153. this->Makefile->GetDefinition( linkType.c_str() );
  154. if(linkTypeString)
  155. {
  156. if(strcmp(linkTypeString, "debug") == 0)
  157. {
  158. llt = cmTarget::DEBUG;
  159. }
  160. if(strcmp(linkTypeString, "optimized") == 0)
  161. {
  162. llt = cmTarget::OPTIMIZED;
  163. }
  164. }
  165. this->HandleLibrary(args[i].c_str(), llt);
  166. }
  167. }
  168. // Make sure the last argument was not a library type specifier.
  169. if(haveLLT)
  170. {
  171. cmOStringStream e;
  172. e << "The \"" << this->LinkLibraryTypeNames[llt]
  173. << "\" argument must be followed by a library.";
  174. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  175. cmSystemTools::SetFatalErrorOccured();
  176. }
  177. // If the INTERFACE option was given, make sure the
  178. // LINK_INTERFACE_LIBRARIES property exists. This allows the
  179. // command to be used to specify an empty link interface.
  180. if(this->DoingInterface &&
  181. !this->Target->GetProperty("LINK_INTERFACE_LIBRARIES"))
  182. {
  183. this->Target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  184. }
  185. return true;
  186. }
  187. //----------------------------------------------------------------------------
  188. void
  189. cmTargetLinkLibrariesCommand
  190. ::LinkLibraryTypeSpecifierWarning(int left, int right)
  191. {
  192. cmOStringStream w;
  193. w << "Link library type specifier \""
  194. << this->LinkLibraryTypeNames[left] << "\" is followed by specifier \""
  195. << this->LinkLibraryTypeNames[right] << "\" instead of a library name. "
  196. << "The first specifier will be ignored.";
  197. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  198. }
  199. //----------------------------------------------------------------------------
  200. void
  201. cmTargetLinkLibrariesCommand::HandleLibrary(const char* lib,
  202. cmTarget::LinkLibraryType llt)
  203. {
  204. // Handle normal case first.
  205. if(!this->DoingInterface)
  206. {
  207. this->Makefile
  208. ->AddLinkLibraryForTarget(this->Target->GetName(), lib, llt);
  209. return;
  210. }
  211. // Get the list of configurations considered to be DEBUG.
  212. std::vector<std::string> const& debugConfigs =
  213. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  214. std::string prop;
  215. // Include this library in the link interface for the target.
  216. if(llt == cmTarget::DEBUG || llt == cmTarget::GENERAL)
  217. {
  218. // Put in the DEBUG configuration interfaces.
  219. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  220. i != debugConfigs.end(); ++i)
  221. {
  222. prop = "LINK_INTERFACE_LIBRARIES_";
  223. prop += *i;
  224. this->Target->AppendProperty(prop.c_str(), lib);
  225. }
  226. }
  227. if(llt == cmTarget::OPTIMIZED || llt == cmTarget::GENERAL)
  228. {
  229. // Put in the non-DEBUG configuration interfaces.
  230. this->Target->AppendProperty("LINK_INTERFACE_LIBRARIES", lib);
  231. // Make sure the DEBUG configuration interfaces exist so that the
  232. // general one will not be used as a fall-back.
  233. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  234. i != debugConfigs.end(); ++i)
  235. {
  236. prop = "LINK_INTERFACE_LIBRARIES_";
  237. prop += *i;
  238. if(!this->Target->GetProperty(prop.c_str()))
  239. {
  240. this->Target->SetProperty(prop.c_str(), "");
  241. }
  242. }
  243. }
  244. }