From d3829fb6cdba2dc2ee5e10964fcbed7800a584a0 Mon Sep 17 00:00:00 2001 From: Anthony Printup <92564080+anthonyprintup@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:49:59 +0200 Subject: [PATCH] Trim additional new lines at the end of the generated CMakeLists.txt --- src/cmake_generator.cpp | 15 ++++++++++++--- tests/CMakeLists.txt | 1 - 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index 5ba9282..1f50deb 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -1378,10 +1378,19 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { } } + // Fetch the generated CMakeLists.txt output from the stringstream buffer + auto generated_cmake = ss.str(); + + // Make sure the file ends in a single newline + while (!generated_cmake.empty() && std::isspace(generated_cmake.back())) { + generated_cmake.pop_back(); + } + generated_cmake += '\n'; + // Generate CMakeLists.txt auto list_path = fs::path(path) / "CMakeLists.txt"; - auto should_regenerate = [&list_path, &ss]() { + auto should_regenerate = [&list_path, &generated_cmake]() { if (!fs::exists(list_path)) return true; @@ -1391,11 +1400,11 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { } std::string data((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); - return data != ss.str(); + return data != generated_cmake; }(); if (should_regenerate) { - create_file(list_path, ss.str()); + create_file(list_path, generated_cmake); } auto generate_subdir = [path, &project](const fs::path &sub) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7478c3d..448180a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -109,4 +109,3 @@ add_test( "$" build ) -