cmExportLibraryDependenciesCommand.cxx 5.5 KB

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