cmExportLibraryDependenciesCommand.cxx 5.1 KB

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