Skip to content

Commit fef2a02

Browse files
committed
Error when recursively globbing in the project root
Closes #48
1 parent fc74a5e commit fef2a02

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/cmake_generator.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static std::string format(const char *format, tsl::ordered_map<std::string, std:
3434
return s;
3535
}
3636

37-
static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs::path &toml_dir) {
37+
static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs::path &toml_dir, bool root_project) {
3838
std::vector<std::string> temp;
3939

4040
auto extract_suffix = [](const fs::path &base, const fs::path &full) {
@@ -47,6 +47,10 @@ static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs
4747
auto stem = name.filename().stem().string();
4848
auto ext = name.extension();
4949

50+
if (root_project && stem == "**" && name == name.filename()) {
51+
throw std::runtime_error("Recursive globbing not allowed in project root: " + name.string());
52+
}
53+
5054
if (stem == "*") {
5155
for (const auto &f : fs::directory_iterator(toml_dir / name.parent_path(), fs::directory_options::follow_directory_symlink)) {
5256
if (!f.is_directory() && f.path().extension() == ext) {
@@ -71,11 +75,11 @@ static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs
7175
return temp;
7276
}
7377

74-
static std::vector<std::string> expand_cmake_paths(const std::vector<std::string> &sources, const fs::path &toml_dir) {
78+
static std::vector<std::string> expand_cmake_paths(const std::vector<std::string> &sources, const fs::path &toml_dir, bool root_project) {
7579
// TODO: add duplicate checking
7680
std::vector<std::string> result;
7781
for (const auto &src : sources) {
78-
auto expanded = expand_cmake_path(src, toml_dir);
82+
auto expanded = expand_cmake_path(src, toml_dir, root_project);
7983
for (const auto &f : expanded) {
8084
result.push_back(f);
8185
}
@@ -476,6 +480,9 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
476480
throw std::runtime_error("No cmake.toml found!");
477481
}
478482

483+
// Root project doesn't have a parent
484+
auto root_project = parent_project == nullptr;
485+
479486
parser::Project project(parent_project, path, false);
480487
Generator gen(project);
481488

@@ -492,8 +499,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
492499
comment("See " + cmkr_url + " for more information");
493500
endl();
494501

495-
// Root project doesn't have a parent
496-
if (parent_project == nullptr) {
502+
if (root_project) {
497503
cmd("cmake_minimum_required")("VERSION", project.cmake_version).endl();
498504

499505
if (!project.allow_in_tree) {
@@ -814,7 +820,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
814820

815821
if (tmplate != nullptr) {
816822
gen.handle_condition(tmplate->outline.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
817-
auto sources = expand_cmake_paths(condition_sources, path);
823+
auto sources = expand_cmake_paths(condition_sources, path, root_project);
818824
if (sources.empty()) {
819825
auto source_key = condition.empty() ? "sources" : (condition + ".sources");
820826
throw std::runtime_error(target.name + " " + source_key + " wildcard found 0 files");
@@ -824,7 +830,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
824830
}
825831

826832
gen.handle_condition(target.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
827-
auto sources = expand_cmake_paths(condition_sources, path);
833+
auto sources = expand_cmake_paths(condition_sources, path, root_project);
828834
if (sources.empty()) {
829835
auto source_key = condition.empty() ? "sources" : (condition + ".sources");
830836
throw std::runtime_error(target.name + " " + source_key + " wildcard found 0 files");
@@ -1026,7 +1032,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
10261032
auto dirs = std::make_pair("DIRS", inst.dirs);
10271033
std::vector<std::string> files_data;
10281034
if (!inst.files.empty()) {
1029-
files_data = expand_cmake_paths(inst.files, path);
1035+
files_data = expand_cmake_paths(inst.files, path, root_project);
10301036
if (files_data.empty()) {
10311037
throw std::runtime_error("[[install]] files wildcard did not resolve to any files");
10321038
}

0 commit comments

Comments
 (0)