cmAddLibraryCommand.cxx 11 KB

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