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