Merge pull request #146 from build-cpp/improved-conditions

Improve error-prone condition handling
main
Duncan Ogilvie 3 months ago committed by GitHub
commit 9f2cfe4083
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -109,6 +109,13 @@ clang-any = "CMAKE_CXX_COMPILER_ID MATCHES \"Clang\" OR CMAKE_C_COMPILER_ID MATC
root = "CMKR_ROOT_PROJECT" root = "CMKR_ROOT_PROJECT"
x64 = "CMAKE_SIZEOF_VOID_P EQUAL 8" x64 = "CMAKE_SIZEOF_VOID_P EQUAL 8"
x32 = "CMAKE_SIZEOF_VOID_P EQUAL 4" x32 = "CMAKE_SIZEOF_VOID_P EQUAL 4"
android = "ANDROID"
apple = "APPLE"
bsd = "BSD"
cygwin = "CYGWIN"
ios = "IOS"
xcode = "XCODE"
wince = "WINCE"
``` ```
## Subdirectories ## Subdirectories

@ -577,7 +577,7 @@ struct Generator {
} }
cmd("if", "NOTE: unnamed condition")(RawArg(cmake_condition(condition))); cmd("if", "NOTE: unnamed condition")(RawArg(cmake_condition(condition)));
} else { } else {
cmd("if", condition)(RawArg(found->second)); cmd("if", condition)(RawArg(cmake_condition(found->second)));
} }
return true; return true;
} }

@ -274,6 +274,13 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
conditions["root"] = R"cmake(CMKR_ROOT_PROJECT)cmake"; conditions["root"] = R"cmake(CMKR_ROOT_PROJECT)cmake";
conditions["x64"] = R"cmake(CMAKE_SIZEOF_VOID_P EQUAL 8)cmake"; conditions["x64"] = R"cmake(CMAKE_SIZEOF_VOID_P EQUAL 8)cmake";
conditions["x32"] = R"cmake(CMAKE_SIZEOF_VOID_P EQUAL 4)cmake"; conditions["x32"] = R"cmake(CMAKE_SIZEOF_VOID_P EQUAL 4)cmake";
conditions["android"] = R"cmake(ANDROID)cmake";
conditions["apple"] = R"cmake(APPLE)cmake";
conditions["bsd"] = R"cmake(BSD)cmake";
conditions["cygwin"] = R"cmake(CYGWIN)cmake";
conditions["ios"] = R"cmake(IOS)cmake";
conditions["xcode"] = R"cmake(XCODE)cmake";
conditions["wince"] = R"cmake(WINCE)cmake";
} else { } else {
conditions = parent->conditions; conditions = parent->conditions;
templates = parent->templates; templates = parent->templates;
@ -432,6 +439,9 @@ Project::Project(const Project *parent, const std::string &path, bool build) : p
} }
options.push_back(o); options.push_back(o);
// Add a condition matching the option name
conditions.emplace(o.name, o.name);
// Add an implicit condition for the option // Add an implicit condition for the option
auto ncondition = normalize(o.name); auto ncondition = normalize(o.name);
if (ncondition.find(nproject_prefix) == 0) { if (ncondition.find(nproject_prefix) == 0) {
@ -870,9 +880,8 @@ bool Project::cmake_minimum_version(int major, int minor) const {
} }
bool Project::is_condition_name(const std::string &name) { bool Project::is_condition_name(const std::string &name) {
auto is_named_condition = true;
for (auto ch : name) { for (auto ch : name) {
if (!(ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z'))) { if (!std::isalnum(ch) && ch != '-' && ch != '_') {
return false; return false;
} }
} }

Loading…
Cancel
Save