Skip to content

Commit 4de1500

Browse files
committed
Refactor cmake injection
1 parent 8537096 commit 4de1500

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/cmake_generator.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,14 @@ struct Generator {
397397
}
398398
}
399399
}
400+
401+
void conditional_includes(const parser::ConditionVector &include) {
402+
handle_condition(include, [this](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
403+
}
404+
405+
void conditional_cmake(const parser::Condition<std::string> &cmake) {
406+
handle_condition(cmake, [this](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
407+
}
400408
};
401409

402410
struct ConditionScope {
@@ -482,8 +490,6 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
482490
auto cmd = [&gen](const std::string &command) { return gen.cmd(command); };
483491
auto comment = [&gen](const std::string &comment) { return gen.comment(comment); };
484492
auto endl = [&gen]() { gen.endl(); };
485-
auto inject_includes = [&gen](const std::vector<std::string> &includes) { gen.inject_includes(includes); };
486-
auto inject_cmake = [&gen](const std::string &cmake) { gen.inject_cmake(cmake); };
487493

488494
std::string cmkr_url = "https://github.com/build-cpp/cmkr";
489495
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) {
589595
endl();
590596
}
591597

592-
gen.handle_condition(project.include_before, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
593-
gen.handle_condition(project.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
598+
gen.conditional_includes(project.include_before);
599+
gen.conditional_cmake(project.cmake_before);
594600

595601
if (!project.project_name.empty()) {
596602
auto languages = std::make_pair("LANGUAGES", project.project_languages);
@@ -599,8 +605,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
599605
cmd("project")(project.project_name, languages, version, description).endl();
600606
}
601607

602-
gen.handle_condition(project.include_after, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
603-
gen.handle_condition(project.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
608+
gen.conditional_includes(project.include_after);
609+
gen.conditional_cmake(project.cmake_after);
604610

605611
if (!project.vcpkg.packages.empty()) {
606612
// 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) {
702708
for (const auto &content : project.contents) {
703709
ConditionScope cs(gen, content.condition);
704710

705-
gen.handle_condition(content.include_before,
706-
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
707-
gen.handle_condition(content.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
711+
gen.conditional_includes(content.include_before);
712+
gen.conditional_cmake(content.cmake_before);
708713

709714
std::string version_info = "";
710715
if (content.arguments.contains("GIT_TAG")) {
@@ -720,9 +725,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
720725
ss << ")\n";
721726
cmd("FetchContent_MakeAvailable")(content.name).endl();
722727

723-
gen.handle_condition(content.include_after,
724-
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
725-
gen.handle_condition(content.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
728+
gen.conditional_includes(content.include_after);
729+
gen.conditional_cmake(content.cmake_after);
726730
}
727731
}
728732

@@ -767,14 +771,13 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
767771
for (const auto &subdir : project.subdirs) {
768772
ConditionScope cs(gen, subdir.condition);
769773

770-
gen.handle_condition(subdir.include_before,
771-
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
772-
gen.handle_condition(subdir.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
774+
gen.conditional_includes(subdir.include_before);
775+
gen.conditional_cmake(subdir.cmake_before);
773776

774777
add_subdir(subdir.name);
775778

776-
gen.handle_condition(subdir.include_after, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
777-
gen.handle_condition(subdir.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
779+
gen.conditional_includes(subdir.include_after);
780+
gen.conditional_cmake(subdir.cmake_after);
778781
}
779782

780783
if (!project.targets.empty()) {
@@ -804,14 +807,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
804807
cmd("set")("CMKR_TARGET", target.name);
805808

806809
if (tmplate != nullptr) {
807-
gen.handle_condition(tmplate->outline.include_before,
808-
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
809-
gen.handle_condition(tmplate->outline.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
810+
gen.conditional_includes(tmplate->outline.include_before);
811+
gen.conditional_cmake(tmplate->outline.cmake_before);
810812
}
811813

812-
gen.handle_condition(target.include_before,
813-
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
814-
gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
814+
gen.conditional_includes(target.include_before);
815+
gen.conditional_cmake(target.cmake_before);
815816

816817
auto sources_var = target.name + "_SOURCES";
817818

@@ -992,14 +993,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
992993
});
993994
}
994995

995-
gen.handle_condition(target.include_after,
996-
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
997-
gen.handle_condition(target.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
996+
gen.conditional_includes(target.include_after);
997+
gen.conditional_cmake(target.cmake_after);
998998

999999
if (tmplate != nullptr) {
1000-
gen.handle_condition(tmplate->outline.include_after,
1001-
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
1002-
gen.handle_condition(tmplate->outline.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
1000+
gen.conditional_includes(tmplate->outline.include_after);
1001+
gen.conditional_cmake(tmplate->outline.cmake_after);
10031002
}
10041003

10051004
cmd("unset")("CMKR_TARGET");

0 commit comments

Comments
 (0)