cmAddLibraryCommand.cxx 4.8 KB

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