cmAddLibraryCommand.cxx 9.5 KB

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