cmAddLibraryCommand.cxx 13 KB

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