cmAddLibraryCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmAddLibraryCommand.h"
  4. #include <sstream>
  5. #include "cmGeneratorExpression.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmState.h"
  9. #include "cmStateTypes.h"
  10. #include "cmSystemTools.h"
  11. #include "cmTarget.h"
  12. #include "cmake.h"
  13. class cmExecutionStatus;
  14. // cmLibraryCommand
  15. bool cmAddLibraryCommand::InitialPass(std::vector<std::string> const& args,
  16. cmExecutionStatus&)
  17. {
  18. if (args.empty()) {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. // Library type defaults to value of BUILD_SHARED_LIBS, if it exists,
  23. // otherwise it defaults to static library.
  24. cmStateEnums::TargetType type = cmStateEnums::SHARED_LIBRARY;
  25. if (cmSystemTools::IsOff(
  26. this->Makefile->GetDefinition("BUILD_SHARED_LIBS"))) {
  27. type = cmStateEnums::STATIC_LIBRARY;
  28. }
  29. bool excludeFromAll = false;
  30. bool importTarget = false;
  31. bool importGlobal = false;
  32. std::vector<std::string>::const_iterator s = args.begin();
  33. std::string const& libName = *s;
  34. ++s;
  35. // If the second argument is "SHARED" or "STATIC", then it controls
  36. // the type of library. Otherwise, it is treated as a source or
  37. // source list name. There may be two keyword arguments, check for them
  38. bool haveSpecifiedType = false;
  39. bool isAlias = false;
  40. while (s != args.end()) {
  41. std::string libType = *s;
  42. if (libType == "STATIC") {
  43. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  44. std::ostringstream e;
  45. e << "INTERFACE library specified with conflicting STATIC type.";
  46. this->SetError(e.str());
  47. return false;
  48. }
  49. ++s;
  50. type = cmStateEnums::STATIC_LIBRARY;
  51. haveSpecifiedType = true;
  52. } else if (libType == "SHARED") {
  53. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  54. std::ostringstream e;
  55. e << "INTERFACE library specified with conflicting SHARED type.";
  56. this->SetError(e.str());
  57. return false;
  58. }
  59. ++s;
  60. type = cmStateEnums::SHARED_LIBRARY;
  61. haveSpecifiedType = true;
  62. } else if (libType == "MODULE") {
  63. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  64. std::ostringstream e;
  65. e << "INTERFACE library specified with conflicting MODULE type.";
  66. this->SetError(e.str());
  67. return false;
  68. }
  69. ++s;
  70. type = cmStateEnums::MODULE_LIBRARY;
  71. haveSpecifiedType = true;
  72. } else if (libType == "OBJECT") {
  73. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  74. std::ostringstream e;
  75. e << "INTERFACE library specified with conflicting OBJECT type.";
  76. this->SetError(e.str());
  77. return false;
  78. }
  79. ++s;
  80. type = cmStateEnums::OBJECT_LIBRARY;
  81. haveSpecifiedType = true;
  82. } else if (libType == "UNKNOWN") {
  83. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  84. std::ostringstream e;
  85. e << "INTERFACE library specified with conflicting UNKNOWN type.";
  86. this->SetError(e.str());
  87. return false;
  88. }
  89. ++s;
  90. type = cmStateEnums::UNKNOWN_LIBRARY;
  91. haveSpecifiedType = true;
  92. } else if (libType == "ALIAS") {
  93. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  94. std::ostringstream e;
  95. e << "INTERFACE library specified with conflicting ALIAS type.";
  96. this->SetError(e.str());
  97. return false;
  98. }
  99. ++s;
  100. isAlias = true;
  101. } else if (libType == "INTERFACE") {
  102. if (haveSpecifiedType) {
  103. std::ostringstream e;
  104. e << "INTERFACE library specified with conflicting/multiple types.";
  105. this->SetError(e.str());
  106. return false;
  107. }
  108. if (isAlias) {
  109. std::ostringstream e;
  110. e << "INTERFACE library specified with conflicting ALIAS type.";
  111. this->SetError(e.str());
  112. return false;
  113. }
  114. if (excludeFromAll) {
  115. std::ostringstream e;
  116. e << "INTERFACE library may not be used with EXCLUDE_FROM_ALL.";
  117. this->SetError(e.str());
  118. return false;
  119. }
  120. ++s;
  121. type = cmStateEnums::INTERFACE_LIBRARY;
  122. haveSpecifiedType = true;
  123. } else if (*s == "EXCLUDE_FROM_ALL") {
  124. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  125. std::ostringstream e;
  126. e << "INTERFACE library may not be used with EXCLUDE_FROM_ALL.";
  127. this->SetError(e.str());
  128. return false;
  129. }
  130. ++s;
  131. excludeFromAll = true;
  132. } else if (*s == "IMPORTED") {
  133. ++s;
  134. importTarget = true;
  135. } else if (importTarget && *s == "GLOBAL") {
  136. ++s;
  137. importGlobal = true;
  138. } else if (type == cmStateEnums::INTERFACE_LIBRARY && *s == "GLOBAL") {
  139. std::ostringstream e;
  140. e << "GLOBAL option may only be used with IMPORTED libraries.";
  141. this->SetError(e.str());
  142. return false;
  143. } else {
  144. break;
  145. }
  146. }
  147. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  148. if (s != args.end()) {
  149. std::ostringstream e;
  150. e << "INTERFACE library requires no source arguments.";
  151. this->SetError(e.str());
  152. return false;
  153. }
  154. if (importGlobal && !importTarget) {
  155. std::ostringstream e;
  156. e << "INTERFACE library specified as GLOBAL, but not as IMPORTED.";
  157. this->SetError(e.str());
  158. return false;
  159. }
  160. }
  161. bool nameOk = cmGeneratorExpression::IsValidTargetName(libName) &&
  162. !cmGlobalGenerator::IsReservedTarget(libName);
  163. if (nameOk && !importTarget && !isAlias) {
  164. nameOk = libName.find(':') == std::string::npos;
  165. }
  166. if (!nameOk && !this->Makefile->CheckCMP0037(libName, type)) {
  167. return false;
  168. }
  169. if (isAlias) {
  170. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  171. this->SetError("Invalid name for ALIAS: " + libName);
  172. return false;
  173. }
  174. if (excludeFromAll) {
  175. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  176. return false;
  177. }
  178. if (importTarget || importGlobal) {
  179. this->SetError("IMPORTED with ALIAS is not allowed.");
  180. return false;
  181. }
  182. if (args.size() != 3) {
  183. std::ostringstream e;
  184. e << "ALIAS requires exactly one target argument.";
  185. this->SetError(e.str());
  186. return false;
  187. }
  188. std::string const& aliasedName = *s;
  189. if (this->Makefile->IsAlias(aliasedName)) {
  190. std::ostringstream e;
  191. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  192. << aliasedName << "\" is itself an ALIAS.";
  193. this->SetError(e.str());
  194. return false;
  195. }
  196. cmTarget* aliasedTarget =
  197. this->Makefile->FindTargetToUse(aliasedName, true);
  198. if (!aliasedTarget) {
  199. std::ostringstream e;
  200. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  201. << aliasedName << "\" does not already "
  202. "exist.";
  203. this->SetError(e.str());
  204. return false;
  205. }
  206. cmStateEnums::TargetType aliasedType = aliasedTarget->GetType();
  207. if (aliasedType != cmStateEnums::SHARED_LIBRARY &&
  208. aliasedType != cmStateEnums::STATIC_LIBRARY &&
  209. aliasedType != cmStateEnums::MODULE_LIBRARY &&
  210. aliasedType != cmStateEnums::OBJECT_LIBRARY &&
  211. aliasedType != cmStateEnums::INTERFACE_LIBRARY) {
  212. std::ostringstream e;
  213. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  214. << aliasedName << "\" is not a library.";
  215. this->SetError(e.str());
  216. return false;
  217. }
  218. this->Makefile->AddAlias(libName, aliasedName);
  219. return true;
  220. }
  221. if (importTarget && excludeFromAll) {
  222. this->SetError("excludeFromAll with IMPORTED target makes no sense.");
  223. return false;
  224. }
  225. /* ideally we should check whether for the linker language of the target
  226. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  227. STATIC. But at this point we know only the name of the target, but not
  228. yet its linker language. */
  229. if ((type == cmStateEnums::SHARED_LIBRARY ||
  230. type == cmStateEnums::MODULE_LIBRARY) &&
  231. !this->Makefile->GetState()->GetGlobalPropertyAsBool(
  232. "TARGET_SUPPORTS_SHARED_LIBS")) {
  233. std::ostringstream w;
  234. w << "ADD_LIBRARY called with "
  235. << (type == cmStateEnums::SHARED_LIBRARY ? "SHARED" : "MODULE")
  236. << " option but the target platform does not support dynamic linking. "
  237. "Building a STATIC library instead. This may lead to problems.";
  238. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  239. type = cmStateEnums::STATIC_LIBRARY;
  240. }
  241. // Handle imported target creation.
  242. if (importTarget) {
  243. // The IMPORTED signature requires a type to be specified explicitly.
  244. if (!haveSpecifiedType) {
  245. this->SetError("called with IMPORTED argument but no library type.");
  246. return false;
  247. }
  248. if (type == cmStateEnums::OBJECT_LIBRARY) {
  249. std::string reason;
  250. if (!this->Makefile->GetGlobalGenerator()->HasKnownObjectFileLocation(
  251. &reason)) {
  252. this->Makefile->IssueMessage(
  253. cmake::FATAL_ERROR,
  254. "The OBJECT library type may not be used for IMPORTED libraries" +
  255. reason + ".");
  256. return true;
  257. }
  258. }
  259. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  260. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  261. std::ostringstream e;
  262. e << "Invalid name for IMPORTED INTERFACE library target: " << libName;
  263. this->SetError(e.str());
  264. return false;
  265. }
  266. }
  267. // Make sure the target does not already exist.
  268. if (this->Makefile->FindTargetToUse(libName)) {
  269. std::ostringstream e;
  270. e << "cannot create imported target \"" << libName
  271. << "\" because another target with the same name already exists.";
  272. this->SetError(e.str());
  273. return false;
  274. }
  275. // Create the imported target.
  276. this->Makefile->AddImportedTarget(libName, type, importGlobal);
  277. return true;
  278. }
  279. // A non-imported target may not have UNKNOWN type.
  280. if (type == cmStateEnums::UNKNOWN_LIBRARY) {
  281. this->Makefile->IssueMessage(
  282. cmake::FATAL_ERROR,
  283. "The UNKNOWN library type may be used only for IMPORTED libraries.");
  284. return true;
  285. }
  286. // Enforce name uniqueness.
  287. {
  288. std::string msg;
  289. if (!this->Makefile->EnforceUniqueName(libName, msg)) {
  290. this->SetError(msg);
  291. return false;
  292. }
  293. }
  294. std::vector<std::string> srclists;
  295. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  296. if (!cmGeneratorExpression::IsValidTargetName(libName) ||
  297. libName.find("::") != std::string::npos) {
  298. std::ostringstream e;
  299. e << "Invalid name for INTERFACE library target: " << libName;
  300. this->SetError(e.str());
  301. return false;
  302. }
  303. this->Makefile->AddLibrary(libName, type, srclists, excludeFromAll);
  304. return true;
  305. }
  306. srclists.insert(srclists.end(), s, args.end());
  307. this->Makefile->AddLibrary(libName, type, srclists, excludeFromAll);
  308. return true;
  309. }