cmAddLibraryCommand.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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());
  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());
  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());
  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());
  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());
  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());
  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());
  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());
  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());
  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());
  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 if(type == cmTarget::INTERFACE_LIBRARY && *s == "GLOBAL")
  169. {
  170. cmOStringStream e;
  171. e << "GLOBAL option may only be used with IMPORTED libraries.";
  172. this->SetError(e.str());
  173. return false;
  174. }
  175. else
  176. {
  177. break;
  178. }
  179. }
  180. if (type == cmTarget::INTERFACE_LIBRARY)
  181. {
  182. if (s != args.end())
  183. {
  184. cmOStringStream e;
  185. e << "INTERFACE library requires no source arguments.";
  186. this->SetError(e.str());
  187. return false;
  188. }
  189. if (importGlobal && !importTarget)
  190. {
  191. cmOStringStream e;
  192. e << "INTERFACE library specified as GLOBAL, but not as IMPORTED.";
  193. this->SetError(e.str());
  194. return false;
  195. }
  196. }
  197. bool nameOk = cmGeneratorExpression::IsValidTargetName(libName) &&
  198. !cmGlobalGenerator::IsReservedTarget(libName);
  199. if (nameOk && !importTarget && !isAlias)
  200. {
  201. nameOk = libName.find(":") == std::string::npos;
  202. }
  203. if (!nameOk)
  204. {
  205. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  206. cmOStringStream e;
  207. bool issueMessage = false;
  208. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0037))
  209. {
  210. case cmPolicies::WARN:
  211. if(type != cmTarget::INTERFACE_LIBRARY)
  212. {
  213. e << (this->Makefile->GetPolicies()
  214. ->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. cmOStringStream 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. cmOStringStream 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. cmOStringStream 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. cmOStringStream 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. cmOStringStream 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::STATIC_LIBRARY) &&
  316. (type != cmTarget::OBJECT_LIBRARY) &&
  317. (type != cmTarget::INTERFACE_LIBRARY) &&
  318. (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
  319. "TARGET_SUPPORTS_SHARED_LIBS") == false))
  320. {
  321. cmOStringStream 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. cmOStringStream 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. cmOStringStream 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. cmOStringStream 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. while (s != args.end())
  414. {
  415. srclists.push_back(*s);
  416. ++s;
  417. }
  418. this->Makefile->AddLibrary(libName, type, srclists, excludeFromAll);
  419. return true;
  420. }