cmAddLibraryCommand.cxx 12 KB

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