Skip to content

Commit f02ccc2

Browse files
committed
Refactor fetch-content to vector<Content>
1 parent 37e9a1f commit f02ccc2

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

include/project_parser.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ struct Subdir {
119119
ConditionVector include_after;
120120
};
121121

122+
struct Content {
123+
std::string name;
124+
tsl::ordered_map<std::string, std::string> arguments;
125+
};
126+
122127
struct Project {
123128
// This is the CMake version required to use all cmkr versions.
124129
std::string cmake_version = "3.15";
@@ -145,7 +150,7 @@ struct Project {
145150
std::vector<Option> options;
146151
std::vector<Package> packages;
147152
Vcpkg vcpkg;
148-
tsl::ordered_map<std::string, std::map<std::string, std::string>> contents;
153+
std::vector<Content> contents;
149154
std::vector<Target> targets;
150155
std::vector<Test> tests;
151156
std::vector<Install> installs;

src/cmake_generator.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -554,30 +554,14 @@ int generate_cmake(const char *path, const parser::Project *parent_project) {
554554

555555
if (!project.contents.empty()) {
556556
cmd("include")("FetchContent").endl();
557-
for (const auto &dep : project.contents) {
558-
cmd("message")("STATUS", "Fetching " + dep.first + "...");
559-
ss << "FetchContent_Declare(\n\t" << dep.first << "\n";
560-
for (const auto &arg : dep.second) {
561-
std::string first_arg = arg.first;
562-
if (first_arg == "git") {
563-
first_arg = "GIT_REPOSITORY";
564-
} else if (first_arg == "tag") {
565-
first_arg = "GIT_TAG";
566-
} else if (first_arg == "svn") {
567-
first_arg = "SVN_REPOSITORY";
568-
} else if (first_arg == "rev") {
569-
first_arg = "SVN_REVISION";
570-
} else if (first_arg == "url") {
571-
first_arg = "URL";
572-
} else if (first_arg == "hash") {
573-
first_arg = "URL_HASH";
574-
} else {
575-
// don't change arg
576-
}
577-
ss << "\t" << first_arg << "\n\t\t" << arg.second << "\n";
557+
for (const auto &content : project.contents) {
558+
cmd("message")("STATUS", "Fetching " + content.name + "...");
559+
ss << "FetchContent_Declare(\n\t" << content.name << "\n";
560+
for (const auto &arg : content.arguments) {
561+
ss << "\t" << arg.first << "\n\t\t" << arg.second << "\n";
578562
}
579563
ss << ")\n";
580-
cmd("FetchContent_MakeAvailable")(dep.first).endl();
564+
cmd("FetchContent_MakeAvailable")(content.name).endl();
581565
}
582566
}
583567

src/project_parser.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,32 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
187187
}
188188
}
189189

190-
// TODO: refactor to std::vector<Content> instead of this hacky thing?
191190
if (toml.contains("fetch-content")) {
192-
contents = toml::find<decltype(contents)>(toml, "fetch-content");
191+
const auto &fc = toml::find(toml, "fetch-content").as_table();
192+
for (const auto &itr : fc) {
193+
Content content;
194+
content.name = itr.first;
195+
for (const auto &argItr : itr.second.as_table()) {
196+
auto key = argItr.first;
197+
if (key == "git") {
198+
key = "GIT_REPOSITORY";
199+
} else if (key == "tag") {
200+
key = "GIT_TAG";
201+
} else if (key == "svn") {
202+
key = "SVN_REPOSITORY";
203+
} else if (key == "rev") {
204+
key = "SVN_REVISION";
205+
} else if (key == "url") {
206+
key = "URL";
207+
} else if (key == "hash") {
208+
key = "URL_HASH";
209+
} else {
210+
// don't change arg
211+
}
212+
content.arguments.emplace(key, argItr.second.as_string());
213+
}
214+
contents.emplace_back(std::move(content));
215+
}
193216
}
194217

195218
if (toml.contains("bin")) {

0 commit comments

Comments
 (0)