cmBinUtilsLinuxELFLinker.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "cmBinUtilsLinuxELFLinker.h"
  4. #include <sstream>
  5. #include <cm/memory>
  6. #include <cm/string_view>
  7. #include <cmsys/RegularExpression.hxx>
  8. #include "cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool.h"
  9. #include "cmLDConfigLDConfigTool.h"
  10. #include "cmMakefile.h"
  11. #include "cmMessageType.h"
  12. #include "cmRuntimeDependencyArchive.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. static std::string ReplaceOrigin(const std::string& rpath,
  16. const std::string& origin)
  17. {
  18. static const cmsys::RegularExpression originRegex(
  19. "(\\$ORIGIN)([^a-zA-Z0-9_]|$)");
  20. static const cmsys::RegularExpression originCurlyRegex("\\${ORIGIN}");
  21. cmsys::RegularExpressionMatch match;
  22. if (originRegex.find(rpath.c_str(), match)) {
  23. cm::string_view pathv(rpath);
  24. auto begin = pathv.substr(0, match.start(1));
  25. auto end = pathv.substr(match.end(1));
  26. return cmStrCat(begin, origin, end);
  27. }
  28. if (originCurlyRegex.find(rpath.c_str(), match)) {
  29. cm::string_view pathv(rpath);
  30. auto begin = pathv.substr(0, match.start());
  31. auto end = pathv.substr(match.end());
  32. return cmStrCat(begin, origin, end);
  33. }
  34. return rpath;
  35. }
  36. cmBinUtilsLinuxELFLinker::cmBinUtilsLinuxELFLinker(
  37. cmRuntimeDependencyArchive* archive)
  38. : cmBinUtilsLinker(archive)
  39. {
  40. }
  41. bool cmBinUtilsLinuxELFLinker::Prepare()
  42. {
  43. std::string tool = this->Archive->GetGetRuntimeDependenciesTool();
  44. if (tool.empty()) {
  45. tool = "objdump";
  46. }
  47. if (tool == "objdump") {
  48. this->Tool =
  49. cm::make_unique<cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool>(
  50. this->Archive);
  51. } else {
  52. std::ostringstream e;
  53. e << "Invalid value for CMAKE_GET_RUNTIME_DEPENDENCIES_TOOL: " << tool;
  54. this->SetError(e.str());
  55. return false;
  56. }
  57. std::string ldConfigTool =
  58. this->Archive->GetMakefile()->GetSafeDefinition("CMAKE_LDCONFIG_TOOL");
  59. if (ldConfigTool.empty()) {
  60. ldConfigTool = "ldconfig";
  61. }
  62. if (ldConfigTool == "ldconfig") {
  63. this->LDConfigTool =
  64. cm::make_unique<cmLDConfigLDConfigTool>(this->Archive);
  65. } else {
  66. std::ostringstream e;
  67. e << "Invalid value for CMAKE_LDCONFIG_TOOL: " << ldConfigTool;
  68. this->SetError(e.str());
  69. return false;
  70. }
  71. return true;
  72. }
  73. bool cmBinUtilsLinuxELFLinker::ScanDependencies(
  74. std::string const& file, cmStateEnums::TargetType /* unused */)
  75. {
  76. std::vector<std::string> parentRpaths;
  77. return this->ScanDependencies(file, parentRpaths);
  78. }
  79. bool cmBinUtilsLinuxELFLinker::ScanDependencies(
  80. std::string const& file, std::vector<std::string> const& parentRpaths)
  81. {
  82. std::string origin = cmSystemTools::GetFilenamePath(file);
  83. std::vector<std::string> needed;
  84. std::vector<std::string> rpaths;
  85. std::vector<std::string> runpaths;
  86. if (!this->Tool->GetFileInfo(file, needed, rpaths, runpaths)) {
  87. return false;
  88. }
  89. for (auto& runpath : runpaths) {
  90. runpath = ReplaceOrigin(runpath, origin);
  91. }
  92. for (auto& rpath : rpaths) {
  93. rpath = ReplaceOrigin(rpath, origin);
  94. }
  95. std::vector<std::string> searchPaths;
  96. if (!runpaths.empty()) {
  97. searchPaths = runpaths;
  98. } else {
  99. searchPaths = rpaths;
  100. searchPaths.insert(searchPaths.end(), parentRpaths.begin(),
  101. parentRpaths.end());
  102. }
  103. std::vector<std::string> ldConfigPaths;
  104. if (!this->LDConfigTool->GetLDConfigPaths(ldConfigPaths)) {
  105. return false;
  106. }
  107. searchPaths.insert(searchPaths.end(), ldConfigPaths.begin(),
  108. ldConfigPaths.end());
  109. for (auto const& dep : needed) {
  110. if (!this->Archive->IsPreExcluded(dep)) {
  111. std::string path;
  112. bool resolved = false;
  113. if (dep.find('/') != std::string::npos) {
  114. this->SetError("Paths to dependencies are not supported");
  115. return false;
  116. }
  117. if (!this->ResolveDependency(dep, searchPaths, path, resolved)) {
  118. return false;
  119. }
  120. if (resolved) {
  121. if (!this->Archive->IsPostExcluded(path)) {
  122. bool unique;
  123. this->Archive->AddResolvedPath(dep, path, unique);
  124. if (unique && !this->ScanDependencies(path, rpaths)) {
  125. return false;
  126. }
  127. }
  128. } else {
  129. this->Archive->AddUnresolvedPath(dep);
  130. }
  131. }
  132. }
  133. return true;
  134. }
  135. bool cmBinUtilsLinuxELFLinker::ResolveDependency(
  136. std::string const& name, std::vector<std::string> const& searchPaths,
  137. std::string& path, bool& resolved)
  138. {
  139. for (auto const& searchPath : searchPaths) {
  140. path = cmStrCat(searchPath, '/', name);
  141. if (cmSystemTools::PathExists(path)) {
  142. resolved = true;
  143. return true;
  144. }
  145. }
  146. for (auto const& searchPath : this->Archive->GetSearchDirectories()) {
  147. path = cmStrCat(searchPath, '/', name);
  148. if (cmSystemTools::PathExists(path)) {
  149. std::ostringstream warning;
  150. warning << "Dependency " << name << " found in search directory:\n "
  151. << searchPath
  152. << "\nSee file(GET_RUNTIME_DEPENDENCIES) documentation for "
  153. << "more information.";
  154. this->Archive->GetMakefile()->IssueMessage(MessageType::WARNING,
  155. warning.str());
  156. resolved = true;
  157. return true;
  158. }
  159. }
  160. resolved = false;
  161. return true;
  162. }