Skip to content

Commit ad17890

Browse files
authored
Merge pull request #85 from build-cpp/settings-rename
Rename `[settings]` to `[variables]`
2 parents 1d95fab + c0b98b3 commit ad17890

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

docs/cmake-toml.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ nav_order: 3
99

1010
This page is a reference for the options in the `cmake.toml` file. If you think anything is missing or unclear, please [edit this page](https://github.com/build-cpp/cmkr/edit/main/docs/cmake-toml.md) or open an [issue](https://github.com/build-cpp/cmkr/issues).
1111

12-
See the [examples](/examples) section for concrete examples.
12+
This is a reference page. Check out the [examples](/examples) and the [cmkr topic](https://github.com/topics/cmkr) as well.
13+
{:.info}
1314

1415
## CMake configuration
1516

@@ -93,6 +94,16 @@ include-before = ["cmake/before-subdir.cmake"]
9394
include-after = ["cmake/after-subdir.cmake"]
9495
```
9596

97+
## Variables
98+
99+
```toml
100+
[variables]
101+
MYBOOL = true
102+
MYSTRING = "hello"
103+
```
104+
105+
Variables emit a [`set`](https://cmake.org/cmake/help/latest/command/set.html) and can be used to configure subprojects and packages.
106+
96107
## Vcpkg
97108

98109
```toml

include/project_parser.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using Condition = tsl::ordered_map<std::string, T>;
1414

1515
using ConditionVector = Condition<std::vector<std::string>>;
1616

17-
struct Setting {
17+
struct Variable {
1818
std::string name;
1919
std::string comment;
2020
mpark::variant<bool, std::string> val;
@@ -185,7 +185,7 @@ struct Project {
185185
Condition<std::string> cmake_after;
186186
ConditionVector include_before;
187187
ConditionVector include_after;
188-
std::vector<Setting> settings;
188+
std::vector<Variable> variables;
189189
std::vector<Option> options;
190190
std::vector<Package> packages;
191191
Vcpkg vcpkg;

src/cmake_generator.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,9 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
621621
endl();
622622
}
623623

624-
// TODO: rename to 'variables'?
625-
if (!project.settings.empty()) {
626-
comment("Settings");
627-
for (const auto &set : project.settings) {
624+
if (!project.variables.empty()) {
625+
comment("Variables");
626+
for (const auto &set : project.variables) {
628627
std::string set_val;
629628
if (set.val.index() == 1) {
630629
set_val = mpark::get<1>(set.val);

src/project_parser.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ static MsvcRuntimeType parse_msvcRuntimeType(const std::string &name) {
3232

3333
using TomlBasicValue = toml::basic_value<toml::discard_comments, tsl::ordered_map, std::vector>;
3434

35-
static std::string format_key_error(const std::string &error, const toml::key &ky, const TomlBasicValue &value) {
35+
static std::string format_key_message(const std::string &message, const toml::key &ky, const TomlBasicValue &value) {
3636
auto loc = value.location();
3737
auto line_number_str = std::to_string(loc.line());
3838
auto line_width = line_number_str.length();
3939
auto line_str = loc.line_str();
4040

4141
std::ostringstream oss;
42-
oss << "[error] " << error << '\n';
42+
oss << message << "\n";
4343
oss << " --> " << loc.file_name() << ':' << loc.line() << '\n';
4444

4545
oss << std::string(line_width + 2, ' ') << "|\n";
@@ -57,6 +57,14 @@ static std::string format_key_error(const std::string &error, const toml::key &k
5757
return oss.str();
5858
}
5959

60+
static void throw_key_error(const std::string &error, const toml::key &ky, const TomlBasicValue &value) {
61+
throw std::runtime_error(format_key_message("[error] " + error, ky, value));
62+
}
63+
64+
static void print_key_warning(const std::string &message, const toml::key &ky, const TomlBasicValue &value) {
65+
puts(format_key_message("[warning] " + message, ky, value).c_str());
66+
}
67+
6068
class TomlChecker {
6169
const TomlBasicValue &m_v;
6270
tsl::ordered_set<toml::key> m_visited;
@@ -133,27 +141,27 @@ class TomlChecker {
133141
const auto &ky = itr.first;
134142
if (m_conditionVisited.contains(ky)) {
135143
if (!conditions.contains(ky)) {
136-
throw std::runtime_error(format_key_error("Unknown condition '" + ky + "'", ky, itr.second));
144+
throw_key_error("Unknown condition '" + ky + "'", ky, itr.second);
137145
}
138146

139147
for (const auto &jtr : itr.second.as_table()) {
140148
if (!m_visited.contains(jtr.first)) {
141-
throw std::runtime_error(format_key_error("Unknown key '" + jtr.first + "'", jtr.first, jtr.second));
149+
throw_key_error("Unknown key '" + jtr.first + "'", jtr.first, jtr.second);
142150
}
143151
}
144152
} else if (!m_visited.contains(ky)) {
145153
if (itr.second.is_table()) {
146154
for (const auto &jtr : itr.second.as_table()) {
147155
if (!m_visited.contains(jtr.first)) {
148-
throw std::runtime_error(format_key_error("Unknown key '" + jtr.first + "'", jtr.first, jtr.second));
156+
throw_key_error("Unknown key '" + jtr.first + "'", jtr.first, jtr.second);
149157
}
150158
}
151159
}
152-
throw std::runtime_error(format_key_error("Unknown key '" + ky + "'", ky, itr.second));
160+
throw_key_error("Unknown key '" + ky + "'", ky, itr.second);
153161
} else if (ky == "condition") {
154162
std::string condition = itr.second.as_string();
155163
if (!conditions.contains(condition)) {
156-
throw std::runtime_error(format_key_error("Unknown condition '" + condition + "'", condition, itr.second));
164+
throw_key_error("Unknown condition '" + condition + "'", condition, itr.second);
157165
}
158166
}
159167
}
@@ -191,7 +199,7 @@ class TomlCheckerRoot {
191199
if (check_root) {
192200
for (const auto &itr : m_root.as_table()) {
193201
if (!m_visisted.contains(itr.first)) {
194-
throw std::runtime_error(format_key_error("Unknown key '" + itr.first + "'", itr.first, itr.second));
202+
throw_key_error("Unknown key '" + itr.first + "'", itr.first, itr.second);
195203
}
196204
}
197205
}
@@ -219,7 +227,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
219227
cmake.required("version", cmake_version);
220228

221229
if (cmake.contains("bin-dir")) {
222-
throw std::runtime_error("bin-dir has been renamed to build-dir");
230+
throw_key_error("bin-dir has been renamed to build-dir", "bin-dir", cmake.find("bin-dir"));
223231
}
224232

225233
cmake.optional("build-dir", build_dir);
@@ -297,7 +305,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
297305
error += " - " + type_name + "\n";
298306
}
299307
error.pop_back(); // Remove last newline
300-
throw std::runtime_error(format_key_error(error, msvc_runtime, project.find("msvc-runtime")));
308+
throw_key_error(error, msvc_runtime, project.find("msvc-runtime"));
301309
}
302310
}
303311
}
@@ -319,11 +327,21 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
319327
}
320328
}
321329

322-
if (checker.contains("settings")) {
330+
if (checker.contains("variables")) {
323331
using set_map = tsl::ordered_map<std::string, TomlBasicValue>;
324-
const auto &sets = toml::find<set_map>(toml, "settings");
325-
for (const auto &itr : sets) {
326-
Setting s;
332+
auto vars = toml::find<set_map>(toml, "variables");
333+
if (checker.contains("settings")) {
334+
print_key_warning("[settings] has been renamed to [variables]", "settings", toml.at("settings"));
335+
const auto &sets = toml::find<set_map>(toml, "settings");
336+
for (const auto &itr : sets) {
337+
if (!vars.insert(itr).second) {
338+
throw_key_error("Key '" + itr.first + "' shadows existing variable", itr.first, itr.second);
339+
}
340+
}
341+
}
342+
343+
for (const auto &itr : vars) {
344+
Variable s;
327345
s.name = itr.first;
328346
const auto &value = itr.second;
329347
if (value.is_boolean()) {
@@ -344,7 +362,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
344362
setting.optional("cache", s.cache);
345363
setting.optional("force", s.force);
346364
}
347-
settings.push_back(s);
365+
variables.push_back(s);
348366
}
349367
}
350368

@@ -454,7 +472,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
454472
} else if (is_cmake_arg(key)) {
455473
// allow passthrough of ExternalProject options
456474
} else if (!c.visisted(key)) {
457-
throw std::runtime_error(format_key_error("Unknown key '" + argItr.first + "'", argItr.first, argItr.second));
475+
throw_key_error("Unknown key '" + argItr.first + "'", argItr.first, argItr.second);
458476
}
459477

460478
// Make sure not to emit keys like "condition" in the FetchContent call
@@ -469,7 +487,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
469487
}
470488

471489
if (checker.contains("bin")) {
472-
throw std::runtime_error("[[bin]] has been renamed to [[target]]");
490+
throw_key_error("[[bin]] has been renamed to [target.<name>]", "", toml.at("bin"));
473491
}
474492

475493
auto parse_target = [&](const std::string &name, TomlChecker &t, bool isTemplate) {
@@ -508,7 +526,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
508526
}
509527
}
510528
error.pop_back(); // Remove last newline
511-
throw std::runtime_error(format_key_error(error, target.type_name, t.find("type")));
529+
throw_key_error(error, target.type_name, t.find("type"));
512530
}
513531

514532
t.optional("sources", target.sources);
@@ -570,7 +588,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
570588
} else {
571589
report = &t.find(condItr.first).as_table().find("msvc-runtime").value();
572590
}
573-
throw std::runtime_error(format_key_error(error, condItr.second, *report));
591+
throw_key_error(error, condItr.second, *report);
574592
}
575593
}
576594
}
@@ -626,13 +644,13 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
626644

627645
for (const auto &type_name : targetTypeNames) {
628646
if (name == type_name) {
629-
throw std::runtime_error(format_key_error("Reserved template name '" + name + "'", name, itr.second));
647+
throw_key_error("Reserved template name '" + name + "'", name, itr.second);
630648
}
631649
}
632650

633651
for (const auto &tmplate : templates) {
634652
if (name == tmplate.outline.name) {
635-
throw std::runtime_error(format_key_error("Template '" + name + "' already defined", name, itr.second));
653+
throw_key_error("Template '" + name + "' already defined", name, itr.second);
636654
}
637655
}
638656

@@ -707,7 +725,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
707725
package.features.emplace_back(feature);
708726
}
709727
} else {
710-
throw std::runtime_error("Invalid vcpkg package '" + package_str + "'");
728+
throw_key_error("Invalid package name '" + package_str + "'", "packages", p);
711729
}
712730
vcpkg.packages.emplace_back(std::move(package));
713731
}

0 commit comments

Comments
 (0)