cmAddLibraryCommand.cxx 13 KB

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