cmTargetLinkLibrariesCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. const cmPolicies::PolicyStatus policy22Status
  214. = this->Target->GetPolicyStatusCMP0022();
  215. // If any of the LINK_ options were given, make sure the
  216. // LINK_INTERFACE_LIBRARIES target property exists.
  217. // Use of any of the new keywords implies awareness of
  218. // this property. And if no libraries are named, it should
  219. // result in an empty link interface.
  220. if((policy22Status == cmPolicies::OLD ||
  221. policy22Status == cmPolicies::WARN) &&
  222. this->CurrentProcessingState != ProcessingLinkLibraries &&
  223. !this->Target->GetProperty("LINK_INTERFACE_LIBRARIES"))
  224. {
  225. this->Target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  226. }
  227. return true;
  228. }
  229. //----------------------------------------------------------------------------
  230. void
  231. cmTargetLinkLibrariesCommand
  232. ::LinkLibraryTypeSpecifierWarning(int left, int right)
  233. {
  234. cmOStringStream w;
  235. w << "Link library type specifier \""
  236. << this->LinkLibraryTypeNames[left] << "\" is followed by specifier \""
  237. << this->LinkLibraryTypeNames[right] << "\" instead of a library name. "
  238. << "The first specifier will be ignored.";
  239. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  240. }
  241. //----------------------------------------------------------------------------
  242. void
  243. cmTargetLinkLibrariesCommand::HandleLibrary(const char* lib,
  244. cmTarget::LinkLibraryType llt)
  245. {
  246. // Handle normal case first.
  247. if(this->CurrentProcessingState != ProcessingLinkInterface)
  248. {
  249. this->Makefile
  250. ->AddLinkLibraryForTarget(this->Target->GetName(), lib, llt);
  251. if (this->CurrentProcessingState != ProcessingPublicInterface)
  252. {
  253. if (this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  254. {
  255. this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
  256. ("$<LINK_ONLY:" +
  257. this->Target->GetDebugGeneratorExpressions(lib, llt) +
  258. ">").c_str());
  259. }
  260. // Not LINK_INTERFACE_LIBRARIES or LINK_PUBLIC, do not add to interface.
  261. return;
  262. }
  263. }
  264. this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
  265. this->Target->GetDebugGeneratorExpressions(lib, llt).c_str());
  266. const cmPolicies::PolicyStatus policy22Status
  267. = this->Target->GetPolicyStatusCMP0022();
  268. if (policy22Status != cmPolicies::OLD
  269. && policy22Status != cmPolicies::WARN)
  270. {
  271. return;
  272. }
  273. // Get the list of configurations considered to be DEBUG.
  274. std::vector<std::string> const& debugConfigs =
  275. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  276. std::string prop;
  277. // Include this library in the link interface for the target.
  278. if(llt == cmTarget::DEBUG || llt == cmTarget::GENERAL)
  279. {
  280. // Put in the DEBUG configuration interfaces.
  281. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  282. i != debugConfigs.end(); ++i)
  283. {
  284. prop = "LINK_INTERFACE_LIBRARIES_";
  285. prop += *i;
  286. this->Target->AppendProperty(prop.c_str(), lib);
  287. }
  288. }
  289. if(llt == cmTarget::OPTIMIZED || llt == cmTarget::GENERAL)
  290. {
  291. // Put in the non-DEBUG configuration interfaces.
  292. this->Target->AppendProperty("LINK_INTERFACE_LIBRARIES", lib);
  293. // Make sure the DEBUG configuration interfaces exist so that the
  294. // general one will not be used as a fall-back.
  295. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  296. i != debugConfigs.end(); ++i)
  297. {
  298. prop = "LINK_INTERFACE_LIBRARIES_";
  299. prop += *i;
  300. if(!this->Target->GetProperty(prop.c_str()))
  301. {
  302. this->Target->SetProperty(prop.c_str(), "");
  303. }
  304. }
  305. }
  306. }