cmAddLibraryCommand.cxx 5.0 KB

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