cmAddLibraryCommand.cxx 3.6 KB

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