cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool.cxx 1.9 KB

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