cmAddLibraryCommand.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "cmExecutionStatus.h"
  5. #include "cmGeneratorExpression.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmMessageType.h"
  9. #include "cmPolicies.h"
  10. #include "cmState.h"
  11. #include "cmStateTypes.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmTarget.h"
  14. #include "cmValue.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. ++s;
  107. type = cmStateEnums::INTERFACE_LIBRARY;
  108. haveSpecifiedType = true;
  109. } else if (*s == "EXCLUDE_FROM_ALL") {
  110. ++s;
  111. excludeFromAll = true;
  112. } else if (*s == "IMPORTED") {
  113. ++s;
  114. importTarget = true;
  115. } else if (importTarget && *s == "GLOBAL") {
  116. ++s;
  117. importGlobal = true;
  118. } else if (type == cmStateEnums::INTERFACE_LIBRARY && *s == "GLOBAL") {
  119. status.SetError(
  120. "GLOBAL option may only be used with IMPORTED libraries.");
  121. return false;
  122. } else {
  123. break;
  124. }
  125. }
  126. if (importTarget && !importGlobal) {
  127. importGlobal = mf.IsImportedTargetGlobalScope();
  128. }
  129. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  130. if (importGlobal && !importTarget) {
  131. status.SetError(
  132. "INTERFACE library specified as GLOBAL, but not as IMPORTED.");
  133. return false;
  134. }
  135. }
  136. bool nameOk = cmGeneratorExpression::IsValidTargetName(libName) &&
  137. !cmGlobalGenerator::IsReservedTarget(libName);
  138. if (nameOk && !importTarget && !isAlias) {
  139. nameOk = libName.find(':') == std::string::npos;
  140. }
  141. if (!nameOk && !mf.CheckCMP0037(libName, type)) {
  142. return false;
  143. }
  144. if (isAlias) {
  145. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  146. status.SetError("Invalid name for ALIAS: " + libName);
  147. return false;
  148. }
  149. if (excludeFromAll) {
  150. status.SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  151. return false;
  152. }
  153. if (importTarget || importGlobal) {
  154. status.SetError("IMPORTED with ALIAS is not allowed.");
  155. return false;
  156. }
  157. if (args.size() != 3) {
  158. status.SetError("ALIAS requires exactly one target argument.");
  159. return false;
  160. }
  161. if (mf.GetPolicyStatus(cmPolicies::CMP0107) == cmPolicies::NEW) {
  162. // Make sure the target does not already exist.
  163. if (mf.FindTargetToUse(libName)) {
  164. status.SetError(cmStrCat(
  165. "cannot create ALIAS target \"", libName,
  166. "\" because another target with the same name already exists."));
  167. return false;
  168. }
  169. }
  170. std::string const& aliasedName = *s;
  171. if (mf.IsAlias(aliasedName)) {
  172. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  173. "\" because target \"", aliasedName,
  174. "\" is itself an ALIAS."));
  175. return false;
  176. }
  177. cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
  178. if (!aliasedTarget) {
  179. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  180. "\" because target \"", aliasedName,
  181. "\" does not already exist."));
  182. return false;
  183. }
  184. cmStateEnums::TargetType aliasedType = aliasedTarget->GetType();
  185. if (aliasedType != cmStateEnums::SHARED_LIBRARY &&
  186. aliasedType != cmStateEnums::STATIC_LIBRARY &&
  187. aliasedType != cmStateEnums::MODULE_LIBRARY &&
  188. aliasedType != cmStateEnums::OBJECT_LIBRARY &&
  189. aliasedType != cmStateEnums::INTERFACE_LIBRARY &&
  190. !(aliasedType == cmStateEnums::UNKNOWN_LIBRARY &&
  191. aliasedTarget->IsImported())) {
  192. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  193. "\" because target \"", aliasedName,
  194. "\" is not a library."));
  195. return false;
  196. }
  197. mf.AddAlias(libName, aliasedName,
  198. !aliasedTarget->IsImported() ||
  199. aliasedTarget->IsImportedGloballyVisible());
  200. return true;
  201. }
  202. if (importTarget && excludeFromAll) {
  203. status.SetError("excludeFromAll with IMPORTED target makes no sense.");
  204. return false;
  205. }
  206. /* ideally we should check whether for the linker language of the target
  207. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  208. STATIC. But at this point we know only the name of the target, but not
  209. yet its linker language. */
  210. if ((type == cmStateEnums::SHARED_LIBRARY ||
  211. type == cmStateEnums::MODULE_LIBRARY) &&
  212. !mf.GetState()->GetGlobalPropertyAsBool("TARGET_SUPPORTS_SHARED_LIBS")) {
  213. mf.IssueMessage(
  214. MessageType::AUTHOR_WARNING,
  215. cmStrCat(
  216. "ADD_LIBRARY called with ",
  217. (type == cmStateEnums::SHARED_LIBRARY ? "SHARED" : "MODULE"),
  218. " option but the target platform does not support dynamic linking. ",
  219. "Building a STATIC library instead. This may lead to problems."));
  220. type = cmStateEnums::STATIC_LIBRARY;
  221. }
  222. // Handle imported target creation.
  223. if (importTarget) {
  224. // The IMPORTED signature requires a type to be specified explicitly.
  225. if (!haveSpecifiedType) {
  226. status.SetError("called with IMPORTED argument but no library type.");
  227. return false;
  228. }
  229. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  230. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  231. status.SetError(cmStrCat(
  232. "Invalid name for IMPORTED INTERFACE library target: ", libName));
  233. return false;
  234. }
  235. }
  236. // Make sure the target does not already exist.
  237. if (mf.FindTargetToUse(libName)) {
  238. status.SetError(cmStrCat(
  239. "cannot create imported target \"", libName,
  240. "\" because another target with the same name already exists."));
  241. return false;
  242. }
  243. // Create the imported target.
  244. mf.AddImportedTarget(libName, type, importGlobal);
  245. return true;
  246. }
  247. // A non-imported target may not have UNKNOWN type.
  248. if (type == cmStateEnums::UNKNOWN_LIBRARY) {
  249. mf.IssueMessage(
  250. MessageType::FATAL_ERROR,
  251. "The UNKNOWN library type may be used only for IMPORTED libraries.");
  252. return true;
  253. }
  254. // Enforce name uniqueness.
  255. {
  256. std::string msg;
  257. if (!mf.EnforceUniqueName(libName, msg)) {
  258. status.SetError(msg);
  259. return false;
  260. }
  261. }
  262. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  263. if (!cmGeneratorExpression::IsValidTargetName(libName) ||
  264. libName.find("::") != std::string::npos) {
  265. status.SetError(
  266. cmStrCat("Invalid name for INTERFACE library target: ", libName));
  267. return false;
  268. }
  269. }
  270. std::vector<std::string> srcs(s, args.end());
  271. mf.AddLibrary(libName, type, srcs, excludeFromAll);
  272. return true;
  273. }