Skip to content

Commit 49440f9

Browse files
authored
Merge pull request #92 from build-cpp/improved-options
Improve and document the `[options]` feature
2 parents 233cada + 0c19c3d commit 49440f9

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

docs/cmake-toml.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ include-before = ["cmake/before-subdir.cmake"]
9494
include-after = ["cmake/after-subdir.cmake"]
9595
```
9696

97+
## Options
98+
99+
```toml
100+
[options]
101+
MYPROJECT_BUILD_TESTS = false
102+
MYPROJECT_SPECIAL_OPTION = { value = true, help = "Docstring for this option." }
103+
```
104+
105+
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).
106+
97107
## Variables
98108

99109
```toml

include/project_parser.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ using ConditionVector = Condition<std::vector<std::string>>;
1616

1717
struct Variable {
1818
std::string name;
19-
std::string comment;
20-
mpark::variant<bool, std::string> val;
19+
std::string help;
20+
mpark::variant<bool, std::string> value;
2121
bool cache = false;
2222
bool force = false;
2323
};
2424

2525
struct Option {
2626
std::string name;
27-
std::string comment;
28-
bool val = false;
27+
std::string help;
28+
bool value = false;
2929
};
3030

3131
struct Package {

src/cmake_generator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
656656
if (!project.options.empty()) {
657657
comment("Options");
658658
for (const auto &opt : project.options) {
659-
cmd("option")(opt.name, RawArg(Command::quote(opt.comment)), opt.val ? "ON" : "OFF");
659+
cmd("option")(opt.name, RawArg(Command::quote(opt.help)), opt.value ? "ON" : "OFF");
660660
}
661661
endl();
662662
}
@@ -665,16 +665,16 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
665665
comment("Variables");
666666
for (const auto &set : project.variables) {
667667
std::string set_val;
668-
if (set.val.index() == 1) {
669-
set_val = mpark::get<1>(set.val);
668+
if (set.value.index() == 1) {
669+
set_val = mpark::get<1>(set.value);
670670
} else {
671-
set_val = mpark::get<0>(set.val) ? "ON" : "OFF";
671+
set_val = mpark::get<0>(set.value) ? "ON" : "OFF";
672672
}
673673

674674
if (set.cache) {
675-
auto typ = set.val.index() == 1 ? "STRING" : "BOOL";
675+
auto typ = set.value.index() == 1 ? "STRING" : "BOOL";
676676
auto force = set.force ? "FORCE" : "";
677-
cmd("set")(set.name, set_val, typ, set.comment, force);
677+
cmd("set")(set.name, set_val, typ, set.help, force);
678678
} else {
679679
cmd("set")(set.name, set_val);
680680
}

src/project_parser.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,18 +347,18 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
347347
s.name = itr.first;
348348
const auto &value = itr.second;
349349
if (value.is_boolean()) {
350-
s.val = value.as_boolean();
350+
s.value = value.as_boolean();
351351
} else if (value.is_string()) {
352-
s.val = value.as_string();
352+
s.value = value.as_string();
353353
} else {
354354
auto &setting = checker.create(value);
355-
setting.optional("comment", s.comment);
355+
setting.optional("help", s.help);
356356
if (setting.contains("value")) {
357357
const auto &v = setting.find("value");
358358
if (v.is_boolean()) {
359-
s.val = v.as_boolean();
359+
s.value = v.as_boolean();
360360
} else {
361-
s.val = v.as_string();
361+
s.value = v.as_string();
362362
}
363363
}
364364
setting.optional("cache", s.cache);
@@ -376,13 +376,14 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
376376
o.name = itr.first;
377377
const auto &value = itr.second;
378378
if (value.is_boolean()) {
379-
o.val = value.as_boolean();
379+
o.value = value.as_boolean();
380380
} else {
381381
auto &option = checker.create(value);
382-
option.optional("comment", o.comment);
383-
option.optional("value", o.val);
382+
option.optional("help", o.help);
383+
option.optional("value", o.value);
384384
}
385385
options.push_back(o);
386+
conditions.emplace(o.name, o.name);
386387
}
387388
}
388389

@@ -661,6 +662,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
661662

662663
t.optional("add-function", tmplate.add_function);
663664
t.optional("pass-sources-to-add-function", tmplate.pass_sources_to_add_function);
665+
t.optional("pass-sources", tmplate.pass_sources_to_add_function);
664666

665667
templates.push_back(tmplate);
666668
}

0 commit comments

Comments
 (0)