cmAddLibraryCommand.cxx 10 KB

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