cmExportLibraryDependencies.cxx 6.3 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 "cmExportLibraryDependencies.h"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmake.h"
  15. #include "cmVersion.h"
  16. #include <cmsys/auto_ptr.hxx>
  17. bool cmExportLibraryDependenciesCommand
  18. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  19. {
  20. if(args.size() < 1 )
  21. {
  22. this->SetError("called with incorrect number of arguments");
  23. return false;
  24. }
  25. // store the arguments for the final pass
  26. this->Filename = args[0];
  27. this->Append = false;
  28. if(args.size() > 1)
  29. {
  30. if(args[1] == "APPEND")
  31. {
  32. this->Append = true;
  33. }
  34. }
  35. return true;
  36. }
  37. void cmExportLibraryDependenciesCommand::FinalPass()
  38. {
  39. // export_library_dependencies() shouldn't modify anything
  40. // ensure this by calling a const method
  41. this->ConstFinalPass();
  42. }
  43. void cmExportLibraryDependenciesCommand::ConstFinalPass() const
  44. {
  45. // Use copy-if-different if not appending.
  46. cmsys::auto_ptr<std::ofstream> foutPtr;
  47. if(this->Append)
  48. {
  49. cmsys::auto_ptr<std::ofstream> ap(
  50. new std::ofstream(this->Filename.c_str(), std::ios::app));
  51. foutPtr = ap;
  52. }
  53. else
  54. {
  55. cmsys::auto_ptr<cmGeneratedFileStream> ap(
  56. new cmGeneratedFileStream(this->Filename.c_str(), true));
  57. ap->SetCopyIfDifferent(true);
  58. foutPtr = ap;
  59. }
  60. std::ostream& fout = *foutPtr.get();
  61. if (!fout)
  62. {
  63. cmSystemTools::Error("Error Writing ", this->Filename.c_str());
  64. cmSystemTools::ReportLastSystemError("");
  65. return;
  66. }
  67. // Collect dependency information about all library targets built in
  68. // the project.
  69. cmake* cm = this->Makefile->GetCMakeInstance();
  70. cmGlobalGenerator* global = cm->GetGlobalGenerator();
  71. const std::vector<cmLocalGenerator *>& locals = global->GetLocalGenerators();
  72. std::map<cmStdString, cmStdString> libDepsOld;
  73. std::map<cmStdString, cmStdString> libDepsNew;
  74. std::map<cmStdString, cmStdString> libTypes;
  75. for(std::vector<cmLocalGenerator *>::const_iterator i = locals.begin();
  76. i != locals.end(); ++i)
  77. {
  78. const cmLocalGenerator* gen = *i;
  79. const cmTargets &tgts = gen->GetMakefile()->GetTargets();
  80. for(cmTargets::const_iterator l = tgts.begin();
  81. l != tgts.end(); ++l)
  82. {
  83. // Get the current target.
  84. cmTarget const& target = l->second;
  85. // Skip non-library targets.
  86. if(target.GetType() < cmTarget::STATIC_LIBRARY
  87. || target.GetType() > cmTarget::MODULE_LIBRARY)
  88. {
  89. continue;
  90. }
  91. // Construct the dependency variable name.
  92. std::string targetEntry = target.GetName();
  93. targetEntry += "_LIB_DEPENDS";
  94. // Construct the dependency variable value. It is safe to use
  95. // the target GetLinkLibraries method here because this code is
  96. // called at the end of configure but before generate so library
  97. // dependencies have yet to be analyzed. Therefore the value
  98. // will be the direct link dependencies.
  99. std::string valueOld;
  100. std::string valueNew;
  101. cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
  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(0, lib.c_str()))
  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<cmStdString, cmStdString>::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(" << vertest << ")\n";
  169. fout << " # Information for CMake 2.4 and lower.\n";
  170. for(std::map<cmStdString, cmStdString>::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<cmStdString, cmStdString>::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(" << vertest << ")\n";
  188. return;
  189. }