Merge pull request #92 from build-cpp/improved-options

Improve and document the `[options]` feature
main
Duncan Ogilvie 2 years ago committed by GitHub
commit 49440f99e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -94,6 +94,16 @@ include-before = ["cmake/before-subdir.cmake"]
include-after = ["cmake/after-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 ## Variables
```toml ```toml

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

@ -656,7 +656,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
if (!project.options.empty()) { if (!project.options.empty()) {
comment("Options"); comment("Options");
for (const auto &opt : project.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(); endl();
} }
@ -665,16 +665,16 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
comment("Variables"); comment("Variables");
for (const auto &set : project.variables) { for (const auto &set : project.variables) {
std::string set_val; std::string set_val;
if (set.val.index() == 1) { if (set.value.index() == 1) {
set_val = mpark::get<1>(set.val); set_val = mpark::get<1>(set.value);
} else { } else {
set_val = mpark::get<0>(set.val) ? "ON" : "OFF"; set_val = mpark::get<0>(set.value) ? "ON" : "OFF";
} }
if (set.cache) { if (set.cache) {
auto typ = set.val.index() == 1 ? "STRING" : "BOOL"; auto typ = set.value.index() == 1 ? "STRING" : "BOOL";
auto force = set.force ? "FORCE" : ""; 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 { } else {
cmd("set")(set.name, set_val); 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; s.name = itr.first;
const auto &value = itr.second; const auto &value = itr.second;
if (value.is_boolean()) { if (value.is_boolean()) {
s.val = value.as_boolean(); s.value = value.as_boolean();
} else if (value.is_string()) { } else if (value.is_string()) {
s.val = value.as_string(); s.value = value.as_string();
} else { } else {
auto &setting = checker.create(value); auto &setting = checker.create(value);
setting.optional("comment", s.comment); setting.optional("help", s.help);
if (setting.contains("value")) { if (setting.contains("value")) {
const auto &v = setting.find("value"); const auto &v = setting.find("value");
if (v.is_boolean()) { if (v.is_boolean()) {
s.val = v.as_boolean(); s.value = v.as_boolean();
} else { } else {
s.val = v.as_string(); s.value = v.as_string();
} }
} }
setting.optional("cache", s.cache); 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; o.name = itr.first;
const auto &value = itr.second; const auto &value = itr.second;
if (value.is_boolean()) { if (value.is_boolean()) {
o.val = value.as_boolean(); o.value = value.as_boolean();
} else { } else {
auto &option = checker.create(value); auto &option = checker.create(value);
option.optional("comment", o.comment); option.optional("help", o.help);
option.optional("value", o.val); option.optional("value", o.value);
} }
options.push_back(o); 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("add-function", tmplate.add_function);
t.optional("pass-sources-to-add-function", tmplate.pass_sources_to_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); templates.push_back(tmplate);
} }

Loading…
Cancel
Save