From e028381bf1c5d1fdf464ed835a549be4a0569adb Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Wed, 12 Sep 2012 04:20:42 +0200 Subject: [PATCH] Extend the generator expression language with more logic. Generator expressions for comparing strings, evaluating strings as booleans, and for creating literal right-angle-brackets and commas are added. Those may be needed in some cases where they appear in literals. --- Source/cmDocumentGeneratorExpressions.h | 6 ++ Source/cmGeneratorExpressionEvaluator.cxx | 68 +++++++++++++++++++++++ Tests/GeneratorExpression/CMakeLists.txt | 17 ++++++ Tests/GeneratorExpression/check.cmake | 17 ++++++ 4 files changed, 108 insertions(+) diff --git a/Source/cmDocumentGeneratorExpressions.h b/Source/cmDocumentGeneratorExpressions.h index 74c673a35..5622da093 100644 --- a/Source/cmDocumentGeneratorExpressions.h +++ b/Source/cmDocumentGeneratorExpressions.h @@ -20,6 +20,12 @@ " $<1:...> = content of \"...\"\n" \ " $ = '1' if config is \"cfg\", else '0'\n" \ " $ = configuration name\n" \ + " $ = '1' if the '...' is true, else '0'\n" \ + " $ = '1' if a is STREQUAL b, else '0'\n" \ + " $ = A literal '>'. Used to compare " \ + "strings which contain a '>' for example.\n" \ + " $ = A literal ','. Used to compare " \ + "strings which contain a ',' for example.\n" \ " $ = main file (.exe, .so.1.2, .a)\n" \ " $ = file used to link (.a, .lib, .so)\n" \ " $ = file with soname (.so.3)\n" \ diff --git a/Source/cmGeneratorExpressionEvaluator.cxx b/Source/cmGeneratorExpressionEvaluator.cxx index acc844a39..8573273a9 100644 --- a/Source/cmGeneratorExpressionEvaluator.cxx +++ b/Source/cmGeneratorExpressionEvaluator.cxx @@ -137,6 +137,66 @@ static const struct NotNode : public cmGeneratorExpressionNode } } notNode; +//---------------------------------------------------------------------------- +static const struct BoolNode : public cmGeneratorExpressionNode +{ + BoolNode() {} + + virtual int NumExpectedParameters() const { return 1; } + + std::string Evaluate(const std::vector ¶meters, + cmGeneratorExpressionContext *, + const GeneratorExpressionContent *) const + { + return !cmSystemTools::IsOff(parameters.begin()->c_str()) ? "1" : "0"; + } +} boolNode; + +//---------------------------------------------------------------------------- +static const struct StrEqualNode : public cmGeneratorExpressionNode +{ + StrEqualNode() {} + + virtual int NumExpectedParameters() const { return 2; } + + std::string Evaluate(const std::vector ¶meters, + cmGeneratorExpressionContext *, + const GeneratorExpressionContent *) const + { + return *parameters.begin() == parameters.at(1) ? "1" : "0"; + } +} strEqualNode; + +//---------------------------------------------------------------------------- +static const struct Angle_RNode : public cmGeneratorExpressionNode +{ + Angle_RNode() {} + + virtual int NumExpectedParameters() const { return 0; } + + std::string Evaluate(const std::vector &, + cmGeneratorExpressionContext *, + const GeneratorExpressionContent *) const + { + return ">"; + } +} angle_rNode; + +//---------------------------------------------------------------------------- +static const struct CommaNode : public cmGeneratorExpressionNode +{ + CommaNode() {} + + virtual int NumExpectedParameters() const { return 0; } + + std::string Evaluate(const std::vector &, + cmGeneratorExpressionContext *, + const GeneratorExpressionContent *) const + { + return ","; + } +} commaNode; + //---------------------------------------------------------------------------- static const struct ConfigurationNode : public cmGeneratorExpressionNode { @@ -392,6 +452,14 @@ cmGeneratorExpressionNode* GetNode(const std::string &identifier) return &targetLinkerFileDirNode; else if (identifier == "TARGET_SONAME_FILE_DIR") return &targetSoNameFileDirNode; + else if (identifier == "STREQUAL") + return &strEqualNode; + else if (identifier == "BOOL") + return &boolNode; + else if (identifier == "ANGLE-R") + return &angle_rNode; + else if (identifier == "COMMA") + return &commaNode; return 0; } diff --git a/Tests/GeneratorExpression/CMakeLists.txt b/Tests/GeneratorExpression/CMakeLists.txt index 2b135dcb5..79a8abbf3 100644 --- a/Tests/GeneratorExpression/CMakeLists.txt +++ b/Tests/GeneratorExpression/CMakeLists.txt @@ -22,6 +22,23 @@ add_custom_target(check ALL -Dtest_or_1=$ -Dtest_or_1_0=$ -Dtest_or_1_1=$ + -Dtest_bool_notfound=$ + -Dtest_bool_foo_notfound=$ + -Dtest_bool_true=$ + -Dtest_bool_false=$ + -Dtest_bool_on=$ + -Dtest_bool_off=$ + -Dtest_bool_no=$ + -Dtest_bool_n=$ + -Dtest_strequal_yes_yes=$ + -Dtest_strequal_yes_yes_cs=$ + -Dtest_strequal_yes_no=$ + -Dtest_strequal_no_yes=$ + -Dtest_strequal_angle_r=$,$> + -Dtest_strequal_comma=$,$> + -Dtest_strequal_angle_r_comma=$,$> + -Dtest_angle_r=$ + -Dtest_comma=$ -P ${CMAKE_CURRENT_SOURCE_DIR}/check.cmake COMMAND ${CMAKE_COMMAND} -E echo "check done" VERBATIM diff --git a/Tests/GeneratorExpression/check.cmake b/Tests/GeneratorExpression/check.cmake index e243d8528..6fb87a282 100644 --- a/Tests/GeneratorExpression/check.cmake +++ b/Tests/GeneratorExpression/check.cmake @@ -23,3 +23,20 @@ check(test_or_0_1 "1") check(test_or_1 "1") check(test_or_1_0 "1") check(test_or_1_1 "1") +check(test_bool_notfound "0") +check(test_bool_foo_notfound "0") +check(test_bool_true "1") +check(test_bool_false "0") +check(test_bool_on "1") +check(test_bool_off "0") +check(test_bool_no "0") +check(test_bool_n "0") +check(test_strequal_yes_yes "1") +check(test_strequal_yes_yes_cs "0") +check(test_strequal_yes_no "0") +check(test_strequal_no_yes "0") +check(test_strequal_angle_r "1") +check(test_strequal_comma "1") +check(test_strequal_angle_r_comma "0") +check(test_angle_r ">") +check(test_comma ",")