From 868d991c7c304f78ea9daf3dfc077ecb01c94233 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 2 Apr 2021 17:33:33 +0200 Subject: [PATCH] Fix path expansion for subdirectories --- src/cmkrlib/gen.cpp | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/cmkrlib/gen.cpp b/src/cmkrlib/gen.cpp index e5677bf..7f0faef 100644 --- a/src/cmkrlib/gen.cpp +++ b/src/cmkrlib/gen.cpp @@ -37,24 +37,34 @@ std::string format(const char *fmt, Args... args) { return temp; } -static std::vector expand_cmake_path(const fs::path &p) { +static std::vector expand_cmake_path(const fs::path &name, const fs::path &toml_dir) { std::vector temp; - auto stem = p.filename().stem().string(); - auto ext = p.extension(); + + auto extract_suffix = [](const fs::path &base, const fs::path &full) { + // TODO: delet this + auto fullpath = full.string(); + auto base_len = base.string().length(); + auto delet = fullpath.substr(base_len + 1, fullpath.length() - base_len); + return delet; + }; + + auto stem = name.filename().stem().string(); + auto ext = name.extension(); + if (stem == "*") { - for (const auto &f : fs::directory_iterator(p.parent_path(), fs::directory_options::follow_directory_symlink)) { + 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) { - temp.push_back(f.path().string()); + temp.push_back(extract_suffix(toml_dir, f)); } } } else if (stem == "**") { - for (const auto &f : fs::recursive_directory_iterator(p.parent_path(), fs::directory_options::follow_directory_symlink)) { + 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) { - temp.push_back(f.path().string()); + temp.push_back(extract_suffix(toml_dir, f.path())); } } } else { - temp.push_back(p.string()); + temp.push_back(name.string()); } // Normalize all paths to work with CMake (it needs a / on Windows as well) for (auto &path : temp) { @@ -63,12 +73,11 @@ static std::vector expand_cmake_path(const fs::path &p) { return temp; } -static std::vector expand_cmake_paths(const std::vector &sources) { +static std::vector expand_cmake_paths(const std::vector &sources, const fs::path &toml_dir) { // TODO: add duplicate checking std::vector result; for (const auto &src : sources) { - auto path = fs::path(src); - auto expanded = expand_cmake_path(path); + auto expanded = expand_cmake_path(src, toml_dir); for (const auto &f : expanded) { result.push_back(f); } @@ -476,7 +485,7 @@ int generate_cmake(const char *path, bool root) { inject_cmake(target.cmake_before); if (!target.sources.empty()) { - auto sources = expand_cmake_paths(target.sources); + auto sources = expand_cmake_paths(target.sources, path); if (sources.empty()) { throw std::runtime_error(target.name + " sources wildcard found 0 files"); } @@ -577,7 +586,7 @@ int generate_cmake(const char *path, bool root) { auto dirs = std::make_pair("DIRS", inst.dirs); std::vector files_data; if (!inst.files.empty()) { - files_data = expand_cmake_paths(inst.files); + files_data = expand_cmake_paths(inst.files, path); if (files_data.empty()) { throw std::runtime_error("[[install]] files wildcard did not resolve to any files"); } @@ -616,8 +625,9 @@ int generate_cmake(const char *path, bool root) { } for (const auto &sub : cmake.subdirs) { - if (fs::exists(fs::path(sub) / "cmake.toml")) - generate_cmake(sub.c_str(), false); + auto subpath = fs::path(path) / fs::path(sub); + if (fs::exists(subpath / "cmake.toml")) + generate_cmake(subpath.string().c_str(), false); } return 0;