cmExportLibraryDependenciesCommand.cxx 5.0 KB

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