cmake/Source/cmSearchPath.cxx

244 lines
7.2 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2015-04-27 22:25:09 +02:00
#include "cmSearchPath.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include <algorithm>
#include <cassert>
#include <utility>
2022-08-04 22:12:04 +02:00
#include <cm/optional>
2016-07-09 11:21:54 +02:00
#include "cmFindCommon.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2022-08-04 22:12:04 +02:00
#include "cmWindowsRegistry.h"
2015-04-27 22:25:09 +02:00
cmSearchPath::cmSearchPath(cmFindCommon* findCmd)
2016-07-09 11:21:54 +02:00
: FC(findCmd)
2015-04-27 22:25:09 +02:00
{
}
2019-11-11 23:01:05 +01:00
cmSearchPath::~cmSearchPath() = default;
2015-04-27 22:25:09 +02:00
2022-03-29 21:10:50 +02:00
void cmSearchPath::ExtractWithout(const std::set<std::string>& ignorePaths,
const std::set<std::string>& ignorePrefixes,
2015-04-27 22:25:09 +02:00
std::vector<std::string>& outPaths,
bool clear) const
{
2016-07-09 11:21:54 +02:00
if (clear) {
2015-04-27 22:25:09 +02:00
outPaths.clear();
2016-07-09 11:21:54 +02:00
}
2022-03-29 21:10:50 +02:00
for (auto const& path : this->Paths) {
if (ignorePaths.count(path.Path) == 0 &&
ignorePrefixes.count(path.Prefix) == 0) {
outPaths.push_back(path.Path);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
void cmSearchPath::AddPath(const std::string& path)
{
2022-03-29 21:10:50 +02:00
this->AddPathInternal(path, "");
2015-04-27 22:25:09 +02:00
}
void cmSearchPath::AddUserPath(const std::string& path)
{
2018-01-26 17:06:56 +01:00
assert(this->FC != nullptr);
2015-04-27 22:25:09 +02:00
std::vector<std::string> outPaths;
2022-08-04 22:12:04 +02:00
cmWindowsRegistry registry(*this->FC->Makefile,
cmWindowsRegistry::SimpleTypes);
auto expandedPaths = registry.ExpandExpression(path, this->FC->RegistryView);
if (expandedPaths) {
for (const auto& expandedPath : expandedPaths.value()) {
cmSystemTools::GlobDirs(expandedPath, outPaths);
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Process them all from the current directory
2018-01-26 17:06:56 +01:00
for (std::string const& p : outPaths) {
2018-10-28 12:09:07 +01:00
this->AddPathInternal(
2022-03-29 21:10:50 +02:00
p, "", this->FC->Makefile->GetCurrentSourceDirectory().c_str());
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
void cmSearchPath::AddCMakePath(const std::string& variable)
{
2018-01-26 17:06:56 +01:00
assert(this->FC != nullptr);
2015-04-27 22:25:09 +02:00
// Get a path from a CMake variable.
2021-11-20 13:41:27 +01:00
if (cmValue value = this->FC->Makefile->GetDefinition(variable)) {
2021-09-14 00:13:48 +02:00
std::vector<std::string> expanded = cmExpandedList(*value);
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& p : expanded) {
2018-10-28 12:09:07 +01:00
this->AddPathInternal(
2022-03-29 21:10:50 +02:00
p, "", this->FC->Makefile->GetCurrentSourceDirectory().c_str());
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
void cmSearchPath::AddEnvPath(const std::string& variable)
{
std::vector<std::string> expanded;
cmSystemTools::GetPath(expanded, variable.c_str());
2018-01-26 17:06:56 +01:00
for (std::string const& p : expanded) {
2022-03-29 21:10:50 +02:00
this->AddPathInternal(p, "");
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
void cmSearchPath::AddCMakePrefixPath(const std::string& variable)
{
2018-01-26 17:06:56 +01:00
assert(this->FC != nullptr);
2015-04-27 22:25:09 +02:00
// Get a path from a CMake variable.
2021-11-20 13:41:27 +01:00
if (cmValue value = this->FC->Makefile->GetDefinition(variable)) {
2021-09-14 00:13:48 +02:00
std::vector<std::string> expanded = cmExpandedList(*value);
2015-04-27 22:25:09 +02:00
2018-10-28 12:09:07 +01:00
this->AddPrefixPaths(
expanded, this->FC->Makefile->GetCurrentSourceDirectory().c_str());
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
2015-08-17 11:37:30 +02:00
static std::string cmSearchPathStripBin(std::string const& s)
{
// If the path is a PREFIX/bin case then add its parent instead.
2016-07-09 11:21:54 +02:00
if ((cmHasLiteralSuffix(s, "/bin")) || (cmHasLiteralSuffix(s, "/sbin"))) {
2015-08-17 11:37:30 +02:00
return cmSystemTools::GetFilenamePath(s);
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
return s;
2015-08-17 11:37:30 +02:00
}
void cmSearchPath::AddEnvPrefixPath(const std::string& variable, bool stripBin)
2015-04-27 22:25:09 +02:00
{
std::vector<std::string> expanded;
cmSystemTools::GetPath(expanded, variable.c_str());
2016-07-09 11:21:54 +02:00
if (stripBin) {
2015-08-17 11:37:30 +02:00
std::transform(expanded.begin(), expanded.end(), expanded.begin(),
cmSearchPathStripBin);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
this->AddPrefixPaths(expanded);
}
void cmSearchPath::AddSuffixes(const std::vector<std::string>& suffixes)
{
2022-03-29 21:10:50 +02:00
std::vector<PathWithPrefix> inPaths;
2015-04-27 22:25:09 +02:00
inPaths.swap(this->Paths);
2016-07-09 11:21:54 +02:00
this->Paths.reserve(inPaths.size() * (suffixes.size() + 1));
2015-04-27 22:25:09 +02:00
2022-03-29 21:10:50 +02:00
for (PathWithPrefix& inPath : inPaths) {
cmSystemTools::ConvertToUnixSlashes(inPath.Path);
cmSystemTools::ConvertToUnixSlashes(inPath.Prefix);
2015-04-27 22:25:09 +02:00
// if *i is only / then do not add a //
// this will get incorrectly considered a network
// path on windows and cause huge delays.
2022-03-29 21:10:50 +02:00
std::string p = inPath.Path;
2019-11-11 23:01:05 +01:00
if (!p.empty() && p.back() != '/') {
2015-04-27 22:25:09 +02:00
p += "/";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Combine with all the suffixes
2018-01-26 17:06:56 +01:00
for (std::string const& suffix : suffixes) {
2022-03-29 21:10:50 +02:00
this->Paths.push_back(PathWithPrefix{ p + suffix, inPath.Prefix });
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// And now the original w/o any suffix
2018-04-23 21:13:27 +02:00
this->Paths.push_back(std::move(inPath));
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
void cmSearchPath::AddPrefixPaths(const std::vector<std::string>& paths,
2016-07-09 11:21:54 +02:00
const char* base)
2015-04-27 22:25:09 +02:00
{
2018-01-26 17:06:56 +01:00
assert(this->FC != nullptr);
2015-04-27 22:25:09 +02:00
// default for programs
std::string subdir = "bin";
2016-07-09 11:21:54 +02:00
if (this->FC->CMakePathName == "INCLUDE") {
2015-04-27 22:25:09 +02:00
subdir = "include";
2016-07-09 11:21:54 +02:00
} else if (this->FC->CMakePathName == "LIBRARY") {
2015-04-27 22:25:09 +02:00
subdir = "lib";
2016-07-09 11:21:54 +02:00
} else if (this->FC->CMakePathName == "FRAMEWORK") {
2018-01-26 17:06:56 +01:00
subdir.clear(); // ? what to do for frameworks ?
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& path : paths) {
std::string dir = path;
2019-11-11 23:01:05 +01:00
if (!subdir.empty() && !dir.empty() && dir.back() != '/') {
2015-04-27 22:25:09 +02:00
dir += "/";
2016-07-09 11:21:54 +02:00
}
2022-03-29 21:10:50 +02:00
std::string prefix = dir;
if (!prefix.empty() && prefix != "/") {
prefix.erase(prefix.size() - 1);
}
2016-07-09 11:21:54 +02:00
if (subdir == "include" || subdir == "lib") {
2021-11-20 13:41:27 +01:00
cmValue arch =
2015-04-27 22:25:09 +02:00
this->FC->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE");
2021-09-14 00:13:48 +02:00
if (cmNonempty(arch)) {
2023-05-23 16:38:00 +02:00
std::string archNoUnknown = *arch;
auto unknownAtPos = archNoUnknown.find("-unknown-");
bool foundUnknown = unknownAtPos != std::string::npos;
if (foundUnknown) {
// Replace "-unknown-" with "-".
archNoUnknown.replace(unknownAtPos, 9, "-");
}
2020-08-30 11:54:41 +02:00
if (this->FC->Makefile->IsDefinitionSet("CMAKE_SYSROOT") &&
this->FC->Makefile->IsDefinitionSet(
"CMAKE_PREFIX_LIBRARY_ARCHITECTURE")) {
2023-05-23 16:38:00 +02:00
if (foundUnknown) {
this->AddPathInternal(cmStrCat('/', archNoUnknown, dir, subdir),
cmStrCat('/', archNoUnknown, prefix), base);
}
2022-03-29 21:10:50 +02:00
this->AddPathInternal(cmStrCat('/', *arch, dir, subdir),
cmStrCat('/', *arch, prefix), base);
2020-08-30 11:54:41 +02:00
} else {
2023-05-23 16:38:00 +02:00
if (foundUnknown) {
this->AddPathInternal(cmStrCat(dir, subdir, '/', archNoUnknown),
prefix, base);
}
2022-03-29 21:10:50 +02:00
this->AddPathInternal(cmStrCat(dir, subdir, '/', *arch), prefix,
base);
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::string add = dir + subdir;
2016-07-09 11:21:54 +02:00
if (add != "/") {
2022-03-29 21:10:50 +02:00
this->AddPathInternal(add, prefix, base);
2016-07-09 11:21:54 +02:00
}
if (subdir == "bin") {
2022-03-29 21:10:50 +02:00
this->AddPathInternal(dir + "sbin", prefix, base);
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
if (!subdir.empty() && path != "/") {
2022-03-29 21:10:50 +02:00
this->AddPathInternal(path, prefix, base);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
2022-03-29 21:10:50 +02:00
void cmSearchPath::AddPathInternal(const std::string& path,
const std::string& prefix, const char* base)
2015-04-27 22:25:09 +02:00
{
2018-01-26 17:06:56 +01:00
assert(this->FC != nullptr);
2015-04-27 22:25:09 +02:00
2022-03-29 21:10:50 +02:00
std::string collapsedPath = cmSystemTools::CollapseFullPath(path, base);
2015-04-27 22:25:09 +02:00
2022-03-29 21:10:50 +02:00
if (collapsedPath.empty()) {
2015-04-27 22:25:09 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2022-03-29 21:10:50 +02:00
std::string collapsedPrefix;
if (!prefix.empty()) {
collapsedPrefix = cmSystemTools::CollapseFullPath(prefix, base);
}
2015-04-27 22:25:09 +02:00
// Insert the path if has not already been emitted.
2022-03-29 21:10:50 +02:00
PathWithPrefix pathWithPrefix{ std::move(collapsedPath),
std::move(collapsedPrefix) };
if (this->FC->SearchPathsEmitted.insert(pathWithPrefix).second) {
this->Paths.emplace_back(std::move(pathWithPrefix));
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}