/src/CMake/Source/cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.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 "cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool.h" |
5 | | |
6 | | #include <istream> |
7 | | #include <vector> |
8 | | |
9 | | #include <cmsys/RegularExpression.hxx> |
10 | | |
11 | | #include "cmRuntimeDependencyArchive.h" |
12 | | #include "cmStringAlgorithms.h" |
13 | | #include "cmUVProcessChain.h" |
14 | | #include "cmUVStream.h" |
15 | | |
16 | | cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool:: |
17 | | cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool( |
18 | | cmRuntimeDependencyArchive* archive) |
19 | 0 | : cmBinUtilsMacOSMachOGetRuntimeDependenciesTool(archive) |
20 | 0 | { |
21 | 0 | } |
22 | | |
23 | | bool cmBinUtilsMacOSMachOOToolGetRuntimeDependenciesTool::GetFileInfo( |
24 | | std::string const& file, std::vector<std::string>& libs, |
25 | | std::vector<std::string>& rpaths) |
26 | 0 | { |
27 | 0 | std::vector<std::string> command; |
28 | 0 | if (!this->Archive->GetGetRuntimeDependenciesCommand("otool", command)) { |
29 | 0 | this->SetError("Could not find otool"); |
30 | 0 | return false; |
31 | 0 | } |
32 | 0 | command.emplace_back("-l"); |
33 | 0 | command.emplace_back(file); |
34 | |
|
35 | 0 | cmUVProcessChainBuilder builder; |
36 | 0 | builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT) |
37 | 0 | .AddCommand(command); |
38 | |
|
39 | 0 | auto process = builder.Start(); |
40 | 0 | if (!process.Valid() || process.GetStatus(0).SpawnResult != 0) { |
41 | 0 | this->SetError(cmStrCat("Failed to start otool process for:\n ", file)); |
42 | 0 | return false; |
43 | 0 | } |
44 | | |
45 | 0 | std::string line; |
46 | 0 | static cmsys::RegularExpression const rpathRegex("^ *cmd LC_RPATH$"); |
47 | 0 | static cmsys::RegularExpression const loadDylibRegex( |
48 | 0 | "^ *cmd LC_LOAD(_WEAK)?_DYLIB$"); |
49 | 0 | static cmsys::RegularExpression const pathRegex( |
50 | 0 | "^ *path (.*) \\(offset [0-9]+\\)$"); |
51 | 0 | static cmsys::RegularExpression const nameRegex( |
52 | 0 | "^ *name (.*) \\(offset [0-9]+\\)$"); |
53 | 0 | cmUVIStream output(process.OutputStream()); |
54 | 0 | while (std::getline(output, line)) { |
55 | 0 | cmsys::RegularExpressionMatch cmdMatch; |
56 | 0 | if (rpathRegex.find(line.c_str(), cmdMatch)) { |
57 | | // NOLINTNEXTLINE(misc-redundant-expression) |
58 | 0 | if (!std::getline(output, line) || !std::getline(output, line)) { |
59 | 0 | this->SetError("Invalid output from otool"); |
60 | 0 | return false; |
61 | 0 | } |
62 | | |
63 | 0 | cmsys::RegularExpressionMatch pathMatch; |
64 | 0 | if (pathRegex.find(line.c_str(), pathMatch)) { |
65 | 0 | rpaths.push_back(pathMatch.match(1)); |
66 | 0 | } else { |
67 | 0 | this->SetError("Invalid output from otool"); |
68 | 0 | return false; |
69 | 0 | } |
70 | 0 | } else if (loadDylibRegex.find(line.c_str(), cmdMatch)) { |
71 | | // NOLINTNEXTLINE(misc-redundant-expression) |
72 | 0 | if (!std::getline(output, line) || !std::getline(output, line)) { |
73 | 0 | this->SetError("Invalid output from otool"); |
74 | 0 | return false; |
75 | 0 | } |
76 | | |
77 | 0 | cmsys::RegularExpressionMatch nameMatch; |
78 | 0 | if (nameRegex.find(line.c_str(), nameMatch)) { |
79 | 0 | libs.push_back(nameMatch.match(1)); |
80 | 0 | } else { |
81 | 0 | this->SetError("Invalid output from otool"); |
82 | 0 | return false; |
83 | 0 | } |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | if (!process.Wait()) { |
88 | 0 | this->SetError(cmStrCat("Failed to wait on otool process for:\n ", file)); |
89 | 0 | return false; |
90 | 0 | } |
91 | 0 | if (process.GetStatus(0).ExitStatus != 0) { |
92 | 0 | this->SetError(cmStrCat("Failed to run otool on:\n ", file)); |
93 | 0 | return false; |
94 | 0 | } |
95 | | |
96 | 0 | return true; |
97 | 0 | } |