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 <memory> // IWYU pragma: keep
  7. #include <utility>
  8. #include "cmAlgorithms.h"
  9. #include "cmGeneratedFileStream.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmStateTypes.h"
  13. #include "cmSystemTools.h"
  14. #include "cmTarget.h"
  15. #include "cmTargetLinkLibraryType.h"
  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. std::unique_ptr<cmsys::ofstream> foutPtr;
  45. if (this->Append) {
  46. foutPtr =
  47. cm::make_unique<cmsys::ofstream>(this->Filename.c_str(), std::ios::app);
  48. } else {
  49. std::unique_ptr<cmGeneratedFileStream> ap(
  50. new cmGeneratedFileStream(this->Filename.c_str(), 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.c_str());
  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. const cmTargets& tgts = local->GetTargets();
  70. for (auto const& tgt : tgts) {
  71. // Get the current target.
  72. cmTarget const& target = tgt.second;
  73. // Skip non-library targets.
  74. if (target.GetType() < cmStateEnums::STATIC_LIBRARY ||
  75. target.GetType() > cmStateEnums::MODULE_LIBRARY) {
  76. continue;
  77. }
  78. // Construct the dependency variable name.
  79. std::string targetEntry = target.GetName();
  80. targetEntry += "_LIB_DEPENDS";
  81. // Construct the dependency variable value with the direct link
  82. // dependencies.
  83. std::string valueOld;
  84. std::string valueNew;
  85. cmTarget::LinkLibraryVectorType const& libs =
  86. target.GetOriginalLinkLibraries();
  87. for (cmTarget::LibraryID const& li : libs) {
  88. std::string ltVar = li.first;
  89. ltVar += "_LINK_TYPE";
  90. std::string ltValue;
  91. switch (li.second) {
  92. case GENERAL_LibraryType:
  93. valueNew += "general;";
  94. ltValue = "general";
  95. break;
  96. case DEBUG_LibraryType:
  97. valueNew += "debug;";
  98. ltValue = "debug";
  99. break;
  100. case OPTIMIZED_LibraryType:
  101. valueNew += "optimized;";
  102. ltValue = "optimized";
  103. break;
  104. }
  105. std::string lib = li.first;
  106. if (cmTarget* libtgt = global->FindTarget(lib)) {
  107. // Handle simple output name changes. This command is
  108. // deprecated so we do not support full target name
  109. // translation (which requires per-configuration info).
  110. if (const char* outname = libtgt->GetProperty("OUTPUT_NAME")) {
  111. lib = outname;
  112. }
  113. }
  114. valueOld += lib;
  115. valueOld += ";";
  116. valueNew += lib;
  117. valueNew += ";";
  118. std::string& ltEntry = libTypes[ltVar];
  119. if (ltEntry.empty()) {
  120. ltEntry = ltValue;
  121. } else if (ltEntry != ltValue) {
  122. ltEntry = "general";
  123. }
  124. }
  125. libDepsNew[targetEntry] = valueNew;
  126. libDepsOld[targetEntry] = valueOld;
  127. }
  128. }
  129. // Generate dependency information for both old and new style CMake
  130. // versions.
  131. const char* vertest =
  132. "\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" GREATER 2.4";
  133. fout << "# Generated by CMake\n\n";
  134. fout << "if(" << vertest << ")\n";
  135. fout << " # Information for CMake 2.6 and above.\n";
  136. for (auto const& i : libDepsNew) {
  137. if (!i.second.empty()) {
  138. fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
  139. }
  140. }
  141. fout << "else()\n";
  142. fout << " # Information for CMake 2.4 and lower.\n";
  143. for (auto const& i : libDepsOld) {
  144. if (!i.second.empty()) {
  145. fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
  146. }
  147. }
  148. for (auto const& i : libTypes) {
  149. if (i.second != "general") {
  150. fout << " set(\"" << i.first << "\" \"" << i.second << "\")\n";
  151. }
  152. }
  153. fout << "endif()\n";
  154. }