cmTargetLinkLibrariesCommand.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. cmOStringStream e;
  34. e << "Cannot specify link libraries for target \"" << args[0] << "\" "
  35. << "which is not built by this project.";
  36. // The bad target is the only argument, just warn, don't fail, because
  37. // there is probably some code out there which would stop building
  38. // otherwise:
  39. if (args.size() < 2)
  40. {
  41. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  42. }
  43. else
  44. {
  45. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  46. cmSystemTools::SetFatalErrorOccured();
  47. }
  48. return true;
  49. }
  50. // but we might not have any libs after variable expansion
  51. if(args.size() < 2)
  52. {
  53. return true;
  54. }
  55. // Keep track of link configuration specifiers.
  56. cmTarget::LinkLibraryType llt = cmTarget::GENERAL;
  57. bool haveLLT = false;
  58. // Start with primary linking and switch to link interface
  59. // specification when the keyword is encountered.
  60. this->DoingInterface = false;
  61. // add libraries, nothe that there is an optional prefix
  62. // of debug and optimized than can be used
  63. for(unsigned int i=1; i < args.size(); ++i)
  64. {
  65. if(args[i] == "LINK_INTERFACE_LIBRARIES")
  66. {
  67. this->DoingInterface = true;
  68. if(i != 1)
  69. {
  70. this->Makefile->IssueMessage(
  71. cmake::FATAL_ERROR,
  72. "The LINK_INTERFACE_LIBRARIES option must appear as the second "
  73. "argument, just after the target name."
  74. );
  75. return true;
  76. }
  77. }
  78. else if(args[i] == "debug")
  79. {
  80. if(haveLLT)
  81. {
  82. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::DEBUG);
  83. }
  84. llt = cmTarget::DEBUG;
  85. haveLLT = true;
  86. }
  87. else if(args[i] == "optimized")
  88. {
  89. if(haveLLT)
  90. {
  91. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::OPTIMIZED);
  92. }
  93. llt = cmTarget::OPTIMIZED;
  94. haveLLT = true;
  95. }
  96. else if(args[i] == "general")
  97. {
  98. if(haveLLT)
  99. {
  100. this->LinkLibraryTypeSpecifierWarning(llt, cmTarget::GENERAL);
  101. }
  102. llt = cmTarget::GENERAL;
  103. haveLLT = true;
  104. }
  105. else if(haveLLT)
  106. {
  107. // The link type was specified by the previous argument.
  108. haveLLT = false;
  109. this->HandleLibrary(args[i].c_str(), llt);
  110. }
  111. else
  112. {
  113. // Lookup old-style cache entry if type is unspecified. So if you
  114. // do a target_link_libraries(foo optimized bar) it will stay optimized
  115. // and not use the lookup. As there maybe the case where someone has
  116. // specifed that a library is both debug and optimized. (this check is
  117. // only there for backwards compatibility when mixing projects built
  118. // with old versions of CMake and new)
  119. llt = cmTarget::GENERAL;
  120. std::string linkType = args[0];
  121. linkType += "_LINK_TYPE";
  122. const char* linkTypeString =
  123. this->Makefile->GetDefinition( linkType.c_str() );
  124. if(linkTypeString)
  125. {
  126. if(strcmp(linkTypeString, "debug") == 0)
  127. {
  128. llt = cmTarget::DEBUG;
  129. }
  130. if(strcmp(linkTypeString, "optimized") == 0)
  131. {
  132. llt = cmTarget::OPTIMIZED;
  133. }
  134. }
  135. this->HandleLibrary(args[i].c_str(), llt);
  136. }
  137. }
  138. // Make sure the last argument was not a library type specifier.
  139. if(haveLLT)
  140. {
  141. cmOStringStream e;
  142. e << "The \"" << this->LinkLibraryTypeNames[llt]
  143. << "\" argument must be followed by a library.";
  144. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  145. cmSystemTools::SetFatalErrorOccured();
  146. }
  147. // If the INTERFACE option was given, make sure the
  148. // LINK_INTERFACE_LIBRARIES property exists. This allows the
  149. // command to be used to specify an empty link interface.
  150. if(this->DoingInterface &&
  151. !this->Target->GetProperty("LINK_INTERFACE_LIBRARIES"))
  152. {
  153. this->Target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  154. }
  155. return true;
  156. }
  157. //----------------------------------------------------------------------------
  158. void
  159. cmTargetLinkLibrariesCommand
  160. ::LinkLibraryTypeSpecifierWarning(int left, int right)
  161. {
  162. cmOStringStream w;
  163. w << "Link library type specifier \""
  164. << this->LinkLibraryTypeNames[left] << "\" is followed by specifier \""
  165. << this->LinkLibraryTypeNames[right] << "\" instead of a library name. "
  166. << "The first specifier will be ignored.";
  167. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  168. }
  169. //----------------------------------------------------------------------------
  170. void
  171. cmTargetLinkLibrariesCommand::HandleLibrary(const char* lib,
  172. cmTarget::LinkLibraryType llt)
  173. {
  174. // Handle normal case first.
  175. if(!this->DoingInterface)
  176. {
  177. this->Makefile
  178. ->AddLinkLibraryForTarget(this->Target->GetName(), lib, llt);
  179. return;
  180. }
  181. // Get the list of configurations considered to be DEBUG.
  182. std::vector<std::string> const& debugConfigs =
  183. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  184. std::string prop;
  185. // Include this library in the link interface for the target.
  186. if(llt == cmTarget::DEBUG || llt == cmTarget::GENERAL)
  187. {
  188. // Put in the DEBUG configuration interfaces.
  189. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  190. i != debugConfigs.end(); ++i)
  191. {
  192. prop = "LINK_INTERFACE_LIBRARIES_";
  193. prop += *i;
  194. this->Target->AppendProperty(prop.c_str(), lib);
  195. }
  196. }
  197. if(llt == cmTarget::OPTIMIZED || llt == cmTarget::GENERAL)
  198. {
  199. // Put in the non-DEBUG configuration interfaces.
  200. this->Target->AppendProperty("LINK_INTERFACE_LIBRARIES", lib);
  201. // Make sure the DEBUG configuration interfaces exist so that the
  202. // general one will not be used as a fall-back.
  203. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  204. i != debugConfigs.end(); ++i)
  205. {
  206. prop = "LINK_INTERFACE_LIBRARIES_";
  207. prop += *i;
  208. if(!this->Target->GetProperty(prop.c_str()))
  209. {
  210. this->Target->SetProperty(prop.c_str(), "");
  211. }
  212. }
  213. }
  214. }