Skip to content

Commit 6d1cf3d

Browse files
committed
search: add name-like search paths
This adds support for finding cps files in `$ROOT/$name/*.cps`, as well as files with capitalization.
1 parent 2f2b172 commit 6d1cf3d

3 files changed

Lines changed: 70 additions & 7 deletions

File tree

src/cps/search.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <tl/expected.hpp>
1515

1616
#include <algorithm>
17+
#include <cctype>
1718
#include <deque>
1819
#include <filesystem>
1920
#include <fstream>
@@ -136,19 +137,46 @@ namespace cps::search {
136137
return std::vector<fs::path>{name};
137138
}
138139

140+
// If the given name is not all lower, we need to search that as well.
141+
std::vector<std::string> names{std::string{name}};
142+
std::string lc{name};
143+
std::transform(lc.begin(), lc.end(), lc.begin(), [](const unsigned char ch) { return std::tolower(ch); });
144+
if (name != lc) {
145+
names.push_back(lc);
146+
}
147+
139148
// TODO: Need something like pkgconf's --personality option
140149
// TODO: we likely either need to return all possible files, or load
141150
// a file
142151
// TODO: what to do about finding multiple versions of the same
143152
// dependency?
153+
154+
// Each prefix must be searched for (assuming Name is the term):
155+
//
156+
// - Name/*.cps
157+
// - name/*.cps
158+
// - Name.cps
159+
// - name.cps
160+
//
161+
// TODO: should we allow symlinks?
144162
auto && paths = search_paths(env);
145163
std::vector<fs::path> found{};
146-
for (auto && path : paths) {
147-
if (fs::is_directory(path)) {
148-
// TODO: <name-like>
149-
const fs::path file = path / fmt::format("{}.cps", name);
150-
if (fs::is_regular_file(file)) {
151-
found.push_back(file);
164+
for (auto && cname : names) {
165+
for (auto && path : paths) {
166+
if (fs::is_directory(path)) {
167+
const fs::path namelike = path / cname;
168+
if (fs::is_directory(namelike)) {
169+
for (auto && trial : fs::directory_iterator(namelike)) {
170+
auto && tpath = trial.path();
171+
if (fs::is_regular_file(tpath) && tpath.extension() == ".cps") {
172+
found.push_back(tpath);
173+
}
174+
}
175+
}
176+
const fs::path file = path / fmt::format("{}.cps", cname);
177+
if (fs::is_regular_file(file)) {
178+
found.push_back(file);
179+
}
152180
}
153181
}
154182
}
@@ -402,7 +430,7 @@ namespace cps::search {
402430

403431
} // namespace
404432

405-
Result::Result(){};
433+
Result::Result() {};
406434

407435
tl::expected<Result, std::string> find_package(std::string_view name, Env env) {
408436
return find_package(name, {}, true, env, std::nullopt);

tests/cases/cps-config.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,21 @@ name = "Star components override by name"
120120
cps = "full"
121121
args = ["flags", "--component", "star_values_override", "--cflags", "--print-errors"]
122122
expected = "-fvectorize -I/usr/local/include -I/opt/include -DBAR=2 -DFOO=1 -DOTHER"
123+
124+
[[case]]
125+
name = "name-like search in directory"
126+
cps = "dir"
127+
args = ["flags", "--cflags", "--libs", "--print-errors"]
128+
expected = "-I/usr/include/dir -l/usr/lib/libdir.so"
129+
130+
[[case]]
131+
name = "name-like search in directory different case"
132+
cps = "DiR"
133+
args = ["flags", "--cflags", "--libs", "--print-errors"]
134+
expected = "-I/usr/include/dir -l/usr/lib/libdir.so"
135+
136+
[[case]]
137+
name = "name-like search with different case"
138+
cps = "MiNimAl"
139+
args = ["flags", "--cflags", "--print-errors"]
140+
expected = "-fopenmp -I/usr/local/include -I/opt/include -DFOO=1 -DBAR=2 -DOTHER"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "dir",
3+
"cps_version": "0.12.0",
4+
"version": "1.2.0",
5+
"components": {
6+
"default": {
7+
"type": "archive",
8+
"includes": {
9+
"c": ["/usr/include/dir"]
10+
},
11+
"location": "/usr/lib/libdir.so"
12+
}
13+
},
14+
"default_components": [
15+
"default"
16+
]
17+
}

0 commit comments

Comments
 (0)