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