cmExportLibraryDependenciesCommand.cxx 5.5 KB

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