toml-checker
MoAlyousef 4 years ago
parent bb7a0bc0ed
commit ffdedbdace

@ -33,6 +33,7 @@ struct Package {
}; };
struct Vcpkg { struct Vcpkg {
std::string url;
std::vector<std::string> packages; std::vector<std::string> packages;
}; };

@ -192,6 +192,12 @@ CMake::CMake(const std::string &path, bool build) {
get_optional(t, "link-options", target.link_options); get_optional(t, "link-options", target.link_options);
get_optional(t, "precompile-headers", target.precompile_headers); get_optional(t, "precompile-headers", target.precompile_headers);
if (!target.headers.empty()) {
auto &sources = target.sources.nth(0).value();
const auto &headers = target.headers.nth(0)->second;
sources.insert(sources.end(), headers.begin(), headers.end());
}
if (t.contains("alias")) { if (t.contains("alias")) {
target.alias = toml::find(t, "alias").as_string(); target.alias = toml::find(t, "alias").as_string();
} }
@ -251,6 +257,7 @@ CMake::CMake(const std::string &path, bool build) {
if (toml.contains("vcpkg")) { if (toml.contains("vcpkg")) {
const auto &v = toml::find(toml, "vcpkg"); const auto &v = toml::find(toml, "vcpkg");
vcpkg.url = toml::find(v, "url").as_string();
vcpkg.packages = toml::find<decltype(vcpkg.packages)>(v, "packages"); vcpkg.packages = toml::find<decltype(vcpkg.packages)>(v, "packages");
} }

@ -11,6 +11,7 @@
#include <iomanip> #include <iomanip>
#include <new> #include <new>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <regex>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
@ -522,26 +523,35 @@ int generate_cmake(const char *path, bool root) {
} }
} }
if (!cmake.vcpkg.packages.empty()) { if (!cmake.vcpkg.packages.empty() && !cmake.vcpkg.url.empty()) {
cmd("include")("FetchContent"); auto vcpkg_escape_identifier = [](const std::string &name) -> std::string {
cmd("message")("STATUS", "Fetching vcpkg..."); const std::regex ok("[a-z0-9]+(-[a-z0-9]+)*");
cmd("FetchContent_Declare")("vcpkg", "URL", "https://github.com/microsoft/vcpkg/archive/refs/tags/2021.05.12.tar.gz"); const std::regex reserved("prn|aux|nul|con|lpt[1-9]|com[1-9]|core|default");
cmd("FetchContent_MakeAvailable")("vcpkg"); std::cmatch m;
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake"); if (!std::regex_match(name.c_str(), m, reserved) && std::regex_match(name.c_str(), m, ok)) {
return name;
} else {
// should probably throw!
return "project-name";
}
};
using namespace nlohmann; using namespace nlohmann;
json j; json j;
j["$schema"] = "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json"; j["$schema"] = "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json";
j["name"] = cmake.project_name; j["name"] = vcpkg_escape_identifier(cmake.project_name);
if (cmake.project_version.empty()) if (!cmake.project_version.empty())
throw std::runtime_error("vcpkg manifest mode requires that the project have a version string!");
j["version"] = cmake.project_version; j["version"] = cmake.project_version;
j["dependencies"] = cmake.vcpkg.packages; j["dependencies"] = cmake.vcpkg.packages;
cmd("include")("FetchContent");
cmd("message")("STATUS", "Fetching vcpkg...");
cmd("FetchContent_Declare")("vcpkg", "URL", cmake.vcpkg.url);
cmd("FetchContent_MakeAvailable")("vcpkg");
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
std::ofstream ofs("vcpkg.json"); std::ofstream ofs("vcpkg.json");
if (!ofs) { if (!ofs) {
throw std::runtime_error("Failed to create a vcpkg.json manifest file!"); throw std::runtime_error("Failed to create a vcpkg.json manifest file!");
} }
ofs << std::setw(4) << j << std::endl; ofs << std::setw(2) << j << std::endl;
ofs.close();
} }
if (!cmake.packages.empty()) { if (!cmake.packages.empty()) {
@ -616,21 +626,8 @@ int generate_cmake(const char *path, bool root) {
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); }); [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); }); gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
auto headers_var = target.name + "_HEADERS";
auto sources_var = target.name + "_SOURCES"; auto sources_var = target.name + "_SOURCES";
if (!target.headers.empty()) {
cmd("set")(headers_var, RawArg("\"\"")).endl();
gen.handle_condition(target.headers, [&](const std::string &condition, const std::vector<std::string> &condition_headers) {
auto headers = expand_cmake_paths(condition_headers, path);
if (headers.empty()) {
auto header_key = condition.empty() ? "headers" : (condition + ".headers");
throw std::runtime_error(target.name + " " + header_key + " wildcard found 0 files");
}
cmd("list")("APPEND", headers_var, headers);
});
}
bool added_toml = false; bool added_toml = false;
cmd("set")(sources_var, RawArg("\"\"")).endl(); cmd("set")(sources_var, RawArg("\"\"")).endl();
gen.handle_condition(target.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) { gen.handle_condition(target.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
@ -708,10 +705,6 @@ int generate_cmake(const char *path, bool root) {
// clang-format on // clang-format on
} }
if (!target.headers.empty()) {
cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + headers_var + "}").endl();
}
if (!target.sources.empty()) { if (!target.sources.empty()) {
cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + sources_var + "}").endl(); cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + sources_var + "}").endl();
} }

Loading…
Cancel
Save