cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool.cxx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool.h"
  4. #include <sstream>
  5. #include <cmsys/RegularExpression.hxx>
  6. #include "cmRuntimeDependencyArchive.h"
  7. #include "cmSystemTools.h"
  8. #include "cmUVProcessChain.h"
  9. #include "cmUVStream.h"
  10. cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool::
  11. cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool(
  12. cmRuntimeDependencyArchive* archive)
  13. : cmBinUtilsWindowsPEGetRuntimeDependenciesTool(archive)
  14. {
  15. }
  16. bool cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool::GetFileInfo(
  17. const std::string& file, std::vector<std::string>& needed)
  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 regex(
  38. "^[\t ]*DLL Name: ([^\n]*\\.[Dd][Ll][Ll])$");
  39. cmUVPipeIStream output(process.GetLoop(), process.OutputStream());
  40. while (cmSystemTools::GetLineFromStream(output, line)) {
  41. cmsys::RegularExpressionMatch match;
  42. if (regex.find(line.c_str(), match)) {
  43. needed.push_back(match.match(1));
  44. }
  45. }
  46. if (!process.Wait()) {
  47. std::ostringstream e;
  48. e << "Failed to wait on objdump process for:\n " << file;
  49. this->SetError(e.str());
  50. return false;
  51. }
  52. if (process.GetStatus(0).ExitStatus != 0) {
  53. std::ostringstream e;
  54. e << "Failed to run objdump on:\n " << file;
  55. this->SetError(e.str());
  56. return false;
  57. }
  58. return true;
  59. }