cmAddLibraryCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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) {
  143. mf.IssueInvalidTargetNameError(libName);
  144. return false;
  145. }
  146. if (isAlias) {
  147. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  148. status.SetError("Invalid name for ALIAS: " + libName);
  149. return false;
  150. }
  151. if (excludeFromAll) {
  152. status.SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  153. return false;
  154. }
  155. if (importTarget || importGlobal) {
  156. status.SetError("IMPORTED with ALIAS is not allowed.");
  157. return false;
  158. }
  159. if (args.size() != 3) {
  160. status.SetError("ALIAS requires exactly one target argument.");
  161. return false;
  162. }
  163. if (mf.GetPolicyStatus(cmPolicies::CMP0107) == cmPolicies::NEW) {
  164. // Make sure the target does not already exist.
  165. if (mf.FindTargetToUse(libName)) {
  166. status.SetError(cmStrCat(
  167. "cannot create ALIAS target \"", libName,
  168. "\" because another target with the same name already exists."));
  169. return false;
  170. }
  171. }
  172. std::string const& aliasedName = *s;
  173. if (mf.IsAlias(aliasedName)) {
  174. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  175. "\" because target \"", aliasedName,
  176. "\" is itself an ALIAS."));
  177. return false;
  178. }
  179. cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
  180. if (!aliasedTarget) {
  181. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  182. "\" because target \"", aliasedName,
  183. "\" does not already exist."));
  184. return false;
  185. }
  186. cmStateEnums::TargetType aliasedType = aliasedTarget->GetType();
  187. if (aliasedType != cmStateEnums::SHARED_LIBRARY &&
  188. aliasedType != cmStateEnums::STATIC_LIBRARY &&
  189. aliasedType != cmStateEnums::MODULE_LIBRARY &&
  190. aliasedType != cmStateEnums::OBJECT_LIBRARY &&
  191. aliasedType != cmStateEnums::INTERFACE_LIBRARY &&
  192. !(aliasedType == cmStateEnums::UNKNOWN_LIBRARY &&
  193. aliasedTarget->IsImported())) {
  194. status.SetError(cmStrCat("cannot create ALIAS target \"", libName,
  195. "\" because target \"", aliasedName,
  196. "\" is not a library."));
  197. return false;
  198. }
  199. mf.AddAlias(libName, aliasedName,
  200. !aliasedTarget->IsImported() ||
  201. aliasedTarget->IsImportedGloballyVisible());
  202. return true;
  203. }
  204. if (importTarget && excludeFromAll) {
  205. status.SetError("excludeFromAll with IMPORTED target makes no sense.");
  206. return false;
  207. }
  208. /* ideally we should check whether for the linker language of the target
  209. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  210. STATIC. But at this point we know only the name of the target, but not
  211. yet its linker language. */
  212. if ((type == cmStateEnums::SHARED_LIBRARY ||
  213. type == cmStateEnums::MODULE_LIBRARY) &&
  214. !mf.GetState()->GetGlobalPropertyAsBool("TARGET_SUPPORTS_SHARED_LIBS")) {
  215. switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0164)) {
  216. case cmPolicies::WARN:
  217. mf.IssueMessage(
  218. MessageType::AUTHOR_WARNING,
  219. cmStrCat(
  220. "ADD_LIBRARY called with ",
  221. (type == cmStateEnums::SHARED_LIBRARY ? "SHARED" : "MODULE"),
  222. " option but the target platform does not support dynamic "
  223. "linking. ",
  224. "Building a STATIC library instead. This may lead to problems."));
  225. CM_FALLTHROUGH;
  226. case cmPolicies::OLD:
  227. type = cmStateEnums::STATIC_LIBRARY;
  228. break;
  229. case cmPolicies::NEW:
  230. mf.IssueMessage(
  231. MessageType::FATAL_ERROR,
  232. cmStrCat(
  233. "ADD_LIBRARY called with ",
  234. (type == cmStateEnums::SHARED_LIBRARY ? "SHARED" : "MODULE"),
  235. " option but the target platform does not support dynamic "
  236. "linking."));
  237. cmSystemTools::SetFatalErrorOccurred();
  238. return false;
  239. default:
  240. break;
  241. }
  242. }
  243. // Handle imported target creation.
  244. if (importTarget) {
  245. // The IMPORTED signature requires a type to be specified explicitly.
  246. if (!haveSpecifiedType) {
  247. status.SetError("called with IMPORTED argument but no library type.");
  248. return false;
  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. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  284. if (!cmGeneratorExpression::IsValidTargetName(libName) ||
  285. libName.find("::") != std::string::npos) {
  286. status.SetError(
  287. cmStrCat("Invalid name for INTERFACE library target: ", libName));
  288. return false;
  289. }
  290. }
  291. std::vector<std::string> srcs(s, args.end());
  292. mf.AddLibrary(libName, type, srcs, excludeFromAll);
  293. return true;
  294. }