cmAddLibraryCommand.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() < 1 )
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. // Library type defaults to value of BUILD_SHARED_LIBS, if it exists,
  24. // otherwise it defaults to static library.
  25. cmTarget::TargetType type = cmTarget::SHARED_LIBRARY;
  26. if (cmSystemTools::IsOff(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  27. {
  28. type = cmTarget::STATIC_LIBRARY;
  29. }
  30. bool excludeFromAll = false;
  31. bool importTarget = 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. while ( s != args.end() )
  39. {
  40. std::string libType = *s;
  41. if(libType == "STATIC")
  42. {
  43. ++s;
  44. type = cmTarget::STATIC_LIBRARY;
  45. }
  46. else if(libType == "SHARED")
  47. {
  48. ++s;
  49. type = cmTarget::SHARED_LIBRARY;
  50. }
  51. else if(libType == "MODULE")
  52. {
  53. ++s;
  54. type = cmTarget::MODULE_LIBRARY;
  55. }
  56. else if(*s == "EXCLUDE_FROM_ALL")
  57. {
  58. ++s;
  59. excludeFromAll = true;
  60. }
  61. else if(*s == "IMPORT")
  62. {
  63. ++s;
  64. importTarget = true;
  65. }
  66. else
  67. {
  68. break;
  69. }
  70. }
  71. /* ideally we should check whether for the linker language of the target
  72. CMAKE_${LANG}_CREATE_SHARED_LIBRARY is defined and if not default to
  73. STATIC. But at this point we know only the name of the target, but not
  74. yet its linker language. */
  75. if ((type != cmTarget::STATIC_LIBRARY) &&
  76. (this->Makefile->GetCMakeInstance()->GetPropertyAsBool(
  77. "TARGET_SUPPORTS_SHARED_LIBS") == false))
  78. {
  79. std::string msg = "ADD_LIBRARY for library ";
  80. msg += args[0];
  81. msg += " is used with the ";
  82. msg += type==cmTarget::SHARED_LIBRARY ? "SHARED" : "MODULE";
  83. msg += " option, but the target platform supports only STATIC libraries. "
  84. "Building it STATIC instead. This may lead to problems.";
  85. cmSystemTools::Message(msg.c_str() ,"Warning");
  86. type = cmTarget::STATIC_LIBRARY;
  87. }
  88. if (importTarget)
  89. {
  90. this->Makefile->AddNewTarget(type, libName.c_str(), true);
  91. return true;
  92. }
  93. if (s == args.end())
  94. {
  95. std::string msg = "You have called ADD_LIBRARY for library ";
  96. msg += args[0];
  97. msg += " without any source files. This typically indicates a problem ";
  98. msg += "with your CMakeLists.txt file";
  99. cmSystemTools::Message(msg.c_str() ,"Warning");
  100. }
  101. std::vector<std::string> srclists;
  102. while (s != args.end())
  103. {
  104. srclists.push_back(*s);
  105. ++s;
  106. }
  107. this->Makefile->AddLibrary(libName.c_str(), type, srclists,
  108. excludeFromAll);
  109. return true;
  110. }