cmTargetLinkLibrariesCommand.cxx 6.8 KB

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