cmExportLibraryDependenciesCommand.cxx 5.1 KB

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