cmExportLibraryDependenciesCommand.cxx 6.2 KB

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