Skip to content

Commit

Permalink
search: add name-like search paths
Browse files Browse the repository at this point in the history
This adds support for finding cps files in `$ROOT/$name/*.cps`, as well
as files with capitalization.
  • Loading branch information
dcbaker committed Sep 26, 2024
1 parent 2f2b172 commit fea8d32
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/cps/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <tl/expected.hpp>

#include <algorithm>
#include <cctype>
#include <deque>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -136,19 +137,46 @@ namespace cps::search {
return std::vector<fs::path>{name};
}

// If the given name is not all lower, we need to search that as well.
std::vector<std::string> names{std::string{name}};
std::string lc{name};
std::transform(lc.begin(), lc.end(), lc.begin(), [](const unsigned char ch) { return std::tolower(ch); });
if (name != lc) {
names.push_back(lc);
}

// TODO: Need something like pkgconf's --personality option
// TODO: we likely either need to return all possible files, or load
// a file
// TODO: what to do about finding multiple versions of the same
// dependency?

// Each prefix must be searched for (assuming Name is the term):
//
// - Name/*.cps
// - name/*.cps
// - Name.cps
// - name.cps
//
// TODO: should we allow symlinks?
auto && paths = search_paths(env);
std::vector<fs::path> found{};
for (auto && path : paths) {
if (fs::is_directory(path)) {
// TODO: <name-like>
const fs::path file = path / fmt::format("{}.cps", name);
if (fs::is_regular_file(file)) {
found.push_back(file);
for (auto && cname : names) {
for (auto && path : paths) {
if (fs::is_directory(path)) {
const fs::path namelike = path / cname;
if (fs::is_directory(namelike)) {
for (auto && trial : fs::directory_iterator(namelike)) {
auto && tpath = trial.path();
if (fs::is_regular_file(tpath) && tpath.extension() == ".cps") {
found.push_back(tpath);
}
}
}
const fs::path file = path / fmt::format("{}.cps", cname);
if (fs::is_regular_file(file)) {
found.push_back(file);
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/cases/cps-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,21 @@ name = "Star components override by name"
cps = "full"
args = ["flags", "--component", "star_values_override", "--cflags", "--print-errors"]
expected = "-fvectorize -I/usr/local/include -I/opt/include -DBAR=2 -DFOO=1 -DOTHER"

[[case]]
name = "name-like search in directory"
cps = "dir"
args = ["flags", "--cflags", "--libs", "--print-errors"]
expected = "-I/usr/include/dir -l/usr/lib/libdir.so"

[[case]]
name = "name-like search in directory different case"
cps = "DiR"
args = ["flags", "--cflags", "--libs", "--print-errors"]
expected = "-I/usr/include/dir -l/usr/lib/libdir.so"

[[case]]
name = "name-like search with different case"
cps = "MiNimAl"
args = ["flags", "--cflags", "--print-errors"]
expected = "-fopenmp -I/usr/local/include -I/opt/include -DFOO=1 -DBAR=2 -DOTHER"
17 changes: 17 additions & 0 deletions tests/cps-files/lib/cps/dir/dir-1.2.cps
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "dir",
"cps_version": "0.12.0",
"version": "1.2.0",
"components": {
"default": {
"type": "archive",
"includes": {
"c": ["/usr/include/dir"]
},
"location": "/usr/lib/libdir.so"
}
},
"default_components": [
"default"
]
}

0 comments on commit fea8d32

Please sign in to comment.