/src/CMake/Source/cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | |
4 | | #include "cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool.h" |
5 | | |
6 | | #include <sstream> |
7 | | #include <vector> |
8 | | |
9 | | #include <cmsys/RegularExpression.hxx> |
10 | | |
11 | | #include "cmRuntimeDependencyArchive.h" |
12 | | #include "cmSystemTools.h" |
13 | | #include "cmUVProcessChain.h" |
14 | | #include "cmUVStream.h" |
15 | | |
16 | | cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool:: |
17 | | cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool( |
18 | | cmRuntimeDependencyArchive* archive) |
19 | 0 | : cmBinUtilsLinuxELFGetRuntimeDependenciesTool(archive) |
20 | 0 | { |
21 | 0 | } |
22 | | |
23 | | bool cmBinUtilsLinuxELFObjdumpGetRuntimeDependenciesTool::GetFileInfo( |
24 | | std::string const& file, std::vector<std::string>& needed, |
25 | | std::vector<std::string>& rpaths, std::vector<std::string>& runpaths) |
26 | 0 | { |
27 | 0 | cmUVProcessChainBuilder builder; |
28 | 0 | builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT); |
29 | |
|
30 | 0 | std::vector<std::string> command; |
31 | 0 | if (!this->Archive->GetGetRuntimeDependenciesCommand("objdump", command)) { |
32 | 0 | this->SetError("Could not find objdump"); |
33 | 0 | return false; |
34 | 0 | } |
35 | 0 | command.emplace_back("-p"); |
36 | 0 | command.push_back(file); |
37 | 0 | builder.AddCommand(command); |
38 | |
|
39 | 0 | auto process = builder.Start(); |
40 | 0 | if (!process.Valid() || process.GetStatus(0).SpawnResult != 0) { |
41 | 0 | std::ostringstream e; |
42 | 0 | e << "Failed to start objdump process for:\n " << file; |
43 | 0 | this->SetError(e.str()); |
44 | 0 | return false; |
45 | 0 | } |
46 | | |
47 | 0 | std::string line; |
48 | 0 | static cmsys::RegularExpression const neededRegex("^ *NEEDED *([^\n]*)$"); |
49 | 0 | static cmsys::RegularExpression const rpathRegex("^ *RPATH *([^\n]*)$"); |
50 | 0 | static cmsys::RegularExpression const runpathRegex("^ *RUNPATH *([^\n]*)$"); |
51 | 0 | cmUVIStream output(process.OutputStream()); |
52 | 0 | while (std::getline(output, line)) { |
53 | 0 | cmsys::RegularExpressionMatch match; |
54 | 0 | if (neededRegex.find(line.c_str(), match)) { |
55 | 0 | needed.push_back(match.match(1)); |
56 | 0 | } else if (rpathRegex.find(line.c_str(), match)) { |
57 | 0 | std::vector<std::string> rpathSplit = |
58 | 0 | cmSystemTools::SplitString(match.match(1), ':'); |
59 | 0 | rpaths.reserve(rpaths.size() + rpathSplit.size()); |
60 | 0 | for (auto const& rpath : rpathSplit) { |
61 | 0 | rpaths.push_back(rpath); |
62 | 0 | } |
63 | 0 | } else if (runpathRegex.find(line.c_str(), match)) { |
64 | 0 | std::vector<std::string> runpathSplit = |
65 | 0 | cmSystemTools::SplitString(match.match(1), ':'); |
66 | 0 | runpaths.reserve(runpaths.size() + runpathSplit.size()); |
67 | 0 | for (auto const& runpath : runpathSplit) { |
68 | 0 | runpaths.push_back(runpath); |
69 | 0 | } |
70 | 0 | } |
71 | 0 | } |
72 | |
|
73 | 0 | if (!process.Wait()) { |
74 | 0 | std::ostringstream e; |
75 | 0 | e << "Failed to wait on objdump process for:\n " << file; |
76 | 0 | this->SetError(e.str()); |
77 | 0 | return false; |
78 | 0 | } |
79 | 0 | if (process.GetStatus(0).ExitStatus != 0) { |
80 | 0 | std::ostringstream e; |
81 | 0 | e << "Failed to run objdump on:\n " << file; |
82 | 0 | this->SetError(e.str()); |
83 | 0 | return false; |
84 | 0 | } |
85 | | |
86 | 0 | return true; |
87 | 0 | } |