cmAddLibraryCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 "cmState.h"
  5. #include "cmStateTypes.h"
  6. #include "cmake.h"
  7. // cmLibraryCommand
  8. bool cmAddLibraryCommand::InitialPass(std::vector<std::string> const& args,
  9. cmExecutionStatus&)
  10. {
  11. if (args.empty()) {
  12. this->SetError("called with incorrect number of arguments");
  13. return false;
  14. }
  15. // Library type defaults to value of BUILD_SHARED_LIBS, if it exists,
  16. // otherwise it defaults to static library.
  17. cmStateEnums::TargetType type = cmStateEnums::SHARED_LIBRARY;
  18. if (cmSystemTools::IsOff(
  19. this->Makefile->GetDefinition("BUILD_SHARED_LIBS"))) {
  20. type = cmStateEnums::STATIC_LIBRARY;
  21. }
  22. bool excludeFromAll = false;
  23. bool importTarget = false;
  24. bool importGlobal = false;
  25. std::vector<std::string>::const_iterator s = args.begin();
  26. std::string libName = *s;
  27. ++s;
  28. // If the second argument is "SHARED" or "STATIC", then it controls
  29. // the type of library. Otherwise, it is treated as a source or
  30. // source list name. There may be two keyword arguments, check for them
  31. bool haveSpecifiedType = false;
  32. bool isAlias = false;
  33. while (s != args.end()) {
  34. std::string libType = *s;
  35. if (libType == "STATIC") {
  36. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  37. std::ostringstream e;
  38. e << "INTERFACE library specified with conflicting STATIC type.";
  39. this->SetError(e.str());
  40. return false;
  41. }
  42. ++s;
  43. type = cmStateEnums::STATIC_LIBRARY;
  44. haveSpecifiedType = true;
  45. } else if (libType == "SHARED") {
  46. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  47. std::ostringstream e;
  48. e << "INTERFACE library specified with conflicting SHARED type.";
  49. this->SetError(e.str());
  50. return false;
  51. }
  52. ++s;
  53. type = cmStateEnums::SHARED_LIBRARY;
  54. haveSpecifiedType = true;
  55. } else if (libType == "MODULE") {
  56. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  57. std::ostringstream e;
  58. e << "INTERFACE library specified with conflicting MODULE type.";
  59. this->SetError(e.str());
  60. return false;
  61. }
  62. ++s;
  63. type = cmStateEnums::MODULE_LIBRARY;
  64. haveSpecifiedType = true;
  65. } else if (libType == "OBJECT") {
  66. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  67. std::ostringstream e;
  68. e << "INTERFACE library specified with conflicting OBJECT type.";
  69. this->SetError(e.str());
  70. return false;
  71. }
  72. ++s;
  73. type = cmStateEnums::OBJECT_LIBRARY;
  74. haveSpecifiedType = true;
  75. } else if (libType == "UNKNOWN") {
  76. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  77. std::ostringstream e;
  78. e << "INTERFACE library specified with conflicting UNKNOWN type.";
  79. this->SetError(e.str());
  80. return false;
  81. }
  82. ++s;
  83. type = cmStateEnums::UNKNOWN_LIBRARY;
  84. haveSpecifiedType = true;
  85. } else if (libType == "ALIAS") {
  86. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  87. std::ostringstream e;
  88. e << "INTERFACE library specified with conflicting ALIAS type.";
  89. this->SetError(e.str());
  90. return false;
  91. }
  92. ++s;
  93. isAlias = true;
  94. } else if (libType == "INTERFACE") {
  95. if (haveSpecifiedType) {
  96. std::ostringstream e;
  97. e << "INTERFACE library specified with conflicting/multiple types.";
  98. this->SetError(e.str());
  99. return false;
  100. }
  101. if (isAlias) {
  102. std::ostringstream e;
  103. e << "INTERFACE library specified with conflicting ALIAS type.";
  104. this->SetError(e.str());
  105. return false;
  106. }
  107. if (excludeFromAll) {
  108. std::ostringstream e;
  109. e << "INTERFACE library may not be used with EXCLUDE_FROM_ALL.";
  110. this->SetError(e.str());
  111. return false;
  112. }
  113. ++s;
  114. type = cmStateEnums::INTERFACE_LIBRARY;
  115. haveSpecifiedType = true;
  116. } else if (*s == "EXCLUDE_FROM_ALL") {
  117. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  118. std::ostringstream e;
  119. e << "INTERFACE library may not be used with EXCLUDE_FROM_ALL.";
  120. this->SetError(e.str());
  121. return false;
  122. }
  123. ++s;
  124. excludeFromAll = true;
  125. } else if (*s == "IMPORTED") {
  126. ++s;
  127. importTarget = true;
  128. } else if (importTarget && *s == "GLOBAL") {
  129. ++s;
  130. importGlobal = true;
  131. } else if (type == cmStateEnums::INTERFACE_LIBRARY && *s == "GLOBAL") {
  132. std::ostringstream e;
  133. e << "GLOBAL option may only be used with IMPORTED libraries.";
  134. this->SetError(e.str());
  135. return false;
  136. } else {
  137. break;
  138. }
  139. }
  140. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  141. if (s != args.end()) {
  142. std::ostringstream e;
  143. e << "INTERFACE library requires no source arguments.";
  144. this->SetError(e.str());
  145. return false;
  146. }
  147. if (importGlobal && !importTarget) {
  148. std::ostringstream e;
  149. e << "INTERFACE library specified as GLOBAL, but not as IMPORTED.";
  150. this->SetError(e.str());
  151. return false;
  152. }
  153. }
  154. bool nameOk = cmGeneratorExpression::IsValidTargetName(libName) &&
  155. !cmGlobalGenerator::IsReservedTarget(libName);
  156. if (nameOk && !importTarget && !isAlias) {
  157. nameOk = libName.find(":") == std::string::npos;
  158. }
  159. if (!nameOk) {
  160. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  161. std::ostringstream e;
  162. bool issueMessage = false;
  163. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0037)) {
  164. case cmPolicies::WARN:
  165. if (type != cmStateEnums::INTERFACE_LIBRARY) {
  166. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0037) << "\n";
  167. issueMessage = true;
  168. }
  169. case cmPolicies::OLD:
  170. break;
  171. case cmPolicies::NEW:
  172. case cmPolicies::REQUIRED_IF_USED:
  173. case cmPolicies::REQUIRED_ALWAYS:
  174. issueMessage = true;
  175. messageType = cmake::FATAL_ERROR;
  176. }
  177. if (issueMessage) {
  178. e << "The target name \"" << libName
  179. << "\" is reserved or not valid for certain "
  180. "CMake features, such as generator expressions, and may result "
  181. "in undefined behavior.";
  182. this->Makefile->IssueMessage(messageType, e.str());
  183. if (messageType == cmake::FATAL_ERROR) {
  184. return false;
  185. }
  186. }
  187. }
  188. if (isAlias) {
  189. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  190. this->SetError("Invalid name for ALIAS: " + libName);
  191. return false;
  192. }
  193. if (excludeFromAll) {
  194. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  195. return false;
  196. }
  197. if (importTarget || importGlobal) {
  198. this->SetError("IMPORTED with ALIAS is not allowed.");
  199. return false;
  200. }
  201. if (args.size() != 3) {
  202. std::ostringstream e;
  203. e << "ALIAS requires exactly one target argument.";
  204. this->SetError(e.str());
  205. return false;
  206. }
  207. const char* aliasedName = s->c_str();
  208. if (this->Makefile->IsAlias(aliasedName)) {
  209. std::ostringstream e;
  210. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  211. << aliasedName << "\" is itself an ALIAS.";
  212. this->SetError(e.str());
  213. return false;
  214. }
  215. cmTarget* aliasedTarget =
  216. this->Makefile->FindTargetToUse(aliasedName, true);
  217. if (!aliasedTarget) {
  218. std::ostringstream e;
  219. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  220. << aliasedName << "\" does not already "
  221. "exist.";
  222. this->SetError(e.str());
  223. return false;
  224. }
  225. cmStateEnums::TargetType aliasedType = aliasedTarget->GetType();
  226. if (aliasedType != cmStateEnums::SHARED_LIBRARY &&
  227. aliasedType != cmStateEnums::STATIC_LIBRARY &&
  228. aliasedType != cmStateEnums::MODULE_LIBRARY &&
  229. aliasedType != cmStateEnums::OBJECT_LIBRARY &&
  230. aliasedType != cmStateEnums::INTERFACE_LIBRARY) {
  231. std::ostringstream e;
  232. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  233. << aliasedName << "\" is not a library.";
  234. this->SetError(e.str());
  235. return false;
  236. }
  237. if (aliasedTarget->IsImported()) {
  238. std::ostringstream e;
  239. e << "cannot create ALIAS target \"" << libName << "\" because target \""
  240. << aliasedName << "\" is IMPORTED.";
  241. this->SetError(e.str());
  242. return false;
  243. }
  244. this->Makefile->AddAlias(libName, aliasedName);
  245. return true;
  246. }
  247. if (importTarget && excludeFromAll) {
  248. this->SetError("excludeFromAll with IMPORTED target makes no sense.");
  249. return false;
  250. }
  251. /* ideally we should check whether for the linker language of the target
  252. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  253. STATIC. But at this point we know only the name of the target, but not
  254. yet its linker language. */
  255. if ((type == cmStateEnums::SHARED_LIBRARY ||
  256. type == cmStateEnums::MODULE_LIBRARY) &&
  257. (this->Makefile->GetState()->GetGlobalPropertyAsBool(
  258. "TARGET_SUPPORTS_SHARED_LIBS") == false)) {
  259. std::ostringstream w;
  260. w << "ADD_LIBRARY called with "
  261. << (type == cmStateEnums::SHARED_LIBRARY ? "SHARED" : "MODULE")
  262. << " option but the target platform does not support dynamic linking. "
  263. "Building a STATIC library instead. This may lead to problems.";
  264. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  265. type = cmStateEnums::STATIC_LIBRARY;
  266. }
  267. // Handle imported target creation.
  268. if (importTarget) {
  269. // The IMPORTED signature requires a type to be specified explicitly.
  270. if (!haveSpecifiedType) {
  271. this->SetError("called with IMPORTED argument but no library type.");
  272. return false;
  273. }
  274. if (type == cmStateEnums::OBJECT_LIBRARY) {
  275. this->Makefile->IssueMessage(
  276. cmake::FATAL_ERROR,
  277. "The OBJECT library type may not be used for IMPORTED libraries.");
  278. return true;
  279. }
  280. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  281. if (!cmGeneratorExpression::IsValidTargetName(libName)) {
  282. std::ostringstream e;
  283. e << "Invalid name for IMPORTED INTERFACE library target: " << libName;
  284. this->SetError(e.str());
  285. return false;
  286. }
  287. }
  288. // Make sure the target does not already exist.
  289. if (this->Makefile->FindTargetToUse(libName)) {
  290. std::ostringstream e;
  291. e << "cannot create imported target \"" << libName
  292. << "\" because another target with the same name already exists.";
  293. this->SetError(e.str());
  294. return false;
  295. }
  296. // Create the imported target.
  297. this->Makefile->AddImportedTarget(libName, type, importGlobal);
  298. return true;
  299. }
  300. // A non-imported target may not have UNKNOWN type.
  301. if (type == cmStateEnums::UNKNOWN_LIBRARY) {
  302. this->Makefile->IssueMessage(
  303. cmake::FATAL_ERROR,
  304. "The UNKNOWN library type may be used only for IMPORTED libraries.");
  305. return true;
  306. }
  307. // Enforce name uniqueness.
  308. {
  309. std::string msg;
  310. if (!this->Makefile->EnforceUniqueName(libName, msg)) {
  311. this->SetError(msg);
  312. return false;
  313. }
  314. }
  315. std::vector<std::string> srclists;
  316. if (type == cmStateEnums::INTERFACE_LIBRARY) {
  317. if (!cmGeneratorExpression::IsValidTargetName(libName) ||
  318. libName.find("::") != std::string::npos) {
  319. std::ostringstream e;
  320. e << "Invalid name for INTERFACE library target: " << libName;
  321. this->SetError(e.str());
  322. return false;
  323. }
  324. this->Makefile->AddLibrary(libName, type, srclists, excludeFromAll);
  325. return true;
  326. }
  327. if (s == args.end()) {
  328. std::string msg = "You have called ADD_LIBRARY for library ";
  329. msg += args[0];
  330. msg += " without any source files. This typically indicates a problem ";
  331. msg += "with your CMakeLists.txt file";
  332. cmSystemTools::Message(msg.c_str(), "Warning");
  333. }
  334. srclists.insert(srclists.end(), s, args.end());
  335. this->Makefile->AddLibrary(libName, type, srclists, excludeFromAll);
  336. return true;
  337. }