Skip to content

Commit 33d4cc4

Browse files
Fixed globbing when multiple extensions are present in the file name
1 parent 9288c8a commit 33d4cc4

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

docs/examples/globbing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ description = "Globbing sources"
2020
[target.mylib]
2121
type = "static"
2222
alias = "mylib::mylib"
23-
sources = ["mylib/**.hpp", "mylib/**.cpp"]
23+
sources = ["mylib/**.hpp", "mylib/**.cpp", "mylib/**.gen.cxx"]
2424
include-directories = ["mylib/include"]
2525

2626
# Single-folder glob in example/src/

src/cmake_generator.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,38 @@ static std::string format(const char *format, const tsl::ordered_map<std::string
5555
}
5656

5757
static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs::path &toml_dir, bool is_root_project) {
58-
std::vector<std::string> temp;
5958

60-
auto extract_suffix = [](const fs::path &base, const fs::path &full) {
59+
auto const extract_suffix = [](const fs::path &base, const fs::path &full) {
6160
auto fullpath = full.string();
6261
auto base_len = base.string().length();
63-
auto delet = fullpath.substr(base_len + 1, fullpath.length() - base_len);
64-
return delet;
62+
return fullpath.substr(base_len + 1, fullpath.length() - base_len);
6563
};
66-
67-
auto stem = name.filename().stem().string();
68-
auto ext = name.extension();
64+
auto const extract_path_parts = [](const fs::path &file_path) -> std::pair<std::string, std::string> {
65+
const auto path_as_string = file_path.string();
66+
auto const dot_position = path_as_string.find('.');
67+
if (dot_position != std::string::npos) {
68+
return {path_as_string.substr(0, dot_position), path_as_string.substr(dot_position)};
69+
}
70+
return {path_as_string, {}};
71+
};
72+
auto const path_parts = extract_path_parts(name.filename());
73+
auto const &stem = path_parts.first;
74+
auto const &extension = path_parts.second;
6975

7076
if (is_root_project && stem == "**" && name == name.filename()) {
7177
throw std::runtime_error("Recursive globbing not allowed in project root: " + name.string());
7278
}
7379

80+
std::vector<std::string> temp;
7481
if (stem == "*") {
7582
for (const auto &f : fs::directory_iterator(toml_dir / name.parent_path(), fs::directory_options::follow_directory_symlink)) {
76-
if (!f.is_directory() && f.path().extension() == ext) {
83+
if (!f.is_directory() && extract_path_parts(f.path().filename()).second == extension) {
7784
temp.push_back(extract_suffix(toml_dir, f));
7885
}
7986
}
8087
} else if (stem == "**") {
8188
for (const auto &f : fs::recursive_directory_iterator(toml_dir / name.parent_path(), fs::directory_options::follow_directory_symlink)) {
82-
if (!f.is_directory() && f.path().extension() == ext) {
89+
if (!f.is_directory() && extract_path_parts(f.path().filename()).second == extension) {
8390
temp.push_back(extract_suffix(toml_dir, f.path()));
8491
}
8592
}

tests/globbing/cmake.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description = "Globbing sources"
66
[target.mylib]
77
type = "static"
88
alias = "mylib::mylib"
9-
sources = ["mylib/**.hpp", "mylib/**.cpp"]
9+
sources = ["mylib/**.hpp", "mylib/**.cpp", "mylib/**.gen.cxx"]
1010
include-directories = ["mylib/include"]
1111

1212
# Single-folder glob in example/src/

tests/globbing/mylib/include/mylib/mylib.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
namespace mylib {
66
std::string message();
7+
std::string message2();
78
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#error This file should not be included in the sources
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <mylib/mylib.hpp>
2+
3+
std::string mylib::message2() {
4+
return "cmkr is awesome2!";
5+
}

0 commit comments

Comments
 (0)