cmExportLibraryDependenciesCommand.cxx 5.1 KB

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