cmAddLibraryCommand.cxx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. bool nameOk = cmGeneratorExpression::IsValidTargetName(libName);
  104. if (nameOk && !importTarget && !isAlias)
  105. {
  106. nameOk = libName.find(":") == std::string::npos;
  107. }
  108. if (!nameOk)
  109. {
  110. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  111. bool issueMessage = false;
  112. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0037))
  113. {
  114. case cmPolicies::WARN:
  115. issueMessage = type != cmTarget::INTERFACE_LIBRARY;
  116. case cmPolicies::OLD:
  117. break;
  118. case cmPolicies::NEW:
  119. case cmPolicies::REQUIRED_IF_USED:
  120. case cmPolicies::REQUIRED_ALWAYS:
  121. issueMessage = true;
  122. messageType = cmake::FATAL_ERROR;
  123. }
  124. if (issueMessage)
  125. {
  126. cmOStringStream e;
  127. e << (this->Makefile->GetPolicies()
  128. ->GetPolicyWarning(cmPolicies::CMP0037)) << "\n";
  129. e << "The target name \"" << libName << "\" is not valid for certain "
  130. "CMake features, such as generator expressions, and may result "
  131. "in undefined behavior.";
  132. this->Makefile->IssueMessage(messageType, e.str().c_str());
  133. if (messageType == cmake::FATAL_ERROR)
  134. {
  135. return false;
  136. }
  137. }
  138. }
  139. if (isAlias)
  140. {
  141. if(!cmGeneratorExpression::IsValidTargetName(libName.c_str()))
  142. {
  143. this->SetError(("Invalid name for ALIAS: " + libName).c_str());
  144. return false;
  145. }
  146. if(excludeFromAll)
  147. {
  148. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  149. return false;
  150. }
  151. if(importTarget || importGlobal)
  152. {
  153. this->SetError("IMPORTED with ALIAS is not allowed.");
  154. return false;
  155. }
  156. if(args.size() != 3)
  157. {
  158. cmOStringStream e;
  159. e << "ALIAS requires exactly one target argument.";
  160. this->SetError(e.str().c_str());
  161. return false;
  162. }
  163. const char *aliasedName = s->c_str();
  164. if(this->Makefile->IsAlias(aliasedName))
  165. {
  166. cmOStringStream e;
  167. e << "cannot create ALIAS target \"" << libName
  168. << "\" because target \"" << aliasedName << "\" is itself an ALIAS.";
  169. this->SetError(e.str().c_str());
  170. return false;
  171. }
  172. cmTarget *aliasedTarget =
  173. this->Makefile->FindTargetToUse(aliasedName, true);
  174. if(!aliasedTarget)
  175. {
  176. cmOStringStream e;
  177. e << "cannot create ALIAS target \"" << libName
  178. << "\" because target \"" << aliasedName << "\" does not already "
  179. "exist.";
  180. this->SetError(e.str().c_str());
  181. return false;
  182. }
  183. cmTarget::TargetType aliasedType = aliasedTarget->GetType();
  184. if(aliasedType != cmTarget::SHARED_LIBRARY
  185. && aliasedType != cmTarget::STATIC_LIBRARY
  186. && aliasedType != cmTarget::MODULE_LIBRARY
  187. && aliasedType != cmTarget::OBJECT_LIBRARY
  188. && aliasedType != cmTarget::INTERFACE_LIBRARY)
  189. {
  190. cmOStringStream e;
  191. e << "cannot create ALIAS target \"" << libName
  192. << "\" because target \"" << aliasedName << "\" is not a library.";
  193. this->SetError(e.str().c_str());
  194. return false;
  195. }
  196. if(aliasedTarget->IsImported())
  197. {
  198. cmOStringStream e;
  199. e << "cannot create ALIAS target \"" << libName
  200. << "\" because target \"" << aliasedName << "\" is IMPORTED.";
  201. this->SetError(e.str().c_str());
  202. return false;
  203. }
  204. this->Makefile->AddAlias(libName.c_str(), aliasedTarget);
  205. return true;
  206. }
  207. if(importTarget && excludeFromAll)
  208. {
  209. this->SetError("excludeFromAll with IMPORTED target makes no sense.");
  210. return false;
  211. }
  212. /* ideally we should check whether for the linker language of the target
  213. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  214. STATIC. But at this point we know only the name of the target, but not
  215. yet its linker language. */
  216. if ((type != cmTarget::STATIC_LIBRARY) &&
  217. (type != cmTarget::OBJECT_LIBRARY) &&
  218. (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
  219. "TARGET_SUPPORTS_SHARED_LIBS") == false))
  220. {
  221. cmOStringStream w;
  222. w <<
  223. "ADD_LIBRARY called with " <<
  224. (type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE") <<
  225. " option but the target platform does not support dynamic linking. "
  226. "Building a STATIC library instead. This may lead to problems.";
  227. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  228. type = cmTarget::STATIC_LIBRARY;
  229. }
  230. // Handle imported target creation.
  231. if(importTarget)
  232. {
  233. // The IMPORTED signature requires a type to be specified explicitly.
  234. if (!haveSpecifiedType)
  235. {
  236. this->SetError("called with IMPORTED argument but no library type.");
  237. return false;
  238. }
  239. if(type == cmTarget::OBJECT_LIBRARY)
  240. {
  241. this->Makefile->IssueMessage(
  242. cmake::FATAL_ERROR,
  243. "The OBJECT library type may not be used for IMPORTED libraries."
  244. );
  245. return true;
  246. }
  247. if(type == cmTarget::INTERFACE_LIBRARY)
  248. {
  249. if (!cmGeneratorExpression::IsValidTargetName(libName))
  250. {
  251. cmOStringStream e;
  252. e << "Invalid name for IMPORTED INTERFACE library target: " << libName;
  253. this->SetError(e.str().c_str());
  254. return false;
  255. }
  256. }
  257. // Make sure the target does not already exist.
  258. if(this->Makefile->FindTargetToUse(libName.c_str()))
  259. {
  260. cmOStringStream e;
  261. e << "cannot create imported target \"" << libName
  262. << "\" because another target with the same name already exists.";
  263. this->SetError(e.str().c_str());
  264. return false;
  265. }
  266. // Create the imported target.
  267. this->Makefile->AddImportedTarget(libName.c_str(), type, importGlobal);
  268. return true;
  269. }
  270. // A non-imported target may not have UNKNOWN type.
  271. if(type == cmTarget::UNKNOWN_LIBRARY)
  272. {
  273. this->Makefile->IssueMessage(
  274. cmake::FATAL_ERROR,
  275. "The UNKNOWN library type may be used only for IMPORTED libraries."
  276. );
  277. return true;
  278. }
  279. // Enforce name uniqueness.
  280. {
  281. std::string msg;
  282. if(!this->Makefile->EnforceUniqueName(libName, msg))
  283. {
  284. this->SetError(msg.c_str());
  285. return false;
  286. }
  287. }
  288. std::vector<std::string> srclists;
  289. if(type == cmTarget::INTERFACE_LIBRARY)
  290. {
  291. if (!cmGeneratorExpression::IsValidTargetName(libName)
  292. || libName.find("::") != std::string::npos)
  293. {
  294. cmOStringStream e;
  295. e << "Invalid name for INTERFACE library target: " << libName;
  296. this->SetError(e.str().c_str());
  297. return false;
  298. }
  299. this->Makefile->AddLibrary(libName.c_str(),
  300. type,
  301. srclists,
  302. excludeFromAll);
  303. return true;
  304. }
  305. if (s == args.end())
  306. {
  307. std::string msg = "You have called ADD_LIBRARY for library ";
  308. msg += args[0];
  309. msg += " without any source files. This typically indicates a problem ";
  310. msg += "with your CMakeLists.txt file";
  311. cmSystemTools::Message(msg.c_str() ,"Warning");
  312. }
  313. while (s != args.end())
  314. {
  315. srclists.push_back(*s);
  316. ++s;
  317. }
  318. this->Makefile->AddLibrary(libName.c_str(), type, srclists, excludeFromAll);
  319. return true;
  320. }