Refactor fetch-content to vector<Content>

toml-checker
Duncan Ogilvie 2 years ago
parent 37e9a1f1a0
commit f02ccc2309

@ -119,6 +119,11 @@ struct Subdir {
ConditionVector include_after;
};
struct Content {
std::string name;
tsl::ordered_map<std::string, std::string> arguments;
};
struct Project {
// This is the CMake version required to use all cmkr versions.
std::string cmake_version = "3.15";
@ -145,7 +150,7 @@ struct Project {
std::vector<Option> options;
std::vector<Package> packages;
Vcpkg vcpkg;
tsl::ordered_map<std::string, std::map<std::string, std::string>> contents;
std::vector<Content> contents;
std::vector<Target> targets;
std::vector<Test> tests;
std::vector<Install> installs;

@ -554,30 +554,14 @@ int generate_cmake(const char *path, const parser::Project *parent_project) {
if (!project.contents.empty()) {
cmd("include")("FetchContent").endl();
for (const auto &dep : project.contents) {
cmd("message")("STATUS", "Fetching " + dep.first + "...");
ss << "FetchContent_Declare(\n\t" << dep.first << "\n";
for (const auto &arg : dep.second) {
std::string first_arg = arg.first;
if (first_arg == "git") {
first_arg = "GIT_REPOSITORY";
} else if (first_arg == "tag") {
first_arg = "GIT_TAG";
} else if (first_arg == "svn") {
first_arg = "SVN_REPOSITORY";
} else if (first_arg == "rev") {
first_arg = "SVN_REVISION";
} else if (first_arg == "url") {
first_arg = "URL";
} else if (first_arg == "hash") {
first_arg = "URL_HASH";
} else {
// don't change arg
}
ss << "\t" << first_arg << "\n\t\t" << arg.second << "\n";
for (const auto &content : project.contents) {
cmd("message")("STATUS", "Fetching " + content.name + "...");
ss << "FetchContent_Declare(\n\t" << content.name << "\n";
for (const auto &arg : content.arguments) {
ss << "\t" << arg.first << "\n\t\t" << arg.second << "\n";
}
ss << ")\n";
cmd("FetchContent_MakeAvailable")(dep.first).endl();
cmd("FetchContent_MakeAvailable")(content.name).endl();
}
}

@ -187,9 +187,32 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
// TODO: refactor to std::vector<Content> instead of this hacky thing?
if (toml.contains("fetch-content")) {
contents = toml::find<decltype(contents)>(toml, "fetch-content");
const auto &fc = toml::find(toml, "fetch-content").as_table();
for (const auto &itr : fc) {
Content content;
content.name = itr.first;
for (const auto &argItr : itr.second.as_table()) {
auto key = argItr.first;
if (key == "git") {
key = "GIT_REPOSITORY";
} else if (key == "tag") {
key = "GIT_TAG";
} else if (key == "svn") {
key = "SVN_REPOSITORY";
} else if (key == "rev") {
key = "SVN_REVISION";
} else if (key == "url") {
key = "URL";
} else if (key == "hash") {
key = "URL_HASH";
} else {
// don't change arg
}
content.arguments.emplace(key, argItr.second.as_string());
}
contents.emplace_back(std::move(content));
}
}
if (toml.contains("bin")) {

Loading…
Cancel
Save