You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
316 lines
13 KiB
316 lines
13 KiB
3 years ago
|
#include "project_parser.hpp"
|
||
4 years ago
|
|
||
4 years ago
|
#include "enum_helper.hpp"
|
||
4 years ago
|
#include "fs.hpp"
|
||
4 years ago
|
#include <stdexcept>
|
||
4 years ago
|
#include <toml.hpp>
|
||
4 years ago
|
#include <tsl/ordered_map.h>
|
||
4 years ago
|
|
||
4 years ago
|
template <>
|
||
3 years ago
|
const char *enumStrings<cmkr::parser::TargetType>::data[] = {"executable", "library", "shared", "static", "interface", "custom"};
|
||
4 years ago
|
|
||
4 years ago
|
namespace cmkr {
|
||
3 years ago
|
namespace parser {
|
||
4 years ago
|
|
||
4 years ago
|
using TomlBasicValue = toml::basic_value<toml::preserve_comments, tsl::ordered_map, std::vector>;
|
||
|
|
||
4 years ago
|
template <typename EnumType>
|
||
4 years ago
|
static EnumType to_enum(const std::string &str, const std::string &help_name) {
|
||
4 years ago
|
EnumType value;
|
||
|
try {
|
||
|
std::stringstream ss(str);
|
||
|
ss >> enumFromString(value);
|
||
|
} catch (std::invalid_argument &) {
|
||
|
std::string supported;
|
||
|
for (const auto &s : enumStrings<EnumType>::data) {
|
||
|
if (!supported.empty()) {
|
||
|
supported += ", ";
|
||
|
}
|
||
|
supported += s;
|
||
|
}
|
||
|
throw std::runtime_error("Unknown " + help_name + "'" + str + "'! Supported types are: " + supported);
|
||
|
}
|
||
|
return value;
|
||
|
}
|
||
|
|
||
4 years ago
|
template <typename T>
|
||
|
static void get_optional(const TomlBasicValue &v, const toml::key &ky, T &destination);
|
||
|
|
||
|
template <typename T>
|
||
|
static void get_optional(const TomlBasicValue &v, const toml::key &ky, Condition<T> &destination) {
|
||
|
// TODO: this algorithm in O(n) over the amount of keys, kinda bad
|
||
|
const auto &table = v.as_table();
|
||
|
for (const auto &itr : table) {
|
||
|
const auto &key = itr.first;
|
||
|
const auto &value = itr.second;
|
||
|
if (value.is_table()) {
|
||
|
if (value.contains(ky)) {
|
||
|
destination[key] = toml::find<T>(value, ky);
|
||
|
}
|
||
|
} else if (key == ky) {
|
||
|
destination[""] = toml::find<T>(v, ky);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
template <typename T>
|
||
|
static void get_optional(const TomlBasicValue &v, const toml::key &ky, T &destination) {
|
||
4 years ago
|
// TODO: this currently doesn't allow you to get an optional map<string, X>
|
||
4 years ago
|
if (v.contains(ky)) {
|
||
|
destination = toml::find<T>(v, ky);
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
3 years ago
|
Project::Project(const std::string &path, bool build) {
|
||
4 years ago
|
if (!fs::exists(fs::path(path) / "cmake.toml")) {
|
||
4 years ago
|
throw std::runtime_error("No cmake.toml was found!");
|
||
4 years ago
|
}
|
||
4 years ago
|
const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>((fs::path(path) / "cmake.toml").string());
|
||
4 years ago
|
if (build) {
|
||
|
if (toml.contains("cmake")) {
|
||
|
const auto &cmake = toml::find(toml, "cmake");
|
||
4 years ago
|
|
||
4 years ago
|
if (cmake.contains("bin-dir")) {
|
||
4 years ago
|
throw std::runtime_error("bin-dir has been renamed to build-dir");
|
||
|
}
|
||
|
|
||
4 years ago
|
get_optional(cmake, "build-dir", build_dir);
|
||
|
get_optional(cmake, "generator", generator);
|
||
|
get_optional(cmake, "config", config);
|
||
|
get_optional(cmake, "arguments", gen_args);
|
||
4 years ago
|
}
|
||
|
} else {
|
||
|
if (toml.contains("cmake")) {
|
||
|
const auto &cmake = toml::find(toml, "cmake");
|
||
4 years ago
|
cmake_version = toml::find(cmake, "version").as_string();
|
||
4 years ago
|
get_optional(cmake, "cmkr-include", cmkr_include);
|
||
|
get_optional(cmake, "cpp-flags", cppflags);
|
||
|
get_optional(cmake, "c-flags", cflags);
|
||
|
get_optional(cmake, "link-flags", linkflags);
|
||
4 years ago
|
}
|
||
|
|
||
|
if (toml.contains("project")) {
|
||
|
const auto &project = toml::find(toml, "project");
|
||
4 years ago
|
project_name = toml::find(project, "name").as_string();
|
||
4 years ago
|
get_optional(project, "version", project_version);
|
||
|
get_optional(project, "description", project_description);
|
||
|
get_optional(project, "languages", project_languages);
|
||
|
get_optional(project, "cmake-before", cmake_before);
|
||
|
get_optional(project, "cmake-after", cmake_after);
|
||
|
get_optional(project, "include-before", include_before);
|
||
|
get_optional(project, "include-after", include_after);
|
||
|
get_optional(project, "subdirs", subdirs);
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
if (toml.contains("settings")) {
|
||
4 years ago
|
using set_map = std::map<std::string, TomlBasicValue>;
|
||
4 years ago
|
const auto &sets = toml::find<set_map>(toml, "settings");
|
||
4 years ago
|
for (const auto &set : sets) {
|
||
4 years ago
|
Setting s;
|
||
|
s.name = set.first;
|
||
|
if (set.second.is_boolean()) {
|
||
|
s.val = set.second.as_boolean();
|
||
|
} else if (set.second.is_string()) {
|
||
|
s.val = set.second.as_string();
|
||
|
} else {
|
||
4 years ago
|
get_optional(set.second, "comment", s.comment);
|
||
4 years ago
|
if (set.second.contains("value")) {
|
||
|
auto v = toml::find(set.second, "value");
|
||
|
if (v.is_boolean()) {
|
||
|
s.val = v.as_boolean();
|
||
|
} else {
|
||
|
s.val = v.as_string();
|
||
|
}
|
||
|
}
|
||
4 years ago
|
get_optional(set.second, "cache", s.cache);
|
||
|
get_optional(set.second, "force", s.force);
|
||
4 years ago
|
}
|
||
|
settings.push_back(s);
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
if (toml.contains("options")) {
|
||
4 years ago
|
using opts_map = tsl::ordered_map<std::string, TomlBasicValue>;
|
||
4 years ago
|
const auto &opts = toml::find<opts_map>(toml, "options");
|
||
4 years ago
|
for (const auto &opt : opts) {
|
||
4 years ago
|
Option o;
|
||
|
o.name = opt.first;
|
||
4 years ago
|
if (opt.second.is_boolean()) {
|
||
|
o.val = opt.second.as_boolean();
|
||
|
} else {
|
||
4 years ago
|
get_optional(opt.second, "comment", o.comment);
|
||
|
get_optional(opt.second, "value", o.val);
|
||
4 years ago
|
}
|
||
4 years ago
|
options.push_back(o);
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
if (toml.contains("find-package")) {
|
||
4 years ago
|
using pkg_map = tsl::ordered_map<std::string, TomlBasicValue>;
|
||
4 years ago
|
const auto &pkgs = toml::find<pkg_map>(toml, "find-package");
|
||
|
for (const auto &pkg : pkgs) {
|
||
|
Package p;
|
||
|
p.name = pkg.first;
|
||
|
if (pkg.second.is_string()) {
|
||
|
p.version = pkg.second.as_string();
|
||
|
} else {
|
||
4 years ago
|
get_optional(pkg.second, "version", p.version);
|
||
|
get_optional(pkg.second, "required", p.required);
|
||
|
get_optional(pkg.second, "config", p.config);
|
||
|
get_optional(pkg.second, "components", p.components);
|
||
4 years ago
|
}
|
||
|
packages.push_back(p);
|
||
|
}
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
// TODO: refactor to std::vector<Content> instead of this hacky thing?
|
||
4 years ago
|
if (toml.contains("fetch-content")) {
|
||
|
contents = toml::find<decltype(contents)>(toml, "fetch-content");
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
if (toml.contains("bin")) {
|
||
4 years ago
|
throw std::runtime_error("[[bin]] has been renamed to [[target]]");
|
||
|
}
|
||
|
|
||
|
if (toml.contains("target")) {
|
||
4 years ago
|
const auto &ts = toml::find(toml, "target").as_table();
|
||
4 years ago
|
|
||
4 years ago
|
for (const auto &itr : ts) {
|
||
|
const auto &t = itr.second;
|
||
4 years ago
|
Target target;
|
||
4 years ago
|
target.name = itr.first;
|
||
4 years ago
|
target.type = to_enum<TargetType>(toml::find(t, "type").as_string(), "target type");
|
||
4 years ago
|
|
||
4 years ago
|
get_optional(t, "headers", target.headers);
|
||
4 years ago
|
get_optional(t, "sources", target.sources);
|
||
3 years ago
|
|
||
4 years ago
|
get_optional(t, "compile-definitions", target.compile_definitions);
|
||
3 years ago
|
get_optional(t, "private-compile-definitions", target.private_compile_definitions);
|
||
|
|
||
4 years ago
|
get_optional(t, "compile-features", target.compile_features);
|
||
3 years ago
|
get_optional(t, "private-compile-features", target.private_compile_features);
|
||
|
|
||
4 years ago
|
get_optional(t, "compile-options", target.compile_options);
|
||
3 years ago
|
get_optional(t, "private-compile-options", target.private_compile_options);
|
||
|
|
||
4 years ago
|
get_optional(t, "include-directories", target.include_directories);
|
||
3 years ago
|
get_optional(t, "private-include-directories", target.private_include_directories);
|
||
|
|
||
4 years ago
|
get_optional(t, "link-directories", target.link_directories);
|
||
3 years ago
|
get_optional(t, "private-link-directories", target.private_link_directories);
|
||
|
|
||
4 years ago
|
get_optional(t, "link-libraries", target.link_libraries);
|
||
3 years ago
|
get_optional(t, "private-link-libraries", target.private_link_libraries);
|
||
|
|
||
4 years ago
|
get_optional(t, "link-options", target.link_options);
|
||
3 years ago
|
get_optional(t, "private-link-options", target.private_link_options);
|
||
|
|
||
4 years ago
|
get_optional(t, "precompile-headers", target.precompile_headers);
|
||
3 years ago
|
get_optional(t, "private-precompile-headers", target.private_precompile_headers);
|
||
4 years ago
|
|
||
4 years ago
|
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());
|
||
|
}
|
||
|
|
||
3 years ago
|
if (t.contains("condition")) {
|
||
|
target.condition = toml::find(t, "condition").as_string();
|
||
|
}
|
||
|
|
||
4 years ago
|
if (t.contains("alias")) {
|
||
|
target.alias = toml::find(t, "alias").as_string();
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
if (t.contains("properties")) {
|
||
3 years ago
|
auto store_property = [&target](const toml::key &k, const TomlBasicValue &v, const std::string &condition) {
|
||
|
if (v.is_array()) {
|
||
4 years ago
|
std::string property_list;
|
||
3 years ago
|
for (const auto &list_val : v.as_array()) {
|
||
4 years ago
|
if (!property_list.empty()) {
|
||
|
property_list += ';';
|
||
|
}
|
||
|
property_list += list_val.as_string();
|
||
|
}
|
||
3 years ago
|
target.properties[condition][k] = property_list;
|
||
|
} else if (v.is_boolean()) {
|
||
|
target.properties[condition][k] = v.as_boolean() ? "ON" : "OFF";
|
||
|
} else {
|
||
|
target.properties[condition][k] = v.as_string();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const auto &props = toml::find(t, "properties").as_table();
|
||
|
for (const auto &propKv : props) {
|
||
|
const auto &k = propKv.first;
|
||
|
const auto &v = propKv.second;
|
||
|
if (v.is_table()) {
|
||
|
for (const auto &condKv : v.as_table()) {
|
||
|
store_property(condKv.first, condKv.second, k);
|
||
|
}
|
||
4 years ago
|
} else {
|
||
3 years ago
|
store_property(k, v, "");
|
||
4 years ago
|
}
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
get_optional(t, "cmake-before", target.cmake_before);
|
||
|
get_optional(t, "cmake-after", target.cmake_after);
|
||
|
get_optional(t, "include-before", target.include_before);
|
||
|
get_optional(t, "include-after", target.include_after);
|
||
4 years ago
|
|
||
4 years ago
|
targets.push_back(target);
|
||
4 years ago
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
if (toml.contains("test")) {
|
||
|
const auto &ts = toml::find(toml, "test").as_array();
|
||
|
for (const auto &t : ts) {
|
||
|
Test test;
|
||
|
test.name = toml::find(t, "name").as_string();
|
||
4 years ago
|
get_optional(t, "configurations", test.configurations);
|
||
|
get_optional(t, "working-directory", test.working_directory);
|
||
|
test.command = toml::find(t, "command").as_string();
|
||
|
get_optional(t, "arguments", test.arguments);
|
||
4 years ago
|
tests.push_back(test);
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
if (toml.contains("install")) {
|
||
|
const auto &ts = toml::find(toml, "install").as_array();
|
||
|
for (const auto &t : ts) {
|
||
|
Install inst;
|
||
4 years ago
|
get_optional(t, "targets", inst.targets);
|
||
|
get_optional(t, "files", inst.files);
|
||
|
get_optional(t, "dirs", inst.dirs);
|
||
|
get_optional(t, "configs", inst.configs);
|
||
4 years ago
|
inst.destination = toml::find(t, "destination").as_string();
|
||
|
installs.push_back(inst);
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
if (toml.contains("vcpkg")) {
|
||
|
const auto &v = toml::find(toml, "vcpkg");
|
||
4 years ago
|
get_optional(v, "url", vcpkg.url);
|
||
|
get_optional(v, "version", vcpkg.version);
|
||
4 years ago
|
vcpkg.packages = toml::find<decltype(vcpkg.packages)>(v, "packages");
|
||
|
}
|
||
4 years ago
|
|
||
|
// Reasonable default conditions (you can override these if you desire)
|
||
4 years ago
|
conditions["windows"] = R"cmake(WIN32)cmake";
|
||
|
conditions["macos"] = R"cmake("${CMAKE_SYSTEM_NAME}" MATCHES "Darwin")cmake";
|
||
|
conditions["unix"] = R"cmake(UNIX)cmake";
|
||
|
conditions["bsd"] = R"cmake("${CMAKE_SYSTEM_NAME}" MATCHES "BSD")cmake";
|
||
|
conditions["linux"] = conditions["lunix"] = R"cmake("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")cmake";
|
||
4 years ago
|
|
||
|
if (toml.contains("conditions")) {
|
||
|
auto conds = toml::find<decltype(conditions)>(toml, "conditions");
|
||
|
for (const auto &cond : conds) {
|
||
|
conditions[cond.first] = cond.second;
|
||
|
}
|
||
|
}
|
||
4 years ago
|
}
|
||
|
}
|
||
3 years ago
|
} // namespace parser
|
||
4 years ago
|
} // namespace cmkr
|