From 33d4cc41569cb138e908205791ef7c059bcca0d3 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Thu, 30 May 2024 00:36:14 +0200 Subject: [PATCH] Fixed globbing when multiple extensions are present in the file name --- docs/examples/globbing.md | 2 +- src/cmake_generator.cpp | 25 +++++++++++++------- tests/globbing/cmake.toml | 2 +- tests/globbing/mylib/include/mylib/mylib.hpp | 1 + tests/globbing/mylib/src/mylib/mylib.cxx | 1 + tests/globbing/mylib/src/mylib/mylib.gen.cxx | 5 ++++ 6 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 tests/globbing/mylib/src/mylib/mylib.cxx create mode 100644 tests/globbing/mylib/src/mylib/mylib.gen.cxx diff --git a/docs/examples/globbing.md b/docs/examples/globbing.md index 5ff0b65..326216e 100644 --- a/docs/examples/globbing.md +++ b/docs/examples/globbing.md @@ -20,7 +20,7 @@ description = "Globbing sources" [target.mylib] type = "static" alias = "mylib::mylib" -sources = ["mylib/**.hpp", "mylib/**.cpp"] +sources = ["mylib/**.hpp", "mylib/**.cpp", "mylib/**.gen.cxx"] include-directories = ["mylib/include"] # Single-folder glob in example/src/ diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index f3ed4b0..7ec1368 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -55,31 +55,38 @@ static std::string format(const char *format, const tsl::ordered_map expand_cmake_path(const fs::path &name, const fs::path &toml_dir, bool is_root_project) { - std::vector temp; - auto extract_suffix = [](const fs::path &base, const fs::path &full) { + auto const extract_suffix = [](const fs::path &base, const fs::path &full) { auto fullpath = full.string(); auto base_len = base.string().length(); - auto delet = fullpath.substr(base_len + 1, fullpath.length() - base_len); - return delet; + return fullpath.substr(base_len + 1, fullpath.length() - base_len); }; - - auto stem = name.filename().stem().string(); - auto ext = name.extension(); + auto const extract_path_parts = [](const fs::path &file_path) -> std::pair { + const auto path_as_string = file_path.string(); + auto const dot_position = path_as_string.find('.'); + if (dot_position != std::string::npos) { + return {path_as_string.substr(0, dot_position), path_as_string.substr(dot_position)}; + } + return {path_as_string, {}}; + }; + auto const path_parts = extract_path_parts(name.filename()); + auto const &stem = path_parts.first; + auto const &extension = path_parts.second; if (is_root_project && stem == "**" && name == name.filename()) { throw std::runtime_error("Recursive globbing not allowed in project root: " + name.string()); } + std::vector temp; if (stem == "*") { for (const auto &f : fs::directory_iterator(toml_dir / name.parent_path(), fs::directory_options::follow_directory_symlink)) { - if (!f.is_directory() && f.path().extension() == ext) { + if (!f.is_directory() && extract_path_parts(f.path().filename()).second == extension) { temp.push_back(extract_suffix(toml_dir, f)); } } } else if (stem == "**") { for (const auto &f : fs::recursive_directory_iterator(toml_dir / name.parent_path(), fs::directory_options::follow_directory_symlink)) { - if (!f.is_directory() && f.path().extension() == ext) { + if (!f.is_directory() && extract_path_parts(f.path().filename()).second == extension) { temp.push_back(extract_suffix(toml_dir, f.path())); } } diff --git a/tests/globbing/cmake.toml b/tests/globbing/cmake.toml index e174f84..4afe427 100644 --- a/tests/globbing/cmake.toml +++ b/tests/globbing/cmake.toml @@ -6,7 +6,7 @@ description = "Globbing sources" [target.mylib] type = "static" alias = "mylib::mylib" -sources = ["mylib/**.hpp", "mylib/**.cpp"] +sources = ["mylib/**.hpp", "mylib/**.cpp", "mylib/**.gen.cxx"] include-directories = ["mylib/include"] # Single-folder glob in example/src/ diff --git a/tests/globbing/mylib/include/mylib/mylib.hpp b/tests/globbing/mylib/include/mylib/mylib.hpp index 3774e8f..bffd6fa 100644 --- a/tests/globbing/mylib/include/mylib/mylib.hpp +++ b/tests/globbing/mylib/include/mylib/mylib.hpp @@ -4,4 +4,5 @@ namespace mylib { std::string message(); +std::string message2(); } diff --git a/tests/globbing/mylib/src/mylib/mylib.cxx b/tests/globbing/mylib/src/mylib/mylib.cxx new file mode 100644 index 0000000..bf14392 --- /dev/null +++ b/tests/globbing/mylib/src/mylib/mylib.cxx @@ -0,0 +1 @@ +#error This file should not be included in the sources diff --git a/tests/globbing/mylib/src/mylib/mylib.gen.cxx b/tests/globbing/mylib/src/mylib/mylib.gen.cxx new file mode 100644 index 0000000..c700be6 --- /dev/null +++ b/tests/globbing/mylib/src/mylib/mylib.gen.cxx @@ -0,0 +1,5 @@ +#include + +std::string mylib::message2() { + return "cmkr is awesome2!"; +}