cmAddLibraryCommand.cxx 10 KB

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