add option comments

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

@ -55,11 +55,11 @@ Currently supported fields:
```toml ```toml
[cmake] # required for top-level project [cmake] # required for top-level project
minimum = "3.14" # required minimum = "3.14" # required
subdirs = [] # optional
bin-dir = "bin" # optional
cpp-flags = [] # optional cpp-flags = [] # optional
c-flags = [] # optional c-flags = [] # optional
link-flags = [] # optional link-flags = [] # optional
subdirs = [] # optional
bin-dir = "bin" # optional
generator = "Ninja" # optional generator = "Ninja" # optional
arguments = ["CMAKE_TOOLCHAIN_FILE=/path/to/toolchain"] # 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 [options] # optional
APP_BUILD_STUFF = false # optional APP_BUILD_STUFF = false # optional
APP_OTHER_STUFF = { comment = "does other stuff", value = false } # optional
[[bin]] # required, can define several binaries [[bin]] # required, can define several binaries
name = "app" # required name = "app" # required

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

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

@ -69,12 +69,23 @@ CMake::CMake(const std::string &path, bool build) {
} }
if (toml.contains("options")) { 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"); const auto &opts = toml::find<opts_map>(toml, "options");
for (const auto opt: opts) { for (const auto opt : opts) {
Option o; Option o;
o.name = opt.first; 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); options.push_back(o);
} }
} }

@ -152,7 +152,7 @@ int generate_cmake(const char *path) {
if (!cmake.options.empty()) { if (!cmake.options.empty()) {
for (const auto &opt: cmake.options) { 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