cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool.h"
  4. #include <sstream>
  5. #include <vector>
  6. #include <cmsys/RegularExpression.hxx>
  7. #include "cmRuntimeDependencyArchive.h"
  8. #include "cmSystemTools.h"
  9. #include "cmUVProcessChain.h"
  10. #include "cmUVStream.h"
  11. cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool::
  12. cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool(
  13. cmRuntimeDependencyArchive* archive)
  14. : cmBinUtilsLinuxELFGetRuntimeDependenciesTool(archive)
  15. {
  16. }
  17. bool cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool::GetFileInfo(
  18. std::string const& file, std::vector<std::string>& needed,
  19. std::vector<std::string>& rpaths, std::vector<std::string>& runpaths)
  20. {
  21. cmUVProcessChainBuilder builder;
  22. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT);
  23. std::vector<std::string> command;
  24. if (!this->Archive->GetGetRuntimeDependenciesCommand("objdump", command)) {
  25. this->SetError("Could not find objdump");
  26. return false;
  27. }
  28. command.emplace_back("-p");
  29. command.push_back(file);
  30. builder.AddCommand(command);
  31. auto process = builder.Start();
  32. if (!process.Valid() || process.GetStatus(0).SpawnResult != 0) {
  33. std::ostringstream e;
  34. e << "Failed to start objdump process for:\n " << file;
  35. this->SetError(e.str());
  36. return false;
  37. }
  38. std::string line;
  39. static cmsys::RegularExpression const neededRegex("^ *NEEDED *([^\n]*)$");
  40. static cmsys::RegularExpression const rpathRegex("^ *RPATH *([^\n]*)$");
  41. static cmsys::RegularExpression const runpathRegex("^ *RUNPATH *([^\n]*)$");
  42. cmUVPipeIStream output(process.GetLoop(), process.OutputStream());
  43. while (std::getline(output, line)) {
  44. cmsys::RegularExpressionMatch match;
  45. if (neededRegex.find(line.c_str(), match)) {
  46. needed.push_back(match.match(1));
  47. } else if (rpathRegex.find(line.c_str(), match)) {
  48. std::vector<std::string> rpathSplit =
  49. cmSystemTools::SplitString(match.match(1), ':');
  50. rpaths.reserve(rpaths.size() + rpathSplit.size());
  51. for (auto const& rpath : rpathSplit) {
  52. rpaths.push_back(rpath);
  53. }
  54. } else if (runpathRegex.find(line.c_str(), match)) {
  55. std::vector<std::string> runpathSplit =
  56. cmSystemTools::SplitString(match.match(1), ':');
  57. runpaths.reserve(runpaths.size() + runpathSplit.size());
  58. for (auto const& runpath : runpathSplit) {
  59. runpaths.push_back(runpath);
  60. }
  61. }
  62. }
  63. if (!process.Wait()) {
  64. std::ostringstream e;
  65. e << "Failed to wait on objdump process for:\n " << file;
  66. this->SetError(e.str());
  67. return false;
  68. }
  69. if (process.GetStatus(0).ExitStatus != 0) {
  70. std::ostringstream e;
  71. e << "Failed to run objdump on:\n " << file;
  72. this->SetError(e.str());
  73. return false;
  74. }
  75. return true;
  76. }