diff --git a/src/cmake_generator.cpp b/src/cmake_generator.cpp index 389b3d5..42072b8 100644 --- a/src/cmake_generator.cpp +++ b/src/cmake_generator.cpp @@ -397,6 +397,14 @@ struct Generator { } } } + + void conditional_includes(const parser::ConditionVector &include) { + handle_condition(include, [this](const std::string &, const std::vector &includes) { inject_includes(includes); }); + } + + void conditional_cmake(const parser::Condition &cmake) { + handle_condition(cmake, [this](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + } }; struct ConditionScope { @@ -482,8 +490,6 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { auto cmd = [&gen](const std::string &command) { return gen.cmd(command); }; auto comment = [&gen](const std::string &comment) { return gen.comment(comment); }; auto endl = [&gen]() { gen.endl(); }; - auto inject_includes = [&gen](const std::vector &includes) { gen.inject_includes(includes); }; - auto inject_cmake = [&gen](const std::string &cmake) { gen.inject_cmake(cmake); }; std::string cmkr_url = "https://github.com/build-cpp/cmkr"; comment("This file is automatically generated from cmake.toml - DO NOT EDIT"); @@ -589,8 +595,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { endl(); } - gen.handle_condition(project.include_before, [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(project.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(project.include_before); + gen.conditional_cmake(project.cmake_before); if (!project.project_name.empty()) { auto languages = std::make_pair("LANGUAGES", project.project_languages); @@ -599,8 +605,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { cmd("project")(project.project_name, languages, version, description).endl(); } - gen.handle_condition(project.include_after, [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(project.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(project.include_after); + gen.conditional_cmake(project.cmake_after); if (!project.vcpkg.packages.empty()) { // Allow the user to specify a url or derive it from the version @@ -702,9 +708,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { for (const auto &content : project.contents) { ConditionScope cs(gen, content.condition); - gen.handle_condition(content.include_before, - [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(content.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(content.include_before); + gen.conditional_cmake(content.cmake_before); std::string version_info = ""; if (content.arguments.contains("GIT_TAG")) { @@ -720,9 +725,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { ss << ")\n"; cmd("FetchContent_MakeAvailable")(content.name).endl(); - gen.handle_condition(content.include_after, - [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(content.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(content.include_after); + gen.conditional_cmake(content.cmake_after); } } @@ -767,14 +771,13 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { for (const auto &subdir : project.subdirs) { ConditionScope cs(gen, subdir.condition); - gen.handle_condition(subdir.include_before, - [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(subdir.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(subdir.include_before); + gen.conditional_cmake(subdir.cmake_before); add_subdir(subdir.name); - gen.handle_condition(subdir.include_after, [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(subdir.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(subdir.include_after); + gen.conditional_cmake(subdir.cmake_after); } if (!project.targets.empty()) { @@ -804,14 +807,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { cmd("set")("CMKR_TARGET", target.name); if (tmplate != nullptr) { - gen.handle_condition(tmplate->outline.include_before, - [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(tmplate->outline.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(tmplate->outline.include_before); + gen.conditional_cmake(tmplate->outline.cmake_before); } - gen.handle_condition(target.include_before, - [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(target.include_before); + gen.conditional_cmake(target.cmake_before); auto sources_var = target.name + "_SOURCES"; @@ -992,14 +993,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) { }); } - gen.handle_condition(target.include_after, - [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(target.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(target.include_after); + gen.conditional_cmake(target.cmake_after); if (tmplate != nullptr) { - gen.handle_condition(tmplate->outline.include_after, - [&](const std::string &, const std::vector &includes) { inject_includes(includes); }); - gen.handle_condition(tmplate->outline.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); + gen.conditional_includes(tmplate->outline.include_after); + gen.conditional_cmake(tmplate->outline.cmake_after); } cmd("unset")("CMKR_TARGET");