cmExportLibraryDependenciesCommand.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "cmExportLibraryDependenciesCommand.h"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmake.h"
  14. #include "cmVersion.h"
  15. #include <cmsys/auto_ptr.hxx>
  16. bool cmExportLibraryDependenciesCommand
  17. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  18. {
  19. if(this->Disallowed(cmPolicies::CMP0033,
  20. "The export_library_dependencies command should not be called; "
  21. "see CMP0033."))
  22. { return true; }
  23. if(args.size() < 1 )
  24. {
  25. this->SetError("called with incorrect number of arguments");
  26. return false;
  27. }
  28. // store the arguments for the final pass
  29. this->Filename = args[0];
  30. this->Append = false;
  31. if(args.size() > 1)
  32. {
  33. if(args[1] == "APPEND")
  34. {
  35. this->Append = true;
  36. }
  37. }
  38. return true;
  39. }
  40. void cmExportLibraryDependenciesCommand::FinalPass()
  41. {
  42. // export_library_dependencies() shouldn't modify anything
  43. // ensure this by calling a const method
  44. this->ConstFinalPass();
  45. }
  46. void cmExportLibraryDependenciesCommand::ConstFinalPass() const
  47. {
  48. // Use copy-if-different if not appending.
  49. cmsys::auto_ptr<cmsys::ofstream> foutPtr;
  50. if(this->Append)
  51. {
  52. cmsys::auto_ptr<cmsys::ofstream> ap(
  53. new cmsys::ofstream(this->Filename.c_str(), std::ios::app));
  54. foutPtr = ap;
  55. }
  56. else
  57. {
  58. cmsys::auto_ptr<cmGeneratedFileStream> ap(
  59. new cmGeneratedFileStream(this->Filename.c_str(), true));
  60. ap->SetCopyIfDifferent(true);
  61. foutPtr = ap;
  62. }
  63. std::ostream& fout = *foutPtr.get();
  64. if (!fout)
  65. {
  66. cmSystemTools::Error("Error Writing ", this->Filename.c_str());
  67. cmSystemTools::ReportLastSystemError("");
  68. return;
  69. }
  70. // Collect dependency information about all library targets built in
  71. // the project.
  72. cmake* cm = this->Makefile->GetCMakeInstance();
  73. cmGlobalGenerator* global = cm->GetGlobalGenerator();
  74. const std::vector<cmMakefile*>& locals = global->GetMakefiles();
  75. std::map<std::string, std::string> libDepsOld;
  76. std::map<std::string, std::string> libDepsNew;
  77. std::map<std::string, std::string> libTypes;
  78. for(std::vector<cmMakefile*>::const_iterator i = locals.begin();
  79. i != locals.end(); ++i)
  80. {
  81. const cmTargets &tgts = (*i)->GetTargets();
  82. for(cmTargets::const_iterator l = tgts.begin();
  83. l != tgts.end(); ++l)
  84. {
  85. // Get the current target.
  86. cmTarget const& target = l->second;
  87. // Skip non-library targets.
  88. if(target.GetType() < cmTarget::STATIC_LIBRARY
  89. || target.GetType() > cmTarget::MODULE_LIBRARY)
  90. {
  91. continue;
  92. }
  93. // Construct the dependency variable name.
  94. std::string targetEntry = target.GetName();
  95. targetEntry += "_LIB_DEPENDS";
  96. // Construct the dependency variable value with the direct link
  97. // dependencies.
  98. std::string valueOld;
  99. std::string valueNew;
  100. cmTarget::LinkLibraryVectorType const& libs =
  101. target.GetOriginalLinkLibraries();
  102. for(cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
  103. li != libs.end(); ++li)
  104. {
  105. std::string ltVar = li->first;
  106. ltVar += "_LINK_TYPE";
  107. std::string ltValue;
  108. switch(li->second)
  109. {
  110. case cmTarget::GENERAL:
  111. valueNew += "general;";
  112. ltValue = "general";
  113. break;
  114. case cmTarget::DEBUG:
  115. valueNew += "debug;";
  116. ltValue = "debug";
  117. break;
  118. case cmTarget::OPTIMIZED:
  119. valueNew += "optimized;";
  120. ltValue = "optimized";
  121. break;
  122. }
  123. std::string lib = li->first;
  124. if(cmTarget* libtgt = global->FindTarget(lib))
  125. {
  126. // Handle simple output name changes. This command is
  127. // deprecated so we do not support full target name
  128. // translation (which requires per-configuration info).
  129. if(const char* outname = libtgt->GetProperty("OUTPUT_NAME"))
  130. {
  131. lib = outname;
  132. }
  133. }
  134. valueOld += lib;
  135. valueOld += ";";
  136. valueNew += lib;
  137. valueNew += ";";
  138. std::string& ltEntry = libTypes[ltVar];
  139. if(ltEntry.empty())
  140. {
  141. ltEntry = ltValue;
  142. }
  143. else if(ltEntry != ltValue)
  144. {
  145. ltEntry = "general";
  146. }
  147. }
  148. libDepsNew[targetEntry] = valueNew;
  149. libDepsOld[targetEntry] = valueOld;
  150. }
  151. }
  152. // Generate dependency information for both old and new style CMake
  153. // versions.
  154. const char* vertest =
  155. "\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" GREATER 2.4";
  156. fout << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
  157. fout << "if(" << vertest << ")\n";
  158. fout << " # Information for CMake 2.6 and above.\n";
  159. for(std::map<std::string, std::string>::const_iterator
  160. i = libDepsNew.begin();
  161. i != libDepsNew.end(); ++i)
  162. {
  163. if(!i->second.empty())
  164. {
  165. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  166. }
  167. }
  168. fout << "else()\n";
  169. fout << " # Information for CMake 2.4 and lower.\n";
  170. for(std::map<std::string, std::string>::const_iterator
  171. i = libDepsOld.begin();
  172. i != libDepsOld.end(); ++i)
  173. {
  174. if(!i->second.empty())
  175. {
  176. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  177. }
  178. }
  179. for(std::map<std::string, std::string>::const_iterator i = libTypes.begin();
  180. i != libTypes.end(); ++i)
  181. {
  182. if(i->second != "general")
  183. {
  184. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  185. }
  186. }
  187. fout << "endif()\n";
  188. return;
  189. }