resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmLDConfigLDConfigTool.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 "cmLDConfigLDConfigTool.h"
5
6 #include <istream>
7 #include <string>
8 #include <vector>
9
10 #include "cmsys/RegularExpression.hxx"
11
12 #include "cmMakefile.h"
13 #include "cmRuntimeDependencyArchive.h"
14 #include "cmStringAlgorithms.h"
15 #include "cmSystemTools.h"
16 #include "cmUVProcessChain.h"
17
18 cmLDConfigLDConfigTool::cmLDConfigLDConfigTool(
19   cmRuntimeDependencyArchive* archive)
20   : cmLDConfigTool(archive)
21 {
22 }
23
24 bool cmLDConfigLDConfigTool::GetLDConfigPaths(std::vector<std::string>& paths)
25 {
26   std::string ldConfigPath =
27     this->Archive->GetMakefile()->GetSafeDefinition("CMAKE_LDCONFIG_COMMAND");
28   if (ldConfigPath.empty()) {
29     ldConfigPath = cmSystemTools::FindProgram(
30       "ldconfig", { "/sbin", "/usr/sbin", "/usr/local/sbin" });
31     if (ldConfigPath.empty()) {
32       this->Archive->SetError("Could not find ldconfig");
33       return false;
34     }
35   }
36
37   std::vector<std::string> ldConfigCommand = cmExpandedList(ldConfigPath);
38   ldConfigCommand.emplace_back("-v");
39   ldConfigCommand.emplace_back("-N"); // Don't rebuild the cache.
40   ldConfigCommand.emplace_back("-X"); // Don't update links.
41
42   cmUVProcessChainBuilder builder;
43   builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
44     .AddCommand(ldConfigCommand);
45   auto process = builder.Start();
46   if (!process.Valid()) {
47     this->Archive->SetError("Failed to start ldconfig process");
48     return false;
49   }
50
51   std::string line;
52   static const cmsys::RegularExpression regex("^([^\t:]*):");
53   while (std::getline(*process.OutputStream(), line)) {
54     cmsys::RegularExpressionMatch match;
55     if (regex.find(line.c_str(), match)) {
56       paths.push_back(match.match(1));
57     }
58   }
59
60   if (!process.Wait()) {
61     this->Archive->SetError("Failed to wait on ldconfig process");
62     return false;
63   }
64   auto status = process.GetStatus();
65   if (!status[0] || status[0]->ExitStatus != 0) {
66     this->Archive->SetError("Failed to run ldconfig");
67     return false;
68   }
69
70   return true;
71 }