cmExportLibraryDependenciesCommand.cxx 5.6 KB

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