add option comments

self-hosting
MoAlyousef 4 years ago
parent 3b09103d98
commit e6219d6571

@ -55,11 +55,11 @@ Currently supported fields:
```toml
[cmake] # required for top-level project
minimum = "3.14" # required
subdirs = [] # optional
bin-dir = "bin" # optional
cpp-flags = [] # optional
c-flags = [] # optional
link-flags = [] # optional
subdirs = [] # optional
bin-dir = "bin" # optional
generator = "Ninja" # optional
arguments = ["CMAKE_TOOLCHAIN_FILE=/path/to/toolchain"] # optional
@ -76,6 +76,7 @@ toml11 = { GIT_REPOSITORY = "https://github.com/ToruNiina/toml11", GIT_TAG = "v3
[options] # optional
APP_BUILD_STUFF = false # optional
APP_OTHER_STUFF = { comment = "does other stuff", value = false } # optional
[[bin]] # required, can define several binaries
name = "app" # required

@ -8,6 +8,7 @@ namespace cmkr::cmake {
struct Option {
std::string name;
std::string comment;
bool val;
};

@ -12,11 +12,11 @@ int %s() {
const char *cmake_toml = R"lit(
[cmake]
minimum = "3.14"
# subdirs = []
# bin-dir = ""
# cpp-flags = []
# c-flags = []
# link-flags = []
# subdirs = []
# generator = ""
# arguments = []

@ -69,12 +69,23 @@ CMake::CMake(const std::string &path, bool build) {
}
if (toml.contains("options")) {
using opts_map = std::map<std::string, bool>;
using opts_map =
std::map<std::string, toml::basic_value<toml::discard_comments, std::unordered_map,
std::vector>>;
const auto &opts = toml::find<opts_map>(toml, "options");
for (const auto opt: opts) {
for (const auto opt : opts) {
Option o;
o.name = opt.first;
o.val = opt.second;
if (opt.second.is_boolean()) {
o.val = opt.second.as_boolean();
} else {
if (opt.second.contains("comment")) {
o.comment = toml::find(opt.second, "comment").as_string();
}
if (opt.second.contains("value")) {
o.val = toml::find(opt.second, "value").as_boolean();
}
}
options.push_back(o);
}
}

@ -152,7 +152,7 @@ int generate_cmake(const char *path) {
if (!cmake.options.empty()) {
for (const auto &opt: cmake.options) {
ss << "option(" << opt.name << " \"" << opt.name << "\" " << (opt.val ? "ON" : "OFF") << ")\n";
ss << "option(" << opt.name << " \"" << opt.comment << "\" " << (opt.val ? "ON" : "OFF") << ")\n";
}
}

Loading…
Cancel
Save