From 1d1ea3fdfd022ad6ac12190b549cec13cc35cb14 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 27 Nov 2020 16:00:54 +0100 Subject: [PATCH] Fix path generation on Windows --- cmake/CMakeLists.txt | 3 ++- src/gen.cpp | 16 ++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 4fb8b85..68219a3 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -2,6 +2,7 @@ # Regenerate CMakeLists.txt file when necessary include(cmkr.cmake OPTIONAL RESULT_VARIABLE CMKR_INCLUDE_RESULT) + if(CMKR_INCLUDE_RESULT) cmkr() endif() @@ -14,7 +15,7 @@ set(example_PROJECT_VERSION 0.1.0) project(example VERSION ${example_PROJECT_VERSION}) set(EXAMPLE_SOURCES - "src/example.cpp" + src/example.cpp ) add_executable(example ${EXAMPLE_SOURCES}) diff --git a/src/gen.cpp b/src/gen.cpp index 920a2db..1bd33dc 100644 --- a/src/gen.cpp +++ b/src/gen.cpp @@ -38,17 +38,21 @@ std::string format(const char *fmt, Args... args) { return temp; } -std::vector expand_path(const fs::path &p) { - std::vector temp; +static std::vector expand_cmake_path(const fs::path &p) { + std::vector temp; if (p.filename().stem().string() == "*") { auto ext = p.extension(); for (const auto &f : fs::directory_iterator(p.parent_path())) { if (f.path().extension() == ext) { - temp.push_back(f.path()); + temp.push_back(f.path().string()); } } } else { - temp.push_back(p); + temp.push_back(p.string()); + } + // Normalize all paths to work with CMake (it needs a / on Windows as well) + for(auto& path : temp) { + std::replace(path.begin(), path.end(), '\\', '/'); } return temp; } @@ -263,7 +267,7 @@ int generate_cmake(const char *path) { ss << "set(" << detail::to_upper(bin.name) << "_SOURCES\n"; for (const auto &src : bin.sources) { auto path = fs::path(src); - auto expanded = detail::expand_path(path); + auto expanded = detail::expand_cmake_path(path); for (const auto &f : expanded) { ss << "\t" << f << "\n"; } @@ -357,7 +361,7 @@ int generate_cmake(const char *path) { if (!inst.files.empty()) { ss << "\tFILES "; for (const auto &file : inst.files) { - auto path = detail::expand_path(fs::path(file)); + auto path = detail::expand_cmake_path(fs::path(file)); for (const auto &f : path) ss << f << " "; }