cmAddLibraryCommand.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmAddLibraryCommand.h"
  14. #include "cmake.h"
  15. // cmLibraryCommand
  16. bool cmAddLibraryCommand
  17. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  18. {
  19. if(args.size() < 1 )
  20. {
  21. this->SetError("called with incorrect number of arguments");
  22. return false;
  23. }
  24. // Library type defaults to value of BUILD_SHARED_LIBS, if it exists,
  25. // otherwise it defaults to static library.
  26. cmTarget::TargetType type = cmTarget::SHARED_LIBRARY;
  27. if (cmSystemTools::IsOff(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  28. {
  29. type = cmTarget::STATIC_LIBRARY;
  30. }
  31. bool excludeFromAll = false;
  32. bool importTarget = false;
  33. std::vector<std::string>::const_iterator s = args.begin();
  34. std::string libName = *s;
  35. ++s;
  36. // If the second argument is "SHARED" or "STATIC", then it controls
  37. // the type of library. Otherwise, it is treated as a source or
  38. // source list name. There may be two keyword arguments, check for them
  39. bool haveSpecifiedType = false;
  40. while ( s != args.end() )
  41. {
  42. std::string libType = *s;
  43. if(libType == "STATIC")
  44. {
  45. ++s;
  46. type = cmTarget::STATIC_LIBRARY;
  47. haveSpecifiedType = true;
  48. }
  49. else if(libType == "SHARED")
  50. {
  51. ++s;
  52. type = cmTarget::SHARED_LIBRARY;
  53. haveSpecifiedType = true;
  54. }
  55. else if(libType == "MODULE")
  56. {
  57. ++s;
  58. type = cmTarget::MODULE_LIBRARY;
  59. haveSpecifiedType = true;
  60. }
  61. else if(*s == "EXCLUDE_FROM_ALL")
  62. {
  63. ++s;
  64. excludeFromAll = true;
  65. }
  66. else if(*s == "IMPORTED")
  67. {
  68. ++s;
  69. importTarget = true;
  70. }
  71. else
  72. {
  73. break;
  74. }
  75. }
  76. /* ideally we should check whether for the linker language of the target
  77. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  78. STATIC. But at this point we know only the name of the target, but not
  79. yet its linker language. */
  80. if ((type != cmTarget::STATIC_LIBRARY) &&
  81. (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
  82. "TARGET_SUPPORTS_SHARED_LIBS") == false))
  83. {
  84. std::string msg = "ADD_LIBRARY for library ";
  85. msg += args[0];
  86. msg += " is used with the ";
  87. msg += type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE";
  88. msg += " option, but the target platform supports only STATIC libraries. "
  89. "Building it STATIC instead. This may lead to problems.";
  90. cmSystemTools::Message(msg.c_str() ,"Warning");
  91. type = cmTarget::STATIC_LIBRARY;
  92. }
  93. // The IMPORTED signature requires a type to be specified explicitly.
  94. if(importTarget && !haveSpecifiedType)
  95. {
  96. this->SetError("called with IMPORTED argument but no library type.");
  97. return false;
  98. }
  99. // Handle imported target creation.
  100. if(importTarget)
  101. {
  102. // Make sure the target does not already exist.
  103. if(this->Makefile->FindTargetToUse(libName.c_str()))
  104. {
  105. cmOStringStream e;
  106. e << "cannot create imported target \"" << libName
  107. << "\" because another target with the same name already exists.";
  108. this->SetError(e.str().c_str());
  109. return false;
  110. }
  111. // Create the imported target.
  112. this->Makefile->AddImportedTarget(libName.c_str(), type);
  113. return true;
  114. }
  115. // Enforce name uniqueness.
  116. {
  117. std::string msg;
  118. if(!this->Makefile->EnforceUniqueName(libName, msg))
  119. {
  120. this->SetError(msg.c_str());
  121. return false;
  122. }
  123. }
  124. if (s == args.end())
  125. {
  126. std::string msg = "You have called ADD_LIBRARY for library ";
  127. msg += args[0];
  128. msg += " without any source files. This typically indicates a problem ";
  129. msg += "with your CMakeLists.txt file";
  130. cmSystemTools::Message(msg.c_str() ,"Warning");
  131. }
  132. std::vector<std::string> srclists;
  133. while (s != args.end())
  134. {
  135. srclists.push_back(*s);
  136. ++s;
  137. }
  138. this->Makefile->AddLibrary(libName.c_str(), type, srclists,
  139. excludeFromAll);
  140. return true;
  141. }