cmAddLibraryCommand.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. std::ostringstream 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. std::ostringstream 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. std::ostringstream 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. std::ostringstream 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. std::ostringstream 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. std::ostringstream 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. std::ostringstream e;
  124. e << "INTERFACE library specified with conflicting/multiple types.";
  125. this->SetError(e.str());
  126. return false;
  127. }
  128. if (isAlias)
  129. {
  130. std::ostringstream e;
  131. e << "INTERFACE library specified with conflicting ALIAS type.";
  132. this->SetError(e.str());
  133. return false;
  134. }
  135. if (excludeFromAll)
  136. {
  137. std::ostringstream 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. std::ostringstream 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. std::ostringstream 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. std::ostringstream e;
  185. e << "INTERFACE library requires no source arguments.";
  186. this->SetError(e.str());
  187. return false;
  188. }
  189. if (importGlobal && !importTarget)
  190. {
  191. std::ostringstream 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. std::ostringstream 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. 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->GetCMakeInstance()->GetPropertyAsBool(
  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. while (s != args.end())
  413. {
  414. srclists.push_back(*s);
  415. ++s;
  416. }
  417. this->Makefile->AddLibrary(libName, type, srclists, excludeFromAll);
  418. return true;
  419. }