cmAddLibraryCommand.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 << cmPolicies::GetPolicyWarning(cmPolicies::CMP0037) << "\n";
  215. issueMessage = true;
  216. }
  217. case cmPolicies::OLD:
  218. break;
  219. case cmPolicies::NEW:
  220. case cmPolicies::REQUIRED_IF_USED:
  221. case cmPolicies::REQUIRED_ALWAYS:
  222. issueMessage = true;
  223. messageType = cmake::FATAL_ERROR;
  224. }
  225. if (issueMessage)
  226. {
  227. e << "The target name \"" << libName <<
  228. "\" is reserved or not valid for certain "
  229. "CMake features, such as generator expressions, and may result "
  230. "in undefined behavior.";
  231. this->Makefile->IssueMessage(messageType, e.str());
  232. if (messageType == cmake::FATAL_ERROR)
  233. {
  234. return false;
  235. }
  236. }
  237. }
  238. if (isAlias)
  239. {
  240. if(!cmGeneratorExpression::IsValidTargetName(libName))
  241. {
  242. this->SetError("Invalid name for ALIAS: " + libName);
  243. return false;
  244. }
  245. if(excludeFromAll)
  246. {
  247. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  248. return false;
  249. }
  250. if(importTarget || importGlobal)
  251. {
  252. this->SetError("IMPORTED with ALIAS is not allowed.");
  253. return false;
  254. }
  255. if(args.size() != 3)
  256. {
  257. std::ostringstream e;
  258. e << "ALIAS requires exactly one target argument.";
  259. this->SetError(e.str());
  260. return false;
  261. }
  262. const char *aliasedName = s->c_str();
  263. if(this->Makefile->IsAlias(aliasedName))
  264. {
  265. std::ostringstream e;
  266. e << "cannot create ALIAS target \"" << libName
  267. << "\" because target \"" << aliasedName << "\" is itself an ALIAS.";
  268. this->SetError(e.str());
  269. return false;
  270. }
  271. cmTarget *aliasedTarget =
  272. this->Makefile->FindTargetToUse(aliasedName, true);
  273. if(!aliasedTarget)
  274. {
  275. std::ostringstream e;
  276. e << "cannot create ALIAS target \"" << libName
  277. << "\" because target \"" << aliasedName << "\" does not already "
  278. "exist.";
  279. this->SetError(e.str());
  280. return false;
  281. }
  282. cmTarget::TargetType aliasedType = aliasedTarget->GetType();
  283. if(aliasedType != cmTarget::SHARED_LIBRARY
  284. && aliasedType != cmTarget::STATIC_LIBRARY
  285. && aliasedType != cmTarget::MODULE_LIBRARY
  286. && aliasedType != cmTarget::OBJECT_LIBRARY
  287. && aliasedType != cmTarget::INTERFACE_LIBRARY)
  288. {
  289. std::ostringstream e;
  290. e << "cannot create ALIAS target \"" << libName
  291. << "\" because target \"" << aliasedName << "\" is not a library.";
  292. this->SetError(e.str());
  293. return false;
  294. }
  295. if(aliasedTarget->IsImported())
  296. {
  297. std::ostringstream e;
  298. e << "cannot create ALIAS target \"" << libName
  299. << "\" because target \"" << aliasedName << "\" is IMPORTED.";
  300. this->SetError(e.str());
  301. return false;
  302. }
  303. this->Makefile->AddAlias(libName, aliasedTarget);
  304. return true;
  305. }
  306. if(importTarget && excludeFromAll)
  307. {
  308. this->SetError("excludeFromAll with IMPORTED target makes no sense.");
  309. return false;
  310. }
  311. /* ideally we should check whether for the linker language of the target
  312. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  313. STATIC. But at this point we know only the name of the target, but not
  314. yet its linker language. */
  315. if ((type == cmTarget::SHARED_LIBRARY ||
  316. type == cmTarget::MODULE_LIBRARY) &&
  317. (this->Makefile->GetState()->GetGlobalPropertyAsBool(
  318. "TARGET_SUPPORTS_SHARED_LIBS") == false))
  319. {
  320. std::ostringstream w;
  321. w <<
  322. "ADD_LIBRARY called with " <<
  323. (type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE") <<
  324. " option but the target platform does not support dynamic linking. "
  325. "Building a STATIC library instead. This may lead to problems.";
  326. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  327. type = cmTarget::STATIC_LIBRARY;
  328. }
  329. // Handle imported target creation.
  330. if(importTarget)
  331. {
  332. // The IMPORTED signature requires a type to be specified explicitly.
  333. if (!haveSpecifiedType)
  334. {
  335. this->SetError("called with IMPORTED argument but no library type.");
  336. return false;
  337. }
  338. if(type == cmTarget::OBJECT_LIBRARY)
  339. {
  340. this->Makefile->IssueMessage(
  341. cmake::FATAL_ERROR,
  342. "The OBJECT library type may not be used for IMPORTED libraries."
  343. );
  344. return true;
  345. }
  346. if(type == cmTarget::INTERFACE_LIBRARY)
  347. {
  348. if (!cmGeneratorExpression::IsValidTargetName(libName))
  349. {
  350. std::ostringstream e;
  351. e << "Invalid name for IMPORTED INTERFACE library target: " << libName;
  352. this->SetError(e.str());
  353. return false;
  354. }
  355. }
  356. // Make sure the target does not already exist.
  357. if(this->Makefile->FindTargetToUse(libName))
  358. {
  359. std::ostringstream e;
  360. e << "cannot create imported target \"" << libName
  361. << "\" because another target with the same name already exists.";
  362. this->SetError(e.str());
  363. return false;
  364. }
  365. // Create the imported target.
  366. this->Makefile->AddImportedTarget(libName, type, importGlobal);
  367. return true;
  368. }
  369. // A non-imported target may not have UNKNOWN type.
  370. if(type == cmTarget::UNKNOWN_LIBRARY)
  371. {
  372. this->Makefile->IssueMessage(
  373. cmake::FATAL_ERROR,
  374. "The UNKNOWN library type may be used only for IMPORTED libraries."
  375. );
  376. return true;
  377. }
  378. // Enforce name uniqueness.
  379. {
  380. std::string msg;
  381. if(!this->Makefile->EnforceUniqueName(libName, msg))
  382. {
  383. this->SetError(msg);
  384. return false;
  385. }
  386. }
  387. std::vector<std::string> srclists;
  388. if(type == cmTarget::INTERFACE_LIBRARY)
  389. {
  390. if (!cmGeneratorExpression::IsValidTargetName(libName)
  391. || libName.find("::") != std::string::npos)
  392. {
  393. std::ostringstream e;
  394. e << "Invalid name for INTERFACE library target: " << libName;
  395. this->SetError(e.str());
  396. return false;
  397. }
  398. this->Makefile->AddLibrary(libName,
  399. type,
  400. srclists,
  401. excludeFromAll);
  402. return true;
  403. }
  404. if (s == args.end())
  405. {
  406. std::string msg = "You have called ADD_LIBRARY for library ";
  407. msg += args[0];
  408. msg += " without any source files. This typically indicates a problem ";
  409. msg += "with your CMakeLists.txt file";
  410. cmSystemTools::Message(msg.c_str() ,"Warning");
  411. }
  412. srclists.insert(srclists.end(), s, args.end());
  413. this->Makefile->AddLibrary(libName, type, srclists, excludeFromAll);
  414. return true;
  415. }