cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool.cxx 2.8 KB

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