cmExportLibraryDependenciesCommand.cxx 5.1 KB

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