cmAddLibraryCommand.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // Check for an existing target with this name.
  100. cmTarget* existing = this->Makefile->FindTargetToUse(libName.c_str());
  101. if(importTarget)
  102. {
  103. // Make sure the target does not already exist.
  104. if(existing)
  105. {
  106. cmOStringStream e;
  107. e << "cannot create imported target \"" << libName
  108. << "\" because another target with the same name already exists.";
  109. this->SetError(e.str().c_str());
  110. return false;
  111. }
  112. // Create the imported target.
  113. this->Makefile->AddImportedTarget(libName.c_str(), type);
  114. return true;
  115. }
  116. else
  117. {
  118. // Make sure the target does not conflict with an imported target.
  119. // This should really enforce global name uniqueness for targets
  120. // built within the project too, but that may break compatiblity
  121. // with projects in which it was accidentally working.
  122. if(existing && existing->IsImported())
  123. {
  124. cmOStringStream e;
  125. e << "cannot create target \"" << libName
  126. << "\" because an imported target with the same name already exists.";
  127. this->SetError(e.str().c_str());
  128. return false;
  129. }
  130. }
  131. if (s == args.end())
  132. {
  133. std::string msg = "You have called ADD_LIBRARY for library ";
  134. msg += args[0];
  135. msg += " without any source files. This typically indicates a problem ";
  136. msg += "with your CMakeLists.txt file";
  137. cmSystemTools::Message(msg.c_str() ,"Warning");
  138. }
  139. std::vector<std::string> srclists;
  140. while (s != args.end())
  141. {
  142. srclists.push_back(*s);
  143. ++s;
  144. }
  145. this->Makefile->AddLibrary(libName.c_str(), type, srclists,
  146. excludeFromAll);
  147. return true;
  148. }