Skip to content

Commit 9f2cfe4

Browse files
authored
Merge pull request #146 from build-cpp/improved-conditions
Improve error-prone condition handling
2 parents 88377a8 + 15ad1dc commit 9f2cfe4

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

docs/cmake-toml.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ clang-any = "CMAKE_CXX_COMPILER_ID MATCHES \"Clang\" OR CMAKE_C_COMPILER_ID MATC
109109
root = "CMKR_ROOT_PROJECT"
110110
x64 = "CMAKE_SIZEOF_VOID_P EQUAL 8"
111111
x32 = "CMAKE_SIZEOF_VOID_P EQUAL 4"
112+
android = "ANDROID"
113+
apple = "APPLE"
114+
bsd = "BSD"
115+
cygwin = "CYGWIN"
116+
ios = "IOS"
117+
xcode = "XCODE"
118+
wince = "WINCE"
112119
```
113120

114121
## Subdirectories

src/cmake_generator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ struct Generator {
577577
}
578578
cmd("if", "NOTE: unnamed condition")(RawArg(cmake_condition(condition)));
579579
} else {
580-
cmd("if", condition)(RawArg(found->second));
580+
cmd("if", condition)(RawArg(cmake_condition(found->second)));
581581
}
582582
return true;
583583
}

src/project_parser.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
274274
conditions["root"] = R"cmake(CMKR_ROOT_PROJECT)cmake";
275275
conditions["x64"] = R"cmake(CMAKE_SIZEOF_VOID_P EQUAL 8)cmake";
276276
conditions["x32"] = R"cmake(CMAKE_SIZEOF_VOID_P EQUAL 4)cmake";
277+
conditions["android"] = R"cmake(ANDROID)cmake";
278+
conditions["apple"] = R"cmake(APPLE)cmake";
279+
conditions["bsd"] = R"cmake(BSD)cmake";
280+
conditions["cygwin"] = R"cmake(CYGWIN)cmake";
281+
conditions["ios"] = R"cmake(IOS)cmake";
282+
conditions["xcode"] = R"cmake(XCODE)cmake";
283+
conditions["wince"] = R"cmake(WINCE)cmake";
277284
} else {
278285
conditions = parent->conditions;
279286
templates = parent->templates;
@@ -432,6 +439,9 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
432439
}
433440
options.push_back(o);
434441

442+
// Add a condition matching the option name
443+
conditions.emplace(o.name, o.name);
444+
435445
// Add an implicit condition for the option
436446
auto ncondition = normalize(o.name);
437447
if (ncondition.find(nproject_prefix) == 0) {
@@ -870,9 +880,8 @@ bool Project::cmake_minimum_version(int major, int minor) const {
870880
}
871881

872882
bool Project::is_condition_name(const std::string &name) {
873-
auto is_named_condition = true;
874883
for (auto ch : name) {
875-
if (!(ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z'))) {
884+
if (!std::isalnum(ch) && ch != '-' && ch != '_') {
876885
return false;
877886
}
878887
}

0 commit comments

Comments
 (0)