Skip to content

Commit 90519ab

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 72e4089 commit 90519ab

3 files changed

Lines changed: 69 additions & 6 deletions

File tree

src/cps/search.cpp

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

1717
#include <algorithm>
18+
#include <cctype>
1819
#include <deque>
1920
#include <filesystem>
2021
#include <fstream>
@@ -139,19 +140,46 @@ namespace cps::search {
139140
return std::vector<fs::path>{name};
140141
}
141142

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

tests/cases/cps-config.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,21 @@ name = "Sets compat_version"
127127
cps = "needs-compat-version"
128128
args = ["flags", "--libs", "--print-errors"]
129129
expected = "-l/usr/lib/libfoo.a"
130+
131+
[[case]]
132+
name = "name-like search in directory"
133+
cps = "dir"
134+
args = ["flags", "--cflags", "--libs", "--print-errors"]
135+
expected = "-I/usr/include/dir -l/usr/lib/libdir.so"
136+
137+
[[case]]
138+
name = "name-like search in directory different case"
139+
cps = "DiR"
140+
args = ["flags", "--cflags", "--libs", "--print-errors"]
141+
expected = "-I/usr/include/dir -l/usr/lib/libdir.so"
142+
143+
[[case]]
144+
name = "name-like search with different case"
145+
cps = "MiNimAl"
146+
args = ["flags", "--cflags", "--print-errors"]
147+
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)