resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3
4 #include "cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool.h"
5
6 #include <sstream>
7
8 #include <cmsys/RegularExpression.hxx>
9
10 #include "cmRuntimeDependencyArchive.h"
11 #include "cmUVProcessChain.h"
12
13 cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool::
14   cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool(
15     cmRuntimeDependencyArchive* archive)
16   : cmBinUtilsWindowsPEGetRuntimeDependenciesTool(archive)
17 {
18 }
19
20 bool cmBinUtilsWindowsPEDumpbinGetRuntimeDependenciesTool::GetFileInfo(
21   const std::string& file, std::vector<std::string>& needed)
22 {
23   cmUVProcessChainBuilder builder;
24   builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT);
25
26   std::vector<std::string> command;
27   if (!this->Archive->GetGetRuntimeDependenciesCommand("dumpbin", command)) {
28     this->SetError("Could not find dumpbin");
29     return false;
30   }
31   command.emplace_back("/dependents");
32   command.push_back(file);
33   builder.AddCommand(command);
34
35   auto process = builder.Start();
36   if (!process.Valid()) {
37     std::ostringstream e;
38     e << "Failed to start dumpbin process for:\n  " << file;
39     this->SetError(e.str());
40     return false;
41   }
42
43   std::string line;
44   static const cmsys::RegularExpression regex(
45     "^    ([^\n]*\\.[Dd][Ll][Ll])\r$");
46   while (std::getline(*process.OutputStream(), line)) {
47     cmsys::RegularExpressionMatch match;
48     if (regex.find(line.c_str(), match)) {
49       needed.push_back(match.match(1));
50     }
51   }
52
53   if (!process.Wait()) {
54     std::ostringstream e;
55     e << "Failed to wait on dumpbin process for:\n  " << file;
56     this->SetError(e.str());
57     return false;
58   }
59   auto status = process.GetStatus();
60   if (!status[0] || status[0]->ExitStatus != 0) {
61     std::ostringstream e;
62     e << "Failed to run dumpbin on:\n  " << file;
63     this->SetError(e.str());
64     return false;
65   }
66
67   return true;
68 }