cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.h"
  4. #include <sstream>
  5. #include <cmsys/RegularExpression.hxx>
  6. #include "cmRuntimeDependencyArchive.h"
  7. #include "cmUVProcessChain.h"
  8. cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool::
  9. cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool(
  10. cmRuntimeDependencyArchive* archive)
  11. : cmBinUtilsMacOSMachOGetRuntimeDependenciesTool(archive)
  12. {
  13. }
  14. bool cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool::GetFileInfo(
  15. std::string const& file, std::vector<std::string>& libs,
  16. std::vector<std::string>& rpaths)
  17. {
  18. std::vector<std::string> command;
  19. if (!this->Archive->GetGetRuntimeDependenciesCommand("otool", command)) {
  20. this->SetError("Could not find otool");
  21. return false;
  22. }
  23. command.emplace_back("-l");
  24. command.emplace_back(file);
  25. cmUVProcessChainBuilder builder;
  26. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
  27. .AddCommand(command);
  28. auto process = builder.Start();
  29. if (!process.Valid()) {
  30. std::ostringstream e;
  31. e << "Failed to start otool process for:\n " << file;
  32. this->SetError(e.str());
  33. return false;
  34. }
  35. std::string line;
  36. static const cmsys::RegularExpression rpathRegex("^ *cmd LC_RPATH$");
  37. static const cmsys::RegularExpression loadDylibRegex(
  38. "^ *cmd LC_LOAD(_WEAK)?_DYLIB$");
  39. static const cmsys::RegularExpression pathRegex(
  40. "^ *path (.*) \\(offset [0-9]+\\)$");
  41. static const cmsys::RegularExpression nameRegex(
  42. "^ *name (.*) \\(offset [0-9]+\\)$");
  43. while (std::getline(*process.OutputStream(), line)) {
  44. cmsys::RegularExpressionMatch cmdMatch;
  45. if (rpathRegex.find(line.c_str(), cmdMatch)) {
  46. if (!std::getline(*process.OutputStream(), line) ||
  47. !std::getline(*process.OutputStream(), line)) {
  48. this->SetError("Invalid output from otool");
  49. return false;
  50. }
  51. cmsys::RegularExpressionMatch pathMatch;
  52. if (pathRegex.find(line.c_str(), pathMatch)) {
  53. rpaths.push_back(pathMatch.match(1));
  54. } else {
  55. this->SetError("Invalid output from otool");
  56. return false;
  57. }
  58. } else if (loadDylibRegex.find(line.c_str(), cmdMatch)) {
  59. if (!std::getline(*process.OutputStream(), line) ||
  60. !std::getline(*process.OutputStream(), line)) {
  61. this->SetError("Invalid output from otool");
  62. return false;
  63. }
  64. cmsys::RegularExpressionMatch nameMatch;
  65. if (nameRegex.find(line.c_str(), nameMatch)) {
  66. libs.push_back(nameMatch.match(1));
  67. } else {
  68. this->SetError("Invalid output from otool");
  69. return false;
  70. }
  71. }
  72. }
  73. if (!process.Wait()) {
  74. std::ostringstream e;
  75. e << "Failed to wait on otool process for:\n " << file;
  76. this->SetError(e.str());
  77. return false;
  78. }
  79. auto status = process.GetStatus();
  80. if (!status[0] || status[0]->ExitStatus != 0) {
  81. std::ostringstream e;
  82. e << "Failed to run otool on:\n " << file;
  83. this->SetError(e.str());
  84. return false;
  85. }
  86. return true;
  87. }