cmAddLibraryCommand.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 "cmAddLibraryCommand.h"
  11. #include "cmake.h"
  12. // cmLibraryCommand
  13. bool cmAddLibraryCommand
  14. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  15. {
  16. if(args.size() < 1 )
  17. {
  18. this->SetError("called with incorrect number of arguments");
  19. return false;
  20. }
  21. // Library type defaults to value of BUILD_SHARED_LIBS, if it exists,
  22. // otherwise it defaults to static library.
  23. cmTarget::TargetType type = cmTarget::SHARED_LIBRARY;
  24. if (cmSystemTools::IsOff(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  25. {
  26. type = cmTarget::STATIC_LIBRARY;
  27. }
  28. bool excludeFromAll = false;
  29. bool importTarget = false;
  30. bool importGlobal = false;
  31. std::vector<std::string>::const_iterator s = args.begin();
  32. std::string libName = *s;
  33. ++s;
  34. // If the second argument is "SHARED" or "STATIC", then it controls
  35. // the type of library. Otherwise, it is treated as a source or
  36. // source list name. There may be two keyword arguments, check for them
  37. bool haveSpecifiedType = false;
  38. bool isAlias = false;
  39. while ( s != args.end() )
  40. {
  41. std::string libType = *s;
  42. if(libType == "STATIC")
  43. {
  44. ++s;
  45. type = cmTarget::STATIC_LIBRARY;
  46. haveSpecifiedType = true;
  47. }
  48. else if(libType == "SHARED")
  49. {
  50. ++s;
  51. type = cmTarget::SHARED_LIBRARY;
  52. haveSpecifiedType = true;
  53. }
  54. else if(libType == "MODULE")
  55. {
  56. ++s;
  57. type = cmTarget::MODULE_LIBRARY;
  58. haveSpecifiedType = true;
  59. }
  60. else if(libType == "OBJECT")
  61. {
  62. ++s;
  63. type = cmTarget::OBJECT_LIBRARY;
  64. haveSpecifiedType = true;
  65. }
  66. else if(libType == "UNKNOWN")
  67. {
  68. ++s;
  69. type = cmTarget::UNKNOWN_LIBRARY;
  70. haveSpecifiedType = true;
  71. }
  72. else if(libType == "ALIAS")
  73. {
  74. ++s;
  75. isAlias = true;
  76. }
  77. else if(*s == "EXCLUDE_FROM_ALL")
  78. {
  79. ++s;
  80. excludeFromAll = true;
  81. }
  82. else if(*s == "IMPORTED")
  83. {
  84. ++s;
  85. importTarget = true;
  86. }
  87. else if(importTarget && *s == "GLOBAL")
  88. {
  89. ++s;
  90. importGlobal = true;
  91. }
  92. else
  93. {
  94. break;
  95. }
  96. }
  97. if (isAlias)
  98. {
  99. if(!cmGeneratorExpression::IsValidTargetName(libName.c_str()))
  100. {
  101. this->SetError(("Invalid name for ALIAS: " + libName).c_str());
  102. return false;
  103. }
  104. if(excludeFromAll)
  105. {
  106. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  107. return false;
  108. }
  109. if(importTarget || importGlobal)
  110. {
  111. this->SetError("IMPORTED with ALIAS is not allowed.");
  112. return false;
  113. }
  114. if(args.size() != 3)
  115. {
  116. cmOStringStream e;
  117. e << "ALIAS requires exactly one target argument.";
  118. this->SetError(e.str().c_str());
  119. return false;
  120. }
  121. const char *aliasedName = s->c_str();
  122. if(this->Makefile->IsAlias(aliasedName))
  123. {
  124. cmOStringStream e;
  125. e << "cannot create ALIAS target \"" << libName
  126. << "\" because target \"" << aliasedName << "\" is itself an ALIAS.";
  127. this->SetError(e.str().c_str());
  128. return false;
  129. }
  130. cmTarget *aliasedTarget =
  131. this->Makefile->FindTargetToUse(aliasedName, true);
  132. if(!aliasedTarget)
  133. {
  134. cmOStringStream e;
  135. e << "cannot create ALIAS target \"" << libName
  136. << "\" because target \"" << aliasedName << "\" does not already "
  137. "exist.";
  138. this->SetError(e.str().c_str());
  139. return false;
  140. }
  141. cmTarget::TargetType aliasedType = aliasedTarget->GetType();
  142. if(aliasedType != cmTarget::SHARED_LIBRARY
  143. && aliasedType != cmTarget::STATIC_LIBRARY
  144. && aliasedType != cmTarget::MODULE_LIBRARY
  145. && aliasedType != cmTarget::OBJECT_LIBRARY)
  146. {
  147. cmOStringStream e;
  148. e << "cannot create ALIAS target \"" << libName
  149. << "\" because target \"" << aliasedName << "\" is not a library.";
  150. this->SetError(e.str().c_str());
  151. return false;
  152. }
  153. if(aliasedTarget->IsImported())
  154. {
  155. cmOStringStream e;
  156. e << "cannot create ALIAS target \"" << libName
  157. << "\" because target \"" << aliasedName << "\" is IMPORTED.";
  158. this->SetError(e.str().c_str());
  159. return false;
  160. }
  161. this->Makefile->AddAlias(libName.c_str(), aliasedTarget);
  162. return true;
  163. }
  164. if(importTarget && excludeFromAll)
  165. {
  166. this->SetError("excludeFromAll with IMPORTED target makes no sense.");
  167. return false;
  168. }
  169. /* ideally we should check whether for the linker language of the target
  170. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  171. STATIC. But at this point we know only the name of the target, but not
  172. yet its linker language. */
  173. if ((type != cmTarget::STATIC_LIBRARY) &&
  174. (type != cmTarget::OBJECT_LIBRARY) &&
  175. (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
  176. "TARGET_SUPPORTS_SHARED_LIBS") == false))
  177. {
  178. cmOStringStream w;
  179. w <<
  180. "ADD_LIBRARY called with " <<
  181. (type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE") <<
  182. " option but the target platform does not support dynamic linking. "
  183. "Building a STATIC library instead. This may lead to problems.";
  184. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  185. type = cmTarget::STATIC_LIBRARY;
  186. }
  187. // Handle imported target creation.
  188. if(importTarget)
  189. {
  190. // The IMPORTED signature requires a type to be specified explicitly.
  191. if (!haveSpecifiedType)
  192. {
  193. this->SetError("called with IMPORTED argument but no library type.");
  194. return false;
  195. }
  196. if(type == cmTarget::OBJECT_LIBRARY)
  197. {
  198. this->Makefile->IssueMessage(
  199. cmake::FATAL_ERROR,
  200. "The OBJECT library type may not be used for IMPORTED libraries."
  201. );
  202. return true;
  203. }
  204. // Make sure the target does not already exist.
  205. if(this->Makefile->FindTargetToUse(libName.c_str()))
  206. {
  207. cmOStringStream e;
  208. e << "cannot create imported target \"" << libName
  209. << "\" because another target with the same name already exists.";
  210. this->SetError(e.str().c_str());
  211. return false;
  212. }
  213. // Create the imported target.
  214. this->Makefile->AddImportedTarget(libName.c_str(), type, importGlobal);
  215. return true;
  216. }
  217. // A non-imported target may not have UNKNOWN type.
  218. if(type == cmTarget::UNKNOWN_LIBRARY)
  219. {
  220. this->Makefile->IssueMessage(
  221. cmake::FATAL_ERROR,
  222. "The UNKNOWN library type may be used only for IMPORTED libraries."
  223. );
  224. return true;
  225. }
  226. // Enforce name uniqueness.
  227. {
  228. std::string msg;
  229. if(!this->Makefile->EnforceUniqueName(libName, msg))
  230. {
  231. this->SetError(msg.c_str());
  232. return false;
  233. }
  234. }
  235. if (s == args.end())
  236. {
  237. std::string msg = "You have called ADD_LIBRARY for library ";
  238. msg += args[0];
  239. msg += " without any source files. This typically indicates a problem ";
  240. msg += "with your CMakeLists.txt file";
  241. cmSystemTools::Message(msg.c_str() ,"Warning");
  242. }
  243. std::vector<std::string> srclists;
  244. while (s != args.end())
  245. {
  246. srclists.push_back(*s);
  247. ++s;
  248. }
  249. this->Makefile->AddLibrary(libName.c_str(), type, srclists, excludeFromAll);
  250. return true;
  251. }