cmAddLibraryCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <cmext/algorithm>
  5. #include "cmExecutionStatus.h"
  6. #include "cmGeneratorExpression.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmPolicies.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. if (mf.GetPolicyStatus(cmPolicies::CMP0107) == cmPolicies::NEW) {
  173. // Make sure the target does not already exist.
  174. if (mf.FindTargetToUse(libName)) {
  175. status.SetError(cmStrCat(
  176. "cannot create ALIAS target \"", libName,
  177. "\" because another target with the same name already exists."));
  178. return false;
  179. }
  180. }
  181. std::string const& aliasedName = *s;
  182. if (mf.IsAlias(aliasedName)) {
  183. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  184. "\" because target \"", aliasedName,
  185. "\" is itself an ALIAS."));
  186. return false;
  187. }
  188. cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
  189. if (!aliasedTarget) {
  190. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  191. "\" because target \"", aliasedName,
  192. "\" does not already exist."));
  193. return false;
  194. }
  195. cmStateEnums::TargetType aliasedType = aliasedTarget->GetType();
  196. if (aliasedType != cmStateEnums::SHARED_LIBRARY &&
  197. aliasedType != cmStateEnums::STATIC_LIBRARY &&
  198. aliasedType != cmStateEnums::MODULE_LIBRARY &&
  199. aliasedType != cmStateEnums::OBJECT_LIBRARY &&
  200. aliasedType != cmStateEnums::INTERFACE_LIBRARY &&
  201. !(aliasedType == cmStateEnums::UNKNOWN_LIBRARY &&
  202. aliasedTarget->IsImported())) {
  203. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  204. "\" because target \"", aliasedName,
  205. "\" is not a library."));
  206. return false;
  207. }
  208. mf.AddAlias(libName, aliasedName,
  209. !aliasedTarget->IsImported() ||
  210. aliasedTarget->IsImportedGloballyVisible());
  211. return true;
  212. }
  213. if (importTarget && excludeFromAll) {
  214. status.SetError("excludeFromAll with IMPORTED target makes no sense.");
  215. return false;
  216. }
  217. /* ideally we should check whether for the linker language of the target
  218. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  219. STATIC. But at this point we know only the name of the target, but not
  220. yet its linker language. */
  221. if ((type == cmStateEnums::SHARED_LIBRARY ||
  222. type == cmStateEnums::MODULE_LIBRARY) &&
  223. !mf.GetState()->GetGlobalPropertyAsBool("TARGET_SUPPORTS_SHARED_LIBS")) {
  224. mf.IssueMessage(
  225. MessageType::AUTHOR_WARNING,
  226. cmStrCat(
  227. "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. type = cmStateEnums::STATIC_LIBRARY;
  232. }
  233. // Handle imported target creation.
  234. if (importTarget) {
  235. // The IMPORTED signature requires a type to be specified explicitly.
  236. if (!haveSpecifiedType) {
  237. status.SetError("called with IMPORTED argument but no library type.");
  238. return false;
  239. }
  240. if (type == cmStateEnums::OBJECT_LIBRARY) {
  241. std::string reason;
  242. if (!mf.GetGlobalGenerator()->HasKnownObjectFileLocation(&reason)) {
  243. mf.IssueMessage(
  244. MessageType::FATAL_ERROR,
  245. "The OBJECT library type may not be used for IMPORTED libraries" +
  246. reason + ".");
  247. return true;
  248. }
  249. }
  250. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  251. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  252. status.SetError(cmStrCat(
  253. "Invalid name for IMPORTED INTERFACE library target: ", libName));
  254. return false;
  255. }
  256. }
  257. // Make sure the target does not already exist.
  258. if (mf.FindTargetToUse(libName)) {
  259. status.SetError(cmStrCat(
  260. "cannot create imported target \"", libName,
  261. "\" because another target with the same name already exists."));
  262. return false;
  263. }
  264. // Create the imported target.
  265. mf.AddImportedTarget(libName, type, importGlobal);
  266. return true;
  267. }
  268. // A non-imported target may not have UNKNOWN type.
  269. if (type == cmStateEnums::UNKNOWN_LIBRARY) {
  270. mf.IssueMessage(
  271. MessageType::FATAL_ERROR,
  272. "The UNKNOWN library type may be used only for IMPORTED libraries.");
  273. return true;
  274. }
  275. // Enforce name uniqueness.
  276. {
  277. std::string msg;
  278. if (!mf.EnforceUniqueName(libName, msg)) {
  279. status.SetError(msg);
  280. return false;
  281. }
  282. }
  283. std::vector<std::string> srclists;
  284. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  285. if (!cmGeneratorExpression::IsValidTargetName(libName) ||
  286. libName.find("::") != std::string::npos) {
  287. status.SetError(
  288. cmStrCat("Invalid name for INTERFACE library target: ", libName));
  289. return false;
  290. }
  291. mf.AddLibrary(libName, type, srclists, excludeFromAll);
  292. return true;
  293. }
  294. cm::append(srclists, s, args.end());
  295. mf.AddLibrary(libName, type, srclists, excludeFromAll);
  296. return true;
  297. }