blob: d062f1b6322670e86d64a38fbc492ae90419f188 [file]
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmLDConfigLDConfigTool.h"
#include <istream>
#include <string>
#include <vector>
#include "cmsys/RegularExpression.hxx"
#include "cmList.h"
#include "cmMakefile.h"
#include "cmRuntimeDependencyArchive.h"
#include "cmSystemTools.h"
#include "cmUVProcessChain.h"
#include "cmUVStream.h"
cmLDConfigLDConfigTool::cmLDConfigLDConfigTool(
cmRuntimeDependencyArchive* archive)
: cmLDConfigTool(archive)
{
}
bool cmLDConfigLDConfigTool::GetLDConfigPaths(std::vector<std::string>& paths)
{
std::string ldConfigPath =
this->Archive->GetMakefile()->GetSafeDefinition("CMAKE_LDCONFIG_COMMAND");
if (ldConfigPath.empty()) {
ldConfigPath = cmSystemTools::FindProgram(
"ldconfig", { "/sbin", "/usr/sbin", "/usr/local/sbin" });
if (ldConfigPath.empty()) {
this->Archive->SetError("Could not find ldconfig");
return false;
}
}
cmList ldConfigCommand{ ldConfigPath };
#ifdef __FreeBSD__
ldConfigCommand.emplace_back("-r"); // List directories
#else
ldConfigCommand.emplace_back("-v");
ldConfigCommand.emplace_back("-N"); // Don't rebuild the cache.
ldConfigCommand.emplace_back("-X"); // Don't update links.
#endif
cmUVProcessChainBuilder builder;
builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
.AddCommand(ldConfigCommand);
auto process = builder.Start();
if (!process.Valid() || process.GetStatus(0).SpawnResult != 0) {
this->Archive->SetError("Failed to start ldconfig process");
return false;
}
std::string line;
cmUVIStream output(process.OutputStream());
#ifdef __FreeBSD__
// FreeBSD ldconfig output:
// /var/run/ld-elf.so.hints:
// search directories: /lib:/usr/lib:...
// Extract everything after "search directories: " and split by ':'
static cmsys::RegularExpression regex("search directories: (.*)$");
while (std::getline(output, line)) {
cmsys::RegularExpressionMatch match;
if (!regex.find(line.c_str(), match))
continue;
auto allPaths = std::stringstream(match.match(1));
std::string path;
while (std::getline(allPaths, path, ':'))
if (!path.empty())
paths.push_back(path);
}
#else
// Linux ldconfig output:
// /lib64: (from <builtin>:0)
// Extract the "/lib64" part
static cmsys::RegularExpression const regex("^([^\t:]*):");
while (std::getline(output, line)) {
cmsys::RegularExpressionMatch match;
if (regex.find(line.c_str(), match)) {
paths.push_back(match.match(1));
}
}
#endif
if (!process.Wait()) {
this->Archive->SetError("Failed to wait on ldconfig process");
return false;
}
if (process.GetStatus(0).ExitStatus != 0) {
this->Archive->SetError("Failed to run ldconfig");
return false;
}
return true;
}