cmAddLibraryCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <sstream>
  5. #include "cmAlgorithms.h"
  6. #include "cmExecutionStatus.h"
  7. #include "cmGeneratorExpression.h"
  8. #include "cmGlobalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmMessageType.h"
  11. #include "cmState.h"
  12. #include "cmStateTypes.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmTarget.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. std::vector<std::string>::const_iterator 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. std::ostringstream e;
  45. e << "INTERFACE library specified with conflicting STATIC type.";
  46. status.SetError(e.str());
  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. std::ostringstream e;
  55. e << "INTERFACE library specified with conflicting SHARED type.";
  56. status.SetError(e.str());
  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. std::ostringstream e;
  65. e << "INTERFACE library specified with conflicting MODULE type.";
  66. status.SetError(e.str());
  67. return false;
  68. }
  69. ++s;
  70. type = cmStateEnums::MODULE_LIBRARY;
  71. haveSpecifiedType = true;
  72. } else if (libType == "OBJECT") {
  73. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  74. std::ostringstream e;
  75. e << "INTERFACE library specified with conflicting OBJECT type.";
  76. status.SetError(e.str());
  77. return false;
  78. }
  79. ++s;
  80. type = cmStateEnums::OBJECT_LIBRARY;
  81. haveSpecifiedType = true;
  82. } else if (libType == "UNKNOWN") {
  83. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  84. std::ostringstream e;
  85. e << "INTERFACE library specified with conflicting UNKNOWN type.";
  86. status.SetError(e.str());
  87. return false;
  88. }
  89. ++s;
  90. type = cmStateEnums::UNKNOWN_LIBRARY;
  91. haveSpecifiedType = true;
  92. } else if (libType == "ALIAS") {
  93. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  94. std::ostringstream e;
  95. e << "INTERFACE library specified with conflicting ALIAS type.";
  96. status.SetError(e.str());
  97. return false;
  98. }
  99. ++s;
  100. isAlias = true;
  101. } else if (libType == "INTERFACE") {
  102. if (haveSpecifiedType) {
  103. std::ostringstream e;
  104. e << "INTERFACE library specified with conflicting/multiple types.";
  105. status.SetError(e.str());
  106. return false;
  107. }
  108. if (isAlias) {
  109. std::ostringstream e;
  110. e << "INTERFACE library specified with conflicting ALIAS type.";
  111. status.SetError(e.str());
  112. return false;
  113. }
  114. if (excludeFromAll) {
  115. std::ostringstream e;
  116. e << "INTERFACE library may not be used with EXCLUDE_FROM_ALL.";
  117. status.SetError(e.str());
  118. return false;
  119. }
  120. ++s;
  121. type = cmStateEnums::INTERFACE_LIBRARY;
  122. haveSpecifiedType = true;
  123. } else if (*s == "EXCLUDE_FROM_ALL") {
  124. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  125. std::ostringstream e;
  126. e << "INTERFACE library may not be used with EXCLUDE_FROM_ALL.";
  127. status.SetError(e.str());
  128. return false;
  129. }
  130. ++s;
  131. excludeFromAll = true;
  132. } else if (*s == "IMPORTED") {
  133. ++s;
  134. importTarget = true;
  135. } else if (importTarget && *s == "GLOBAL") {
  136. ++s;
  137. importGlobal = true;
  138. } else if (type == cmStateEnums::INTERFACE_LIBRARY && *s == "GLOBAL") {
  139. std::ostringstream e;
  140. e << "GLOBAL option may only be used with IMPORTED libraries.";
  141. status.SetError(e.str());
  142. return false;
  143. } else {
  144. break;
  145. }
  146. }
  147. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  148. if (s != args.end()) {
  149. std::ostringstream e;
  150. e << "INTERFACE library requires no source arguments.";
  151. status.SetError(e.str());
  152. return false;
  153. }
  154. if (importGlobal && !importTarget) {
  155. std::ostringstream e;
  156. e << "INTERFACE library specified as GLOBAL, but not as IMPORTED.";
  157. status.SetError(e.str());
  158. return false;
  159. }
  160. }
  161. bool nameOk = cmGeneratorExpression::IsValidTargetName(libName) &&
  162. !cmGlobalGenerator::IsReservedTarget(libName);
  163. if (nameOk && !importTarget && !isAlias) {
  164. nameOk = libName.find(':') == std::string::npos;
  165. }
  166. if (!nameOk && !mf.CheckCMP0037(libName, type)) {
  167. return false;
  168. }
  169. if (isAlias) {
  170. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  171. status.SetError("Invalid name for ALIAS: " + libName);
  172. return false;
  173. }
  174. if (excludeFromAll) {
  175. status.SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  176. return false;
  177. }
  178. if (importTarget || importGlobal) {
  179. status.SetError("IMPORTED with ALIAS is not allowed.");
  180. return false;
  181. }
  182. if (args.size() != 3) {
  183. std::ostringstream e;
  184. e << "ALIAS requires exactly one target argument.";
  185. status.SetError(e.str());
  186. return false;
  187. }
  188. std::string const& aliasedName = *s;
  189. if (mf.IsAlias(aliasedName)) {
  190. std::ostringstream e;
  191. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  192. << aliasedName << "\" is itself an ALIAS.";
  193. status.SetError(e.str());
  194. return false;
  195. }
  196. cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
  197. if (!aliasedTarget) {
  198. std::ostringstream e;
  199. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  200. << aliasedName
  201. << "\" does not already "
  202. "exist.";
  203. status.SetError(e.str());
  204. return false;
  205. }
  206. cmStateEnums::TargetType aliasedType = aliasedTarget->GetType();
  207. if (aliasedType != cmStateEnums::SHARED_LIBRARY &&
  208. aliasedType != cmStateEnums::STATIC_LIBRARY &&
  209. aliasedType != cmStateEnums::MODULE_LIBRARY &&
  210. aliasedType != cmStateEnums::OBJECT_LIBRARY &&
  211. aliasedType != cmStateEnums::INTERFACE_LIBRARY &&
  212. !(aliasedType == cmStateEnums::UNKNOWN_LIBRARY &&
  213. aliasedTarget->IsImported())) {
  214. std::ostringstream e;
  215. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  216. << aliasedName << "\" is not a library.";
  217. status.SetError(e.str());
  218. return false;
  219. }
  220. if (aliasedTarget->IsImported() &&
  221. !aliasedTarget->IsImportedGloballyVisible()) {
  222. std::ostringstream e;
  223. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  224. << aliasedName << "\" is imported but not globally visible.";
  225. status.SetError(e.str());
  226. return false;
  227. }
  228. mf.AddAlias(libName, aliasedName);
  229. return true;
  230. }
  231. if (importTarget && excludeFromAll) {
  232. status.SetError("excludeFromAll with IMPORTED target makes no sense.");
  233. return false;
  234. }
  235. /* ideally we should check whether for the linker language of the target
  236. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  237. STATIC. But at this point we know only the name of the target, but not
  238. yet its linker language. */
  239. if ((type == cmStateEnums::SHARED_LIBRARY ||
  240. type == cmStateEnums::MODULE_LIBRARY) &&
  241. !mf.GetState()->GetGlobalPropertyAsBool("TARGET_SUPPORTS_SHARED_LIBS")) {
  242. std::ostringstream w;
  243. w << "ADD_LIBRARY called with "
  244. << (type == cmStateEnums::SHARED_LIBRARY ? "SHARED" : "MODULE")
  245. << " option but the target platform does not support dynamic linking. "
  246. "Building a STATIC library instead. This may lead to problems.";
  247. mf.IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  248. type = cmStateEnums::STATIC_LIBRARY;
  249. }
  250. // Handle imported target creation.
  251. if (importTarget) {
  252. // The IMPORTED signature requires a type to be specified explicitly.
  253. if (!haveSpecifiedType) {
  254. status.SetError("called with IMPORTED argument but no library type.");
  255. return false;
  256. }
  257. if (type == cmStateEnums::OBJECT_LIBRARY) {
  258. std::string reason;
  259. if (!mf.GetGlobalGenerator()->HasKnownObjectFileLocation(&reason)) {
  260. mf.IssueMessage(
  261. MessageType::FATAL_ERROR,
  262. "The OBJECT library type may not be used for IMPORTED libraries" +
  263. reason + ".");
  264. return true;
  265. }
  266. }
  267. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  268. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  269. std::ostringstream e;
  270. e << "Invalid name for IMPORTED INTERFACE library target: " << libName;
  271. status.SetError(e.str());
  272. return false;
  273. }
  274. }
  275. // Make sure the target does not already exist.
  276. if (mf.FindTargetToUse(libName)) {
  277. std::ostringstream e;
  278. e << "cannot create imported target \"" << libName
  279. << "\" because another target with the same name already exists.";
  280. status.SetError(e.str());
  281. return false;
  282. }
  283. // Create the imported target.
  284. mf.AddImportedTarget(libName, type, importGlobal);
  285. return true;
  286. }
  287. // A non-imported target may not have UNKNOWN type.
  288. if (type == cmStateEnums::UNKNOWN_LIBRARY) {
  289. mf.IssueMessage(
  290. MessageType::FATAL_ERROR,
  291. "The UNKNOWN library type may be used only for IMPORTED libraries.");
  292. return true;
  293. }
  294. // Enforce name uniqueness.
  295. {
  296. std::string msg;
  297. if (!mf.EnforceUniqueName(libName, msg)) {
  298. status.SetError(msg);
  299. return false;
  300. }
  301. }
  302. std::vector<std::string> srclists;
  303. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  304. if (!cmGeneratorExpression::IsValidTargetName(libName) ||
  305. libName.find("::") != std::string::npos) {
  306. std::ostringstream e;
  307. e << "Invalid name for INTERFACE library target: " << libName;
  308. status.SetError(e.str());
  309. return false;
  310. }
  311. mf.AddLibrary(libName, type, srclists, excludeFromAll);
  312. return true;
  313. }
  314. cmAppend(srclists, s, args.end());
  315. mf.AddLibrary(libName, type, srclists, excludeFromAll);
  316. return true;
  317. }