cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool.cxx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool.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. cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool::
  12. cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool(
  13. cmRuntimeDependencyArchive* archive)
  14. : cmBinUtilsWindowsPEGetRuntimeDependenciesTool(archive)
  15. {
  16. }
  17. bool cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool::GetFileInfo(
  18. std::string const& file, std::vector<std::string>& needed)
  19. {
  20. cmUVProcessChainBuilder builder;
  21. builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT);
  22. std::vector<std::string> command;
  23. if (!this->Archive->GetGetRuntimeDependenciesCommand("objdump", command)) {
  24. this->SetError("Could not find objdump");
  25. return false;
  26. }
  27. command.emplace_back("-p");
  28. command.push_back(file);
  29. builder.AddCommand(command);
  30. auto process = builder.Start();
  31. if (!process.Valid() || process.GetStatus(0).SpawnResult != 0) {
  32. std::ostringstream e;
  33. e << "Failed to start objdump process for:\n " << file;
  34. this->SetError(e.str());
  35. return false;
  36. }
  37. std::string line;
  38. static cmsys::RegularExpression const regex(
  39. "^[\t ]*DLL Name: ([^\n]*\\.[Dd][Ll][Ll])$");
  40. cmUVPipeIStream output(process.GetLoop(), process.OutputStream());
  41. while (cmSystemTools::GetLineFromStream(output, line)) {
  42. cmsys::RegularExpressionMatch match;
  43. if (regex.find(line.c_str(), match)) {
  44. needed.push_back(match.match(1));
  45. }
  46. }
  47. if (!process.Wait()) {
  48. std::ostringstream e;
  49. e << "Failed to wait on objdump process for:\n " << file;
  50. this->SetError(e.str());
  51. return false;
  52. }
  53. if (process.GetStatus(0).ExitStatus != 0) {
  54. std::ostringstream e;
  55. e << "Failed to run objdump on:\n " << file;
  56. this->SetError(e.str());
  57. return false;
  58. }
  59. return true;
  60. }