cmBinUtilsWindowsPEObjdumpGetRuntimeDependenciesTool.cxx 2.0 KB

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