Improve the code related to [options]

main
Duncan Ogilvie 2 years ago
parent 233cadadd0
commit 0c19c3da0c

@ -94,6 +94,16 @@ include-before = ["cmake/before-subdir.cmake"]
include-after = ["cmake/after-subdir.cmake"]
```
## Options
```toml
[options]
MYPROJECT_BUILD_TESTS = false
MYPROJECT_SPECIAL_OPTION = { value = true, help = "Docstring for this option." }
```
Options correspond to [CMake cache variables](https://cmake.org/cmake/help/book/mastering-cmake/chapter/CMake%20Cache.html) that can be used to customize your project at configure-time. You can configure with `cmake -DMYPROJECT_BUILD_TESTS=ON` to enable the option. Every options automatically gets a corresponding [condition](#conditions).
## Variables
```toml

@ -16,16 +16,16 @@ using ConditionVector = Condition<std::vector<std::string>>;
struct Variable {
std::string name;
std::string comment;
mpark::variant<bool, std::string> val;
std::string help;
mpark::variant<bool, std::string> value;
bool cache = false;
bool force = false;
};
struct Option {
std::string name;
std::string comment;
bool val = false;
std::string help;
bool value = false;
};
struct Package {

@ -656,7 +656,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
if (!project.options.empty()) {
comment("Options");
for (const auto &opt : project.options) {
cmd("option")(opt.name, RawArg(Command::quote(opt.comment)), opt.val ? "ON" : "OFF");
cmd("option")(opt.name, RawArg(Command::quote(opt.help)), opt.value ? "ON" : "OFF");
}
endl();
}
@ -665,16 +665,16 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
comment("Variables");
for (const auto &set : project.variables) {
std::string set_val;
if (set.val.index() == 1) {
set_val = mpark::get<1>(set.val);
if (set.value.index() == 1) {
set_val = mpark::get<1>(set.value);
} else {
set_val = mpark::get<0>(set.val) ? "ON" : "OFF";
set_val = mpark::get<0>(set.value) ? "ON" : "OFF";
}
if (set.cache) {
auto typ = set.val.index() == 1 ? "STRING" : "BOOL";
auto typ = set.value.index() == 1 ? "STRING" : "BOOL";
auto force = set.force ? "FORCE" : "";
cmd("set")(set.name, set_val, typ, set.comment, force);
cmd("set")(set.name, set_val, typ, set.help, force);
} else {
cmd("set")(set.name, set_val);
}

@ -347,18 +347,18 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
s.name = itr.first;
const auto &value = itr.second;
if (value.is_boolean()) {
s.val = value.as_boolean();
s.value = value.as_boolean();
} else if (value.is_string()) {
s.val = value.as_string();
s.value = value.as_string();
} else {
auto &setting = checker.create(value);
setting.optional("comment", s.comment);
setting.optional("help", s.help);
if (setting.contains("value")) {
const auto &v = setting.find("value");
if (v.is_boolean()) {
s.val = v.as_boolean();
s.value = v.as_boolean();
} else {
s.val = v.as_string();
s.value = v.as_string();
}
}
setting.optional("cache", s.cache);
@ -376,13 +376,14 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
o.name = itr.first;
const auto &value = itr.second;
if (value.is_boolean()) {
o.val = value.as_boolean();
o.value = value.as_boolean();
} else {
auto &option = checker.create(value);
option.optional("comment", o.comment);
option.optional("value", o.val);
option.optional("help", o.help);
option.optional("value", o.value);
}
options.push_back(o);
conditions.emplace(o.name, o.name);
}
}
@ -661,6 +662,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
t.optional("add-function", tmplate.add_function);
t.optional("pass-sources-to-add-function", tmplate.pass_sources_to_add_function);
t.optional("pass-sources", tmplate.pass_sources_to_add_function);
templates.push_back(tmplate);
}

Loading…
Cancel
Save