cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool.cxx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool.h"
  4. #include <sstream>
  5. #include <vector>
  6. #include <cmsys/RegularExpression.hxx>
  7. #include "cmRuntimeDependencyArchive.h"
  8. #include "cmUVProcessChain.h"
  9. #include "cmUVStream.h"
  10. cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool::
  11. cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool(
  12. cmRuntimeDependencyArchive* archive)
  13. : cmBinUtilsWindowsPEGetRuntimeDependenciesTool(archive)
  14. {
  15. }
  16. bool cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool::GetFileInfo(
  17. std::string const& 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("dumpbin", command)) {
  23. this->SetError("Could not find dumpbin");
  24. return false;
  25. }
  26. command.emplace_back("/dependents");
  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 dumpbin process for:\n " << file;
  33. this->SetError(e.str());
  34. return false;
  35. }
  36. std::string line;
  37. static cmsys::RegularExpression const regex(
  38. "^ ([^\n]*\\.[Dd][Ll][Ll])\r$");
  39. cmUVPipeIStream output(process.GetLoop(), process.OutputStream());
  40. while (std::getline(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 dumpbin 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 dumpbin on:\n " << file;
  55. this->SetError(e.str());
  56. return false;
  57. }
  58. return true;
  59. }