cmExportLibraryDependenciesCommand.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "cmGeneratedFileStream.h"
  8. #include "cmGlobalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmPolicies.h"
  11. #include "cmStateTypes.h"
  12. #include "cmSystemTools.h"
  13. #include "cmTarget.h"
  14. #include "cmTargetLinkLibraryType.h"
  15. #include "cm_auto_ptr.hxx"
  16. #include "cm_unordered_map.hxx"
  17. #include "cmake.h"
  18. class cmExecutionStatus;
  19. bool cmExportLibraryDependenciesCommand::InitialPass(
  20. std::vector<std::string> const& args, cmExecutionStatus&)
  21. {
  22. if (this->Disallowed(
  23. cmPolicies::CMP0033,
  24. "The export_library_dependencies command should not be called; "
  25. "see CMP0033.")) {
  26. return true;
  27. }
  28. if (args.empty()) {
  29. this->SetError("called with incorrect number of arguments");
  30. return false;
  31. }
  32. // store the arguments for the final pass
  33. this->Filename = args[0];
  34. this->Append = false;
  35. if (args.size() > 1) {
  36. if (args[1] == "APPEND") {
  37. this->Append = true;
  38. }
  39. }
  40. return true;
  41. }
  42. void cmExportLibraryDependenciesCommand::FinalPass()
  43. {
  44. // export_library_dependencies() shouldn't modify anything
  45. // ensure this by calling a const method
  46. this->ConstFinalPass();
  47. }
  48. void cmExportLibraryDependenciesCommand::ConstFinalPass() const
  49. {
  50. // Use copy-if-different if not appending.
  51. CM_AUTO_PTR<cmsys::ofstream> foutPtr;
  52. if (this->Append) {
  53. CM_AUTO_PTR<cmsys::ofstream> ap(
  54. new cmsys::ofstream(this->Filename.c_str(), std::ios::app));
  55. foutPtr = ap;
  56. } else {
  57. CM_AUTO_PTR<cmGeneratedFileStream> ap(
  58. new cmGeneratedFileStream(this->Filename.c_str(), true));
  59. ap->SetCopyIfDifferent(true);
  60. foutPtr = ap;
  61. }
  62. std::ostream& fout = *foutPtr;
  63. if (!fout) {
  64. cmSystemTools::Error("Error Writing ", this->Filename.c_str());
  65. cmSystemTools::ReportLastSystemError("");
  66. return;
  67. }
  68. // Collect dependency information about all library targets built in
  69. // the project.
  70. cmake* cm = this->Makefile->GetCMakeInstance();
  71. cmGlobalGenerator* global = cm->GetGlobalGenerator();
  72. const std::vector<cmMakefile*>& locals = global->GetMakefiles();
  73. std::map<std::string, std::string> libDepsOld;
  74. std::map<std::string, std::string> libDepsNew;
  75. std::map<std::string, std::string> libTypes;
  76. for (std::vector<cmMakefile*>::const_iterator i = locals.begin();
  77. i != locals.end(); ++i) {
  78. const cmTargets& tgts = (*i)->GetTargets();
  79. for (cmTargets::const_iterator l = tgts.begin(); l != tgts.end(); ++l) {
  80. // Get the current target.
  81. cmTarget const& target = l->second;
  82. // Skip non-library targets.
  83. if (target.GetType() < cmStateEnums::STATIC_LIBRARY ||
  84. target.GetType() > cmStateEnums::MODULE_LIBRARY) {
  85. continue;
  86. }
  87. // Construct the dependency variable name.
  88. std::string targetEntry = target.GetName();
  89. targetEntry += "_LIB_DEPENDS";
  90. // Construct the dependency variable value with the direct link
  91. // dependencies.
  92. std::string valueOld;
  93. std::string valueNew;
  94. cmTarget::LinkLibraryVectorType const& libs =
  95. target.GetOriginalLinkLibraries();
  96. for (cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
  97. li != libs.end(); ++li) {
  98. std::string ltVar = li->first;
  99. ltVar += "_LINK_TYPE";
  100. std::string ltValue;
  101. switch (li->second) {
  102. case GENERAL_LibraryType:
  103. valueNew += "general;";
  104. ltValue = "general";
  105. break;
  106. case DEBUG_LibraryType:
  107. valueNew += "debug;";
  108. ltValue = "debug";
  109. break;
  110. case OPTIMIZED_LibraryType:
  111. valueNew += "optimized;";
  112. ltValue = "optimized";
  113. break;
  114. }
  115. std::string lib = li->first;
  116. if (cmTarget* libtgt = global->FindTarget(lib)) {
  117. // Handle simple output name changes. This command is
  118. // deprecated so we do not support full target name
  119. // translation (which requires per-configuration info).
  120. if (const char* outname = libtgt->GetProperty("OUTPUT_NAME")) {
  121. lib = outname;
  122. }
  123. }
  124. valueOld += lib;
  125. valueOld += ";";
  126. valueNew += lib;
  127. valueNew += ";";
  128. std::string& ltEntry = libTypes[ltVar];
  129. if (ltEntry.empty()) {
  130. ltEntry = ltValue;
  131. } else if (ltEntry != ltValue) {
  132. ltEntry = "general";
  133. }
  134. }
  135. libDepsNew[targetEntry] = valueNew;
  136. libDepsOld[targetEntry] = valueOld;
  137. }
  138. }
  139. // Generate dependency information for both old and new style CMake
  140. // versions.
  141. const char* vertest =
  142. "\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" GREATER 2.4";
  143. fout << "# Generated by CMake\n\n";
  144. fout << "if(" << vertest << ")\n";
  145. fout << " # Information for CMake 2.6 and above.\n";
  146. for (std::map<std::string, std::string>::const_iterator i =
  147. libDepsNew.begin();
  148. i != libDepsNew.end(); ++i) {
  149. if (!i->second.empty()) {
  150. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  151. }
  152. }
  153. fout << "else()\n";
  154. fout << " # Information for CMake 2.4 and lower.\n";
  155. for (std::map<std::string, std::string>::const_iterator i =
  156. libDepsOld.begin();
  157. i != libDepsOld.end(); ++i) {
  158. if (!i->second.empty()) {
  159. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  160. }
  161. }
  162. for (std::map<std::string, std::string>::const_iterator i = libTypes.begin();
  163. i != libTypes.end(); ++i) {
  164. if (i->second != "general") {
  165. fout << " set(\"" << i->first << "\" \"" << i->second << "\")\n";
  166. }
  167. }
  168. fout << "endif()\n";
  169. }