cmAddLibraryCommand.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. while ( s != args.end() )
  39. {
  40. std::string libType = *s;
  41. if(libType == "STATIC")
  42. {
  43. ++s;
  44. type = cmTarget::STATIC_LIBRARY;
  45. haveSpecifiedType = true;
  46. }
  47. else if(libType == "SHARED")
  48. {
  49. ++s;
  50. type = cmTarget::SHARED_LIBRARY;
  51. haveSpecifiedType = true;
  52. }
  53. else if(libType == "MODULE")
  54. {
  55. ++s;
  56. type = cmTarget::MODULE_LIBRARY;
  57. haveSpecifiedType = true;
  58. }
  59. else if(libType == "OBJECT")
  60. {
  61. ++s;
  62. type = cmTarget::OBJECT_LIBRARY;
  63. haveSpecifiedType = true;
  64. }
  65. else if(libType == "UNKNOWN")
  66. {
  67. ++s;
  68. type = cmTarget::UNKNOWN_LIBRARY;
  69. haveSpecifiedType = true;
  70. }
  71. else if(*s == "EXCLUDE_FROM_ALL")
  72. {
  73. ++s;
  74. excludeFromAll = true;
  75. }
  76. else if(*s == "IMPORTED")
  77. {
  78. ++s;
  79. importTarget = true;
  80. }
  81. else if(importTarget && *s == "GLOBAL")
  82. {
  83. ++s;
  84. importGlobal = true;
  85. }
  86. else
  87. {
  88. break;
  89. }
  90. }
  91. /* ideally we should check whether for the linker language of the target
  92. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  93. STATIC. But at this point we know only the name of the target, but not
  94. yet its linker language. */
  95. if ((type != cmTarget::STATIC_LIBRARY) &&
  96. (type != cmTarget::OBJECT_LIBRARY) &&
  97. (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
  98. "TARGET_SUPPORTS_SHARED_LIBS") == false))
  99. {
  100. cmOStringStream w;
  101. w <<
  102. "ADD_LIBRARY called with " <<
  103. (type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE") <<
  104. " option but the target platform does not support dynamic linking. "
  105. "Building a STATIC library instead. This may lead to problems.";
  106. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  107. type = cmTarget::STATIC_LIBRARY;
  108. }
  109. // Handle imported target creation.
  110. if(importTarget)
  111. {
  112. // The IMPORTED signature requires a type to be specified explicitly.
  113. if (!haveSpecifiedType)
  114. {
  115. this->SetError("called with IMPORTED argument but no library type.");
  116. return false;
  117. }
  118. if(type == cmTarget::OBJECT_LIBRARY)
  119. {
  120. this->Makefile->IssueMessage(
  121. cmake::FATAL_ERROR,
  122. "The OBJECT library type may not be used for IMPORTED libraries."
  123. );
  124. return true;
  125. }
  126. // Make sure the target does not already exist.
  127. if(this->Makefile->FindTargetToUse(libName.c_str()))
  128. {
  129. cmOStringStream e;
  130. e << "cannot create imported target \"" << libName
  131. << "\" because another target with the same name already exists.";
  132. this->SetError(e.str().c_str());
  133. return false;
  134. }
  135. // Create the imported target.
  136. this->Makefile->AddImportedTarget(libName.c_str(), type, importGlobal);
  137. return true;
  138. }
  139. // A non-imported target may not have UNKNOWN type.
  140. if(type == cmTarget::UNKNOWN_LIBRARY)
  141. {
  142. this->Makefile->IssueMessage(
  143. cmake::FATAL_ERROR,
  144. "The UNKNOWN library type may be used only for IMPORTED libraries."
  145. );
  146. return true;
  147. }
  148. // Enforce name uniqueness.
  149. {
  150. std::string msg;
  151. if(!this->Makefile->EnforceUniqueName(libName, msg))
  152. {
  153. this->SetError(msg.c_str());
  154. return false;
  155. }
  156. }
  157. if (s == args.end())
  158. {
  159. std::string msg = "You have called ADD_LIBRARY for library ";
  160. msg += args[0];
  161. msg += " without any source files. This typically indicates a problem ";
  162. msg += "with your CMakeLists.txt file";
  163. cmSystemTools::Message(msg.c_str() ,"Warning");
  164. }
  165. std::vector<std::string> srclists;
  166. while (s != args.end())
  167. {
  168. srclists.push_back(*s);
  169. ++s;
  170. }
  171. this->Makefile->AddLibrary(libName.c_str(), type, srclists, excludeFromAll);
  172. return true;
  173. }