cmAddLibraryCommand.cxx 12 KB

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