cmAddLibraryCommand.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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(libType == "INTERFACE")
  78. {
  79. ++s;
  80. type = cmTarget::INTERFACE_LIBRARY;
  81. haveSpecifiedType = true;
  82. }
  83. else if(*s == "EXCLUDE_FROM_ALL")
  84. {
  85. ++s;
  86. excludeFromAll = true;
  87. }
  88. else if(*s == "IMPORTED")
  89. {
  90. ++s;
  91. importTarget = true;
  92. }
  93. else if(importTarget && *s == "GLOBAL")
  94. {
  95. ++s;
  96. importGlobal = true;
  97. }
  98. else
  99. {
  100. break;
  101. }
  102. }
  103. if (isAlias)
  104. {
  105. if(!cmGeneratorExpression::IsValidTargetName(libName.c_str()))
  106. {
  107. this->SetError(("Invalid name for ALIAS: " + libName).c_str());
  108. return false;
  109. }
  110. if(excludeFromAll)
  111. {
  112. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  113. return false;
  114. }
  115. if(importTarget || importGlobal)
  116. {
  117. this->SetError("IMPORTED with ALIAS is not allowed.");
  118. return false;
  119. }
  120. if(args.size() != 3)
  121. {
  122. cmOStringStream e;
  123. e << "ALIAS requires exactly one target argument.";
  124. this->SetError(e.str().c_str());
  125. return false;
  126. }
  127. const char *aliasedName = s->c_str();
  128. if(this->Makefile->IsAlias(aliasedName))
  129. {
  130. cmOStringStream e;
  131. e << "cannot create ALIAS target \"" << libName
  132. << "\" because target \"" << aliasedName << "\" is itself an ALIAS.";
  133. this->SetError(e.str().c_str());
  134. return false;
  135. }
  136. cmTarget *aliasedTarget =
  137. this->Makefile->FindTargetToUse(aliasedName, true);
  138. if(!aliasedTarget)
  139. {
  140. cmOStringStream e;
  141. e << "cannot create ALIAS target \"" << libName
  142. << "\" because target \"" << aliasedName << "\" does not already "
  143. "exist.";
  144. this->SetError(e.str().c_str());
  145. return false;
  146. }
  147. cmTarget::TargetType aliasedType = aliasedTarget->GetType();
  148. if(aliasedType != cmTarget::SHARED_LIBRARY
  149. && aliasedType != cmTarget::STATIC_LIBRARY
  150. && aliasedType != cmTarget::MODULE_LIBRARY
  151. && aliasedType != cmTarget::OBJECT_LIBRARY
  152. && aliasedType != cmTarget::INTERFACE_LIBRARY)
  153. {
  154. cmOStringStream e;
  155. e << "cannot create ALIAS target \"" << libName
  156. << "\" because target \"" << aliasedName << "\" is not a library.";
  157. this->SetError(e.str().c_str());
  158. return false;
  159. }
  160. if(aliasedTarget->IsImported())
  161. {
  162. cmOStringStream e;
  163. e << "cannot create ALIAS target \"" << libName
  164. << "\" because target \"" << aliasedName << "\" is IMPORTED.";
  165. this->SetError(e.str().c_str());
  166. return false;
  167. }
  168. this->Makefile->AddAlias(libName.c_str(), aliasedTarget);
  169. return true;
  170. }
  171. if(importTarget && excludeFromAll)
  172. {
  173. this->SetError("excludeFromAll with IMPORTED target makes no sense.");
  174. return false;
  175. }
  176. /* ideally we should check whether for the linker language of the target
  177. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  178. STATIC. But at this point we know only the name of the target, but not
  179. yet its linker language. */
  180. if ((type != cmTarget::STATIC_LIBRARY) &&
  181. (type != cmTarget::OBJECT_LIBRARY) &&
  182. (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
  183. "TARGET_SUPPORTS_SHARED_LIBS") == false))
  184. {
  185. cmOStringStream w;
  186. w <<
  187. "ADD_LIBRARY called with " <<
  188. (type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE") <<
  189. " option but the target platform does not support dynamic linking. "
  190. "Building a STATIC library instead. This may lead to problems.";
  191. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  192. type = cmTarget::STATIC_LIBRARY;
  193. }
  194. // Handle imported target creation.
  195. if(importTarget)
  196. {
  197. // The IMPORTED signature requires a type to be specified explicitly.
  198. if (!haveSpecifiedType)
  199. {
  200. this->SetError("called with IMPORTED argument but no library type.");
  201. return false;
  202. }
  203. if(type == cmTarget::OBJECT_LIBRARY)
  204. {
  205. this->Makefile->IssueMessage(
  206. cmake::FATAL_ERROR,
  207. "The OBJECT library type may not be used for IMPORTED libraries."
  208. );
  209. return true;
  210. }
  211. if(type == cmTarget::INTERFACE_LIBRARY)
  212. {
  213. if (!cmGeneratorExpression::IsValidTargetName(libName))
  214. {
  215. cmOStringStream e;
  216. e << "Invalid name for IMPORTED INTERFACE library target: " << libName;
  217. this->SetError(e.str().c_str());
  218. return false;
  219. }
  220. }
  221. // Make sure the target does not already exist.
  222. if(this->Makefile->FindTargetToUse(libName.c_str()))
  223. {
  224. cmOStringStream e;
  225. e << "cannot create imported target \"" << libName
  226. << "\" because another target with the same name already exists.";
  227. this->SetError(e.str().c_str());
  228. return false;
  229. }
  230. // Create the imported target.
  231. this->Makefile->AddImportedTarget(libName.c_str(), type, importGlobal);
  232. return true;
  233. }
  234. // A non-imported target may not have UNKNOWN type.
  235. if(type == cmTarget::UNKNOWN_LIBRARY)
  236. {
  237. this->Makefile->IssueMessage(
  238. cmake::FATAL_ERROR,
  239. "The UNKNOWN library type may be used only for IMPORTED libraries."
  240. );
  241. return true;
  242. }
  243. // Enforce name uniqueness.
  244. {
  245. std::string msg;
  246. if(!this->Makefile->EnforceUniqueName(libName, msg))
  247. {
  248. this->SetError(msg.c_str());
  249. return false;
  250. }
  251. }
  252. std::vector<std::string> srclists;
  253. if(type == cmTarget::INTERFACE_LIBRARY)
  254. {
  255. if (!cmGeneratorExpression::IsValidTargetName(libName)
  256. || libName.find("::") != std::string::npos)
  257. {
  258. cmOStringStream e;
  259. e << "Invalid name for INTERFACE library target: " << libName;
  260. this->SetError(e.str().c_str());
  261. return false;
  262. }
  263. this->Makefile->AddLibrary(libName.c_str(),
  264. type,
  265. srclists,
  266. excludeFromAll);
  267. return true;
  268. }
  269. if (s == args.end())
  270. {
  271. std::string msg = "You have called ADD_LIBRARY for library ";
  272. msg += args[0];
  273. msg += " without any source files. This typically indicates a problem ";
  274. msg += "with your CMakeLists.txt file";
  275. cmSystemTools::Message(msg.c_str() ,"Warning");
  276. }
  277. while (s != args.end())
  278. {
  279. srclists.push_back(*s);
  280. ++s;
  281. }
  282. this->Makefile->AddLibrary(libName.c_str(), type, srclists, excludeFromAll);
  283. return true;
  284. }