cmExportLibraryDependenciesCommand.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. It is safe to use
  99. // the target GetLinkLibraries method here because this code is
  100. // called at the end of configure but before generate so library
  101. // dependencies have yet to be analyzed. Therefore the value
  102. // will be the direct link dependencies.
  103. std::string valueOld;
  104. std::string valueNew;
  105. cmTarget::LinkLibraryVectorType const& libs = target.GetLinkLibraries();
  106. for(cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
  107. li != libs.end(); ++li)
  108. {
  109. std::string ltVar = li->first;
  110. ltVar += "_LINK_TYPE";
  111. std::string ltValue;
  112. switch(li->second)
  113. {
  114. case cmTarget::GENERAL:
  115. valueNew += "general;";
  116. ltValue = "general";
  117. break;
  118. case cmTarget::DEBUG:
  119. valueNew += "debug;";
  120. ltValue = "debug";
  121. break;
  122. case cmTarget::OPTIMIZED:
  123. valueNew += "optimized;";
  124. ltValue = "optimized";
  125. break;
  126. }
  127. std::string lib = li->first;
  128. if(cmTarget* libtgt = global->FindTarget(lib))
  129. {
  130. // Handle simple output name changes. This command is
  131. // deprecated so we do not support full target name
  132. // translation (which requires per-configuration info).
  133. if(const char* outname = libtgt->GetProperty("OUTPUT_NAME"))
  134. {
  135. lib = outname;
  136. }
  137. }
  138. valueOld += lib;
  139. valueOld += ";";
  140. valueNew += lib;
  141. valueNew += ";";
  142. std::string& ltEntry = libTypes[ltVar];
  143. if(ltEntry.empty())
  144. {
  145. ltEntry = ltValue;
  146. }
  147. else if(ltEntry != ltValue)
  148. {
  149. ltEntry = "general";
  150. }
  151. }
  152. libDepsNew[targetEntry] = valueNew;
  153. libDepsOld[targetEntry] = valueOld;
  154. }
  155. }
  156. // Generate dependency information for both old and new style CMake
  157. // versions.
  158. const char* vertest =
  159. "\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" GREATER 2.4";
  160. fout << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
  161. fout << "if(" << vertest << ")\n";
  162. fout << " # Information for CMake 2.6 and above.\n";
  163. for(std::map<std::string, std::string>::const_iterator
  164. i = libDepsNew.begin();
  165. i != libDepsNew.end(); ++i)
  166. {
  167. if(!i->second.empty())
  168. {
  169. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  170. }
  171. }
  172. fout << "else()\n";
  173. fout << " # Information for CMake 2.4 and lower.\n";
  174. for(std::map<std::string, std::string>::const_iterator
  175. i = libDepsOld.begin();
  176. i != libDepsOld.end(); ++i)
  177. {
  178. if(!i->second.empty())
  179. {
  180. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  181. }
  182. }
  183. for(std::map<std::string, std::string>::const_iterator i = libTypes.begin();
  184. i != libTypes.end(); ++i)
  185. {
  186. if(i->second != "general")
  187. {
  188. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  189. }
  190. }
  191. fout << "endif()\n";
  192. return;
  193. }